github.com/wallyworld/juju@v0.0.0-20161013125918-6cf1bc9d917a/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  	"github.com/juju/cmd"
     8  	"github.com/juju/errors"
     9  
    10  	"github.com/juju/juju/api/application"
    11  	"github.com/juju/juju/cmd/juju/block"
    12  	"github.com/juju/juju/cmd/modelcmd"
    13  )
    14  
    15  var helpSummary = `
    16  Removes an existing relation between two applications.`[1:]
    17  
    18  var helpDetails = `
    19  An existing relation between the two specified applications will be removed. 
    20  This should not result in either of the applications entering an error state,
    21  but may result in either or both of the applications being unable to continue
    22  normal operation. In the case that there is more than one relation between
    23  two applications it is necessary to specify which is to be removed (see
    24  examples). Relations will automatically be removed when using the`[1:] + "`juju\nremove-application`" + ` command.
    25  
    26  Examples:
    27      juju remove-relation mysql wordpress
    28  
    29  In the case of multiple relations, the relation name should be specified
    30  at least once - the following examples will all have the same effect:
    31  
    32      juju remove-relation mediawiki:db mariadb:db
    33      juju remove-relation mediawiki mariadb:db
    34      juju remove-relation mediawiki:db mariadb
    35   
    36  See also: 
    37      add-relation
    38      remove-application`
    39  
    40  // NewRemoveRelationCommand returns a command to remove a relation between 2 services.
    41  func NewRemoveRelationCommand() cmd.Command {
    42  	cmd := &removeRelationCommand{}
    43  	cmd.newAPIFunc = func() (ApplicationDestroyRelationAPI, error) {
    44  		root, err := cmd.NewAPIRoot()
    45  		if err != nil {
    46  			return nil, errors.Trace(err)
    47  		}
    48  		return application.NewClient(root), nil
    49  
    50  	}
    51  	return modelcmd.Wrap(cmd)
    52  }
    53  
    54  // removeRelationCommand causes an existing application relation to be shut down.
    55  type removeRelationCommand struct {
    56  	modelcmd.ModelCommandBase
    57  	Endpoints  []string
    58  	newAPIFunc func() (ApplicationDestroyRelationAPI, error)
    59  }
    60  
    61  func (c *removeRelationCommand) Info() *cmd.Info {
    62  	return &cmd.Info{
    63  		Name:    "remove-relation",
    64  		Args:    "<application1>[:<relation name1>] <application2>[:<relation name2>]",
    65  		Purpose: helpSummary,
    66  		Doc:     helpDetails,
    67  	}
    68  }
    69  
    70  func (c *removeRelationCommand) Init(args []string) error {
    71  	if len(args) != 2 {
    72  		return errors.Errorf("a relation must involve two applications")
    73  	}
    74  	c.Endpoints = args
    75  	return nil
    76  }
    77  
    78  // ApplicationDestroyRelationAPI defines the API methods that application remove relation command uses.
    79  type ApplicationDestroyRelationAPI interface {
    80  	Close() error
    81  	DestroyRelation(endpoints ...string) error
    82  }
    83  
    84  func (c *removeRelationCommand) Run(_ *cmd.Context) error {
    85  	client, err := c.newAPIFunc()
    86  	if err != nil {
    87  		return err
    88  	}
    89  	defer client.Close()
    90  	return block.ProcessBlockedError(client.DestroyRelation(c.Endpoints...), block.BlockRemove)
    91  }