From be0959004a137bddb116a90557881093e280fed5 Mon Sep 17 00:00:00 2001 From: Megan Marsh Date: Fri, 9 Nov 2018 08:47:22 -0800 Subject: [PATCH] Revert "Revert "Rename attribute api_access_key to organization_id"" --- builder/scaleway/builder_test.go | 2 +- builder/scaleway/config.go | 10 ++- fix/fixer.go | 2 + fix/fixer_scaleway_access_key.go | 54 +++++++++++ fix/fixer_scaleway_access_key_test.go | 89 +++++++++++++++++++ website/source/docs/builders/scaleway.html.md | 11 +-- 6 files changed, 160 insertions(+), 8 deletions(-) create mode 100644 fix/fixer_scaleway_access_key.go create mode 100644 fix/fixer_scaleway_access_key_test.go diff --git a/builder/scaleway/builder_test.go b/builder/scaleway/builder_test.go index 1d42ed7e9..a1a0fccc3 100644 --- a/builder/scaleway/builder_test.go +++ b/builder/scaleway/builder_test.go @@ -9,7 +9,7 @@ import ( func testConfig() map[string]interface{} { return map[string]interface{}{ - "api_access_key": "foo", + "organization_id": "foo", "api_token": "bar", "region": "ams1", "commercial_type": "START1-S", diff --git a/builder/scaleway/config.go b/builder/scaleway/config.go index f7a90c55a..45a8217e8 100644 --- a/builder/scaleway/config.go +++ b/builder/scaleway/config.go @@ -3,6 +3,7 @@ package scaleway import ( "errors" "fmt" + "log" "os" "github.com/hashicorp/packer/common" @@ -20,7 +21,7 @@ type Config struct { Comm communicator.Config `mapstructure:",squash"` Token string `mapstructure:"api_token"` - Organization string `mapstructure:"api_access_key"` + Organization string `mapstructure:"organization_id"` Region string `mapstructure:"region"` Image string `mapstructure:"image"` @@ -57,7 +58,12 @@ func NewConfig(raws ...interface{}) (*Config, []string, error) { c.UserAgent = useragent.String() if c.Organization == "" { - c.Organization = os.Getenv("SCALEWAY_API_ACCESS_KEY") + 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.Token == "" { diff --git a/fix/fixer.go b/fix/fixer.go index 03c9f9948..41dd8e9fd 100644 --- a/fix/fixer.go +++ b/fix/fixer.go @@ -30,6 +30,7 @@ func init() { "parallels-deprecations": new(FixerParallelsDeprecations), "sshkeypath": new(FixerSSHKeyPath), "sshdisableagent": new(FixerSSHDisableAgent), + "scaleway-access-key": new(FixerScalewayAccessKey), "manifest-filename": new(FixerManifestFilename), "amazon-shutdown_behavior": new(FixerAmazonShutdownBehavior), "amazon-enhanced-networking": new(FixerAmazonEnhancedNetworking), @@ -52,6 +53,7 @@ func init() { "parallels-deprecations", "sshkeypath", "sshdisableagent", + "scaleway-access-key", "manifest-filename", "amazon-shutdown_behavior", "amazon-enhanced-networking", diff --git a/fix/fixer_scaleway_access_key.go b/fix/fixer_scaleway_access_key.go new file mode 100644 index 000000000..d134ac32d --- /dev/null +++ b/fix/fixer_scaleway_access_key.go @@ -0,0 +1,54 @@ +package fix + +import ( + "github.com/mitchellh/mapstructure" +) + +// FixerScalewayAccessKey changes the "access_key" of a template +// to "organization_id". +type FixerScalewayAccessKey struct{} + +func (FixerScalewayAccessKey) Fix(input map[string]interface{}) (map[string]interface{}, error) { + // The type we'll decode into; we only care about builders + type template struct { + Builders []map[string]interface{} + } + + // Decode the input into our structure, if we can + var tpl template + if err := mapstructure.Decode(input, &tpl); err != nil { + return nil, err + } + + for _, builder := range tpl.Builders { + if builder["type"] != "scaleway" { + continue + } + + keyRaw, ok := builder["access_key"] + if !ok { + continue + } + + accessKey, ok := keyRaw.(string) + if !ok { + continue + } + + // only assign to organization_id if it doesn't + // already exist; otherwise we'll just ignore access_key + _, organizationIdIncluded := builder["organization_id"] + if !organizationIdIncluded { + builder["organization_id"] = accessKey + } + + delete(builder, "access_key") + } + + input["builders"] = tpl.Builders + return input, nil +} + +func (FixerScalewayAccessKey) Synopsis() string { + return `Updates builders using "access_key" to use "organization_id"` +} diff --git a/fix/fixer_scaleway_access_key_test.go b/fix/fixer_scaleway_access_key_test.go new file mode 100644 index 000000000..61ff7eed8 --- /dev/null +++ b/fix/fixer_scaleway_access_key_test.go @@ -0,0 +1,89 @@ +package fix + +import ( + "reflect" + "testing" +) + +func TestFixerScalewayAccessKey_Fix_Impl(t *testing.T) { + var _ Fixer = new(FixerScalewayAccessKey) +} + +func TestFixerScalewayAccessKey_Fix(t *testing.T) { + cases := []struct { + Input map[string]interface{} + Expected map[string]interface{} + }{ + // No key_path field + { + Input: map[string]interface{}{ + "type": "scaleway", + }, + + Expected: map[string]interface{}{ + "type": "scaleway", + }, + }, + + // organization_id without access_key + { + Input: map[string]interface{}{ + "type": "scaleway", + "organization_id": "0000", + }, + + Expected: map[string]interface{}{ + "type": "scaleway", + "organization_id": "0000", + }, + }, + + // access_key without organization_id + { + Input: map[string]interface{}{ + "type": "scaleway", + "access_key": "1111", + }, + + Expected: map[string]interface{}{ + "type": "scaleway", + "organization_id": "1111", + }, + }, + + // access_key and organization_id + { + Input: map[string]interface{}{ + "type": "scaleway", + "access_key": "2222", + "organization_id": "3333", + }, + + Expected: map[string]interface{}{ + "type": "scaleway", + "organization_id": "3333", + }, + }, + } + + for _, tc := range cases { + var f FixerScalewayAccessKey + + input := map[string]interface{}{ + "builders": []map[string]interface{}{tc.Input}, + } + + expected := map[string]interface{}{ + "builders": []map[string]interface{}{tc.Expected}, + } + + output, err := f.Fix(input) + if err != nil { + t.Fatalf("err: %s", err) + } + + if !reflect.DeepEqual(output, expected) { + t.Fatalf("unexpected: %#v\nexpected: %#v\n", output, expected) + } + } +} diff --git a/website/source/docs/builders/scaleway.html.md b/website/source/docs/builders/scaleway.html.md index e2ce32676..fcf1c9008 100644 --- a/website/source/docs/builders/scaleway.html.md +++ b/website/source/docs/builders/scaleway.html.md @@ -35,11 +35,12 @@ builder. ### Required: -- `api_access_key` (string) - The organization access key to use to identify - your organization. It can also be specified via environment variable - `SCALEWAY_API_ACCESS_KEY`. Your access key is available in the - ["Credentials" section](https://cloud.scaleway.com/#/credentials) of the +- `organization_id` (string) - The organization id to use to identify your + organization. It can also be specified via environment variable + `SCALEWAY_ORGANIZATION`. Your organization id is available in the + ["Account" section](https://cloud.scaleway.com/#/account) of the control panel. + Previously named: `api_access_key` with environment variable: `SCALEWAY_API_ACCESS_KEY` - `api_token` (string) - The token to use to authenticate with your account. It can also be specified via environment variable `SCALEWAY_API_TOKEN`. You @@ -86,7 +87,7 @@ access tokens: ``` json { "type": "scaleway", - "api_access_key": "YOUR API ACCESS KEY", + "organization_id": "YOUR ORGANIZATION ID", "api_token": "YOUR TOKEN", "image": "UUID OF THE BASE IMAGE", "region": "par1",