Skip to content

Commit

Permalink
feat/provider: read .scwrc
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolai86 committed Mar 8, 2019
1 parent 0937ec5 commit 8902a29
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 8 deletions.
79 changes: 71 additions & 8 deletions scaleway/provider.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
package scaleway

import (
"encoding/json"
"errors"
"fmt"
"os"
"sync"

"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/terraform"
homedir "github.com/mitchellh/go-homedir"
)

var mu = sync.Mutex{}
Expand All @@ -23,16 +28,41 @@ func Provider() terraform.ResourceProvider {
"token": {
Type: schema.TypeString,
Required: true,
DefaultFunc: schema.MultiEnvDefaultFunc([]string{
"SCALEWAY_TOKEN",
"SCALEWAY_ACCESS_KEY",
}, nil),
DefaultFunc: schema.SchemaDefaultFunc(func() (interface{}, error) {
for _, k := range []string{"SCALEWAY_TOKEN", "SCALEWAY_ACCESS_KEY"} {
if os.Getenv(k) != "" {
return k, nil
}
}
if path, err := homedir.Expand("~/.scwrc"); err == nil {
scwAPIKey, _, err := readScalewayConfig(path)
if err != nil {
return nil, err
}
return scwAPIKey, nil
}
return nil, errors.New("No token found")
}),
Description: "The API key for Scaleway API operations.",
},
"organization": {
Type: schema.TypeString,
Required: true,
DefaultFunc: schema.EnvDefaultFunc("SCALEWAY_ORGANIZATION", nil),
Type: schema.TypeString,
Required: true,
DefaultFunc: schema.SchemaDefaultFunc(func() (interface{}, error) {
for _, k := range []string{"SCALEWAY_ORGANIZATION"} {
if os.Getenv(k) != "" {
return k, nil
}
}
if path, err := homedir.Expand("~/.scwrc"); err == nil {
_, scwOrganization, err := readScalewayConfig(path)
if err != nil {
return nil, err
}
return scwOrganization, nil
}
return nil, errors.New("No token found")
}),
Description: "The Organization ID (a.k.a. 'access key') for Scaleway API operations.",
},
"region": {
Expand Down Expand Up @@ -68,6 +98,26 @@ func Provider() terraform.ResourceProvider {
}
}

type scalewayConfig struct {
Organization string `json:"organization"`
Token string `json:"token"`
Version string `json:"version"`
}

func readScalewayConfig(path string) (string, string, error) {
f, err := os.Open(path)
if err != nil {
return "", "", err
}
defer f.Close()

var data scalewayConfig
if err := json.NewDecoder(f).Decode(&data); err != nil {
return "", "", err
}
return data.Token, data.Organization, nil
}

func providerConfigure(d *schema.ResourceData) (interface{}, error) {
apiKey := ""
if v, ok := d.Get("token").(string); ok {
Expand All @@ -78,8 +128,21 @@ func providerConfigure(d *schema.ResourceData) (interface{}, error) {
}
}

organization := d.Get("organization").(string)

if apiKey == "" {
if path, err := homedir.Expand("~/.scwrc"); err == nil {
scwAPIKey, scwOrganization, err := readScalewayConfig(path)
if err != nil {
return nil, fmt.Errorf("Error loading credentials from SCW: %s", err)
}
apiKey = scwAPIKey
organization = scwOrganization
}
}

config := Config{
Organization: d.Get("organization").(string),
Organization: organization,
APIKey: apiKey,
Region: d.Get("region").(string),
}
Expand Down
11 changes: 11 additions & 0 deletions scaleway/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/terraform"
homedir "github.com/mitchellh/go-homedir"
)

var testAccProviders map[string]terraform.ResourceProvider
Expand All @@ -30,6 +31,16 @@ func TestProvider_impl(t *testing.T) {

func testAccPreCheck(t *testing.T) {
if v := os.Getenv("SCALEWAY_ORGANIZATION"); v == "" {
if path, err := homedir.Expand("~/.scwrc"); err == nil {
scwAPIKey, scwOrganization, err := readScalewayConfig(path)
if err != nil {
t.Fatalf("failed falling back to %s: %v", path, err)
}
if scwAPIKey == "" && scwOrganization == "" {
t.Fatal("SCALEWAY_TOKEN must be set for acceptance tests")
}
return
}
t.Fatal("SCALEWAY_ORGANIZATION must be set for acceptance tests")
}
tokenFromAccessKey := os.Getenv("SCALEWAY_ACCESS_KEY")
Expand Down
2 changes: 2 additions & 0 deletions website/docs/index.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,8 @@ provider "scaleway" {
- **SCALEWAY_TOKEN**: Your API access `token`, generated by you
- **SCALEWAY_REGION**: The Scaleway region

Alternatively the `~/.scwrc` will be parsed to load your Scaleway CLI configuration, if it exists.

## Volume usage

You can add volumes to bare metal instances.
Expand Down

0 comments on commit 8902a29

Please sign in to comment.