Rename attribute api_access_key to organization

This commit is contained in:
Rémy Léone 2018-10-25 15:10:09 +02:00
parent 254e18ece9
commit e54d2e3591
6 changed files with 160 additions and 8 deletions

View File

@ -9,7 +9,7 @@ import (
func testConfig() map[string]interface{} { func testConfig() map[string]interface{} {
return map[string]interface{}{ return map[string]interface{}{
"api_access_key": "foo", "organization_id": "foo",
"api_token": "bar", "api_token": "bar",
"region": "ams1", "region": "ams1",
"commercial_type": "START1-S", "commercial_type": "START1-S",

View File

@ -3,6 +3,7 @@ package scaleway
import ( import (
"errors" "errors"
"fmt" "fmt"
"log"
"os" "os"
"github.com/hashicorp/packer/common" "github.com/hashicorp/packer/common"
@ -20,7 +21,7 @@ type Config struct {
Comm communicator.Config `mapstructure:",squash"` Comm communicator.Config `mapstructure:",squash"`
Token string `mapstructure:"api_token"` Token string `mapstructure:"api_token"`
Organization string `mapstructure:"api_access_key"` Organization string `mapstructure:"organization_id"`
Region string `mapstructure:"region"` Region string `mapstructure:"region"`
Image string `mapstructure:"image"` Image string `mapstructure:"image"`
@ -57,8 +58,13 @@ func NewConfig(raws ...interface{}) (*Config, []string, error) {
c.UserAgent = useragent.String() c.UserAgent = useragent.String()
if c.Organization == "" { 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") c.Organization = os.Getenv("SCALEWAY_API_ACCESS_KEY")
} }
}
if c.Token == "" { if c.Token == "" {
c.Token = os.Getenv("SCALEWAY_API_TOKEN") c.Token = os.Getenv("SCALEWAY_API_TOKEN")

View File

@ -30,6 +30,7 @@ func init() {
"parallels-deprecations": new(FixerParallelsDeprecations), "parallels-deprecations": new(FixerParallelsDeprecations),
"sshkeypath": new(FixerSSHKeyPath), "sshkeypath": new(FixerSSHKeyPath),
"sshdisableagent": new(FixerSSHDisableAgent), "sshdisableagent": new(FixerSSHDisableAgent),
"scaleway-access-key": new(FixerScalewayAccessKey),
"manifest-filename": new(FixerManifestFilename), "manifest-filename": new(FixerManifestFilename),
"amazon-shutdown_behavior": new(FixerAmazonShutdownBehavior), "amazon-shutdown_behavior": new(FixerAmazonShutdownBehavior),
"amazon-enhanced-networking": new(FixerAmazonEnhancedNetworking), "amazon-enhanced-networking": new(FixerAmazonEnhancedNetworking),
@ -52,6 +53,7 @@ func init() {
"parallels-deprecations", "parallels-deprecations",
"sshkeypath", "sshkeypath",
"sshdisableagent", "sshdisableagent",
"scaleway-access-key",
"manifest-filename", "manifest-filename",
"amazon-shutdown_behavior", "amazon-shutdown_behavior",
"amazon-enhanced-networking", "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: ### Required:
- `api_access_key` (string) - The organization access key to use to identify - `organization_id` (string) - The organization id to use to identify your
your organization. It can also be specified via environment variable organization. It can also be specified via environment variable
`SCALEWAY_API_ACCESS_KEY`. Your access key is available in the `SCALEWAY_ORGANIZATION`. Your organization id is available in the
["Credentials" section](https://cloud.scaleway.com/#/credentials) of the ["Account" section](https://cloud.scaleway.com/#/account) of the
control panel. 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. - `api_token` (string) - The token to use to authenticate with your account.
It can also be specified via environment variable `SCALEWAY_API_TOKEN`. You It can also be specified via environment variable `SCALEWAY_API_TOKEN`. You
@ -86,7 +87,7 @@ access tokens:
``` json ``` json
{ {
"type": "scaleway", "type": "scaleway",
"api_access_key": "YOUR API ACCESS KEY", "organization_id": "YOUR ORGANIZATION ID",
"api_token": "YOUR TOKEN", "api_token": "YOUR TOKEN",
"image": "UUID OF THE BASE IMAGE", "image": "UUID OF THE BASE IMAGE",
"region": "par1", "region": "par1",