~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/share.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 2015 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
 
        "github.com/juju/names"
12
 
 
13
 
        "github.com/juju/juju/cmd/juju/block"
14
 
        "github.com/juju/juju/cmd/modelcmd"
15
 
)
16
 
 
17
 
const shareEnvHelpDoc = `
18
 
Share the current model with another user.
19
 
 
20
 
Examples:
21
 
 juju share-model joe
22
 
     Give local user "joe" access to the current model
23
 
 
24
 
 juju share-model user1 user2 user3@ubuntuone
25
 
     Give two local users and one remote user access to the current model
26
 
 
27
 
 juju share-model sam --model myenv
28
 
     Give local user "sam" access to the model named "myenv"
29
 
 `
30
 
 
31
 
func NewShareCommand() cmd.Command {
32
 
        return modelcmd.Wrap(&shareCommand{})
33
 
}
34
 
 
35
 
// shareCommand represents the command to share an environment with a user(s).
36
 
type shareCommand struct {
37
 
        modelcmd.ModelCommandBase
38
 
        envName string
39
 
        api     ShareEnvironmentAPI
40
 
 
41
 
        // Users to share the environment with.
42
 
        Users []names.UserTag
43
 
}
44
 
 
45
 
// Info implements Command.Info.
46
 
func (c *shareCommand) Info() *cmd.Info {
47
 
        return &cmd.Info{
48
 
                Name:    "share-model",
49
 
                Args:    "<user> ...",
50
 
                Purpose: "share the current model with another user",
51
 
                Doc:     strings.TrimSpace(shareEnvHelpDoc),
52
 
        }
53
 
}
54
 
 
55
 
func (c *shareCommand) Init(args []string) (err error) {
56
 
        if len(args) == 0 {
57
 
                return errors.New("no users specified")
58
 
        }
59
 
 
60
 
        for _, arg := range args {
61
 
                if !names.IsValidUser(arg) {
62
 
                        return errors.Errorf("invalid username: %q", arg)
63
 
                }
64
 
                c.Users = append(c.Users, names.NewUserTag(arg))
65
 
        }
66
 
 
67
 
        return nil
68
 
}
69
 
 
70
 
func (c *shareCommand) getAPI() (ShareEnvironmentAPI, error) {
71
 
        if c.api != nil {
72
 
                return c.api, nil
73
 
        }
74
 
        return c.NewAPIClient()
75
 
}
76
 
 
77
 
// ShareEnvironmentAPI defines the API functions used by the environment share command.
78
 
type ShareEnvironmentAPI interface {
79
 
        Close() error
80
 
        ShareModel(...names.UserTag) error
81
 
}
82
 
 
83
 
func (c *shareCommand) Run(ctx *cmd.Context) error {
84
 
        client, err := c.getAPI()
85
 
        if err != nil {
86
 
                return err
87
 
        }
88
 
        defer client.Close()
89
 
 
90
 
        return block.ProcessBlockedError(client.ShareModel(c.Users...), block.BlockChange)
91
 
}