github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/cmd/juju/caas/remove.go (about) 1 // Copyright 2018 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package caas 5 6 import ( 7 "github.com/juju/cmd" 8 "github.com/juju/errors" 9 "github.com/juju/gnuflag" 10 "github.com/juju/juju/cloud" 11 12 cloudapi "github.com/juju/juju/api/cloud" 13 jujucmd "github.com/juju/juju/cmd" 14 "github.com/juju/juju/cmd/modelcmd" 15 "github.com/juju/juju/jujuclient" 16 ) 17 18 var usageRemoveCAASSummary = ` 19 Removes a k8s endpoint from Juju.`[1:] 20 21 var usageRemoveCAASDetails = ` 22 Removes the specified k8s cloud from the controller (if it is not in use), 23 and user-defined cloud details from this client. 24 25 Examples: 26 juju remove-k8s myk8scloud 27 28 See also: 29 add-k8s 30 ` 31 32 // RemoveCloudAPI is implemented by cloudapi.Client. 33 type RemoveCloudAPI interface { 34 RemoveCloud(string) error 35 Close() error 36 } 37 38 // RemoveCAASCommand is the command that allows you to remove a k8s cloud. 39 type RemoveCAASCommand struct { 40 modelcmd.ControllerCommandBase 41 42 // cloudName is the name of the caas cloud to remove. 43 cloudName string 44 45 cloudMetadataStore CloudMetadataStore 46 fileCredentialStore jujuclient.CredentialStore 47 apiFunc func() (RemoveCloudAPI, error) 48 } 49 50 // NewRemoveCAASCommand returns a command to add caas information. 51 func NewRemoveCAASCommand(cloudMetadataStore CloudMetadataStore) cmd.Command { 52 cmd := &RemoveCAASCommand{ 53 cloudMetadataStore: cloudMetadataStore, 54 fileCredentialStore: jujuclient.NewFileCredentialStore(), 55 } 56 cmd.apiFunc = func() (RemoveCloudAPI, error) { 57 root, err := cmd.NewAPIRoot() 58 if err != nil { 59 return nil, errors.Trace(err) 60 } 61 return cloudapi.NewClient(root), nil 62 } 63 return modelcmd.WrapController(cmd) 64 } 65 66 // Info returns help information about the command. 67 func (c *RemoveCAASCommand) Info() *cmd.Info { 68 return jujucmd.Info(&cmd.Info{ 69 Name: "remove-k8s", 70 Args: "<k8s name>", 71 Purpose: usageRemoveCAASSummary, 72 Doc: usageRemoveCAASDetails, 73 }) 74 } 75 76 // SetFlags initializes the flags supported by the command. 77 func (c *RemoveCAASCommand) SetFlags(f *gnuflag.FlagSet) { 78 c.CommandBase.SetFlags(f) 79 } 80 81 // Init populates the command with the args from the command line. 82 func (c *RemoveCAASCommand) Init(args []string) (err error) { 83 if len(args) == 0 { 84 return errors.Errorf("missing k8s name.") 85 } 86 c.cloudName = args[0] 87 return cmd.CheckEmpty(args[1:]) 88 } 89 90 // Run is defined on the Command interface. 91 func (c *RemoveCAASCommand) Run(ctxt *cmd.Context) error { 92 cloudAPI, err := c.apiFunc() 93 if err != nil { 94 return errors.Trace(err) 95 } 96 defer cloudAPI.Close() 97 98 if err := cloudAPI.RemoveCloud(c.cloudName); err != nil { 99 return errors.Annotatef(err, "cannot remove k8s cloud from controller") 100 } 101 102 if err := removeCloudFromLocal(c.cloudMetadataStore, c.cloudName); err != nil { 103 return errors.Annotatef(err, "cannot remove cloud from local cache") 104 } 105 106 return c.fileCredentialStore.UpdateCredential(c.cloudName, cloud.CloudCredential{}) 107 } 108 109 func removeCloudFromLocal(cloudMetadataStore CloudMetadataStore, cloudName string) error { 110 personalClouds, err := cloudMetadataStore.PersonalCloudMetadata() 111 if err != nil { 112 return errors.Trace(err) 113 } 114 if personalClouds == nil { 115 return nil 116 } 117 _, ok := personalClouds[cloudName] 118 if !ok { 119 return nil 120 } 121 delete(personalClouds, cloudName) 122 return cloudMetadataStore.WritePersonalCloudMetadata(personalClouds) 123 }