github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/cmd/juju/application/expose.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  	jujucmd "github.com/juju/juju/cmd"
    12  	"github.com/juju/juju/cmd/juju/block"
    13  	"github.com/juju/juju/cmd/modelcmd"
    14  )
    15  
    16  var usageExposeSummary = `
    17  Makes an application publicly available over the network.`[1:]
    18  
    19  var usageExposeDetails = `
    20  Adjusts the firewall rules and any relevant security mechanisms of the
    21  cloud to allow public access to the application.
    22  
    23  Examples:
    24      juju expose wordpress
    25  
    26  See also: 
    27      unexpose`[1:]
    28  
    29  // NewExposeCommand returns a command to expose applications.
    30  func NewExposeCommand() modelcmd.ModelCommand {
    31  	return modelcmd.Wrap(&exposeCommand{})
    32  }
    33  
    34  // exposeCommand is responsible exposing applications.
    35  type exposeCommand struct {
    36  	modelcmd.ModelCommandBase
    37  	ApplicationName string
    38  }
    39  
    40  func (c *exposeCommand) Info() *cmd.Info {
    41  	return jujucmd.Info(&cmd.Info{
    42  		Name:    "expose",
    43  		Args:    "<application name>",
    44  		Purpose: usageExposeSummary,
    45  		Doc:     usageExposeDetails,
    46  	})
    47  }
    48  
    49  func (c *exposeCommand) Init(args []string) error {
    50  	if len(args) == 0 {
    51  		return errors.New("no application name specified")
    52  	}
    53  	c.ApplicationName = args[0]
    54  	return cmd.CheckEmpty(args[1:])
    55  }
    56  
    57  type applicationExposeAPI interface {
    58  	Close() error
    59  	Expose(applicationName string) error
    60  	Unexpose(applicationName string) error
    61  }
    62  
    63  func (c *exposeCommand) getAPI() (applicationExposeAPI, error) {
    64  	root, err := c.NewAPIRoot()
    65  	if err != nil {
    66  		return nil, errors.Trace(err)
    67  	}
    68  	return application.NewClient(root), nil
    69  }
    70  
    71  // Run changes the juju-managed firewall to expose any
    72  // ports that were also explicitly marked by units as open.
    73  func (c *exposeCommand) Run(_ *cmd.Context) error {
    74  	client, err := c.getAPI()
    75  	if err != nil {
    76  		return err
    77  	}
    78  	defer client.Close()
    79  	return block.ProcessBlockedError(client.Expose(c.ApplicationName), block.BlockChange)
    80  }