~nskaggs/+junk/xenial-test

« back to all changes in this revision

Viewing changes to src/github.com/Azure/azure-sdk-for-go/arm/examples/helpers/helpers.go

  • Committer: Nicholas Skaggs
  • Date: 2016-10-24 20:56:05 UTC
  • Revision ID: nicholas.skaggs@canonical.com-20161024205605-z8lta0uvuhtxwzwl
Initi with beta15

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package helpers
 
2
 
 
3
import (
 
4
        "encoding/json"
 
5
        "fmt"
 
6
        "io/ioutil"
 
7
        "os"
 
8
        "os/user"
 
9
 
 
10
        "github.com/Azure/azure-sdk-for-go/Godeps/_workspace/src/github.com/Azure/go-autorest/autorest/azure"
 
11
)
 
12
 
 
13
const (
 
14
        credentialsPath = "/.azure/credentials.json"
 
15
)
 
16
 
 
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, "", "  ")
 
21
        return string(j)
 
22
}
 
23
 
 
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)
 
28
}
 
29
 
 
30
// LoadCredentials reads credentials from a ~/.azure/credentials.json file. See the accompanying
 
31
// credentials_sample.json file for an example.
 
32
//
 
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()
 
37
        if err != nil {
 
38
                return nil, fmt.Errorf("ERROR: Unable to determine current user")
 
39
        }
 
40
 
 
41
        n := u.HomeDir + credentialsPath
 
42
        f, err := os.Open(n)
 
43
        if err != nil {
 
44
                return nil, fmt.Errorf("ERROR: Unable to locate or open Azure credentials at %s (%v)", n, err)
 
45
        }
 
46
 
 
47
        b, err := ioutil.ReadAll(f)
 
48
        if err != nil {
 
49
                return nil, fmt.Errorf("ERROR: Unable to read %s (%v)", n, err)
 
50
        }
 
51
 
 
52
        c := map[string]interface{}{}
 
53
        err = json.Unmarshal(b, &c)
 
54
        if err != nil {
 
55
                return nil, fmt.Errorf("ERROR: %s contained invalid JSON (%s)", n, err)
 
56
        }
 
57
 
 
58
        return ensureValueStrings(c), nil
 
59
}
 
60
 
 
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)
 
65
        }
 
66
        return mapOfStrings
 
67
}
 
68
 
 
69
func ensureValueString(value interface{}) string {
 
70
        if value == nil {
 
71
                return ""
 
72
        }
 
73
        switch v := value.(type) {
 
74
        case string:
 
75
                return v
 
76
        default:
 
77
                return fmt.Sprintf("%v", v)
 
78
        }
 
79
}