-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmaasCreds.go
74 lines (62 loc) · 1.62 KB
/
maasCreds.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package gogo
import (
"errors"
"fmt"
"os"
"os/exec"
"strings"
yaml "gopkg.in/yaml.v2"
)
type superCred map[string]creds
func newSuperCred(c creds) *superCred {
sc := superCred{"credentials": c}
return &sc
}
type creds map[string]user
type user map[string]auth
type auth struct {
AuthType string `yaml:"auth-type"`
MaasOauth string `yaml:"maas-oauth"`
}
// CreateMAASCredsYaml is used to create the yaml string to pass to "juju add-credential"
func CreateMAASCredsYaml(cloudName string, username string, maasOauth string) (string, error) {
if cloudName == "" {
return "", errors.New("cloudName must not be empty")
}
if username == "" {
return "", errors.New("User must not be empty")
}
if maasOauth == "" {
return "", errors.New("Maas-Oauth must not be empty")
}
lab := newSuperCred(creds{
cloudName: user{
username: auth{
AuthType: "oauth1",
MaasOauth: maasOauth,
},
},
})
output, err := yaml.Marshal(lab)
if err != nil {
return "", err
}
return string(output), nil
}
// SetMAASCreds will pass in maas credentials to juju add-credential
func (j *Juju) SetMAASCreds() error {
tmp := "JUJU_DATA=" + JujuDataPrefix + j.Name
creds, err := CreateMAASCredsYaml(j.Name, j.MaasCr.Username, j.MaasCr.MaasOauth)
if err != nil {
return fmt.Errorf("setMAASCreds error: %s", err)
}
fmt.Println(creds)
cmd := exec.Command("juju", "add-credential", j.Name, "-f", "/dev/stdin", "--replace")
cmd.Stdin = strings.NewReader(creds)
cmd.Env = append(os.Environ(), tmp)
err = cmd.Run()
if err != nil {
return fmt.Errorf("setMAASCreds error: %v: %s", err, err.(*exec.ExitError).Stderr)
}
return nil
}