providers/google: Use account_file_contents if provided
This commit is contained in:
parent
a8d0a70c03
commit
4ce776d252
|
@ -3,10 +3,12 @@ package google
|
|||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"runtime"
|
||||
"strings"
|
||||
|
||||
|
||||
// TODO(dcunnin): Use version code from version.go
|
||||
|
@ -54,10 +56,25 @@ func (c *Config) loadAndValidate() error {
|
|||
var client *http.Client
|
||||
|
||||
if c.AccountFile != "" {
|
||||
if err := loadJSON(&account, c.AccountFile); err != nil {
|
||||
if c.AccountFileContents != "" {
|
||||
return fmt.Errorf(
|
||||
"Error loading account file '%s': %s",
|
||||
c.AccountFile,
|
||||
"Cannot provide both account_file and account_file_contents",
|
||||
)
|
||||
}
|
||||
|
||||
b, err := ioutil.ReadFile(c.AccountFile)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
c.AccountFileContents = string(b)
|
||||
}
|
||||
|
||||
if c.AccountFileContents != "" {
|
||||
if err := parseJSON(&account, c.AccountFileContents); err != nil {
|
||||
return fmt.Errorf(
|
||||
"Error parsing account file contents '%s': %s",
|
||||
c.AccountFileContents,
|
||||
err)
|
||||
}
|
||||
|
||||
|
@ -150,13 +167,9 @@ type accountFile struct {
|
|||
ClientId string `json:"client_id"`
|
||||
}
|
||||
|
||||
func loadJSON(result interface{}, path string) error {
|
||||
f, err := os.Open(path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer f.Close()
|
||||
func parseJSON(result interface{}, contents string) error {
|
||||
r := strings.NewReader(contents)
|
||||
dec := json.NewDecoder(r)
|
||||
|
||||
dec := json.NewDecoder(f)
|
||||
return dec.Decode(result)
|
||||
}
|
||||
|
|
|
@ -1,24 +1,63 @@
|
|||
package google
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"io/ioutil"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestConfigLoadJSON_account(t *testing.T) {
|
||||
var actual accountFile
|
||||
if err := loadJSON(&actual, "./test-fixtures/fake_account.json"); err != nil {
|
||||
t.Fatalf("err: %s", err)
|
||||
const testFakeAccountFilePath = "./test-fixtures/fake_account.json"
|
||||
|
||||
func TestConfigLoadAndValidate_accountFile(t *testing.T) {
|
||||
config := Config{
|
||||
AccountFile: testFakeAccountFilePath,
|
||||
Project: "my-gce-project",
|
||||
Region: "us-central1",
|
||||
}
|
||||
|
||||
expected := accountFile{
|
||||
PrivateKeyId: "foo",
|
||||
PrivateKey: "bar",
|
||||
ClientEmail: "foo@bar.com",
|
||||
ClientId: "id@foo.com",
|
||||
err := config.loadAndValidate()
|
||||
if err != nil {
|
||||
t.Fatalf("error: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
if !reflect.DeepEqual(actual, expected) {
|
||||
t.Fatalf("bad: %#v", actual)
|
||||
func TestConfigLoadAndValidate_accountFileContents(t *testing.T) {
|
||||
contents, err := ioutil.ReadFile(testFakeAccountFilePath)
|
||||
if err != nil {
|
||||
t.Fatalf("error: %v", err)
|
||||
}
|
||||
config := Config{
|
||||
AccountFileContents: string(contents),
|
||||
Project: "my-gce-project",
|
||||
Region: "us-central1",
|
||||
}
|
||||
|
||||
err = config.loadAndValidate()
|
||||
if err != nil {
|
||||
t.Fatalf("error: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestConfigLoadAndValidate_none(t *testing.T) {
|
||||
config := Config{
|
||||
Project: "my-gce-project",
|
||||
Region: "us-central1",
|
||||
}
|
||||
|
||||
err := config.loadAndValidate()
|
||||
if err != nil {
|
||||
t.Fatalf("error: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestConfigLoadAndValidate_both(t *testing.T) {
|
||||
config := Config{
|
||||
AccountFile: testFakeAccountFilePath,
|
||||
AccountFileContents: "{}",
|
||||
Project: "my-gce-project",
|
||||
Region: "us-central1",
|
||||
}
|
||||
|
||||
if config.loadAndValidate() == nil {
|
||||
t.Fatalf("expected error, but got nil")
|
||||
}
|
||||
}
|
||||
|
|
|
@ -60,6 +60,7 @@ func Provider() terraform.ResourceProvider {
|
|||
func providerConfigure(d *schema.ResourceData) (interface{}, error) {
|
||||
config := Config{
|
||||
AccountFile: d.Get("account_file").(string),
|
||||
AccountFileContents: d.Get("account_file_contents").(string),
|
||||
Project: d.Get("project").(string),
|
||||
Region: d.Get("region").(string),
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue