forked from juju/description
/
cloudcredential.go
132 lines (113 loc) · 3.61 KB
/
cloudcredential.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
// Copyright 2016 Canonical Ltd.
// Licensed under the LGPLv3, see LICENCE file for details.
package description
import (
"github.com/juju/errors"
"github.com/juju/schema"
"gopkg.in/juju/names.v2"
)
// CloudCredential represents the current cloud credential for the model.
type CloudCredential interface {
Owner() string
Cloud() string
Name() string
AuthType() string
Attributes() map[string]string
}
// CloudCredentialArgs is an argument struct used to create a new internal
// cloudCredential type that supports the CloudCredential interface.
type CloudCredentialArgs struct {
Owner names.UserTag
Cloud names.CloudTag
Name string
AuthType string
Attributes map[string]string
}
func newCloudCredential(args CloudCredentialArgs) *cloudCredential {
return &cloudCredential{
Version: 1,
Owner_: args.Owner.Id(),
Cloud_: args.Cloud.Id(),
Name_: args.Name,
AuthType_: args.AuthType,
Attributes_: args.Attributes,
}
}
// cloudCredential represents an IP CloudCredential of some form.
type cloudCredential struct {
Version int `yaml:"version"`
Owner_ string `yaml:"owner"`
Cloud_ string `yaml:"cloud"`
Name_ string `yaml:"name"`
AuthType_ string `yaml:"auth-type"`
Attributes_ map[string]string `yaml:"attributes,omitempty"`
}
// Owner implements CloudCredential.
func (c *cloudCredential) Owner() string {
return c.Owner_
}
// Cloud implements CloudCredential.
func (c *cloudCredential) Cloud() string {
return c.Cloud_
}
// Name implements CloudCredential.
func (c *cloudCredential) Name() string {
return c.Name_
}
// AuthType implements CloudCredential.
func (c *cloudCredential) AuthType() string {
return c.AuthType_
}
// Attributes implements CloudCredential.
func (c *cloudCredential) Attributes() map[string]string {
return c.Attributes_
}
// importCloudCredential constructs a new CloudCredential from a map
// representing a serialised CloudCredential instance.
func importCloudCredential(source map[string]interface{}) (*cloudCredential, error) {
version, err := getVersion(source)
if err != nil {
return nil, errors.Annotate(err, "cloudCredential version schema check failed")
}
importFunc, ok := cloudCredentialDeserializationFuncs[version]
if !ok {
return nil, errors.NotValidf("version %d", version)
}
return importFunc(source)
}
type cloudCredentialDeserializationFunc func(map[string]interface{}) (*cloudCredential, error)
var cloudCredentialDeserializationFuncs = map[int]cloudCredentialDeserializationFunc{
1: importCloudCredentialV1,
}
func importCloudCredentialV1(source map[string]interface{}) (*cloudCredential, error) {
fields := schema.Fields{
"owner": schema.String(),
"cloud": schema.String(),
"name": schema.String(),
"auth-type": schema.String(),
"attributes": schema.StringMap(schema.String()),
}
// Some values don't have to be there.
defaults := schema.Defaults{
"attributes": schema.Omit,
}
checker := schema.FieldMap(fields, defaults)
coerced, err := checker.Coerce(source, nil)
if err != nil {
return nil, errors.Annotatef(err, "cloudCredential v1 schema check failed")
}
valid := coerced.(map[string]interface{})
// From here we know that the map returned from the schema coercion
// contains fields of the right type.
creds := &cloudCredential{
Version: 1,
Owner_: valid["owner"].(string),
Cloud_: valid["cloud"].(string),
Name_: valid["name"].(string),
AuthType_: valid["auth-type"].(string),
}
if attributes, found := valid["attributes"]; found {
creds.Attributes_ = convertToStringMap(attributes)
}
return creds, nil
}