Skip to content

Commit

Permalink
Merge pull request #923 from jfrog/GH-922-restore-user-compatibility
Browse files Browse the repository at this point in the history
Restore user resource backward compatibility with older Artifactory
  • Loading branch information
alexhung authored Apr 2, 2024
2 parents 94f60aa + a08f18a commit 7f996d9
Show file tree
Hide file tree
Showing 9 changed files with 464 additions and 74 deletions.
6 changes: 6 additions & 0 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ archives:
- format: zip
name_template: '{{ .ProjectName }}_{{ .Version }}_{{ .Os }}_{{ .Arch }}'
checksum:
extra_files:
- glob: 'terraform-registry-manifest.json'
name_template: '{{ .ProjectName }}_{{ .Version }}_manifest.json'
name_template: '{{ .ProjectName }}_{{ .Version }}_SHA256SUMS'
algorithm: sha256
signs:
Expand All @@ -48,6 +51,9 @@ signs:
- "--detach-sign"
- "${artifact}"
release:
extra_files:
- glob: 'terraform-registry-manifest.json'
name_template: '{{ .ProjectName }}_{{ .Version }}_manifest.json'
# If you want to manually examine the release before its live, uncomment this line:
# draft: true
changelog:
Expand Down
8 changes: 7 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## 10.4.3 (Apr 1, 2024)

BUG FIXES:

