Skip to content

Commit

Permalink
Merge pull request #1190 from jfrog/GH-1189-fix-unable-to-refresh-mis…
Browse files Browse the repository at this point in the history
…sing-repo

Fix unable to refresh missing repo
  • Loading branch information
alexhung authored Feb 11, 2025
2 parents 243dd0f + 8a9e3f7 commit 101cd0b
Show file tree
Hide file tree
Showing 14 changed files with 164 additions and 140 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## 12.8.4 (Februry 7, 2025). Tested on Artifactory 7.104.6 with Terraform 1.10.5 and OpenTofu 1.9.0

BUG FIXES:

* resource/artifactory_\*\_repository: Fix error when provider tries to refresh the resource for repository that is no longer existed on Artifactory. Issue: [#1189](https://github.com/jfrog/terraform-provider-artifactory/issues/1189) PR: [#1190](https://github.com/jfrog/terraform-provider-artifactory/pull/1190)

## 12.8.3 (January 28, 2025). Tested on Artifactory 7.98.14 with Terraform 1.10.5 and OpenTofu 1.9.0

IMPROVEMENTS:
Expand Down
2 changes: 1 addition & 1 deletion GNUmakefile
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ install: clean build
# mv -v dist/terraform-provider-${PRODUCT}_${LINUX_GORELEASER_ARCH}/terraform-provider-${PRODUCT}_v${NEXT_PROVIDER_VERSION}* ${LINUX_BUILD_PATH} && \
clean:
rm -fR dist terraform.d/ .terraform terraform.tfstate* terraform.d/ .terraform.lock.hcl
rm -fR dist terraform.d/ .terraform terraform.tfstate* .terraform.lock.hcl

release:
@git tag ${NEXT_PROVIDER_VERSION} && git push --mirror
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ type BowerRemoteRepo struct {

var bowerSchema = lo.Assign(
remote.BaseSchema,
remote.VcsRemoteRepoSchemaSDKv2,
VcsRemoteRepoSchemaSDKv2,
map[string]*schema.Schema{
"bower_registry_url": {
Type: schema.TypeString,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ type CocoapodsRemoteRepo struct {

var cocoapodsSchema = lo.Assign(
remote.BaseSchema,
remote.VcsRemoteRepoSchemaSDKv2,
VcsRemoteRepoSchemaSDKv2,
map[string]*schema.Schema{
"pods_specs_repo_url": {
Type: schema.TypeString,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ type ComposerRemoteRepo struct {

var composerSchema = lo.Assign(
remote.BaseSchema,
remote.VcsRemoteRepoSchemaSDKv2,
VcsRemoteRepoSchemaSDKv2,
map[string]*schema.Schema{
"composer_registry_url": {
Type: schema.TypeString,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ type VcsRemoteRepo struct {

var VCSSchema = lo.Assign(
remote.BaseSchema,
remote.VcsRemoteRepoSchemaSDKv2,
VcsRemoteRepoSchemaSDKv2,
map[string]*schema.Schema{
"max_unique_snapshots": {
Type: schema.TypeInt,
Expand Down
17 changes: 17 additions & 0 deletions pkg/artifactory/datasource/repository/remote/remote.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package remote

import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
"github.com/jfrog/terraform-provider-artifactory/v12/pkg/artifactory/resource/repository/remote"
)

Expand All @@ -13,3 +14,19 @@ var getSchema = func(schemas map[int16]map[string]*schema.Schema) map[string]*sc

return s
}

var VcsRemoteRepoSchemaSDKv2 = map[string]*schema.Schema{
"vcs_git_provider": {
Type: schema.TypeString,
Optional: true,
Default: "GITHUB",
ValidateDiagFunc: validation.ToDiagFunc(validation.StringInSlice([]string{"GITHUB", "BITBUCKET", "OLDSTASH", "STASH", "ARTIFACTORY", "CUSTOM"}, false)),
Description: `Artifactory supports proxying the following Git providers out-of-the-box: GitHub or a remote Artifactory instance. Default value is "GITHUB".`,
},
"vcs_git_download_url": {
Type: schema.TypeString,
Optional: true,
ValidateDiagFunc: validation.ToDiagFunc(validation.StringIsNotEmpty),
Description: `This attribute is used when vcs_git_provider is set to 'CUSTOM'. Provided URL will be used as proxy.`,
},
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ package configuration_test
import (
"fmt"
"net/http"
"os"
"regexp"
"strings"
"testing"

"github.com/hashicorp/terraform-plugin-testing/helper/resource"
Expand Down Expand Up @@ -139,6 +141,11 @@ func TestAccArchivePolicy_invalid_conditions(t *testing.T) {
}

func TestAccArchivePolicy_full(t *testing.T) {
archivePolicyEnabled := os.Getenv("JFROG_ARCHIVE_POLICY_ENABLED")
if strings.ToLower(archivePolicyEnabled) != "true" {
t.Skipf("JFROG_ARCHIVE_POLICY_ENABLED env var is not set to 'true'")
}

client := acctest.GetTestResty(t)
version, err := util.GetArtifactoryVersion(client)
if err != nil {
Expand Down Expand Up @@ -325,6 +332,11 @@ func TestAccArchivePolicy_full(t *testing.T) {
}

func TestAccArchivePolicy_with_project_key(t *testing.T) {
archivePolicyEnabled := os.Getenv("JFROG_ARCHIVE_POLICY_ENABLED")
if strings.ToLower(archivePolicyEnabled) != "true" {
t.Skipf("JFROG_ARCHIVE_POLICY_ENABLED env var is not set to 'true'")
}

client := acctest.GetTestResty(t)
version, err := util.GetArtifactoryVersion(client)
if err != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ type PackageCleanupPolicySearchCriteriaAPIModel struct {
ExcludedRepos *[]string `json:"excludedRepos,omitempty"`
IncludedPackages []string `json:"includedPackages"`
ExcludedPackages *[]string `json:"excludedPackages,omitempty"`
IncludeAllProjects *bool `json:"includeAllProjects,omitempty"`
IncludeAllProjects *bool `json:"includeAllProjects"`
IncludedProjects *[]string `json:"includedProjects,omitempty"`
CreatedBeforeInMonths *int64 `json:"createdBeforeInMonths,omitempty"`
LastDownloadedBeforeInMonths *int64 `json:"lastDownloadedBeforeInMonths,omitempty"`
Expand Down Expand Up @@ -454,6 +454,8 @@ var cleanupPolicySchemaV1 = lo.Assign(
},
"include_all_projects": schema.BoolAttribute{
Optional: true,
Computed: true,
Default: booldefault.StaticBool(false),
Description: "Set this to `true` if you want the policy to run on all projects on the platform.",
},
"included_projects": schema.SetAttribute{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,12 @@ func TestAccPackageCleanupPolicy_with_project_key(t *testing.T) {
key = "{{ .repoName }}"
tag_retention = 3
max_unique_tags = 5
lifecycle {
ignore_changes = [
project_key,
]
}
}
resource "project" "myproject" {
Expand All @@ -457,6 +463,11 @@ func TestAccPackageCleanupPolicy_with_project_key(t *testing.T) {
email_notification = true
}
resource "project_repository" "{{ .repoName }}" {
project_key = project.myproject.key
key = artifactory_local_docker_v2_repository.{{ .repoName }}.key
}
resource "artifactory_package_cleanup_policy" "{{ .policyName }}" {
key = "${project.myproject.key}-{{ .policyName }}"
description = "Test policy"
Expand All @@ -475,6 +486,8 @@ func TestAccPackageCleanupPolicy_with_project_key(t *testing.T) {
created_before_in_months = 1
last_downloaded_before_in_months = 6
}
depends_on = [project_repository.{{ .repoName }}]
}`

updatedTemp := `
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,21 @@ import (
"context"
"reflect"

"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/jfrog/terraform-provider-artifactory/v12/pkg/artifactory/resource/repository"
"github.com/samber/lo"
)

func NewMachineLearningLocalRepositoryResource() resource.Resource {
return &MachineLearningLocalRepositoryResource{
localResource: NewLocalRepositoryResource(
repository.MachineLearningType,
"Machine Learning",
reflect.TypeFor[LocalResourceModel](),
reflect.TypeFor[LocalAPIModel](),
reflect.TypeFor[LocalMachineLearningResourceModel](),
reflect.TypeFor[LocalMachineLearningAPIModel](),
),
}
}
Expand All @@ -24,9 +27,84 @@ type MachineLearningLocalRepositoryResource struct {
localResource
}

type LocalMachineLearningResourceModel struct {
LocalResourceModel
}

func (r *LocalMachineLearningResourceModel) GetCreateResourcePlanData(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
// Read Terraform plan data into the model
resp.Diagnostics.Append(req.Plan.Get(ctx, r)...)
}

func (r LocalMachineLearningResourceModel) SetCreateResourceStateData(ctx context.Context, resp *resource.CreateResponse) {
// Save data into Terraform state
resp.Diagnostics.Append(resp.State.Set(ctx, &r)...)
}

func (r *LocalMachineLearningResourceModel) GetReadResourceStateData(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) {
// Read Terraform state data into the model
resp.Diagnostics.Append(req.State.Get(ctx, r)...)
}

func (r LocalMachineLearningResourceModel) SetReadResourceStateData(ctx context.Context, resp *resource.ReadResponse) {
// Save data into Terraform state
resp.Diagnostics.Append(resp.State.Set(ctx, &r)...)
}

func (r *LocalMachineLearningResourceModel) GetUpdateResourcePlanData(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) {
// Read Terraform state data into the model
resp.Diagnostics.Append(req.Plan.Get(ctx, r)...)
}

func (r *LocalMachineLearningResourceModel) GetUpdateResourceStateData(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) {
// Read Terraform state data into the model
resp.Diagnostics.Append(req.State.Get(ctx, r)...)
}

func (r LocalMachineLearningResourceModel) SetUpdateResourceStateData(ctx context.Context, resp *resource.UpdateResponse) {
// Save data into Terraform state
resp.Diagnostics.Append(resp.State.Set(ctx, &r)...)
}

func (r LocalMachineLearningResourceModel) ToAPIModel(ctx context.Context, packageType string) (interface{}, diag.Diagnostics) {
diags := diag.Diagnostics{}

model, d := r.LocalResourceModel.ToAPIModel(ctx, packageType)
if d != nil {
diags.Append(d...)
}

localAPIModel := model.(LocalAPIModel)
localAPIModel.RepoLayoutRef = r.RepoLayoutRef.ValueString()

return LocalMachineLearningAPIModel{
LocalAPIModel: localAPIModel,
}, diags
}

func (r *LocalMachineLearningResourceModel) FromAPIModel(ctx context.Context, apiModel interface{}) diag.Diagnostics {
diags := diag.Diagnostics{}

model := apiModel.(*LocalMachineLearningAPIModel)

r.LocalResourceModel.FromAPIModel(ctx, model.LocalAPIModel)

r.RepoLayoutRef = types.StringValue(model.RepoLayoutRef)

return diags
}

type LocalMachineLearningAPIModel struct {
LocalAPIModel
}

func (r *MachineLearningLocalRepositoryResource) Schema(ctx context.Context, req resource.SchemaRequest, resp *resource.SchemaResponse) {
attributes := lo.Assign(
LocalAttributes,
repository.RepoLayoutRefAttribute(r.Rclass, r.PackageType),
)
resp.Schema = schema.Schema{
Attributes: LocalAttributes,
Attributes: attributes,
Description: r.Description,
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/jfrog/terraform-provider-artifactory/v12/pkg/acctest"
"github.com/jfrog/terraform-provider-artifactory/v12/pkg/artifactory/resource/repository"
"github.com/jfrog/terraform-provider-shared/testutil"
"github.com/jfrog/terraform-provider-shared/util"
)
Expand Down Expand Up @@ -53,6 +54,10 @@ func TestAccLocalMachineLearningRepository(t *testing.T) {
resource.TestCheckResourceAttr(fqrn, "key", name),
resource.TestCheckResourceAttr(fqrn, "blacked_out", strconv.FormatBool(params["blacked_out"].(bool))),
resource.TestCheckResourceAttr(fqrn, "xray_index", strconv.FormatBool(params["xray_index"].(bool))),
resource.TestCheckResourceAttr(fqrn, "repo_layout_ref", func() string {
r, _ := repository.GetDefaultRepoLayoutRef("local", repository.MachineLearningType)
return r
}()), //Check to ensure repository layout is set as per default even when it is not passed.
resource.TestCheckResourceAttr(fqrn, "property_sets.#", "1"),
resource.TestCheckResourceAttr(fqrn, "property_sets.0", params["property_set"].(string)),
resource.TestCheckResourceAttr(fqrn, "archive_browsing_enabled", strconv.FormatBool(params["archive_browsing_enabled"].(bool))),
Expand Down
Loading

0 comments on commit 101cd0b

Please sign in to comment.