Skip to content

Commit

Permalink
Merge pull request #128 from jfrog/GH-103-fix-flooding-issue
Browse files Browse the repository at this point in the history
Gh 103 fix flooding issue
  • Loading branch information
chb0github authored Sep 14, 2021
2 parents 36b5503 + e64261f commit c9fc095
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 5 deletions.
5 changes: 3 additions & 2 deletions pkg/artifactory/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,13 +115,14 @@ func buildResty(URL string) (*resty.Client, error) {
return fmt.Errorf("no response found")
}
if response.StatusCode() >= http.StatusBadRequest {
return fmt.Errorf("request failure %d: %s\n", response.StatusCode(), string(response.Body()[:]))
return fmt.Errorf("%d %s %s\n%s", response.StatusCode(),response.Request.Method, response.Request.URL, string(response.Body()[:]))
}
return nil
}).
SetHeader("content-type", "application/json").
SetHeader("accept", "*/*").
SetHeader("user-agent", "jfrog/terraform-provider-artifactory:"+Version)
SetHeader("user-agent", "jfrog/terraform-provider-artifactory:"+Version).
SetRetryCount(5)
restyBase.DisableWarn = true
return restyBase, nil

Expand Down
12 changes: 10 additions & 2 deletions pkg/artifactory/resource_artifactory_local_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"

"github.com/go-resty/resty/v2"
"regexp"

"net/http"

Expand Down Expand Up @@ -187,11 +188,18 @@ func unmarshalLocalRepository(data *schema.ResourceData) MessyRepo {
return repo
}

func retryOnOverload(response *resty.Response, _ error) bool {
// either 400 or 500 error code seem to appear with this problem
return regexp.MustCompile(".*Could not merge and save new descriptor.*").MatchString(string(response.Body()[:]))
}

func resourceLocalRepositoryCreate(d *schema.ResourceData, m interface{}) error {

repo := unmarshalLocalRepository(d)

_, err := m.(*resty.Client).R().SetBody(repo).Put(repositoriesEndpoint + repo.Key)
// so that we don't effect the settings of the general client, make a copy
// There is an outstanding PR to do this per request
newClient := m.(*resty.Client)
_, err := newClient.AddRetryCondition(retryOnOverload).R().SetBody(repo).Put(repositoriesEndpoint + repo.Key)

if err != nil {
return err
Expand Down
16 changes: 15 additions & 1 deletion sample.tf
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ terraform {
required_providers {
artifactory = {
source = "registry.terraform.io/jfrog/artifactory"
version = "2.3.3"
version = "2.3.5"
}
}
}
Expand Down Expand Up @@ -44,13 +44,27 @@ variable "supported_repo_types" {
"vcs",
]
}
resource "random_id" "randid" {
byte_length = 16
}


resource "artifactory_local_repository" "local" {
count = length(var.supported_repo_types)
key = "${var.supported_repo_types[count.index]}-local"
package_type = var.supported_repo_types[count.index]
xray_index = false
description = "hello ${var.supported_repo_types[count.index]}-local"
}

resource "artifactory_local_repository" "local-rand" {
count = 100
key = "foo-${count.index}-local"
package_type = var.supported_repo_types[random_id.randid.dec % length(var.supported_repo_types)]
xray_index = true
description = "hello ${count.index}-local"
}

provider "artifactory" {
// supply ARTIFACTORY_USERNAME, _PASSWORD and _URL as env vars
}
Expand Down

0 comments on commit c9fc095

Please sign in to comment.