From 288e29b1e07447d58d694045d8a49c3fb424d678 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9my=20L=C3=A9one?= Date: Wed, 28 Oct 2020 17:46:40 +0100 Subject: [PATCH] scaleway: use the SDK functions to load profile from file and env --- builder/scaleway/config.go | 46 +++++++------------ builder/scaleway/step_create_image.go | 4 +- builder/scaleway/step_create_server.go | 4 +- builder/scaleway/step_pre_validate.go | 4 +- builder/scaleway/step_shutdown.go | 2 +- builder/scaleway/step_snapshot.go | 2 +- .../builder/scaleway/Config-required.mdx | 4 ++ 7 files changed, 28 insertions(+), 38 deletions(-) diff --git a/builder/scaleway/config.go b/builder/scaleway/config.go index c88e4fea5..063a0de84 100644 --- a/builder/scaleway/config.go +++ b/builder/scaleway/config.go @@ -6,8 +6,6 @@ package scaleway import ( "errors" "fmt" - "log" - "os" "github.com/hashicorp/packer/common" "github.com/hashicorp/packer/common/uuid" @@ -116,51 +114,39 @@ func (c *Config) Prepare(raws ...interface{}) ([]string, error) { c.UserAgent = useragent.String() - // Deprecated variables - if c.Organization == "" { - if os.Getenv("SCALEWAY_ORGANIZATION") != "" { - c.Organization = os.Getenv("SCALEWAY_ORGANIZATION") - } else { - log.Printf("Deprecation warning: Use SCALEWAY_ORGANIZATION environment variable and organization_id argument instead of api_access_key argument and SCALEWAY_API_ACCESS_KEY environment variable.") - c.Organization = os.Getenv("SCALEWAY_API_ACCESS_KEY") - } - } - if c.Organization != "" { - warnings = append(warnings, "organization_id is deprecated in favor of project_id") - c.ProjectID = c.Organization - } - - if c.Token == "" { - c.Token = os.Getenv("SCALEWAY_API_TOKEN") + configFile, err := scw.LoadConfig() + // If the config file do not exist, don't return an error as we may find config in ENV or flags. + if _, isNotFoundError := err.(*scw.ConfigFileNotFoundError); isNotFoundError { + configFile = &scw.Config{} + } else if err != nil { + return nil, err } - if c.Token != "" { - warnings = append(warnings, "token is deprecated in favor of secret_key") - c.SecretKey = c.Token + activeProfile, err := configFile.GetActiveProfile() + if err != nil { + return nil, err } - if c.Region != "" { - warnings = append(warnings, "region is deprecated in favor of zone") - c.Zone = c.Region - } + envProfile := scw.LoadEnvProfile() + profile := scw.MergeProfiles(activeProfile, envProfile) if c.AccessKey == "" { - c.AccessKey = os.Getenv(scw.ScwAccessKeyEnv) + c.AccessKey = *profile.AccessKey } if c.SecretKey == "" { - c.SecretKey = os.Getenv(scw.ScwSecretKeyEnv) + c.SecretKey = *profile.SecretKey } if c.ProjectID == "" { - c.ProjectID = os.Getenv(scw.ScwDefaultProjectIDEnv) + c.ProjectID = *profile.DefaultProjectID } if c.Zone == "" { - c.Zone = os.Getenv(scw.ScwDefaultZoneEnv) + c.Zone = *profile.DefaultZone } if c.APIURL == "" { - c.APIURL = os.Getenv(scw.ScwAPIURLEnv) + c.APIURL = *profile.APIURL } if c.SnapshotName == "" { diff --git a/builder/scaleway/step_create_image.go b/builder/scaleway/step_create_image.go index 98085d3af..b115bea97 100644 --- a/builder/scaleway/step_create_image.go +++ b/builder/scaleway/step_create_image.go @@ -45,7 +45,7 @@ func (s *stepImage) Run(ctx context.Context, state multistep.StateBag) multistep imageResp, err := instanceAPI.GetImage(&instance.GetImageRequest{ ImageID: imageID, - }) + }, scw.WithContext(ctx)) if err != nil { err := fmt.Errorf("Error getting initial image info: %s", err) state.Put("error", err) @@ -62,7 +62,7 @@ func (s *stepImage) Run(ctx context.Context, state multistep.StateBag) multistep DefaultBootscript: bootscriptID, Name: c.ImageName, RootVolume: snapshotID, - }) + }, scw.WithContext(ctx)) if err != nil { err := fmt.Errorf("Error creating image: %s", err) state.Put("error", err) diff --git a/builder/scaleway/step_create_server.go b/builder/scaleway/step_create_server.go index 3342305b1..3f64135e2 100644 --- a/builder/scaleway/step_create_server.go +++ b/builder/scaleway/step_create_server.go @@ -41,7 +41,7 @@ func (s *stepCreateServer) Run(ctx context.Context, state multistep.StateBag) mu Name: c.ServerName, Image: c.Image, Tags: tags, - }) + }, scw.WithContext(ctx)) if err != nil { err := fmt.Errorf("Error creating server: %s", err) state.Put("error", err) @@ -52,7 +52,7 @@ func (s *stepCreateServer) Run(ctx context.Context, state multistep.StateBag) mu _, err = instanceAPI.ServerAction(&instance.ServerActionRequest{ Action: instance.ServerActionPoweron, ServerID: createServerResp.Server.ID, - }) + }, scw.WithContext(ctx)) if err != nil { err := fmt.Errorf("Error starting server: %s", err) state.Put("error", err) diff --git a/builder/scaleway/step_pre_validate.go b/builder/scaleway/step_pre_validate.go index 0e32b2e5d..2e1eedaf0 100644 --- a/builder/scaleway/step_pre_validate.go +++ b/builder/scaleway/step_pre_validate.go @@ -32,7 +32,7 @@ func (s *stepPreValidate) Run(ctx context.Context, state multistep.StateBag) mul instanceAPI := instance.NewAPI(state.Get("client").(*scw.Client)) images, err := instanceAPI.ListImages( &instance.ListImagesRequest{Name: &s.ImageName}, - scw.WithAllPages()) + scw.WithAllPages(), scw.WithContext(ctx)) if err != nil { err := fmt.Errorf("Error: getting image list: %s", err) state.Put("error", err) @@ -54,7 +54,7 @@ func (s *stepPreValidate) Run(ctx context.Context, state multistep.StateBag) mul snapshots, err := instanceAPI.ListSnapshots( &instance.ListSnapshotsRequest{Name: &s.SnapshotName}, - scw.WithAllPages()) + scw.WithAllPages(), scw.WithContext(ctx)) if err != nil { err := fmt.Errorf("Error: getting snapshot list: %s", err) state.Put("error", err) diff --git a/builder/scaleway/step_shutdown.go b/builder/scaleway/step_shutdown.go index 60d76d907..ce8eb71a4 100644 --- a/builder/scaleway/step_shutdown.go +++ b/builder/scaleway/step_shutdown.go @@ -22,7 +22,7 @@ func (s *stepShutdown) Run(ctx context.Context, state multistep.StateBag) multis _, err := instanceAPI.ServerAction(&instance.ServerActionRequest{ Action: instance.ServerActionPoweroff, ServerID: serverID, - }) + }, scw.WithContext(ctx)) if err != nil { err := fmt.Errorf("Error stopping server: %s", err) state.Put("error", err) diff --git a/builder/scaleway/step_snapshot.go b/builder/scaleway/step_snapshot.go index 8b4fab470..72d4c628a 100644 --- a/builder/scaleway/step_snapshot.go +++ b/builder/scaleway/step_snapshot.go @@ -23,7 +23,7 @@ func (s *stepSnapshot) Run(ctx context.Context, state multistep.StateBag) multis createSnapshotResp, err := instanceAPI.CreateSnapshot(&instance.CreateSnapshotRequest{ Name: c.SnapshotName, VolumeID: volumeID, - }) + }, scw.WithContext(ctx)) if err != nil { err := fmt.Errorf("Error creating snapshot: %s", err) state.Put("error", err) diff --git a/website/pages/partials/builder/scaleway/Config-required.mdx b/website/pages/partials/builder/scaleway/Config-required.mdx index 90123c05d..4e63cf752 100644 --- a/website/pages/partials/builder/scaleway/Config-required.mdx +++ b/website/pages/partials/builder/scaleway/Config-required.mdx @@ -1,15 +1,19 @@ - `access_key` (string) - The AccessKey corresponding to the secret key. + Will be fetched first from the [scaleway configuration file](https://github.com/scaleway/scaleway-sdk-go/blob/master/scw/README.md). It can also be specified via the environment variable SCW_ACCESS_KEY. - `secret_key` (string) - The SecretKey to authenticate against the Scaleway API. + Will be fetched first from the [scaleway configuration file](https://github.com/scaleway/scaleway-sdk-go/blob/master/scw/README.md). It can also be specified via the environment variable SCW_SECRET_KEY. - `project_id` (string) - The Project ID in which the instances, volumes and snapshots will be created. + Will be fetched first from the [scaleway configuration file](https://github.com/scaleway/scaleway-sdk-go/blob/master/scw/README.md). It can also be specified via the environment variable SCW_DEFAULT_PROJECT_ID. - `zone` (string) - The Zone in which the instances, volumes and snapshots will be created. + Will be fetched first from the [scaleway configuration file](https://github.com/scaleway/scaleway-sdk-go/blob/master/scw/README.md). It can also be specified via the environment variable SCW_DEFAULT_ZONE - `image` (string) - The UUID of the base image to use. This is the image