diff --git a/builder/scaleway/config.go b/builder/scaleway/config.go index ba859101c..9b5ae70ad 100644 --- a/builder/scaleway/config.go +++ b/builder/scaleway/config.go @@ -26,18 +26,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"` @@ -117,6 +122,21 @@ func (c *Config) Prepare(raws ...interface{}) ([]string, error) { c.UserAgent = useragent.String(version.ScalewayPluginVersion.FormattedVersion()) + 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 + } + activeProfile, err := configFile.GetActiveProfile() + if err != nil { + return nil, err + } + + envProfile := scw.LoadEnvProfile() + profile := scw.MergeProfiles(activeProfile, envProfile) + // Deprecated variables if c.Organization == "" { if os.Getenv("SCALEWAY_ORGANIZATION") != "" { @@ -145,23 +165,33 @@ func (c *Config) Prepare(raws ...interface{}) ([]string, error) { } if c.AccessKey == "" { - c.AccessKey = os.Getenv(scw.ScwAccessKeyEnv) + if profile.AccessKey != nil { + c.AccessKey = *profile.AccessKey + } } if c.SecretKey == "" { - c.SecretKey = os.Getenv(scw.ScwSecretKeyEnv) + if profile.SecretKey != nil { + c.SecretKey = *profile.SecretKey + } } if c.ProjectID == "" { - c.ProjectID = os.Getenv(scw.ScwDefaultProjectIDEnv) + if profile.DefaultProjectID != nil { + c.ProjectID = *profile.DefaultProjectID + } } if c.Zone == "" { - c.Zone = os.Getenv(scw.ScwDefaultZoneEnv) + if profile.DefaultZone != nil { + c.Zone = *profile.DefaultZone + } } if c.APIURL == "" { - c.APIURL = os.Getenv(scw.ScwAPIURLEnv) + if profile.APIURL != nil { + c.APIURL = *profile.APIURL + } } if c.SnapshotName == "" { @@ -197,12 +227,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 == "" { @@ -212,7 +242,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 == "" { @@ -230,5 +260,6 @@ func (c *Config) Prepare(raws ...interface{}) ([]string, error) { } packer.LogSecretFilter.Set(c.Token) + packer.LogSecretFilter.Set(c.SecretKey) return warnings, nil } diff --git a/builder/scaleway/step_create_image.go b/builder/scaleway/step_create_image.go index 5875700d6..556a029e2 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 52c802b5f..08c4ec3c5 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 c1926a41f..f991b787a 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 9cb016e5a..8c8eb1471 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 29f56d97a..e7e01637a 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-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 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