github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/cmd/juju/crossmodel/remoteendpoints.go (about)

     1  // Copyright 2015 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package crossmodel
     5  
     6  import (
     7  	"gopkg.in/juju/charm.v6"
     8  
     9  	"github.com/juju/juju/api/applicationoffers"
    10  	"github.com/juju/juju/cmd/modelcmd"
    11  )
    12  
    13  // RemoteEndpointsCommandBase is a base for various cross model commands.
    14  type RemoteEndpointsCommandBase struct {
    15  	modelcmd.ControllerCommandBase
    16  }
    17  
    18  // NewRemoteEndpointsAPI returns a remote endpoints api for the root api endpoint
    19  // that the command returns.
    20  func (c *RemoteEndpointsCommandBase) NewRemoteEndpointsAPI(controllerName string) (*applicationoffers.Client, error) {
    21  	root, err := c.CommandBase.NewAPIRoot(c.ClientStore(), controllerName, "")
    22  	if err != nil {
    23  		return nil, err
    24  	}
    25  	return applicationoffers.NewClient(root), nil
    26  }
    27  
    28  // RemoteEndpoint defines the serialization behaviour of remote endpoints.
    29  // This is used in map-style yaml output where remote endpoint name is the key.
    30  type RemoteEndpoint struct {
    31  	// Name is the endpoint name.
    32  	Name string `yaml:"-" json:"-"`
    33  
    34  	// Interface is relation interface.
    35  	Interface string `yaml:"interface" json:"interface"`
    36  
    37  	// Role is relation role.
    38  	Role string `yaml:"role" json:"role"`
    39  }
    40  
    41  // convertRemoteEndpoints takes any number of api-formatted remote applications' endpoints and
    42  // creates a collection of ui-formatted endpoints.
    43  func convertRemoteEndpoints(apiEndpoints ...charm.Relation) map[string]RemoteEndpoint {
    44  	if len(apiEndpoints) == 0 {
    45  		return nil
    46  	}
    47  	output := make(map[string]RemoteEndpoint, len(apiEndpoints))
    48  	for _, one := range apiEndpoints {
    49  		output[one.Name] = RemoteEndpoint{one.Name, one.Interface, string(one.Role)}
    50  	}
    51  	return output
    52  }