Skip to content

Commit

Permalink
Add support to check the connectivity to GoCD during client creation
Browse files Browse the repository at this point in the history
This avoids errors being thown from all resource block defined incase of connectivity issue.
  • Loading branch information
nikhilsbhat committed Feb 22, 2023
1 parent e07b43a commit 6e431de
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 5 deletions.
1 change: 1 addition & 0 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,4 @@ provider "gocd" {
- `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
- `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
1 change: 1 addition & 0 deletions examples/provider.tf
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ provider "gocd" {
// password = "admin"
auth_token = "d8fccbc997d04e917b1490af8e7bf46290ab8c99"
loglevel = "debug"
// skip_check = true
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ require (
github.com/hashicorp/terraform-plugin-docs v0.13.0
github.com/hashicorp/terraform-plugin-sdk/v2 v2.24.1
github.com/mitchellh/mapstructure v1.5.0
github.com/nikhilsbhat/gocd-sdk-go v0.0.9-0.20230215164616-e5498f6f001a
github.com/nikhilsbhat/gocd-sdk-go v0.0.10-0.20230221063610-c7aae78b564b
)

require (
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -205,8 +205,8 @@ github.com/mitchellh/reflectwalk v1.0.0/go.mod h1:mSTlrgnPZtwu0c4WaC2kGObEpuNDbx
github.com/mitchellh/reflectwalk v1.0.2 h1:G2LzWKi524PWgd3mLHV8Y5k7s6XUvT0Gef6zxSIeXaQ=
github.com/mitchellh/reflectwalk v1.0.2/go.mod h1:mSTlrgnPZtwu0c4WaC2kGObEpuNDbx0jmZXqmk4esnw=
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno=
github.com/nikhilsbhat/gocd-sdk-go v0.0.9-0.20230215164616-e5498f6f001a h1:GoDypOvoIeWtmjeLM/GyXKJWC/DSbeir9z/e/pKlFkc=
github.com/nikhilsbhat/gocd-sdk-go v0.0.9-0.20230215164616-e5498f6f001a/go.mod h1:3XwSMe/nFH/I0Kt2+ToKKWFyD6yvJb4HaoP0dBHytY4=
github.com/nikhilsbhat/gocd-sdk-go v0.0.10-0.20230221063610-c7aae78b564b h1:TjndaPTTJof+0PbIabNy2YekPp01eqROfGGPJhD9UjM=
github.com/nikhilsbhat/gocd-sdk-go v0.0.10-0.20230221063610-c7aae78b564b/go.mod h1:3XwSMe/nFH/I0Kt2+ToKKWFyD6yvJb4HaoP0dBHytY4=
github.com/nsf/jsondiff v0.0.0-20200515183724-f29ed568f4ce h1:RPclfga2SEJmgMmz2k+Mg7cowZ8yv4Trqw9UsJby758=
github.com/oklog/run v1.0.0 h1:Ru7dDtJNOyC66gQ5dQmaCa0qIsAUFY3sFpK1Xk8igrw=
github.com/oklog/run v1.0.0/go.mod h1:dlhp/R75TPv97u0XWUtDeV/lRKWPKSdTuV0TZvrmrQA=
Expand Down
9 changes: 9 additions & 0 deletions internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,15 @@ func Provider() *schema.Provider {
DefaultFunc: schema.EnvDefaultFunc("GOCD_LOGLEVEL", "info"),
Description: "loglevel to be set for the api calls made to GoCD",
},
"skip_check": {
Type: schema.TypeBool,
Required: true,
ForceNew: true,
Computed: false,
DefaultFunc: schema.EnvDefaultFunc("GOCD_SKIP_CHECK", "false"),
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",
},
},

ResourcesMap: map[string]*schema.Resource{
Expand Down
21 changes: 19 additions & 2 deletions pkg/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ package client

import (
"context"
"errors"

"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"
)

func GetGoCDClient(ctx context.Context, d *schema.ResourceData) (interface{}, diag.Diagnostics) {
Expand All @@ -15,6 +17,7 @@ func GetGoCDClient(ctx context.Context, d *schema.ResourceData) (interface{}, di
password string
bearerToken string
loglevel string
skipCheck bool
ca []byte
}{}

Expand Down Expand Up @@ -42,6 +45,12 @@ func GetGoCDClient(ctx context.Context, d *schema.ResourceData) (interface{}, di
clientCfg.bearerToken = authToken.(string)
}

if skipCheck, ok := d.GetOk("skip_check"); !ok {
diag.Errorf("'skip_check' was not set")
} else {
clientCfg.skipCheck = skipCheck.(bool)
}

if caFileContent := d.Get("ca_file").(string); len(caFileContent) == 0 {
diag.Errorf("'ca_file' was not set")
} else {
Expand All @@ -54,13 +63,21 @@ func GetGoCDClient(ctx context.Context, d *schema.ResourceData) (interface{}, di
clientCfg.loglevel = loglevel
}

gocdAuth := gocd.Auth{
goCDAuth := gocd.Auth{
UserName: clientCfg.username,
Password: clientCfg.password,
BearerToken: clientCfg.bearerToken,
}

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

if !clientCfg.skipCheck {
if _, err := goCDClient.GetServerHealth(); err != nil {
if !errors.Is(err, goErr.MarshalError{}) {
return nil, diag.Errorf("errored while connecting to server\nerror: %v\ncheck the baseURL and authorization config before rerunning plan again", err)
}
}
}

return goCDClient, nil
}

0 comments on commit 6e431de

Please sign in to comment.