scaleway: use the SDK functions to load profile from file and env

This commit is contained in:
Rémy Léone 2020-10-28 17:46:40 +01:00
parent fb3d357e84
commit 288e29b1e0
7 changed files with 28 additions and 38 deletions

View File

@ -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")
}
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.Organization != "" {
warnings = append(warnings, "organization_id is deprecated in favor of project_id")
c.ProjectID = c.Organization
activeProfile, err := configFile.GetActiveProfile()
if err != nil {
return nil, err
}
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.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 == "" {

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -1,15 +1,19 @@
<!-- Code generated from the comments of the Config struct in builder/scaleway/config.go; DO NOT EDIT MANUALLY -->
- `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