10
"github.com/Azure/azure-sdk-for-go/Godeps/_workspace/src/github.com/Azure/go-autorest/autorest/azure"
14
credentialsPath = "/.azure/credentials.json"
17
// ToJSON returns the passed item as a pretty-printed JSON string. If any JSON error occurs,
18
// it returns the empty string.
19
func ToJSON(v interface{}) string {
20
j, _ := json.MarshalIndent(v, "", " ")
24
// NewServicePrincipalTokenFromCredentials creates a new ServicePrincipalToken using values of the
25
// passed credentials map.
26
func NewServicePrincipalTokenFromCredentials(c map[string]string, scope string) (*azure.ServicePrincipalToken, error) {
27
return azure.NewServicePrincipalToken(c["clientID"], c["clientSecret"], c["tenantID"], scope)
30
// LoadCredentials reads credentials from a ~/.azure/credentials.json file. See the accompanying
31
// credentials_sample.json file for an example.
33
// Note: Storing crendentials in a local file must be secured and not shared. It is used here
34
// simply to reduce code in the examples.
35
func LoadCredentials() (map[string]string, error) {
36
u, err := user.Current()
38
return nil, fmt.Errorf("ERROR: Unable to determine current user")
41
n := u.HomeDir + credentialsPath
44
return nil, fmt.Errorf("ERROR: Unable to locate or open Azure credentials at %s (%v)", n, err)
47
b, err := ioutil.ReadAll(f)
49
return nil, fmt.Errorf("ERROR: Unable to read %s (%v)", n, err)
52
c := map[string]interface{}{}
53
err = json.Unmarshal(b, &c)
55
return nil, fmt.Errorf("ERROR: %s contained invalid JSON (%s)", n, err)
58
return ensureValueStrings(c), nil
61
func ensureValueStrings(mapOfInterface map[string]interface{}) map[string]string {
62
mapOfStrings := make(map[string]string)
63
for key, value := range mapOfInterface {
64
mapOfStrings[key] = ensureValueString(value)
69
func ensureValueString(value interface{}) string {
73
switch v := value.(type) {
77
return fmt.Sprintf("%v", v)