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 1/6] 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 From adb74ba15d36f8e2e45d2fe99253206c22ea0963 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9my=20L=C3=A9one?= Date: Thu, 29 Oct 2020 11:09:04 +0100 Subject: [PATCH 2/6] Fix --- builder/scaleway/config.go | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/builder/scaleway/config.go b/builder/scaleway/config.go index 063a0de84..46bd17927 100644 --- a/builder/scaleway/config.go +++ b/builder/scaleway/config.go @@ -130,23 +130,33 @@ func (c *Config) Prepare(raws ...interface{}) ([]string, error) { profile := scw.MergeProfiles(activeProfile, envProfile) if c.AccessKey == "" { - c.AccessKey = *profile.AccessKey + if profile.AccessKey != nil { + c.AccessKey = *profile.AccessKey + } } if c.SecretKey == "" { - c.SecretKey = *profile.SecretKey + if profile.SecretKey != nil { + c.SecretKey = *profile.SecretKey + } } if c.ProjectID == "" { - c.ProjectID = *profile.DefaultProjectID + if profile.DefaultProjectID != nil { + c.ProjectID = *profile.DefaultProjectID + } } if c.Zone == "" { - c.Zone = *profile.DefaultZone + if profile.DefaultZone != nil { + c.Zone = *profile.DefaultZone + } } if c.APIURL == "" { - c.APIURL = *profile.APIURL + if profile.APIURL != nil { + c.APIURL = *profile.APIURL + } } if c.SnapshotName == "" { @@ -182,12 +192,12 @@ func (c *Config) Prepare(raws ...interface{}) ([]string, error) { } if c.ProjectID == "" { errs = packer.MultiErrorAppend( - errs, errors.New("Scaleway Project ID must be specified")) + errs, errors.New("scaleway Project ID must be specified")) } if c.SecretKey == "" { errs = packer.MultiErrorAppend( - errs, errors.New("Scaleway Secret Key must be specified")) + errs, errors.New("scaleway Secret Key must be specified")) } if c.AccessKey == "" { @@ -197,7 +207,7 @@ func (c *Config) Prepare(raws ...interface{}) ([]string, error) { if c.Zone == "" { errs = packer.MultiErrorAppend( - errs, errors.New("Scaleway Zone is required")) + errs, errors.New("scaleway Zone is required")) } if c.CommercialType == "" { @@ -215,5 +225,6 @@ func (c *Config) Prepare(raws ...interface{}) ([]string, error) { } packer.LogSecretFilter.Set(c.Token) + packer.LogSecretFilter.Set(c.SecretKey) return warnings, nil } From 3413fc5c4672e12602e7714df7b9b4bf157a9967 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9my=20L=C3=A9one?= Date: Thu, 29 Oct 2020 11:19:18 +0100 Subject: [PATCH 3/6] Fix --- builder/scaleway/config.go | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/builder/scaleway/config.go b/builder/scaleway/config.go index 46bd17927..5d1b210b4 100644 --- a/builder/scaleway/config.go +++ b/builder/scaleway/config.go @@ -6,6 +6,8 @@ package scaleway import ( "errors" "fmt" + "log" + "os" "github.com/hashicorp/packer/common" "github.com/hashicorp/packer/common/uuid" @@ -129,6 +131,28 @@ func (c *Config) Prepare(raws ...interface{}) ([]string, error) { envProfile := scw.LoadEnvProfile() profile := scw.MergeProfiles(activeProfile, envProfile) + // 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") + } + if c.Token != "" { + warnings = append(warnings, "token is deprecated in favor of secret_key") + c.SecretKey = c.Token + } + if c.AccessKey == "" { if profile.AccessKey != nil { c.AccessKey = *profile.AccessKey From db96c5c9ae589213269b7bef74b91c92049f0361 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9my=20L=C3=A9one?= Date: Thu, 29 Oct 2020 11:21:31 +0100 Subject: [PATCH 4/6] Fix --- builder/scaleway/config.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/builder/scaleway/config.go b/builder/scaleway/config.go index 5d1b210b4..fa377b414 100644 --- a/builder/scaleway/config.go +++ b/builder/scaleway/config.go @@ -153,6 +153,11 @@ func (c *Config) Prepare(raws ...interface{}) ([]string, error) { c.SecretKey = c.Token } + if c.Region != "" { + warnings = append(warnings, "region is deprecated in favor of zone") + c.Zone = c.Region + } + if c.AccessKey == "" { if profile.AccessKey != nil { c.AccessKey = *profile.AccessKey From 0cfaf4a6202461d21bc27fdd99bb1f43415346e3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9my=20L=C3=A9one?= Date: Thu, 29 Oct 2020 17:45:03 +0100 Subject: [PATCH 5/6] Fix --- builder/scaleway/config.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/builder/scaleway/config.go b/builder/scaleway/config.go index fa377b414..05cc444b4 100644 --- a/builder/scaleway/config.go +++ b/builder/scaleway/config.go @@ -25,18 +25,23 @@ type Config struct { common.PackerConfig `mapstructure:",squash"` Comm communicator.Config `mapstructure:",squash"` // 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. AccessKey string `mapstructure:"access_key" required:"true"` // 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. SecretKey string `mapstructure:"secret_key" required:"true"` // 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. ProjectID string `mapstructure:"project_id" required:"true"` // 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 Zone string `mapstructure:"zone" required:"true"` // The Scaleway API URL to use + // 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_API_URL APIURL string `mapstructure:"api_url"` From 01f6763db09dfea6c7682696eee5be17a342a664 Mon Sep 17 00:00:00 2001 From: Megan Marsh Date: Fri, 20 Nov 2020 14:53:08 -0800 Subject: [PATCH 6/6] fix generation --- website/pages/partials/builder/scaleway/Config-not-required.mdx | 1 + 1 file changed, 1 insertion(+) diff --git a/website/pages/partials/builder/scaleway/Config-not-required.mdx b/website/pages/partials/builder/scaleway/Config-not-required.mdx index 47c8b3e7b..ef73b13c1 100644 --- a/website/pages/partials/builder/scaleway/Config-not-required.mdx +++ b/website/pages/partials/builder/scaleway/Config-not-required.mdx @@ -1,6 +1,7 @@ - `api_url` (string) - The Scaleway API URL to use + 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_API_URL - `snapshot_name` (string) - The name of the resulting snapshot that will