github.com/makyo/juju@v0.0.0-20160425123129-2608902037e9/cmd/juju/service/removerelation.go (about)

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