Skip to content

Commit

Permalink
Merge pull request #97 from jfrog/GH-87-fix-provider-configure-when-g…
Browse files Browse the repository at this point in the history
…et-version-fails

Fix provider configure when get version fails
  • Loading branch information
alexhung committed Jun 20, 2024
2 parents fabefc7 + 5ea920a commit cc2da78
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 35 deletions.
2 changes: 2 additions & 0 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
version: 2

# Visit https://goreleaser.com for documentation on how to customize this
# behavior.
before:
Expand Down
16 changes: 16 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
## 1.8.0 (June 20, 2024). Tested on Artifactory 7.84.15 with Terraform 1.8.5 and OpenTofu 1.7.2

NOTES:

* provider: `check_license` attribute is deprecated and provider no longer checks Artifactory license during initialization. It will be removed in the next major version release.

BUG FIXES:

* provider: Fix incomplete provider initialization if Artifactory version check fails.

IMPROVEMENTS:

* provider: Now allows JFrog Access Token to be unset (i.e. MyJFrog API token is set and only `platform_myjfrog_ip_allowlist` resource is used). Warning message is displayed when either token is not set.

Issue: [#87](https://github.com/jfrog/terraform-provider-platform/issues/87) PR: [#97](https://github.com/jfrog/terraform-provider-platform/pull/97)

## 1.7.4 (May 8, 2024). Tested on Artifactory 7.84.14 with Terraform 1.8.4 and OpenTofu 1.7.2

BUG FIXES:
Expand Down
76 changes: 41 additions & 35 deletions pkg/platform/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ func (p *PlatformProvider) Configure(ctx context.Context, req provider.Configure
// Check environment variables, first available OS variable will be assigned to the var
url := util.CheckEnvVars([]string{"JFROG_URL"}, "")
accessToken := util.CheckEnvVars([]string{"JFROG_ACCESS_TOKEN"}, "")
myJFrogAPIToken := util.CheckEnvVars([]string{"JFROG_MYJFROG_API_TOKEN"}, "")

var config platformProviderModel

Expand Down Expand Up @@ -103,76 +102,82 @@ func (p *PlatformProvider) Configure(ctx context.Context, req provider.Configure
accessToken = config.AccessToken.ValueString()
}

if accessToken == "" {
myJFrogAPIToken := util.CheckEnvVars([]string{"JFROG_MYJFROG_API_TOKEN"}, "")
if config.MyJFrogAPIToken.ValueString() != "" {
myJFrogAPIToken = config.MyJFrogAPIToken.ValueString()
}

if accessToken == "" && myJFrogAPIToken == "" {
resp.Diagnostics.AddError(
"Missing JFrog Access Token",
"While configuring the provider, the Access Token was not found in the JFROG_ACCESS_TOKEN environment variable, provider configuration block access_token attribute, or Terraform Cloud TFC_WORKLOAD_IDENTITY_TOKEN environment variable.",
"Missing JFrog Access Token and MyJFrog API token",
"Neither Access Token nor MyJFrog API Token were found in environment variables or provider configuration. Provider will not function.",
)
return
}

if config.MyJFrogAPIToken.ValueString() != "" {
myJFrogAPIToken = config.MyJFrogAPIToken.ValueString()
if accessToken == "" {
resp.Diagnostics.AddWarning(
"Missing JFrog Access Token",
"Access Token was not found in the JFROG_ACCESS_TOKEN environment variable, provider configuration block access_token attribute, or Terraform Cloud TFC_WORKLOAD_IDENTITY_TOKEN environment variable. Platform functionality will be affected.",
)
}

var myJFrogClient *resty.Client
if len(myJFrogAPIToken) > 0 {
c, err := client.Build("https://my.jfrog.com", productId)
if myJFrogAPIToken == "" {
resp.Diagnostics.AddWarning(
"Missing MyJFrog API Token",
"MyJFrog API Token was not found in the JFROG_MYJFROG_API_TOKEN environment variable or provider configuration block myjfrog_api_token attribute. MyJFrog functionality will be affected.",
)
}

artifactoryVersion := ""
if len(accessToken) > 0 {
_, err = client.AddAuth(platformClient, "", accessToken)
if err != nil {
resp.Diagnostics.AddError(
"Error creating Resty client for MyJFrog",
"Error adding Auth to Resty client",
err.Error(),
)
return
}

c, err = client.AddAuth(c, "", myJFrogAPIToken)
version, err := util.GetArtifactoryVersion(platformClient)
if err != nil {
resp.Diagnostics.AddError(
"Error adding Auth to Resty client for MyJFrog",
err.Error(),
resp.Diagnostics.AddWarning(
"Error getting Artifactory version",
fmt.Sprintf("Provider functionality might be affected by the absence of Artifactory version. %v", err),
)
return
}

myJFrogClient = c
artifactoryVersion = version

featureUsage := fmt.Sprintf("Terraform/%s", req.TerraformVersion)
go util.SendUsage(ctx, platformClient.R(), productId, featureUsage)
}

platformClient, err = client.AddAuth(platformClient, "", accessToken)
myJFrogClient, err := client.Build("https://my.jfrog.com", productId)
if err != nil {
resp.Diagnostics.AddError(
"Error adding Auth to Resty client",
"Error creating Resty client for MyJFrog",
err.Error(),
)
return
}

if config.CheckLicense.IsNull() || config.CheckLicense.ValueBool() {
if licenseErr := util.CheckArtifactoryLicense(platformClient, "Enterprise", "Commercial", "Edge"); licenseErr != nil {
if len(myJFrogAPIToken) > 0 {
_, err := client.AddAuth(myJFrogClient, "", myJFrogAPIToken)
if err != nil {
resp.Diagnostics.AddError(
"Error getting Artifactory license",
licenseErr.Error(),
"Error adding Auth to Resty client for MyJFrog",
err.Error(),
)
return
}
}

version, err := util.GetArtifactoryVersion(platformClient)
if err != nil {
resp.Diagnostics.AddWarning(
"Error getting Artifactory version",
fmt.Sprintf("The provider functionality might be affected by the absence of Artifactory version in the context. %v", err),
)
return
}

featureUsage := fmt.Sprintf("Terraform/%s", req.TerraformVersion)
util.SendUsage(ctx, platformClient.R(), productId, featureUsage)

meta := PlatformProviderMetadata{
ProviderMetadata: util.ProviderMetadata{
Client: platformClient,
ArtifactoryVersion: version,
ArtifactoryVersion: artifactoryVersion,
},
MyJFrogClient: myJFrogClient,
}
Expand Down Expand Up @@ -243,6 +248,7 @@ func (p *PlatformProvider) Schema(ctx context.Context, req provider.SchemaReques
"check_license": schema.BoolAttribute{
Optional: true,
MarkdownDescription: "Toggle for pre-flight checking of Artifactory Pro and Enterprise license. Default to `true`.",
DeprecationMessage: "Remove this attribute from your provider configuration as it is no longer used and the attribute will be removed in the next major version of the provider.",
},
},
}
Expand Down

0 comments on commit cc2da78

Please sign in to comment.