Merge pull request #2811 from sparkprime/verbatim_account

account_file can be verbatim JSON string
This commit is contained in:
Eric Johnson 2015-10-12 15:57:52 -07:00
commit 96d522fb64
3 changed files with 40 additions and 12 deletions

View File

@ -2,7 +2,10 @@ package googlecompute
import ( import (
"encoding/json" "encoding/json"
"fmt"
"io/ioutil"
"os" "os"
"strings"
) )
// accountFile represents the structure of the account file JSON file. // accountFile represents the structure of the account file JSON file.
@ -13,13 +16,37 @@ type accountFile struct {
ClientId string `json:"client_id"` ClientId string `json:"client_id"`
} }
func loadJSON(result interface{}, path string) error { func parseJSON(result interface{}, text string) error {
f, err := os.Open(path) r := strings.NewReader(text)
if err != nil { dec := json.NewDecoder(r)
return err
}
defer f.Close()
dec := json.NewDecoder(f)
return dec.Decode(result) return dec.Decode(result)
} }
func processAccountFile(account_file *accountFile, text string) error {
// Assume text is a JSON string
if err := parseJSON(account_file, text); err != nil {
// If text was not JSON, assume it is a file path instead
if _, err := os.Stat(text); os.IsNotExist(err) {
return fmt.Errorf(
"account_file path does not exist: %s",
text)
}
b, err := ioutil.ReadFile(text)
if err != nil {
return fmt.Errorf(
"Error reading account_file from path '%s': %s",
text, err)
}
contents := string(b)
if err := parseJSON(account_file, contents); err != nil {
return fmt.Errorf(
"Error parsing account file '%s': %s",
contents, err)
}
}
return nil
}

View File

@ -131,9 +131,8 @@ func NewConfig(raws ...interface{}) (*Config, []string, error) {
c.stateTimeout = stateTimeout c.stateTimeout = stateTimeout
if c.AccountFile != "" { if c.AccountFile != "" {
if err := loadJSON(&c.account, c.AccountFile); err != nil { if err := processAccountFile(&c.account, c.AccountFile); err != nil {
errs = packer.MultiErrorAppend( errs = packer.MultiErrorAppend(errs, err)
errs, fmt.Errorf("Failed parsing account file: %s", err))
} }
} }

View File

@ -77,7 +77,9 @@ straightforwarded, it is documented here.
Below is a fully functioning example. It doesn't do anything useful, since no Below is a fully functioning example. It doesn't do anything useful, since no
provisioners are defined, but it will effectively repackage an existing GCE provisioners are defined, but it will effectively repackage an existing GCE
image. The account file is obtained in the previous section. image. The account_file is obtained in the previous section. If it parses as
JSON it is assumed to be the file itself, otherwise it is assumed to be
the path to the file containing the JSON.
``` {.javascript} ``` {.javascript}
{ {