mirror of https://github.com/hashicorp/packer
parent
32066cc46e
commit
0dfb3cc56f
@ -1,52 +1,34 @@
|
|||||||
package googlecompute
|
package googlecompute
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
"strings"
|
|
||||||
)
|
|
||||||
|
|
||||||
// accountFile represents the structure of the account file JSON file.
|
|
||||||
type AccountFile struct {
|
|
||||||
PrivateKeyId string `json:"private_key_id"`
|
|
||||||
PrivateKey string `json:"private_key"`
|
|
||||||
ClientEmail string `json:"client_email"`
|
|
||||||
ClientId string `json:"client_id"`
|
|
||||||
}
|
|
||||||
|
|
||||||
func parseJSON(result interface{}, text string) error {
|
"golang.org/x/oauth2/google"
|
||||||
r := strings.NewReader(text)
|
"golang.org/x/oauth2/jwt"
|
||||||
dec := json.NewDecoder(r)
|
)
|
||||||
return dec.Decode(result)
|
|
||||||
}
|
|
||||||
|
|
||||||
func ProcessAccountFile(account_file *AccountFile, text string) error {
|
func ProcessAccountFile(text string) (*jwt.Config, error) {
|
||||||
// Assume text is a JSON string
|
// Assume text is a JSON string
|
||||||
if err := parseJSON(account_file, text); err != nil {
|
conf, err := google.JWTConfigFromJSON([]byte(text), DriverScopes...)
|
||||||
|
if err != nil {
|
||||||
// If text was not JSON, assume it is a file path instead
|
// If text was not JSON, assume it is a file path instead
|
||||||
if _, err := os.Stat(text); os.IsNotExist(err) {
|
if _, err := os.Stat(text); os.IsNotExist(err) {
|
||||||
return fmt.Errorf(
|
return nil, fmt.Errorf(
|
||||||
"account_file path does not exist: %s",
|
"account_file path does not exist: %s",
|
||||||
text)
|
text)
|
||||||
}
|
}
|
||||||
|
data, err := ioutil.ReadFile(text)
|
||||||
b, err := ioutil.ReadFile(text)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf(
|
return nil, fmt.Errorf(
|
||||||
"Error reading account_file from path '%s': %s",
|
"Error reading account_file from path '%s': %s",
|
||||||
text, err)
|
text, err)
|
||||||
}
|
}
|
||||||
|
conf, err = google.JWTConfigFromJSON(data, DriverScopes...)
|
||||||
contents := string(b)
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("Error parsing account_file: %s", err)
|
||||||
if err := parseJSON(account_file, contents); err != nil {
|
|
||||||
return fmt.Errorf(
|
|
||||||
"Error parsing account file '%s': %s",
|
|
||||||
contents, err)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return conf, nil
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in new issue