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

« back to all changes in this revision

Viewing changes to src/github.com/juju/juju/cmd/juju/model/grantrevoke.go

  • Committer: Martin Packman
  • Date: 2016-03-30 19:31:08 UTC
  • mfrom: (1.1.41)
  • Revision ID: martin.packman@canonical.com-20160330193108-h9iz3ak334uk0z5r
Merge new upstream source 2.0~beta3

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Copyright 2016 Canonical Ltd.
 
2
// Licensed under the AGPLv3, see LICENCE file for details.
 
3
 
 
4
package model
 
5
 
 
6
import (
 
7
        "strings"
 
8
 
 
9
        "github.com/juju/cmd"
 
10
        "github.com/juju/errors"
 
11
        "launchpad.net/gnuflag"
 
12
 
 
13
        "github.com/juju/juju/cmd/juju/block"
 
14
        "github.com/juju/juju/cmd/modelcmd"
 
15
        "github.com/juju/juju/juju/permission"
 
16
)
 
17
 
 
18
type accessCommand struct {
 
19
        modelcmd.ControllerCommandBase
 
20
 
 
21
        User        string
 
22
        ModelNames  []string
 
23
        ModelAccess string
 
24
}
 
25
 
 
26
// SetFlags implements cmd.Command.
 
27
func (c *accessCommand) SetFlags(f *gnuflag.FlagSet) {
 
28
        f.StringVar(&c.ModelAccess, "acl", "read", "access control")
 
29
}
 
30
 
 
31
// Init implements cmd.Command.
 
32
func (c *accessCommand) Init(args []string) error {
 
33
        if len(args) < 1 {
 
34
                return errors.New("no user specified")
 
35
        }
 
36
 
 
37
        if len(args) < 2 {
 
38
                return errors.New("no model specified")
 
39
        }
 
40
 
 
41
        _, err := permission.ParseModelAccess(c.ModelAccess)
 
42
        if err != nil {
 
43
                return err
 
44
        }
 
45
 
 
46
        c.User = args[0]
 
47
        c.ModelNames = args[1:]
 
48
        return nil
 
49
}
 
50
 
 
51
const grantModelHelpDoc = `
 
52
Grant another user access to a model.
 
53
 
 
54
Examples:
 
55
 juju grant joe model1
 
56
     Grant user "joe" default (read) access to the current model
 
57
 
 
58
 juju grant joe model1 --acl=write
 
59
     Grant user "joe" write access to the current model
 
60
 
 
61
 juju grant sam model1 model2
 
62
     Grant user "sam" default (read) access to two models named "model1" and "model2".
 
63
 `
 
64
 
 
65
// NewGrantCommand returns a new grant command.
 
66
func NewGrantCommand() cmd.Command {
 
67
        return modelcmd.WrapController(&grantCommand{})
 
68
}
 
69
 
 
70
// grantCommand represents the command to grant a user access to one or more models.
 
71
type grantCommand struct {
 
72
        accessCommand
 
73
        api GrantModelAPI
 
74
}
 
75
 
 
76
// Info implements Command.Info.
 
77
func (c *grantCommand) Info() *cmd.Info {
 
78
        return &cmd.Info{
 
79
                Name:    "grant",
 
80
                Args:    "<user> <model1> [<model2> .. <modelN>]",
 
81
                Purpose: "grant another user access to the given models",
 
82
                Doc:     strings.TrimSpace(grantModelHelpDoc),
 
83
        }
 
84
}
 
85
 
 
86
func (c *grantCommand) getAPI() (GrantModelAPI, error) {
 
87
        if c.api != nil {
 
88
                return c.api, nil
 
89
        }
 
90
        return c.NewModelManagerAPIClient()
 
91
}
 
92
 
 
93
// GrantModelAPI defines the API functions used by the grant command.
 
94
type GrantModelAPI interface {
 
95
        Close() error
 
96
        GrantModel(user, access string, modelUUIDs ...string) error
 
97
}
 
98
 
 
99
// Run implements cmd.Command.
 
100
func (c *grantCommand) Run(ctx *cmd.Context) error {
 
101
        client, err := c.getAPI()
 
102
        if err != nil {
 
103
                return err
 
104
        }
 
105
        defer client.Close()
 
106
 
 
107
        models, err := c.ModelUUIDs(c.ModelNames)
 
108
        if err != nil {
 
109
                return err
 
110
        }
 
111
        return block.ProcessBlockedError(client.GrantModel(c.User, c.ModelAccess, models...), block.BlockChange)
 
112
}
 
113
 
 
114
const revokeModelHelpDoc = `
 
115
Deny a user access to an model that was previously shared with them.
 
116
 
 
117
Revoking read access also revokes write access.
 
118
 
 
119
Examples:
 
120
 juju revoke joe model1
 
121
     Revoke read access from user "joe" for model "model1".
 
122
 
 
123
 juju revoke joe model1 model2 --acl=write
 
124
     Revoke write access from user "joe" for models "model1" and "model2".
 
125
`
 
126
 
 
127
// NewRevokeCommand returns a new revoke command.
 
128
func NewRevokeCommand() cmd.Command {
 
129
        return modelcmd.WrapController(&revokeCommand{})
 
130
}
 
131
 
 
132
// revokeCommand revokes a user's access to models.
 
133
type revokeCommand struct {
 
134
        accessCommand
 
135
        api RevokeModelAPI
 
136
}
 
137
 
 
138
// Info implements cmd.Command.
 
139
func (c *revokeCommand) Info() *cmd.Info {
 
140
        return &cmd.Info{
 
141
                Name:    "revoke",
 
142
                Args:    "<user> <model1> [<model2> .. <modelN>]",
 
143
                Purpose: "revoke user access to models",
 
144
                Doc:     strings.TrimSpace(revokeModelHelpDoc),
 
145
        }
 
146
}
 
147
 
 
148
func (c *revokeCommand) getAPI() (RevokeModelAPI, error) {
 
149
        if c.api != nil {
 
150
                return c.api, nil
 
151
        }
 
152
        return c.NewModelManagerAPIClient()
 
153
}
 
154
 
 
155
// RevokeModelAPI defines the API functions used by the revoke command.
 
156
type RevokeModelAPI interface {
 
157
        Close() error
 
158
        RevokeModel(user, access string, modelUUIDs ...string) error
 
159
}
 
160
 
 
161
// Run implements cmd.Command.
 
162
func (c *revokeCommand) Run(ctx *cmd.Context) error {
 
163
        client, err := c.getAPI()
 
164
        if err != nil {
 
165
                return err
 
166
        }
 
167
        defer client.Close()
 
168
 
 
169
        modelUUIDs, err := c.ModelUUIDs(c.ModelNames)
 
170
        if err != nil {
 
171
                return err
 
172
        }
 
173
        return block.ProcessBlockedError(client.RevokeModel(c.User, c.ModelAccess, modelUUIDs...), block.BlockChange)
 
174
}