github.com/makyo/juju@v0.0.0-20160425123129-2608902037e9/cmd/juju/service/unexpose.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  	"github.com/juju/cmd"
     8  	"github.com/juju/errors"
     9  
    10  	"github.com/juju/juju/api/service"
    11  	"github.com/juju/juju/cmd/juju/block"
    12  	"github.com/juju/juju/cmd/modelcmd"
    13  )
    14  
    15  var usageUnexposeSummary = `
    16  Removes public availability over the network for a service.`[1:]
    17  
    18  var usageUnexposeDetails = `
    19  Adjusts the firewall rules and any relevant security mechanisms of the
    20  cloud to deny public access to the service.
    21  A service is unexposed by default when it gets created.
    22  
    23  Examples:
    24      juju unexpose wordpress
    25  
    26  See also: 
    27      expose`[1:]
    28  
    29  // NewUnexposeCommand returns a command to unexpose services.
    30  func NewUnexposeCommand() cmd.Command {
    31  	return modelcmd.Wrap(&unexposeCommand{})
    32  }
    33  
    34  // unexposeCommand is responsible exposing services.
    35  type unexposeCommand struct {
    36  	modelcmd.ModelCommandBase
    37  	ServiceName string
    38  }
    39  
    40  func (c *unexposeCommand) Info() *cmd.Info {
    41  	return &cmd.Info{
    42  		Name:    "unexpose",
    43  		Args:    "<service name>",
    44  		Purpose: usageUnexposeSummary,
    45  		Doc:     usageUnexposeDetails,
    46  	}
    47  }
    48  
    49  func (c *unexposeCommand) Init(args []string) error {
    50  	if len(args) == 0 {
    51  		return errors.New("no service name specified")
    52  	}
    53  	c.ServiceName = args[0]
    54  	return cmd.CheckEmpty(args[1:])
    55  }
    56  
    57  func (c *unexposeCommand) getAPI() (serviceExposeAPI, error) {
    58  	root, err := c.NewAPIRoot()
    59  	if err != nil {
    60  		return nil, errors.Trace(err)
    61  	}
    62  	return service.NewClient(root), nil
    63  }
    64  
    65  // Run changes the juju-managed firewall to hide any
    66  // ports that were also explicitly marked by units as closed.
    67  func (c *unexposeCommand) Run(_ *cmd.Context) error {
    68  	client, err := c.getAPI()
    69  	if err != nil {
    70  		return err
    71  	}
    72  	defer client.Close()
    73  	return block.ProcessBlockedError(client.Unexpose(c.ServiceName), block.BlockChange)
    74  }