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 83386ca
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 1 deletion.
39 changes: 38 additions & 1 deletion scaleway/provider.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
package scaleway

import (
"encoding/json"
"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 Down Expand Up @@ -68,6 +72,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 +102,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 83386ca

Please sign in to comment.