forked from juju/juju
/
client.go
170 lines (151 loc) · 4.64 KB
/
client.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
// Copyright 2015 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package annotations
import (
"github.com/juju/errors"
"gopkg.in/juju/names.v2"
"github.com/juju/juju/apiserver/common"
"github.com/juju/juju/apiserver/facade"
"github.com/juju/juju/apiserver/params"
"github.com/juju/juju/permission"
"github.com/juju/juju/state"
)
func init() {
common.RegisterStandardFacade("Annotations", 2, NewAPI)
}
var getState = func(st *state.State) annotationAccess {
return stateShim{st}
}
// Annotations defines the methods on the service API end point.
type Annotations interface {
Get(args params.Entities) params.AnnotationsGetResults
Set(args params.AnnotationsSet) params.ErrorResults
}
// API implements the service interface and is the concrete
// implementation of the api end point.
type API struct {
access annotationAccess
authorizer facade.Authorizer
}
// NewAPI returns a new charm annotator API facade.
func NewAPI(
st *state.State,
resources facade.Resources,
authorizer facade.Authorizer,
) (*API, error) {
if !authorizer.AuthClient() {
return nil, common.ErrPerm
}
return &API{
access: getState(st),
authorizer: authorizer,
}, nil
}
func (api *API) checkCanRead() error {
canRead, err := api.authorizer.HasPermission(permission.ReadAccess, api.access.ModelTag())
if err != nil {
return errors.Trace(err)
}
if !canRead {
return common.ErrPerm
}
return nil
}
func (api *API) checkCanWrite() error {
canWrite, err := api.authorizer.HasPermission(permission.WriteAccess, api.access.ModelTag())
if err != nil {
return errors.Trace(err)
}
if !canWrite {
return common.ErrPerm
}
return nil
}
// Get returns annotations for given entities.
// If annotations cannot be retrieved for a given entity, an error is returned.
// Each entity is treated independently and, hence, will fail or succeed independently.
func (api *API) Get(args params.Entities) params.AnnotationsGetResults {
if err := api.checkCanRead(); err != nil {
result := make([]params.AnnotationsGetResult, len(args.Entities))
for i := range result {
result[i].Error = params.ErrorResult{Error: common.ServerError(err)}
}
return params.AnnotationsGetResults{Results: result}
}
entityResults := []params.AnnotationsGetResult{}
for _, entity := range args.Entities {
anEntityResult := params.AnnotationsGetResult{EntityTag: entity.Tag}
if annts, err := api.getEntityAnnotations(entity.Tag); err != nil {
anEntityResult.Error = params.ErrorResult{annotateError(err, entity.Tag, "getting")}
} else {
anEntityResult.Annotations = annts
}
entityResults = append(entityResults, anEntityResult)
}
return params.AnnotationsGetResults{Results: entityResults}
}
// Set stores annotations for given entities
func (api *API) Set(args params.AnnotationsSet) params.ErrorResults {
if err := api.checkCanWrite(); err != nil {
errorResults := make([]params.ErrorResult, len(args.Annotations))
for i := range errorResults {
errorResults[i].Error = common.ServerError(err)
}
return params.ErrorResults{Results: errorResults}
}
setErrors := []params.ErrorResult{}
for _, entityAnnotation := range args.Annotations {
err := api.setEntityAnnotations(entityAnnotation.EntityTag, entityAnnotation.Annotations)
if err != nil {
setErrors = append(setErrors,
params.ErrorResult{Error: annotateError(err, entityAnnotation.EntityTag, "setting")})
}
}
return params.ErrorResults{Results: setErrors}
}
func annotateError(err error, tag, op string) *params.Error {
return common.ServerError(
errors.Trace(
errors.Annotatef(
err, "while %v annotations to %q", op, tag)))
}
func (api *API) getEntityAnnotations(entityTag string) (map[string]string, error) {
tag, err := names.ParseTag(entityTag)
if err != nil {
return nil, errors.Trace(err)
}
entity, err := api.findEntity(tag)
if err != nil {
return nil, errors.Trace(err)
}
annotations, err := api.access.GetAnnotations(entity)
if err != nil {
return nil, errors.Trace(err)
}
return annotations, nil
}
func (api *API) findEntity(tag names.Tag) (state.GlobalEntity, error) {
entity0, err := api.access.FindEntity(tag)
if err != nil {
if errors.IsNotFound(err) {
return nil, common.ErrPerm
}
return nil, err
}
entity, ok := entity0.(state.GlobalEntity)
if !ok {
return nil, common.NotSupportedError(tag, "annotations")
}
return entity, nil
}
func (api *API) setEntityAnnotations(entityTag string, annotations map[string]string) error {
tag, err := names.ParseTag(entityTag)
if err != nil {
return errors.Trace(err)
}
entity, err := api.findEntity(tag)
if err != nil {
return errors.Trace(err)
}
return api.access.SetAnnotations(entity, annotations)
}