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 Feb 15, 2019
1 parent ea07422 commit 50fd595
Showing 1 changed file with 33 additions and 1 deletion.
34 changes: 33 additions & 1 deletion scaleway/provider.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package scaleway

import (
"encoding/json"
"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 +71,23 @@ 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
}

var data scalewayConfig
err = json.NewDecoder(f).Decode(&data)
return data.Token, data.Organization, err
}

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

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

if apiKey == "" {
if path, err := homedir.Expand(d.Get(".scwrc").(string)); err == nil {
scwAPIKey, scwOrganization, err := readScalewayConfig(path)
if err == nil {
apiKey = scwAPIKey
organization = scwOrganization
}
}
}

config := Config{
Organization: d.Get("organization").(string),
Organization: organization,
APIKey: apiKey,
Region: d.Get("region").(string),
}
Expand Down

0 comments on commit 50fd595

Please sign in to comment.