From 6e431de236ddee8a572fe7dba1ddef7e41606174 Mon Sep 17 00:00:00 2001 From: nikhilsbhat Date: Wed, 22 Feb 2023 09:25:28 +0530 Subject: [PATCH] Add support to check the connectivity to GoCD during client creation This avoids errors being thown from all resource block defined incase of connectivity issue. --- docs/index.md | 1 + examples/provider.tf | 1 + go.mod | 2 +- go.sum | 4 ++-- internal/provider/provider.go | 9 +++++++++ pkg/client/client.go | 21 +++++++++++++++++++-- 6 files changed, 33 insertions(+), 5 deletions(-) diff --git a/docs/index.md b/docs/index.md index e3ce56f..f6a4de6 100644 --- a/docs/index.md +++ b/docs/index.md @@ -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 diff --git a/examples/provider.tf b/examples/provider.tf index e5ea031..484592b 100644 --- a/examples/provider.tf +++ b/examples/provider.tf @@ -13,4 +13,5 @@ provider "gocd" { // password = "admin" auth_token = "d8fccbc997d04e917b1490af8e7bf46290ab8c99" loglevel = "debug" +// skip_check = true } \ No newline at end of file diff --git a/go.mod b/go.mod index 61c356c..93e39ac 100644 --- a/go.mod +++ b/go.mod @@ -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 ( diff --git a/go.sum b/go.sum index 348b6de..fb5acd5 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/internal/provider/provider.go b/internal/provider/provider.go index 75602bb..b61c791 100755 --- a/internal/provider/provider.go +++ b/internal/provider/provider.go @@ -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{ diff --git a/pkg/client/client.go b/pkg/client/client.go index 3f91774..160c5d9 100644 --- a/pkg/client/client.go +++ b/pkg/client/client.go @@ -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) { @@ -15,6 +17,7 @@ func GetGoCDClient(ctx context.Context, d *schema.ResourceData) (interface{}, di password string bearerToken string loglevel string + skipCheck bool ca []byte }{} @@ -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 { @@ -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 }