github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/cmd/juju/application/suspendrelation.go (about) 1 // Copyright 2017 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package application 5 6 import ( 7 "strconv" 8 "strings" 9 10 "github.com/juju/cmd" 11 "github.com/juju/errors" 12 13 "github.com/juju/gnuflag" 14 "github.com/juju/juju/api/application" 15 jujucmd "github.com/juju/juju/cmd" 16 "github.com/juju/juju/cmd/juju/block" 17 "github.com/juju/juju/cmd/modelcmd" 18 ) 19 20 var suspendHelpSummary = ` 21 Suspends a relation to an application offer.`[1:] 22 23 var suspendHelpDetails = ` 24 A relation between an application in another model and an offer in this model will be suspended. 25 The relation-departed and relation-broken hooks will be run for the relation, and the relation 26 status will be set to suspended. The relation is specified using its id. 27 28 Examples: 29 juju suspend-relation 123 30 juju suspend-relation 123 --message "reason for suspending" 31 juju suspend-relation 123 456 --message "reason for suspending" 32 33 See also: 34 add-relation 35 offers 36 remove-relation 37 resume-relation` 38 39 // NewSuspendRelationCommand returns a command to suspend a relation. 40 func NewSuspendRelationCommand() cmd.Command { 41 cmd := &suspendRelationCommand{} 42 cmd.newAPIFunc = func() (SetRelationSuspendedAPI, error) { 43 root, err := cmd.NewAPIRoot() 44 if err != nil { 45 return nil, errors.Trace(err) 46 } 47 return application.NewClient(root), nil 48 } 49 return modelcmd.Wrap(cmd) 50 } 51 52 type suspendRelationCommand struct { 53 modelcmd.ModelCommandBase 54 relationIds []int 55 message string 56 newAPIFunc func() (SetRelationSuspendedAPI, error) 57 } 58 59 func (c *suspendRelationCommand) Info() *cmd.Info { 60 return jujucmd.Info(&cmd.Info{ 61 Name: "suspend-relation", 62 Args: "<relation-id>[ <relation-id>...]", 63 Purpose: suspendHelpSummary, 64 Doc: suspendHelpDetails, 65 }) 66 } 67 68 func (c *suspendRelationCommand) Init(args []string) (err error) { 69 if len(args) == 0 { 70 return errors.New("no relation ids specified") 71 } 72 for _, id := range args { 73 if relId, err := strconv.Atoi(strings.TrimSpace(id)); err != nil || relId < 0 { 74 return errors.NotValidf("relation ID %q", id) 75 } else { 76 c.relationIds = append(c.relationIds, relId) 77 } 78 } 79 return nil 80 } 81 82 func (c *suspendRelationCommand) SetFlags(f *gnuflag.FlagSet) { 83 c.ModelCommandBase.SetFlags(f) 84 f.StringVar(&c.message, "message", "", "reason for suspension") 85 } 86 87 // SetRelationSuspendedAPI defines the API methods that the suspend/resume relation commands use. 88 type SetRelationSuspendedAPI interface { 89 Close() error 90 BestAPIVersion() int 91 SetRelationSuspended(relationIds []int, suspended bool, message string) error 92 } 93 94 func (c *suspendRelationCommand) Run(_ *cmd.Context) error { 95 client, err := c.newAPIFunc() 96 if err != nil { 97 return err 98 } 99 defer client.Close() 100 if client.BestAPIVersion() < 5 { 101 return errors.New("suspending a relation is not supported by this version of Juju") 102 } 103 err = client.SetRelationSuspended(c.relationIds, true, c.message) 104 return block.ProcessBlockedError(err, block.BlockChange) 105 }