github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/cmd/juju/application/resumerelation.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/juju/api/application"
    14  	jujucmd "github.com/juju/juju/cmd"
    15  	"github.com/juju/juju/cmd/juju/block"
    16  	"github.com/juju/juju/cmd/modelcmd"
    17  )
    18  
    19  var resumeHelpSummary = `
    20  Resumes a suspended relation to an application offer.`[1:]
    21  
    22  var resumeHelpDetails = `
    23  A relation between an application in another model and an offer in this model will be resumed. 
    24  The relation-joined and relation-changed hooks will be run for the relation, and the relation
    25  status will be set to joined. The relation is specified using its id.
    26  
    27  Examples:
    28      juju resume-relation 123
    29      juju resume-relation 123 456
    30  
    31  See also: 
    32      add-relation
    33      offers
    34      remove-relation
    35      suspend-relation`
    36  
    37  // NewResumeRelationCommand returns a command to resume a relation.
    38  func NewResumeRelationCommand() cmd.Command {
    39  	cmd := &resumeRelationCommand{}
    40  	cmd.newAPIFunc = func() (SetRelationSuspendedAPI, error) {
    41  		root, err := cmd.NewAPIRoot()
    42  		if err != nil {
    43  			return nil, errors.Trace(err)
    44  		}
    45  		return application.NewClient(root), nil
    46  
    47  	}
    48  	return modelcmd.Wrap(cmd)
    49  }
    50  
    51  type resumeRelationCommand struct {
    52  	modelcmd.ModelCommandBase
    53  	relationIds []int
    54  	newAPIFunc  func() (SetRelationSuspendedAPI, error)
    55  }
    56  
    57  func (c *resumeRelationCommand) Info() *cmd.Info {
    58  	return jujucmd.Info(&cmd.Info{
    59  		Name:    "resume-relation",
    60  		Args:    "<relation-id>[,<relation-id>]",
    61  		Purpose: resumeHelpSummary,
    62  		Doc:     resumeHelpDetails,
    63  	})
    64  }
    65  
    66  func (c *resumeRelationCommand) Init(args []string) (err error) {
    67  	if len(args) == 0 {
    68  		return errors.New("no relation ids specified")
    69  	}
    70  	for _, id := range args {
    71  		if relId, err := strconv.Atoi(strings.TrimSpace(id)); err != nil || relId < 0 {
    72  			return errors.NotValidf("relation ID %q", id)
    73  		} else {
    74  			c.relationIds = append(c.relationIds, relId)
    75  		}
    76  	}
    77  	return nil
    78  }
    79  
    80  func (c *resumeRelationCommand) Run(_ *cmd.Context) error {
    81  	client, err := c.newAPIFunc()
    82  	if err != nil {
    83  		return err
    84  	}
    85  	defer client.Close()
    86  	if client.BestAPIVersion() < 5 {
    87  		return errors.New("resuming a relation is not supported by this version of Juju")
    88  	}
    89  	err = client.SetRelationSuspended(c.relationIds, false, "")
    90  	return block.ProcessBlockedError(err, block.BlockChange)
    91  }