github.com/mhilton/juju-juju@v0.0.0-20150901100907-a94dd2c73455/cmd/juju/commands/endpoint.go (about)

     1  // Copyright 2013 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package commands
     5  
     6  import (
     7  	"github.com/juju/cmd"
     8  	"github.com/juju/errors"
     9  	"launchpad.net/gnuflag"
    10  
    11  	"github.com/juju/juju/cmd/envcmd"
    12  )
    13  
    14  // EndpointCommand returns the API endpoints
    15  type EndpointCommand struct {
    16  	envcmd.EnvCommandBase
    17  	out     cmd.Output
    18  	refresh bool
    19  	all     bool
    20  }
    21  
    22  const endpointDoc = `
    23  Returns the address(es) of the current API server formatted as host:port.
    24  
    25  Without arguments apt-endpoints returns the last endpoint used to successfully
    26  connect to the API server. If a cached endpoints information is available from
    27  the current environment's .jenv file, it is returned without trying to connect
    28  to the API server. When no cache is available or --refresh is given, api-endpoints
    29  connects to the API server, retrieves all known endpoints and updates the .jenv
    30  file before returning the first one. Example:
    31  $ juju api-endpoints
    32  10.0.3.1:17070
    33  
    34  If --all is given, api-endpoints returns all known endpoints. Example:
    35  $ juju api-endpoints --all
    36    10.0.3.1:17070
    37    localhost:170170
    38  
    39  The first endpoint is guaranteed to be an IP address and port. If a single endpoint
    40  is available and it's a hostname, juju tries to resolve it locally first.
    41  
    42  Additionally, you can use the --format argument to specify the output format.
    43  Supported formats are: "yaml", "json", or "smart" (default - host:port, one per line).
    44  `
    45  
    46  func (c *EndpointCommand) Info() *cmd.Info {
    47  	return &cmd.Info{
    48  		Name:    "api-endpoints",
    49  		Args:    "",
    50  		Purpose: "print the API server address(es)",
    51  		Doc:     endpointDoc,
    52  	}
    53  }
    54  
    55  func (c *EndpointCommand) SetFlags(f *gnuflag.FlagSet) {
    56  	c.out.AddFlags(f, "smart", cmd.DefaultFormatters)
    57  	f.BoolVar(&c.refresh, "refresh", false, "connect to the API to ensure an up-to-date endpoint location")
    58  	f.BoolVar(&c.all, "all", false, "display all known endpoints, not just the first one")
    59  }
    60  
    61  // Print out the addresses of the API server endpoints.
    62  func (c *EndpointCommand) Run(ctx *cmd.Context) error {
    63  	apiendpoint, err := endpoint(c.EnvCommandBase, c.refresh)
    64  	if err != nil && !errors.IsNotFound(err) {
    65  		return err
    66  	}
    67  	if errors.IsNotFound(err) || len(apiendpoint.Addresses) == 0 {
    68  		return errors.Errorf("no API endpoints available")
    69  	}
    70  	if c.all {
    71  		return c.out.Write(ctx, apiendpoint.Addresses)
    72  	}
    73  	return c.out.Write(ctx, apiendpoint.Addresses[0:1])
    74  }