Skip to content

Commit

Permalink
Add support for API retries at provider level
Browse files Browse the repository at this point in the history
Resolves: #6
  • Loading branch information
nikhilsbhat committed Mar 22, 2024
1 parent 8bfd702 commit 0dc1c56
Show file tree
Hide file tree
Showing 12 changed files with 157 additions and 66 deletions.
15 changes: 14 additions & 1 deletion docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ provider "gocd" {
username = "admin"
auth_token = "d8fccbc997d04e917b1490af8e7bf46290ab8c99"
skip_check = true
retries {
count = 10
wait_time = 2
}
}
// Create cluster profiles in GoCD
Expand Down Expand Up @@ -66,8 +70,17 @@ It supports both basic auth using `password` and bearer token based auth using `
### Optional

- `auth_token` (String) bearer-token to be used while connecting with GoCD (API: https://api.gocd.org/current/#access-tokens, UI: https://docs.gocd.org/current/configuration/access_tokens.html) cannot co-exist with password based auth.
- `base_url` (String) base url of GoCD server, with which this terraform provider will with (https://gocd.myself.com/go)
- `base_url` (String) base url of GoCD server, with which this terraform provider will connect with (https://gocd.myself.com/go)
- `ca_file` (String) CA file contents, to be used while connecting to GoCD server when CA based auth is enabled
- `loglevel` (String) loglevel to be set for the api calls made to GoCD
- `password` (String) password to be used while connecting with GoCD
- `retries` (Block Set) Retry configs to be set for the API calls made forG GoCD server. (see [below for nested schema](#nestedblock--retries))
- `skip_check` (Boolean) setting this to false will skip a validation done during client creation, this helps by avoiding errors being thrown from all resource/data block defined

<a id="nestedblock--retries"></a>
### Nested Schema for `retries`

Optional:

- `count` (Number) Number of times to retry in case of API failures.
- `wait_time` (Number) Time interval to wait between subsequent calls
2 changes: 1 addition & 1 deletion examples/agents_config.tf
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
resource "gocd_agent" "sample_agent" {
uuid = "7c253df5-a262-4573-bcc8-aedc7e87317d"
uuid = "3a3d8e62-6103-4d05-be92-11fbdf21e945"
environments = ["sample_environment_3"]
}

Expand Down
6 changes: 5 additions & 1 deletion examples/provider.tf
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,14 @@ terraform {
}

provider "gocd" {
base_url = "http://localhost:8153/go"
base_url = "http://localhost:8155/go"
username = "admin"
password = "admin"
// auth_token = "d8fccbc997d04e917b1490af8e7bf46290ab8c99"
loglevel = "debug"
// skip_check = true
retries {
count = 10
wait_time = 2
}
}
12 changes: 6 additions & 6 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ require (
github.com/hashicorp/terraform-plugin-sdk/v2 v2.26.1
github.com/mitchellh/mapstructure v1.5.0
github.com/nikhilsbhat/gocd-cli v0.1.0
github.com/nikhilsbhat/gocd-sdk-go v0.1.9-0.20230922033426-b10dfcdb3dbd
github.com/nikhilsbhat/gocd-sdk-go v0.1.9
github.com/sirupsen/logrus v1.9.3
github.com/spf13/cast v1.5.0
gopkg.in/yaml.v3 v3.0.1
Expand All @@ -24,7 +24,7 @@ require (
github.com/bgentry/speakeasy v0.1.0 // indirect
github.com/fatih/color v1.15.0 // indirect
github.com/ghodss/yaml v1.0.0 // indirect
github.com/go-resty/resty/v2 v2.8.0 // indirect
github.com/go-resty/resty/v2 v2.12.0 // indirect
github.com/go-test/deep v1.0.8 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/google/uuid v1.3.0 // indirect
Expand Down Expand Up @@ -67,11 +67,11 @@ require (
github.com/vmihailenco/msgpack/v5 v5.3.5 // indirect
github.com/vmihailenco/tagparser/v2 v2.0.0 // indirect
github.com/zclconf/go-cty v1.13.1 // indirect
golang.org/x/crypto v0.14.0 // indirect
golang.org/x/crypto v0.21.0 // indirect
golang.org/x/mod v0.8.0 // indirect
golang.org/x/net v0.17.0 // indirect
golang.org/x/sys v0.13.0 // indirect
golang.org/x/text v0.13.0 // indirect
golang.org/x/net v0.22.0 // indirect
golang.org/x/sys v0.18.0 // indirect
golang.org/x/text v0.14.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/genproto v0.0.0-20230410155749-daa745c078e1 // indirect
google.golang.org/grpc v1.56.3 // indirect
Expand Down
35 changes: 19 additions & 16 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ github.com/go-git/go-billy/v5 v5.3.1/go.mod h1:pmpqyWchKfYfrkb/UVH4otLvyi/5gJlGI
github.com/go-git/go-git-fixtures/v4 v4.2.1/go.mod h1:K8zd3kDUAykwTdDCr+I0per6Y6vMiRR/nnVTBtavnB0=
github.com/go-git/go-git/v5 v5.4.2 h1:BXyZu9t0VkbiHtqrsvdq39UDhGJTl1h55VW6CSC4aY4=
github.com/go-git/go-git/v5 v5.4.2/go.mod h1:gQ1kArt6d+n+BGd+/B/I74HwRTLhth2+zti4ihgckDc=
github.com/go-resty/resty/v2 v2.8.0 h1:J29d0JFWwSWrDCysnOK/YjsPMLQTx0TvgJEHVGvf2L8=
github.com/go-resty/resty/v2 v2.8.0/go.mod h1:UCui0cMHekLrSntoMyofdSTaPpinlRHFtPpizuyDW2w=
github.com/go-resty/resty/v2 v2.12.0 h1:rsVL8P90LFvkUYq/V5BTVe203WfRIU4gvcf+yfzJzGA=
github.com/go-resty/resty/v2 v2.12.0/go.mod h1:o0yGPrkS3lOe1+eFajk6kBW8ScXzwU3hD69/gt2yB/0=
github.com/go-test/deep v1.0.8 h1:TDsG77qcSprGbC6vTN8OuXp5g+J+b5Pcguhf7Zt61VM=
github.com/go-test/deep v1.0.8/go.mod h1:5C2ZWiW0ErCdrYzpqxLbTX7MG14M9iiw8DgHncVwcsE=
github.com/golang/protobuf v1.1.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
Expand Down Expand Up @@ -170,8 +170,8 @@ github.com/mitchellh/reflectwalk v1.0.2/go.mod h1:mSTlrgnPZtwu0c4WaC2kGObEpuNDbx
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno=
github.com/nikhilsbhat/gocd-cli v0.1.0 h1:k7myHhSy6tuPZm0dO3RBVjAqBZFQh4XalQZvxpd2VfM=
github.com/nikhilsbhat/gocd-cli v0.1.0/go.mod h1:gfczP6M7WOOXIEc9QdAJ7R846+3CfQsdSEXC44fg3Ng=
github.com/nikhilsbhat/gocd-sdk-go v0.1.9-0.20230922033426-b10dfcdb3dbd h1:7GTqH/4aJkJ3HfbsrgsyXf95WUoFNCb3HR6N/6e1Qb8=
github.com/nikhilsbhat/gocd-sdk-go v0.1.9-0.20230922033426-b10dfcdb3dbd/go.mod h1:ubwvA7YgcWDUXiCghrJGRKTdeVuXpDmNzIp6ZvVzdfk=
github.com/nikhilsbhat/gocd-sdk-go v0.1.9 h1:QG16EwKhxocn0Imza6S+/lE8S0MOw54UDebfJotQx2s=
github.com/nikhilsbhat/gocd-sdk-go v0.1.9/go.mod h1:FFVOLXPhn55evuuyfiRJ23R8I5Ks2n31/DXLBQmaNsw=
github.com/oklog/run v1.0.0 h1:Ru7dDtJNOyC66gQ5dQmaCa0qIsAUFY3sFpK1Xk8igrw=
github.com/oklog/run v1.0.0/go.mod h1:dlhp/R75TPv97u0XWUtDeV/lRKWPKSdTuV0TZvrmrQA=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
Expand Down Expand Up @@ -230,9 +230,9 @@ golang.org/x/crypto v0.0.0-20210322153248-0c34fe9e7dc2/go.mod h1:T9bdIzuCu7OtxOm
golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b/go.mod h1:T9bdIzuCu7OtxOm1hfPfRQxPLYneinmdGuTeoZ9dtd4=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/crypto v0.5.0/go.mod h1:NK/OQwhpMQP3MwtdjgLlYHnH9ebylxKWv3e0fK+mkQU=
golang.org/x/crypto v0.13.0/go.mod h1:y6Z2r+Rw4iayiXXAIxJIDAJ1zMW4yaTpebo8fPOliYc=
golang.org/x/crypto v0.14.0 h1:wBqGXzWJW6m1XrIKlAH0Hs1JJ7+9KBwnIO8v66Q9cHc=
golang.org/x/crypto v0.14.0/go.mod h1:MVFd36DqK4CsrnJYDkBA3VC4m2GkXAM0PvzMCn4JQf4=
golang.org/x/crypto v0.19.0/go.mod h1:Iy9bg/ha4yyC70EfRS8jz+B6ybOBKMaSxLj6P6oBDfU=
golang.org/x/crypto v0.21.0 h1:X31++rzVUdKhX5sWmSOFZxx8UW/ldWx55cbf08iNAMA=
golang.org/x/crypto v0.21.0/go.mod h1:0BP7YvVV9gBbVKyeTG0Gyn+gZm94bibOW5BjDEYAOMs=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
golang.org/x/mod v0.7.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/mod v0.8.0 h1:LUYupSeNrTNCGzR/hVBk2NHZO4hXcVaW1k4Qx7rjPx8=
Expand All @@ -246,9 +246,9 @@ golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug
golang.org/x/net v0.5.0/go.mod h1:DivGGAXEgPSlEBzxGzZI+ZLohi+xUj054jfeKui00ws=
golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
golang.org/x/net v0.10.0/go.mod h1:0qNGK6F8kojg2nk9dLZ2mShWaEBan6FAoqfSigmmuDg=
golang.org/x/net v0.15.0/go.mod h1:idbUs1IY1+zTqbi8yxTbhexhEEk5ur9LInksu6HrEpk=
golang.org/x/net v0.17.0 h1:pVaXccu2ozPjCXewfr1S7xza/zcXTity9cCdXQYSjIM=
golang.org/x/net v0.17.0/go.mod h1:NxSsAGuq816PNPmqtQdLE42eU2Fs7NoRIZrHJAlaCOE=
golang.org/x/net v0.21.0/go.mod h1:bIjVDfnllIU7BJ2DNgfnXvpSvtn8VRwhlsaeUTyUS44=
golang.org/x/net v0.22.0 h1:9sGLhx7iRIHEiX0oAJ3MRZMUCElJgy7Br1nO+AMN3Tc=
golang.org/x/net v0.22.0/go.mod h1:JKghWKKOSdJwpW2GEx0Ja7fmaKnMsbu+MWVZTokSYmg=
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
Expand Down Expand Up @@ -277,24 +277,27 @@ golang.org/x/sys v0.4.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.13.0 h1:Af8nKPmuFypiUBjVoU9V20FiaFXOcuZI21p0ycVYYGE=
golang.org/x/sys v0.13.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.17.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.18.0 h1:DBdB3niSjOA/O0blCZBqDefyWNYveAYMNF1Wum0DYQ4=
golang.org/x/sys v0.18.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.4.0/go.mod h1:9P2UbLfCdcvo3p/nzKvsmas4TnlujnuoV9hGgYzW1lQ=
golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k=
golang.org/x/term v0.8.0/go.mod h1:xPskH00ivmX89bAKVGSKKtLOWNx2+17Eiy94tnKShWo=
golang.org/x/term v0.12.0/go.mod h1:owVbMEjm3cBLCHdkQu9b1opXd4ETQWc3BhuQGKgXgvU=
golang.org/x/term v0.17.0/go.mod h1:lLRBjIVuehSbZlaOtGMbcMncT+aqLLLmKrsjNrUguwk=
golang.org/x/term v0.18.0/go.mod h1:ILwASektA3OnRv7amZ1xhE/KTR+u50pbXfZ03+6Nx58=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/text v0.6.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
golang.org/x/text v0.13.0 h1:ablQoSUd0tRdKxZewP80B+BaqeKJuVhuRxj/dkrun3k=
golang.org/x/text v0.13.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
golang.org/x/time v0.5.0 h1:o7cqy6amK/52YcAKIPlM3a+Fpj35zvRj2TP+e1xFSfk=
golang.org/x/time v0.5.0/go.mod h1:3BpzKBy/shNhVucY/MWOyx10tF3SFh9QdLuxbVysPQM=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
Expand Down
27 changes: 27 additions & 0 deletions internal/provider/gocd_provider_schemas.go
Original file line number Diff line number Diff line change
Expand Up @@ -562,3 +562,30 @@ func usersANdRolesSchema() *schema.Resource {
},
}
}

func retrySchemas() *schema.Schema {
return &schema.Schema{
Type: schema.TypeSet,
Optional: true,
Computed: false,
Description: "Retry configs to be set for the API calls made forG GoCD server.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"count": {
Type: schema.TypeInt,
Optional: true,
Computed: false,
ForceNew: false,
Description: "Number of times to retry in case of API failures.",
},
"wait_time": {
Type: schema.TypeInt,
Optional: true,
Computed: false,
Required: false,
Description: "Time interval to wait between subsequent calls",
},
},
},
}
}
3 changes: 2 additions & 1 deletion internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func Provider() *schema.Provider {
ForceNew: true,
Computed: false,
DefaultFunc: schema.EnvDefaultFunc("GOCD_BASE_URL", "www.gocd.com"),
Description: "base url of GoCD server, with which this terraform provider will with (https://gocd.myself.com/go)",
Description: "base url of GoCD server, with which this terraform provider will connect with (https://gocd.myself.com/go)",
},
"ca_file": {
Type: schema.TypeString,
Expand Down Expand Up @@ -84,6 +84,7 @@ func Provider() *schema.Provider {
Description: "setting this to false will skip a validation done during client creation, this helps by avoiding " +
"errors being thrown from all resource/data block defined",
},
"retries": retrySchemas(),
},

ResourcesMap: map[string]*schema.Resource{
Expand Down
40 changes: 20 additions & 20 deletions internal/provider/resource_auth_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,32 +72,32 @@ func resourceAuthConfig() *schema.Resource {
func resourceAuthConfigCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
defaultConfig := meta.(gocd.GoCd)

if d.IsNewResource() {
id := d.Id()

if len(id) == 0 {
resourceID := utils.String(d.Get(utils.TerraformResourceProfileID))
id = resourceID
}
if !d.IsNewResource() {
return nil
}

cfg := gocd.CommonConfig{
ID: utils.String(d.Get(utils.TerraformResourceProfileID)),
PluginID: utils.String(d.Get(utils.TerraformResourcePluginID)),
Properties: getPluginConfiguration(d.Get(utils.TerraformResourceProperties)),
AllowOnlyKnownUsers: utils.Bool(d.Get(utils.TerraformResourceAllowKnownUser)),
}
id := d.Id()

_, err := defaultConfig.CreateAuthConfig(cfg)
if err != nil {
return diag.Errorf("creating auth configuration %s errored with %v", cfg.ID, err)
}
if len(id) == 0 {
resourceID := utils.String(d.Get(utils.TerraformResourceProfileID))
id = resourceID
}

d.SetId(id)
cfg := gocd.CommonConfig{
ID: utils.String(d.Get(utils.TerraformResourceProfileID)),
PluginID: utils.String(d.Get(utils.TerraformResourcePluginID)),
Properties: getPluginConfiguration(d.Get(utils.TerraformResourceProperties)),
AllowOnlyKnownUsers: utils.Bool(d.Get(utils.TerraformResourceAllowKnownUser)),
}

return resourceAuthConfigRead(ctx, d, meta)
_, err := defaultConfig.CreateAuthConfig(cfg)
if err != nil {
return diag.Errorf("creating auth configuration %s errored with %v", cfg.ID, err)
}

return nil
d.SetId(id)

return resourceAuthConfigRead(ctx, d, meta)
}

func resourceAuthConfigRead(_ context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
Expand Down
38 changes: 19 additions & 19 deletions internal/provider/resource_elastic_agent_profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,31 +50,31 @@ func resourceElasticAgentProfile() *schema.Resource {
func resourceElasticAgentProfileCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
defaultConfig := meta.(gocd.GoCd)

if d.IsNewResource() {
id := d.Id()

if len(id) == 0 {
resourceID := utils.String(d.Get(utils.TerraformResourceProfileID))
id = resourceID
}
if !d.IsNewResource() {
return nil
}

cfg := gocd.CommonConfig{
ID: utils.String(d.Get(utils.TerraformResourceProfileID)),
ClusterProfileID: utils.String(d.Get(utils.TerraformResourceClusterProfileID)),
Properties: getPluginConfiguration(d.Get(utils.TerraformResourceProperties)),
}
id := d.Id()

_, err := defaultConfig.CreateElasticAgentProfile(cfg)
if err != nil {
return diag.Errorf("creating elastic agent profile %s for cluster profile %s errored with %v", cfg.ID, cfg.ClusterProfileID, err)
}
if len(id) == 0 {
resourceID := utils.String(d.Get(utils.TerraformResourceProfileID))
id = resourceID
}

d.SetId(id)
cfg := gocd.CommonConfig{
ID: utils.String(d.Get(utils.TerraformResourceProfileID)),
ClusterProfileID: utils.String(d.Get(utils.TerraformResourceClusterProfileID)),
Properties: getPluginConfiguration(d.Get(utils.TerraformResourceProperties)),
}

return resourceElasticAgentProfileRead(ctx, d, meta)
_, err := defaultConfig.CreateElasticAgentProfile(cfg)
if err != nil {
return diag.Errorf("creating elastic agent profile %s for cluster profile %s errored with %v", cfg.ID, cfg.ClusterProfileID, err)
}

return nil
d.SetId(id)

return resourceElasticAgentProfileRead(ctx, d, meta)
}

func resourceElasticAgentProfileRead(_ context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
Expand Down
27 changes: 27 additions & 0 deletions pkg/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ package client
import (
"context"
"errors"
"log"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/nikhilsbhat/gocd-sdk-go"
goErr "github.com/nikhilsbhat/gocd-sdk-go/pkg/errors"
"github.com/nikhilsbhat/terraform-provider-gocd/pkg/utils"
)

func GetGoCDClient(_ context.Context, d *schema.ResourceData) (interface{}, diag.Diagnostics) {
Expand Down Expand Up @@ -71,6 +73,17 @@ func GetGoCDClient(_ context.Context, d *schema.ResourceData) (interface{}, diag

goCDClient := gocd.NewClient(clientCfg.url, goCDAuth, clientCfg.loglevel, clientCfg.ca)

retryConfigs := getRetryConfig(d.Get(utils.TerraformResourceRetries))
if retryConfigs.count != 0 {
log.Printf("setting API retry count to %d:\n", retryConfigs.count)
goCDClient.SetRetryCount(retryConfigs.count)
}

if retryConfigs.waitTime != 0 {
log.Printf("setting API retry wait time to %d:\n", retryConfigs.waitTime)
goCDClient.SetRetryWaitTime(retryConfigs.waitTime)
}

if !clientCfg.skipCheck {
if _, err := goCDClient.GetServerHealth(); err != nil {
if !errors.Is(err, goErr.MarshalError{}) {
Expand All @@ -81,3 +94,17 @@ func GetGoCDClient(_ context.Context, d *schema.ResourceData) (interface{}, diag

return goCDClient, nil
}

type retryConfig struct {
count int
waitTime int
}

func getRetryConfig(retryConfigs interface{}) retryConfig {
flattenedRetryConfigs := retryConfigs.(*schema.Set).List()[0].(map[string]interface{})

return retryConfig{
count: flattenedRetryConfigs[utils.TerraformResourceCount].(int),
waitTime: flattenedRetryConfigs[utils.TerraformResourceWaitTime].(int),
}
}
3 changes: 3 additions & 0 deletions pkg/utils/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,7 @@ const (
TerraformResourceAuthConfigID = "auth_config_id"
TerraformResourceOperate = "operate"
TerraformResourceAdmins = "admins"
TerraformResourceRetries = "retries"
TerraformResourceCount = "count"
TerraformResourceWaitTime = "wait_time"
)
Loading

0 comments on commit 0dc1c56

Please sign in to comment.