~nskaggs/+junk/xenial-test

« back to all changes in this revision

Viewing changes to src/github.com/juju/juju/cmd/juju/controller/unregister.go

  • Committer: Nicholas Skaggs
  • Date: 2016-10-24 20:56:05 UTC
  • Revision ID: nicholas.skaggs@canonical.com-20161024205605-z8lta0uvuhtxwzwl
Initi with beta15

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 controller
 
5
 
 
6
import (
 
7
        "fmt"
 
8
 
 
9
        "github.com/juju/cmd"
 
10
        "github.com/juju/errors"
 
11
        "launchpad.net/gnuflag"
 
12
 
 
13
        jujucmd "github.com/juju/juju/cmd"
 
14
        "github.com/juju/juju/cmd/modelcmd"
 
15
        "github.com/juju/juju/jujuclient"
 
16
)
 
17
 
 
18
// NewUnregisterCommand returns a command to allow the user to unregister a controller.
 
19
func NewUnregisterCommand(store jujuclient.ClientStore) cmd.Command {
 
20
        if store == nil {
 
21
                panic("valid store must be specified")
 
22
        }
 
23
        cmd := &unregisterCommand{store: store}
 
24
        return modelcmd.WrapBase(cmd)
 
25
}
 
26
 
 
27
// unregisterCommand removes a Juju controller from the local store.
 
28
type unregisterCommand struct {
 
29
        modelcmd.JujuCommandBase
 
30
        controllerName string
 
31
        assumeYes      bool
 
32
        store          jujuclient.ClientStore
 
33
}
 
34
 
 
35
var usageUnregisterDetails = `
 
36
Removes local connection information for the specified controller.
 
37
This command does not destroy the controller.  In order to regain
 
38
access to an unregistered controller, it will need to be added
 
39
again using the juju register command.
 
40
 
 
41
Examples:
 
42
 
 
43
    juju unregister my-controller
 
44
 
 
45
See Also:
 
46
    juju register`
 
47
 
 
48
// Info implements Command.Info
 
49
// `unregister` may seem generic as a command, but aligns with `register`.
 
50
func (c *unregisterCommand) Info() *cmd.Info {
 
51
        return &cmd.Info{
 
52
                Name:    "unregister",
 
53
                Args:    "<controller name>",
 
54
                Purpose: "Unregisters a Juju controller",
 
55
                Doc:     usageUnregisterDetails,
 
56
        }
 
57
}
 
58
 
 
59
// SetFlags implements Command.SetFlags.
 
60
func (c *unregisterCommand) SetFlags(f *gnuflag.FlagSet) {
 
61
        f.BoolVar(&c.assumeYes, "y", false, "Do not prompt for confirmation")
 
62
        f.BoolVar(&c.assumeYes, "yes", false, "")
 
63
}
 
64
 
 
65
// Init implements Command.Init.
 
66
func (c *unregisterCommand) Init(args []string) error {
 
67
        if len(args) < 1 {
 
68
                return errors.New("controller name must be specified")
 
69
        }
 
70
        c.controllerName, args = args[0], args[1:]
 
71
 
 
72
        if err := jujuclient.ValidateControllerName(c.controllerName); err != nil {
 
73
                return err
 
74
        }
 
75
 
 
76
        if err := cmd.CheckEmpty(args); err != nil {
 
77
                return err
 
78
        }
 
79
        return nil
 
80
}
 
81
 
 
82
var unregisterMsg = `
 
83
This command will remove connection information for controller %q.
 
84
Doing so will prevent you from accessing this controller until
 
85
you register it again.
 
86
 
 
87
Continue [y/N]?`[1:]
 
88
 
 
89
func (c *unregisterCommand) Run(ctx *cmd.Context) error {
 
90
 
 
91
        _, err := c.store.ControllerByName(c.controllerName)
 
92
        if err != nil {
 
93
                return errors.Trace(err)
 
94
        }
 
95
 
 
96
        if !c.assumeYes {
 
97
                fmt.Fprintf(ctx.Stdout, unregisterMsg, c.controllerName)
 
98
 
 
99
                if err := jujucmd.UserConfirmYes(ctx); err != nil {
 
100
                        return errors.Annotate(err, "unregistering controller")
 
101
                }
 
102
        }
 
103
 
 
104
        return (c.store.RemoveController(c.controllerName))
 
105
}