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

« back to all changes in this revision

Viewing changes to src/github.com/juju/juju/apiserver/agent/agent.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:
18
18
        "github.com/juju/juju/state"
19
19
        "github.com/juju/juju/state/multiwatcher"
20
20
        "github.com/juju/juju/state/stateenvirons"
 
21
        "github.com/juju/juju/state/watcher"
21
22
)
22
23
 
23
24
func init() {
32
33
        *common.ControllerConfigAPI
33
34
        cloudspec.CloudSpecAPI
34
35
 
35
 
        st   *state.State
36
 
        auth facade.Authorizer
 
36
        st        *state.State
 
37
        auth      facade.Authorizer
 
38
        resources facade.Resources
37
39
}
38
40
 
39
41
// NewAgentAPIV2 returns an object implementing version 2 of the Agent API
55
57
                CloudSpecAPI:        cloudspec.NewCloudSpec(environConfigGetter.CloudSpec, common.AuthFuncForTag(st.ModelTag())),
56
58
                st:                  st,
57
59
                auth:                auth,
 
60
                resources:           resources,
58
61
        }, nil
59
62
}
60
63
 
154
157
        }
155
158
        return pjobs
156
159
}
 
160
 
 
161
// WatchCredentials watches for changes to the specified credentials.
 
162
func (api *AgentAPIV2) WatchCredentials(args params.Entities) (params.NotifyWatchResults, error) {
 
163
        if !api.auth.AuthModelManager() {
 
164
                return params.NotifyWatchResults{}, common.ErrPerm
 
165
        }
 
166
 
 
167
        results := params.NotifyWatchResults{
 
168
                Results: make([]params.NotifyWatchResult, len(args.Entities)),
 
169
        }
 
170
        for i, entity := range args.Entities {
 
171
                credentialTag, err := names.ParseCloudCredentialTag(entity.Tag)
 
172
                if err != nil {
 
173
                        results.Results[i].Error = common.ServerError(err)
 
174
                        continue
 
175
                }
 
176
                watch := api.st.WatchCredential(credentialTag)
 
177
                // Consume the initial event. Technically, API calls to Watch
 
178
                // 'transmit' the initial event in the Watch response. But
 
179
                // NotifyWatchers have no state to transmit.
 
180
                if _, ok := <-watch.Changes(); ok {
 
181
                        results.Results[i].NotifyWatcherId = api.resources.Register(watch)
 
182
                } else {
 
183
                        err = watcher.EnsureErr(watch)
 
184
                        results.Results[i].Error = common.ServerError(err)
 
185
                }
 
186
        }
 
187
        return results, nil
 
188
}