Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Defer connecting to gateway until needed #45

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion spinnaker/api/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand Down
32 changes: 23 additions & 9 deletions spinnaker/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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) {
Expand All @@ -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
}
24 changes: 20 additions & 4 deletions spinnaker/resource_application.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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 {
Expand All @@ -66,15 +74,23 @@ 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)
}

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
Expand Down
2 changes: 1 addition & 1 deletion spinnaker/resource_application_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
32 changes: 26 additions & 6 deletions spinnaker/resource_pipeline.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)

Expand Down Expand Up @@ -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)
Expand All @@ -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")
}
Expand All @@ -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)

Expand All @@ -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)

Expand Down
30 changes: 25 additions & 5 deletions spinnaker/resource_pipeline_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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{})
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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 {
Expand All @@ -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{}
Expand Down