* resource/artifactory_user, resource/artifactory_managed_user, resource/artifactory_unmanaged_user: Restore backward compatibility with Artifactory 7.49.2 and earlier. Issue: [#922](https://github.com/jfrog/terraform-provider-artifactory/issues/922) PR: [#923](https://github.com/jfrog/terraform-provider-artifactory/pull/923)

## 10.4.2 (Mar 27, 2024)

BUG FIXES:
Expand All @@ -8,7 +14,7 @@ BUG FIXES:

BUG FIXES:

* resource/artifactory_user, resource/artifactory_managed_user, resource/artifactory_unmanaged_user: Fix `groups` handling to keep groups membership from Artifactory sychronized and avoid state drift. Also fix inability to create n user without `password` set with `internal_password_disabled` is set to `true`. Issue: [#915](https://github.com/jfrog/terraform-provider-artifactory/issues/915) PR: [#920](https://github.com/jfrog/terraform-provider-artifactory/pull/920)
* resource/artifactory_user, resource/artifactory_managed_user, resource/artifactory_unmanaged_user: Fix `groups` handling to keep groups membership from Artifactory sychronized and avoid state drift. Also fix inability to create a user without `password` set with `internal_password_disabled` is set to `true`. Issue: [#915](https://github.com/jfrog/terraform-provider-artifactory/issues/915) PR: [#920](https://github.com/jfrog/terraform-provider-artifactory/pull/920)

## 10.4.0 (Mar 20, 2024)

Expand Down
5 changes: 2 additions & 3 deletions pkg/acctest/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ func DeleteUser(t *testing.T, name string) error {
restyClient := GetTestResty(t)
_, err := restyClient.R().
SetPathParam("name", name).
Delete(user.UserEndpointPath)
Delete("access/api/v2/users/{name}")

return err
}
Expand All @@ -355,15 +355,14 @@ func CreateUserUpdatable(t *testing.T, name string, email string) {
restyClient := GetTestResty(t)
_, err := restyClient.R().
SetBody(userObj).
Post(user.UsersEndpointPath)
Post("access/api/v2/users")

if err != nil {
t.Fatal(err)
}
}

func CompareArtifactoryVersions(t *testing.T, instanceVersions string) (bool, error) {

fixedVersion, err := version.NewVersion(instanceVersions)
if err != nil {
return false, err
Expand Down
14 changes: 9 additions & 5 deletions pkg/artifactory/datasource/user/datasource_artifactory_user.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/jfrog/terraform-provider-shared/util"
utilsdk "github.com/jfrog/terraform-provider-shared/util/sdk"

"github.com/jfrog/terraform-provider-artifactory/v10/pkg/artifactory"
"github.com/jfrog/terraform-provider-artifactory/v10/pkg/artifactory/resource/user"
"github.com/jfrog/terraform-provider-shared/validator"
)
Expand Down Expand Up @@ -70,11 +71,14 @@ func DataSourceArtifactoryUser() *schema.Resource {
d := &utilsdk.ResourceData{ResourceData: rd}

userName := d.Get("name").(string)
userObj := user.User{}
resp, err := m.(util.ProvderMetadata).Client.R().
SetPathParam("name", userName).
SetResult(&userObj).
Get(user.UserEndpointPath)
var userObj user.User
var artifactoryError artifactory.ArtifactoryErrorsResponse
resp, err := user.ReadUser(
m.(util.ProvderMetadata).Client.R(),
m.(util.ProvderMetadata).ArtifactoryVersion,
userName,
&userObj,
&artifactoryError)

if err != nil {
return diag.FromErr(err)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package user
import (
"net/http"

"github.com/go-resty/resty/v2"
"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
Expand Down Expand Up @@ -71,6 +72,33 @@ func (r *ArtifactoryAnonymousUserResource) Create(ctx context.Context, req resou
)
}

func (r *ArtifactoryAnonymousUserResource) readUser(req *resty.Request, artifactoryVersion, name string, result *ArtifactoryAnonymousUserResourceAPIModel, artifactoryError *artifactory.ArtifactoryErrorsResponse) (*resty.Response, error) {
endpoint := GetUserEndpointPath(artifactoryVersion)

// 7.49.3 or later, use Access API
if ok, err := util.CheckVersion(artifactoryVersion, AccessAPIArtifactoryVersion); err == nil && ok {
return req.
SetPathParam("name", name).
SetResult(&result).
SetError(&artifactoryError).
Get(endpoint)
}

// else use old Artifactory API, which has a slightly differect JSON payload!
var artifactoryResult ArtifactoryUserAPIModel
res, err := req.
SetPathParam("name", name).
SetResult(&artifactoryResult).
SetError(artifactoryError).
Get(endpoint)

*result = ArtifactoryAnonymousUserResourceAPIModel{
Name: artifactoryResult.Name,
}

return res, err
}

func (r *ArtifactoryAnonymousUserResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) {
go util.SendUsageResourceRead(ctx, r.ProviderData.Client, r.ProviderData.ProductId, r.TypeName)

Expand All @@ -83,17 +111,15 @@ func (r *ArtifactoryAnonymousUserResource) Read(ctx context.Context, req resourc
}

// Convert from Terraform data model into API data model
user := &ArtifactoryAnonymousUserResourceAPIModel{}

var user ArtifactoryAnonymousUserResourceAPIModel
var artifactoryError artifactory.ArtifactoryErrorsResponse
response, err := r.ProviderData.Client.R().
SetPathParam("name", data.Id.ValueString()).
SetResult(user).
SetError(&artifactoryError).
Get(UserEndpointPath)

// Treat HTTP 404 Not Found status as a signal to recreate resource
// and return early
response, err := r.readUser(
r.ProviderData.Client.R(),
r.ProviderData.ArtifactoryVersion,
data.Id.ValueString(),
&user,
&artifactoryError)

if err != nil {
utilfw.UnableToRefreshResourceError(resp, err.Error())
return
Expand All @@ -111,7 +137,7 @@ func (r *ArtifactoryAnonymousUserResource) Read(ctx context.Context, req resourc

// Convert from the API data model to the Terraform data model
// and refresh any attribute values.
data.toState(user)
data.toState(&user)

// Save updated data into Terraform state
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ func ResourceArtifactoryUser() *schema.Resource {
}

func resourceUserCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {

passwordGenerator := func(user *User) diag.Diagnostics {
var diags diag.Diagnostics

Expand Down
14 changes: 13 additions & 1 deletion pkg/artifactory/resource/user/resource_artifactory_user_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"regexp"
"testing"

"github.com/go-resty/resty/v2"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/hashicorp/terraform-plugin-testing/terraform"
"github.com/jfrog/terraform-provider-artifactory/v10/pkg/acctest"
Expand Down Expand Up @@ -460,7 +461,18 @@ func testAccCheckManagedUserDestroy(id string) func(*terraform.State) error {
return fmt.Errorf("resource id[%s] not found", id)
}

resp, err := client.R().Get("access/api/v2/users/" + rs.Primary.ID)
var resp *resty.Response
var err error
// 7.49.3 or later, use Access API
if ok, e := util.CheckVersion(acctest.Provider.Meta().(util.ProvderMetadata).ArtifactoryVersion, "7.49.3"); e == nil && ok {
r, er := client.R().Get("access/api/v2/users/" + rs.Primary.ID)
resp = r
err = er
} else {
r, er := client.R().Get("artifactory/api/security/users/" + rs.Primary.ID)
resp = r
err = er
}

if err != nil {
return err
Expand Down
Loading

0 comments on commit 7f996d9

Please sign in to comment.