diff --git a/spinnaker/api/application.go b/spinnaker/api/application.go index 6a5b389..c836e31 100644 --- a/spinnaker/api/application.go +++ b/spinnaker/api/application.go @@ -16,7 +16,7 @@ func GetApplication(client *gate.GatewayClient, applicationName string, dest int if resp != nil && resp.StatusCode == http.StatusNotFound { return fmt.Errorf("Application '%s' not found\n", applicationName) } else if resp.StatusCode != http.StatusOK { - return fmt.Errorf("Encountered an error getting application, status code: %d\n", resp.StatusCode) + return fmt.Errorf("Encountered an error getting application '%s', status code: %d\n", applicationName, resp.StatusCode) } } diff --git a/spinnaker/provider.go b/spinnaker/provider.go index 7821a8a..1d9bf35 100644 --- a/spinnaker/provider.go +++ b/spinnaker/provider.go @@ -4,6 +4,7 @@ import ( "github.com/hashicorp/terraform/helper/schema" "github.com/spf13/pflag" gate "github.com/spinnaker/spin/cmd/gateclient" + "sync" ) func Provider() *schema.Provider { @@ -48,8 +49,24 @@ func Provider() *schema.Provider { } type gateConfig struct { - server string - client *gate.GatewayClient + server string + flags *pflag.FlagSet + clientOnce sync.Once + client *gate.GatewayClient + clientErr error +} + +func (g *gateConfig) lazyInit() { + g.clientOnce.Do(func() { + client, err := gate.NewGateClient(g.flags) + g.client = client + g.clientErr = err + }) +} + +func (g *gateConfig) getClient() (*gate.GatewayClient, error) { + g.lazyInit() + return g.client, g.clientErr } func providerConfigureFunc(data *schema.ResourceData) (interface{}, error) { @@ -67,12 +84,9 @@ func providerConfigureFunc(data *schema.ResourceData) (interface{}, error) { flags.String("config", config, "") flags.String("default-headers", defaultHeaders, "") // flags.Parse() - client, err := gate.NewGateClient(flags) - if err != nil { - return nil, err - } - return gateConfig{ + gc := gateConfig{ + flags: flags, server: data.Get("server").(string), - client: client, - }, nil + } + return gc, nil } diff --git a/spinnaker/resource_application.go b/spinnaker/resource_application.go index 648afa5..d880457 100644 --- a/spinnaker/resource_application.go +++ b/spinnaker/resource_application.go @@ -37,7 +37,11 @@ type applicationRead struct { func resourceApplicationCreate(data *schema.ResourceData, meta interface{}) error { clientConfig := meta.(gateConfig) - client := clientConfig.client + client, err := clientConfig.getClient() + if err != nil { + return err + } + application := data.Get("application").(string) email := data.Get("email").(string) @@ -50,7 +54,11 @@ func resourceApplicationCreate(data *schema.ResourceData, meta interface{}) erro func resourceApplicationRead(data *schema.ResourceData, meta interface{}) error { clientConfig := meta.(gateConfig) - client := clientConfig.client + client, err := clientConfig.getClient() + if err != nil { + return err + } + applicationName := data.Get("application").(string) var app applicationRead if err := api.GetApplication(client, applicationName, &app); err != nil { @@ -66,7 +74,11 @@ func resourceApplicationUpdate(data *schema.ResourceData, meta interface{}) erro func resourceApplicationDelete(data *schema.ResourceData, meta interface{}) error { clientConfig := meta.(gateConfig) - client := clientConfig.client + client, err := clientConfig.getClient() + if err != nil { + return err + } + applicationName := data.Get("application").(string) return api.DeleteAppliation(client, applicationName) @@ -74,7 +86,11 @@ func resourceApplicationDelete(data *schema.ResourceData, meta interface{}) erro func resourceApplicationExists(data *schema.ResourceData, meta interface{}) (bool, error) { clientConfig := meta.(gateConfig) - client := clientConfig.client + client, err := clientConfig.getClient() + if err != nil { + return false, err + } + applicationName := data.Get("application").(string) var app applicationRead diff --git a/spinnaker/resource_application_test.go b/spinnaker/resource_application_test.go index 59caf55..c2e9543 100644 --- a/spinnaker/resource_application_test.go +++ b/spinnaker/resource_application_test.go @@ -40,7 +40,7 @@ func testAccCheckApplicationExists(n string) resource.TestCheckFunc { if rs.Primary.ID == "" { return fmt.Errorf("No Application ID is set") } - client := testAccProvider.Meta().(gateConfig).client + client, _ := testAccProvider.Meta().(*gateConfig).getClient() err := resource.Retry(1*time.Minute, func() *resource.RetryError { _, resp, err := client.ApplicationControllerApi.GetApplicationUsingGET(client.Context, rs.Primary.ID, nil) if resp != nil { diff --git a/spinnaker/resource_pipeline.go b/spinnaker/resource_pipeline.go index b63b0f4..7d8f008 100644 --- a/spinnaker/resource_pipeline.go +++ b/spinnaker/resource_pipeline.go @@ -49,7 +49,11 @@ type pipelineRead struct { func resourcePipelineCreate(data *schema.ResourceData, meta interface{}) error { clientConfig := meta.(gateConfig) - client := clientConfig.client + client, err := clientConfig.getClient() + if err != nil { + return err + } + applicationName := data.Get("application").(string) pipelineName := data.Get("name").(string) pipeline := data.Get("pipeline").(string) @@ -72,7 +76,11 @@ func resourcePipelineCreate(data *schema.ResourceData, meta interface{}) error { func resourcePipelineRead(data *schema.ResourceData, meta interface{}) error { clientConfig := meta.(gateConfig) - client := clientConfig.client + client, err := clientConfig.getClient() + if err != nil { + return err + } + applicationName := data.Get("application").(string) pipelineName := data.Get("name").(string) @@ -102,7 +110,11 @@ func resourcePipelineRead(data *schema.ResourceData, meta interface{}) error { func resourcePipelineUpdate(data *schema.ResourceData, meta interface{}) error { clientConfig := meta.(gateConfig) - client := clientConfig.client + client, err := clientConfig.getClient() + if err != nil { + return err + } + applicationName := data.Get("application").(string) pipelineName := data.Get("name").(string) pipeline := data.Get("pipeline").(string) @@ -113,7 +125,7 @@ func resourcePipelineUpdate(data *schema.ResourceData, meta interface{}) error { } var pipe map[string]interface{} - err := json.Unmarshal([]byte(pipeline), &pipe) + err = json.Unmarshal([]byte(pipeline), &pipe) if err != nil { return fmt.Errorf("could not unmarshal pipeline") } @@ -130,7 +142,11 @@ func resourcePipelineUpdate(data *schema.ResourceData, meta interface{}) error { func resourcePipelineDelete(data *schema.ResourceData, meta interface{}) error { clientConfig := meta.(gateConfig) - client := clientConfig.client + client, err := clientConfig.getClient() + if err != nil { + return err + } + applicationName := data.Get("application").(string) pipelineName := data.Get("name").(string) @@ -143,7 +159,11 @@ func resourcePipelineDelete(data *schema.ResourceData, meta interface{}) error { func resourcePipelineExists(data *schema.ResourceData, meta interface{}) (bool, error) { clientConfig := meta.(gateConfig) - client := clientConfig.client + client, err := clientConfig.getClient() + if err != nil { + return false, err + } + applicationName := data.Get("application").(string) pipelineName := data.Get("name").(string) diff --git a/spinnaker/resource_pipeline_template.go b/spinnaker/resource_pipeline_template.go index 3478393..0bfc3c9 100644 --- a/spinnaker/resource_pipeline_template.go +++ b/spinnaker/resource_pipeline_template.go @@ -39,7 +39,11 @@ type templateRead struct { func resourcePipelineTemplateCreate(data *schema.ResourceData, meta interface{}) error { clientConfig := meta.(gateConfig) - client := clientConfig.client + client, err := clientConfig.getClient() + if err != nil { + return err + } + var templateName string template := data.Get("template").(string) @@ -72,7 +76,11 @@ func resourcePipelineTemplateCreate(data *schema.ResourceData, meta interface{}) func resourcePipelineTemplateRead(data *schema.ResourceData, meta interface{}) error { clientConfig := meta.(gateConfig) - client := clientConfig.client + client, err := clientConfig.getClient() + if err != nil { + return err + } + templateName := data.Id() t := make(map[string]interface{}) @@ -107,7 +115,11 @@ func resourcePipelineTemplateRead(data *schema.ResourceData, meta interface{}) e func resourcePipelineTemplateUpdate(data *schema.ResourceData, meta interface{}) error { clientConfig := meta.(gateConfig) - client := clientConfig.client + client, err := clientConfig.getClient() + if err != nil { + return err + } + var templateName string template := data.Get("template").(string) @@ -137,7 +149,11 @@ func resourcePipelineTemplateUpdate(data *schema.ResourceData, meta interface{}) func resourcePipelineTemplateDelete(data *schema.ResourceData, meta interface{}) error { clientConfig := meta.(gateConfig) - client := clientConfig.client + client, err := clientConfig.getClient() + if err != nil { + return err + } + templateName := data.Id() if err := api.DeletePipelineTemplate(client, templateName); err != nil { @@ -150,7 +166,11 @@ func resourcePipelineTemplateDelete(data *schema.ResourceData, meta interface{}) func resourcePipelineTemplateExists(data *schema.ResourceData, meta interface{}) (bool, error) { clientConfig := meta.(gateConfig) - client := clientConfig.client + client, err := clientConfig.getClient() + if err != nil { + return false, err + } + templateName := data.Id() t := &templateRead{}