~juju-qa/ubuntu/xenial/juju/2.0-rc2

« back to all changes in this revision

Viewing changes to src/github.com/juju/juju/state/cloudcredentials.go

  • Committer: Nicholas Skaggs
  • Date: 2016-09-30 14:39:30 UTC
  • mfrom: (1.8.1)
  • Revision ID: nicholas.skaggs@canonical.com-20160930143930-vwwhrefh6ftckccy
import upstream

Show diffs side-by-side

added added

removed removed

Lines of Context:
23
23
        Owner      string            `bson:"owner"`
24
24
        Cloud      string            `bson:"cloud"`
25
25
        Name       string            `bson:"name"`
 
26
        Revoked    bool              `bson:"revoked"`
26
27
        AuthType   string            `bson:"auth-type"`
27
28
        Attributes map[string]string `bson:"attributes,omitempty"`
28
29
}
48
49
 
49
50
// CloudCredentials returns the user's cloud credentials for a given cloud,
50
51
// keyed by credential name.
51
 
func (st *State) CloudCredentials(user names.UserTag, cloudName string) (
52
 
        map[names.CloudCredentialTag]cloud.Credential, error,
53
 
) {
 
52
func (st *State) CloudCredentials(user names.UserTag, cloudName string) (map[string]cloud.Credential, error) {
54
53
        coll, cleanup := st.getCollection(cloudCredentialsC)
55
54
        defer cleanup()
56
55
 
57
56
        var doc cloudCredentialDoc
58
 
        credentials := make(map[names.CloudCredentialTag]cloud.Credential)
 
57
        credentials := make(map[string]cloud.Credential)
59
58
        iter := coll.Find(bson.D{
60
59
                {"owner", user.Canonical()},
61
60
                {"cloud", cloudName},
65
64
                if err != nil {
66
65
                        return nil, errors.Trace(err)
67
66
                }
68
 
                credentials[tag] = doc.toCredential()
 
67
                credentials[tag.Canonical()] = doc.toCredential()
69
68
        }
70
69
        if err := iter.Err(); err != nil {
71
70
                return nil, errors.Annotatef(
89
88
                if err != nil {
90
89
                        return nil, errors.Annotate(err, "validating cloud credentials")
91
90
                }
92
 
                existingCreds, err := st.CloudCredentials(tag.Owner(), cloudName)
93
 
                if err != nil {
 
91
                _, err = st.CloudCredential(tag)
 
92
                if err != nil && !errors.IsNotFound(err) {
94
93
                        return nil, errors.Maskf(err, "fetching cloud credentials")
95
94
                }
96
 
                if _, ok := existingCreds[tag]; ok {
 
95
                if err == nil {
97
96
                        ops = append(ops, updateCloudCredentialOp(tag, credential))
98
97
                } else {
99
98
                        ops = append(ops, createCloudCredentialOp(tag, credential))
137
136
                        Name:       tag.Name(),
138
137
                        AuthType:   string(cred.AuthType()),
139
138
                        Attributes: cred.Attributes(),
 
139
                        Revoked:    cred.Revoked,
140
140
                },
141
141
        }
142
142
}
151
151
                Update: bson.D{{"$set", bson.D{
152
152
                        {"auth-type", string(cred.AuthType())},
153
153
                        {"attributes", cred.Attributes()},
 
154
                        {"revoked", cred.Revoked},
154
155
                }}},
155
156
        }
156
157
}
171
172
}
172
173
 
173
174
func (c cloudCredentialDoc) cloudCredentialTag() (names.CloudCredentialTag, error) {
174
 
        id := fmt.Sprintf("%s/%s/%s", c.Cloud, c.Owner, c.Name)
 
175
        ownerTag := names.NewUserTag(c.Owner)
 
176
        id := fmt.Sprintf("%s/%s/%s", c.Cloud, ownerTag.Canonical(), c.Name)
175
177
        if !names.IsValidCloudCredential(id) {
176
178
                return names.CloudCredentialTag{}, errors.NotValidf("cloud credential ID")
177
179
        }
180
182
 
181
183
func (c cloudCredentialDoc) toCredential() cloud.Credential {
182
184
        out := cloud.NewCredential(cloud.AuthType(c.AuthType), c.Attributes)
 
185
        out.Revoked = c.Revoked
183
186
        out.Label = c.Name
184
187
        return out
185
188
}
234
237
        }
235
238
        return ops, nil
236
239
}
 
240
 
 
241
// WatchCredential returns a new NotifyWatcher watching for
 
242
// changes to the specified credential.
 
243
func (st *State) WatchCredential(cred names.CloudCredentialTag) NotifyWatcher {
 
244
        filter := func(rawId interface{}) bool {
 
245
                id, ok := rawId.(string)
 
246
                if !ok {
 
247
                        return false
 
248
                }
 
249
                return id == cloudCredentialDocID(cred)
 
250
        }
 
251
        return newNotifyCollWatcher(st, cloudCredentialsC, filter)
 
252
}