github.com/wallyworld/juju@v0.0.0-20161013125918-6cf1bc9d917a/cmd/juju/application/addrelation.go (about) 1 // Copyright 2012, 2013 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package application 5 6 import ( 7 "github.com/juju/cmd" 8 "github.com/juju/errors" 9 10 "github.com/juju/juju/api/application" 11 "github.com/juju/juju/apiserver/params" 12 "github.com/juju/juju/cmd/juju/block" 13 "github.com/juju/juju/cmd/juju/common" 14 "github.com/juju/juju/cmd/modelcmd" 15 ) 16 17 // NewAddRelationCommand returns a command to add a relation between 2 services. 18 func NewAddRelationCommand() cmd.Command { 19 cmd := &addRelationCommand{} 20 cmd.newAPIFunc = func() (ApplicationAddRelationAPI, error) { 21 root, err := cmd.NewAPIRoot() 22 if err != nil { 23 return nil, errors.Trace(err) 24 } 25 return application.NewClient(root), nil 26 27 } 28 return modelcmd.Wrap(cmd) 29 } 30 31 // addRelationCommand adds a relation between two application endpoints. 32 type addRelationCommand struct { 33 modelcmd.ModelCommandBase 34 Endpoints []string 35 newAPIFunc func() (ApplicationAddRelationAPI, error) 36 } 37 38 func (c *addRelationCommand) Info() *cmd.Info { 39 return &cmd.Info{ 40 Name: "add-relation", 41 Aliases: []string{"relate"}, 42 Args: "<application1>[:<relation name1>] <application2>[:<relation name2>]", 43 Purpose: "Add a relation between two applications.", 44 } 45 } 46 47 func (c *addRelationCommand) Init(args []string) error { 48 if len(args) != 2 { 49 return errors.Errorf("a relation must involve two applications") 50 } 51 c.Endpoints = args 52 return nil 53 } 54 55 // ApplicationAddRelationAPI defines the API methods that application add relation command uses. 56 type ApplicationAddRelationAPI interface { 57 Close() error 58 AddRelation(endpoints ...string) (*params.AddRelationResults, error) 59 } 60 61 func (c *addRelationCommand) Run(ctx *cmd.Context) error { 62 client, err := c.newAPIFunc() 63 if err != nil { 64 return err 65 } 66 defer client.Close() 67 _, err = client.AddRelation(c.Endpoints...) 68 if params.IsCodeUnauthorized(err) { 69 common.PermissionsMessage(ctx.Stderr, "add a relation") 70 } 71 return block.ProcessBlockedError(err, block.BlockChange) 72 }