/
client.go
135 lines (120 loc) · 4.13 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
// Copyright 2015 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package annotations
import (
"github.com/juju/errors"
"github.com/juju/names/v5"
apiservererrors "github.com/juju/juju/apiserver/errors"
"github.com/juju/juju/apiserver/facade"
"github.com/juju/juju/core/permission"
"github.com/juju/juju/rpc/params"
"github.com/juju/juju/state"
)
var getState = func(st *state.State, m *state.Model) annotationAccess {
return stateShim{st, m}
}
// 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
}
func (api *API) checkCanRead() error {
return api.authorizer.HasPermission(permission.ReadAccess, api.access.ModelTag())
}
func (api *API) checkCanWrite() error {
return api.authorizer.HasPermission(permission.WriteAccess, api.access.ModelTag())
}
// 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: apiservererrors.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 = apiservererrors.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 apiservererrors.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.Annotations(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, apiservererrors.ErrPerm
}
return nil, err
}
entity, ok := entity0.(state.GlobalEntity)
if !ok {
return nil, apiservererrors.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)
}