/
grantrevokecloud.go
200 lines (163 loc) · 4.98 KB
/
grantrevokecloud.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
// Copyright 2018 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package model
import (
"strings"
"github.com/juju/cmd/v3"
"github.com/juju/errors"
"github.com/juju/names/v5"
"github.com/juju/juju/api/client/cloud"
jujucmd "github.com/juju/juju/cmd"
"github.com/juju/juju/cmd/juju/block"
"github.com/juju/juju/cmd/modelcmd"
"github.com/juju/juju/core/permission"
)
var validCloudAccessLevels = `
Valid access levels are:
`[1:] + strings.Join(filterAccessLevels(permission.AllAccessLevels, permission.ValidateCloudAccess), "\n ")
var usageGrantCloudSummary = `
Grants access level to a Juju user for a cloud.`[1:]
var usageGrantCloudDetails = validCloudAccessLevels
const usageGrantCloudExamples = `
Grant user 'joe' 'add-model' access to cloud 'fluffy':
juju grant-cloud joe add-model fluffy
`
var usageRevokeCloudSummary = `
Revokes access from a Juju user for a cloud.`[1:]
var usageRevokeCloudDetails = `
Revoking admin access, from a user who has that permission, will leave
that user with add-model access. Revoking add-model access, however, also revokes
admin access.
`[1:] + validCloudAccessLevels
const usageRevokeCloudExamples = `
Revoke 'add-model' (and 'admin') access from user 'joe' for cloud 'fluffy':
juju revoke-cloud joe add-model fluffy
Revoke 'admin' access from user 'sam' for clouds 'fluffy' and 'rainy':
juju revoke-cloud sam admin fluffy rainy
`
type accessCloudCommand struct {
modelcmd.ControllerCommandBase
User string
Clouds []string
Access string
}
// Init implements cmd.Command.
func (c *accessCloudCommand) Init(args []string) error {
if len(args) < 1 {
return errors.New("no user specified")
}
if len(args) < 2 {
return errors.New("no permission level specified")
}
c.User = args[0]
c.Access = args[1]
// The remaining args are cloud names.
for _, arg := range args[2:] {
if !names.IsValidCloud(arg) {
return errors.NotValidf("cloud name %q", arg)
}
c.Clouds = append(c.Clouds, arg)
}
// Special case for backwards compatibility.
if c.Access == "addmodel" {
c.Access = "add-model"
}
if len(c.Clouds) > 0 {
return permission.ValidateCloudAccess(permission.Access(c.Access))
}
return errors.Errorf("You need to specify one or more cloud names.\n" +
"See 'juju help grant-cloud'.")
}
// NewGrantCloudCommand returns a new grant command.
func NewGrantCloudCommand() cmd.Command {
return modelcmd.WrapController(&grantCloudCommand{})
}
// grantCloudCommand represents the command to grant a user access to one or more clouds.
type grantCloudCommand struct {
accessCloudCommand
cloudsApi GrantCloudAPI
}
// Info implements Command.Info.
func (c *grantCloudCommand) Info() *cmd.Info {
return jujucmd.Info(&cmd.Info{
Name: "grant-cloud",
Args: "<user name> <permission> <cloud name> ...",
Purpose: usageGrantCloudSummary,
Doc: usageGrantCloudDetails,
Examples: usageGrantCloudExamples,
SeeAlso: []string{
"revoke-cloud",
"add-user",
},
})
}
func (c *grantCloudCommand) getCloudsAPI() (GrantCloudAPI, error) {
if c.cloudsApi != nil {
return c.cloudsApi, nil
}
root, err := c.NewAPIRoot()
if err != nil {
return nil, errors.Trace(err)
}
return cloud.NewClient(root), nil
}
// GrantCloudAPI defines the API functions used by the grant command.
type GrantCloudAPI interface {
Close() error
GrantCloud(user, access string, clouds ...string) error
}
// Run implements cmd.Command.
func (c *grantCloudCommand) Run(ctx *cmd.Context) error {
client, err := c.getCloudsAPI()
if err != nil {
return err
}
defer client.Close()
return block.ProcessBlockedError(client.GrantCloud(c.User, c.Access, c.Clouds...), block.BlockChange)
}
// NewRevokeCloudCommand returns a new revoke command.
func NewRevokeCloudCommand() cmd.Command {
return modelcmd.WrapController(&revokeCloudCommand{})
}
// revokeCloudCommand revokes a user's access to clouds.
type revokeCloudCommand struct {
accessCloudCommand
cloudsApi RevokeCloudAPI
}
// Info implements cmd.Command.
func (c *revokeCloudCommand) Info() *cmd.Info {
return jujucmd.Info(&cmd.Info{
Name: "revoke-cloud",
Args: "<user name> <permission> <cloud name> ...",
Purpose: usageRevokeCloudSummary,
Doc: usageRevokeCloudDetails,
Examples: usageRevokeCloudExamples,
SeeAlso: []string{
"grant-cloud",
},
})
}
func (c *revokeCloudCommand) getCloudAPI() (RevokeCloudAPI, error) {
if c.cloudsApi != nil {
return c.cloudsApi, nil
}
root, err := c.NewAPIRoot()
if err != nil {
return nil, errors.Trace(err)
}
return cloud.NewClient(root), nil
}
// RevokeCloudAPI defines the API functions used by the revoke cloud command.
type RevokeCloudAPI interface {
Close() error
RevokeCloud(user, access string, clouds ...string) error
}
// Run implements cmd.Command.
func (c *revokeCloudCommand) Run(ctx *cmd.Context) error {
client, err := c.getCloudAPI()
if err != nil {
return err
}
defer client.Close()
return block.ProcessBlockedError(client.RevokeCloud(c.User, c.Access, c.Clouds...), block.BlockChange)
}