github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/cmd/juju/application/removerelation.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  	"strconv"
     8  
     9  	"github.com/juju/cmd"
    10  	"github.com/juju/errors"
    11  
    12  	"github.com/juju/juju/api/application"
    13  	jujucmd "github.com/juju/juju/cmd"
    14  	"github.com/juju/juju/cmd/juju/block"
    15  	"github.com/juju/juju/cmd/modelcmd"
    16  )
    17  
    18  var helpSummary = `
    19  Removes an existing relation between two applications.`[1:]
    20  
    21  var helpDetails = `
    22  An existing relation between the two specified applications will be removed. 
    23  This should not result in either of the applications entering an error state,
    24  but may result in either or both of the applications being unable to continue
    25  normal operation. In the case that there is more than one relation between
    26  two applications it is necessary to specify which is to be removed (see
    27  examples). Relations will automatically be removed when using the`[1:] + "`juju\nremove-application`" + ` command.
    28  
    29  The relation is specified using the relation endpoint names, eg
    30    mysql wordpress, or
    31    mediawiki:db mariadb:db
    32  
    33  It is also possible to specify the relation ID, if known. This is useful to
    34  terminate a relation originating from a different model, where only the ID is known. 
    35  
    36  Examples:
    37      juju remove-relation mysql wordpress
    38      juju remove-relation 4
    39  
    40  In the case of multiple relations, the relation name should be specified
    41  at least once - the following examples will all have the same effect:
    42  
    43      juju remove-relation mediawiki:db mariadb:db
    44      juju remove-relation mediawiki mariadb:db
    45      juju remove-relation mediawiki:db mariadb
    46   
    47  See also: 
    48      add-relation
    49      remove-application`
    50  
    51  // NewRemoveRelationCommand returns a command to remove a relation between 2 applications.
    52  func NewRemoveRelationCommand() cmd.Command {
    53  	cmd := &removeRelationCommand{}
    54  	cmd.newAPIFunc = func() (ApplicationDestroyRelationAPI, error) {
    55  		root, err := cmd.NewAPIRoot()
    56  		if err != nil {
    57  			return nil, errors.Trace(err)
    58  		}
    59  		return application.NewClient(root), nil
    60  
    61  	}
    62  	return modelcmd.Wrap(cmd)
    63  }
    64  
    65  // removeRelationCommand causes an existing application relation to be shut down.
    66  type removeRelationCommand struct {
    67  	modelcmd.ModelCommandBase
    68  	RelationId int
    69  	Endpoints  []string
    70  	newAPIFunc func() (ApplicationDestroyRelationAPI, error)
    71  }
    72  
    73  func (c *removeRelationCommand) Info() *cmd.Info {
    74  	return jujucmd.Info(&cmd.Info{
    75  		Name:    "remove-relation",
    76  		Args:    "<application1>[:<relation name1>] <application2>[:<relation name2>] | <relation-id>",
    77  		Purpose: helpSummary,
    78  		Doc:     helpDetails,
    79  	})
    80  }
    81  
    82  func (c *removeRelationCommand) Init(args []string) (err error) {
    83  	if len(args) == 1 {
    84  		if c.RelationId, err = strconv.Atoi(args[0]); err != nil || c.RelationId < 0 {
    85  			return errors.NotValidf("relation ID %q", args[0])
    86  		}
    87  		return nil
    88  	}
    89  	if len(args) != 2 {
    90  		return errors.Errorf("a relation must involve two applications")
    91  	}
    92  	c.Endpoints = args
    93  	return nil
    94  }
    95  
    96  // ApplicationDestroyRelationAPI defines the API methods that application remove relation command uses.
    97  type ApplicationDestroyRelationAPI interface {
    98  	Close() error
    99  	BestAPIVersion() int
   100  	DestroyRelation(endpoints ...string) error
   101  	DestroyRelationId(relationId int) error
   102  }
   103  
   104  func (c *removeRelationCommand) Run(_ *cmd.Context) error {
   105  	client, err := c.newAPIFunc()
   106  	if err != nil {
   107  		return err
   108  	}
   109  	defer client.Close()
   110  	if len(c.Endpoints) == 0 && client.BestAPIVersion() < 5 {
   111  		return errors.New("removing a relation using its ID is not supported by this version of Juju")
   112  	}
   113  	if len(c.Endpoints) > 0 {
   114  		err = client.DestroyRelation(c.Endpoints...)
   115  	} else {
   116  		err = client.DestroyRelationId(c.RelationId)
   117  	}
   118  	return block.ProcessBlockedError(err, block.BlockRemove)
   119  }