From 50fd59515ceaab513789e0185dc09f106354517a Mon Sep 17 00:00:00 2001 From: Raphael Randschau Date: Thu, 14 Feb 2019 22:34:49 -0800 Subject: [PATCH] feat/provider: read .scwrc --- scaleway/provider.go | 34 +++++++++++++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git a/scaleway/provider.go b/scaleway/provider.go index e5aff6a3e7..51cccb6d39 100644 --- a/scaleway/provider.go +++ b/scaleway/provider.go @@ -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{} @@ -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 { @@ -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), }