github.com/mhilton/juju-juju@v0.0.0-20150901100907-a94dd2c73455/api/service/client.go (about)

     1  // Copyright 2014 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  // Package service provides access to the service api facade.
     5  // This facade contains api calls that are specific to services.
     6  // As a rule of thumb, if the argument for an api requries a service name
     7  // and affects only that service then the call belongs here.
     8  package service
     9  
    10  import (
    11  	"github.com/juju/errors"
    12  	"github.com/juju/loggo"
    13  
    14  	"github.com/juju/juju/api"
    15  	"github.com/juju/juju/api/base"
    16  	"github.com/juju/juju/apiserver/params"
    17  	"github.com/juju/juju/constraints"
    18  	"github.com/juju/juju/instance"
    19  	"github.com/juju/juju/storage"
    20  )
    21  
    22  var logger = loggo.GetLogger("juju.api.service")
    23  
    24  // Client allows access to the service API end point.
    25  type Client struct {
    26  	base.ClientFacade
    27  	st     api.Connection
    28  	facade base.FacadeCaller
    29  }
    30  
    31  // NewClient creates a new client for accessing the service api.
    32  func NewClient(st api.Connection) *Client {
    33  	frontend, backend := base.NewClientFacade(st, "Service")
    34  	return &Client{ClientFacade: frontend, st: st, facade: backend}
    35  }
    36  
    37  // SetMetricCredentials sets the metric credentials for the service specified.
    38  func (c *Client) SetMetricCredentials(service string, credentials []byte) error {
    39  	creds := []params.ServiceMetricCredential{
    40  		{service, credentials},
    41  	}
    42  	p := params.ServiceMetricCredentials{creds}
    43  	results := new(params.ErrorResults)
    44  	err := c.facade.FacadeCall("SetMetricCredentials", p, results)
    45  	if err != nil {
    46  		return errors.Trace(err)
    47  	}
    48  	return errors.Trace(results.OneError())
    49  }
    50  
    51  // EnvironmentUUID returns the environment UUID from the client connection.
    52  func (c *Client) EnvironmentUUID() string {
    53  	tag, err := c.st.EnvironTag()
    54  	if err != nil {
    55  		logger.Warningf("environ tag not an environ: %v", err)
    56  		return ""
    57  	}
    58  	return tag.Id()
    59  }
    60  
    61  // ServiceDeploy obtains the charm, either locally or from
    62  // the charm store, and deploys it. It allows the specification of
    63  // requested networks that must be present on the machines where the
    64  // service is deployed. Another way to specify networks to include/exclude
    65  // is using constraints. Placement directives, if provided, specify the
    66  // machine on which the charm is deployed.
    67  func (c *Client) ServiceDeploy(
    68  	charmURL string,
    69  	serviceName string,
    70  	numUnits int,
    71  	configYAML string,
    72  	cons constraints.Value,
    73  	toMachineSpec string,
    74  	placement []*instance.Placement,
    75  	networks []string,
    76  	storage map[string]storage.Constraints,
    77  ) error {
    78  	args := params.ServicesDeploy{
    79  		Services: []params.ServiceDeploy{{
    80  			ServiceName:   serviceName,
    81  			CharmUrl:      charmURL,
    82  			NumUnits:      numUnits,
    83  			ConfigYAML:    configYAML,
    84  			Constraints:   cons,
    85  			ToMachineSpec: toMachineSpec,
    86  			Placement:     placement,
    87  			Networks:      networks,
    88  			Storage:       storage,
    89  		}},
    90  	}
    91  	var results params.ErrorResults
    92  	var err error
    93  	if len(placement) > 0 {
    94  		err = c.facade.FacadeCall("ServicesDeployWithPlacement", args, &results)
    95  		if err != nil {
    96  			if params.IsCodeNotImplemented(err) {
    97  				return errors.Errorf("unsupported --to parameter %q", toMachineSpec)
    98  			}
    99  			return err
   100  		}
   101  	} else {
   102  		err = c.facade.FacadeCall("ServicesDeploy", args, &results)
   103  	}
   104  	if err != nil {
   105  		return err
   106  	}
   107  	return results.OneError()
   108  }