Revert "Revert "Rename attribute api_access_key to organization_id""

This commit is contained in:
Megan Marsh 2018-11-09 08:47:22 -08:00 committed by GitHub
parent c3bf014b5f
commit be0959004a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 160 additions and 8 deletions

View File

@ -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",

View File

@ -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 == "" {

View File

@ -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",

View File

@ -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"`
}

View File

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

View File

@ -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",