Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove special error code handling in response middleware #56

Merged
merged 2 commits into from
Apr 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
## 1.22.2 (Apr 4, 2024)

BUG FIXES:

* Remove special error code handling in response middleware
* Fix error handling in check license and version func

PR: [#56](https://github.com/jfrog/terraform-provider-shared/pull/56)

## 1.22.1 (Mar 20, 2024)

IMPROVEMENTS:
Expand Down
6 changes: 0 additions & 6 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ import (
"fmt"
"net/http"
"net/url"
"os"
"regexp"
"slices"
"strings"

"github.com/go-resty/resty/v2"
Expand Down Expand Up @@ -36,10 +34,6 @@ func Build(URL, productId string) (*resty.Client, error) {
return nil
}

tfLogLevel := strings.ToLower(os.Getenv("TF_LOG"))
if response.StatusCode() >= http.StatusBadRequest && slices.Contains([]string{"debug", "trace"}, tfLogLevel) {
return fmt.Errorf("\n%d %s %s\n%s", response.StatusCode(), response.Request.Method, response.Request.URL, string(response.Body()[:]))
}
return nil
}).
SetHeader("content-type", "application/json").
Expand Down
7 changes: 6 additions & 1 deletion util/fw/fw.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ func CheckArtifactoryLicense(client *resty.Client, licenseTypesToCheck ...string
}

licensesWrapper := LicensesWrapper{}
_, err := client.R().
resp, err := client.R().
SetResult(&licensesWrapper).
Get("/artifactory/api/system/license")

Expand All @@ -101,6 +101,11 @@ func CheckArtifactoryLicense(client *resty.Client, licenseTypesToCheck ...string
return
}

if resp.IsError() {
ds.AddError("Failed to check for license. If your usage doesn't require admin permission, you can set `check_license` attribute to `false` to skip this check.", resp.String())
return
}

var licenseType string
if len(licensesWrapper.Licenses) > 0 {
licenseType = licensesWrapper.Licenses[0].Type
Expand Down
6 changes: 5 additions & 1 deletion util/sdk/sdk.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,14 +237,18 @@ func CheckArtifactoryLicense(client *resty.Client, licenseTypesToCheck ...string
}

licensesWrapper := LicensesWrapper{}
_, err := client.R().
resp, err := client.R().
SetResult(&licensesWrapper).
Get("/artifactory/api/system/license")

if err != nil {
return diag.Errorf("Failed to check for license. If your usage doesn't require admin permission, you can set `check_license` attribute to `false` to skip this check. %s", err)
}

if resp.IsError() {
return diag.Errorf("Failed to check for license. If your usage doesn't require admin permission, you can set `check_license` attribute to `false` to skip this check. %s", resp.String())
}

var licenseType string
if len(licensesWrapper.Licenses) > 0 {
licenseType = licensesWrapper.Licenses[0].Type
Expand Down
18 changes: 15 additions & 3 deletions util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,17 @@ func SendUsage(ctx context.Context, client *resty.Client, productId string, feat

usage := UsageStruct{productId, features}

_, err := client.R().
resp, err := client.R().
SetBody(usage).
Post("artifactory/api/system/usage")

if err != nil {
tflog.Info(ctx, fmt.Sprintf("failed to send usage: %v", err))
}

if resp.IsError() {
tflog.Info(ctx, fmt.Sprintf("failed to send usage: %v", resp.String()))
}
}

func CheckVersion(versionToCheck string, supportedVersion string) (bool, error) {
Expand All @@ -87,14 +91,18 @@ func GetArtifactoryVersion(client *resty.Client) (string, error) {
}

artifactoryVersion := ArtifactoryVersion{}
_, err := client.R().
resp, err := client.R().
SetResult(&artifactoryVersion).
Get("/artifactory/api/system/version")

if err != nil {
return "", fmt.Errorf("failed to get Artifactory version. %s", err)
}

if resp.IsError() {
return "", fmt.Errorf("failed to get Artifactory version. %s", resp.String())
}

return artifactoryVersion.Version, nil
}

Expand All @@ -104,14 +112,18 @@ func GetXrayVersion(client *resty.Client) (string, error) {
}

xrayVersion := XrayVersion{}
_, err := client.R().
resp, err := client.R().
SetResult(&xrayVersion).
Get("/xray/api/v1/system/version")

if err != nil {
return "", fmt.Errorf("failed to get Xray version. %s", err)
}

if resp.IsError() {
return "", fmt.Errorf("failed to get Xray version. %s", resp.String())
}

return xrayVersion.Version, nil
}

Expand Down
Loading