github.com/Pankov404/juju@v0.0.0-20150703034450-be266991dceb/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  
    13  	"github.com/juju/juju/api"
    14  	"github.com/juju/juju/api/base"
    15  	"github.com/juju/juju/apiserver/params"
    16  	"github.com/juju/juju/constraints"
    17  	"github.com/juju/juju/storage"
    18  )
    19  
    20  // Client allows access to the service API end point.
    21  type Client struct {
    22  	base.ClientFacade
    23  	st     *api.State
    24  	facade base.FacadeCaller
    25  }
    26  
    27  // NewClient creates a new client for accessing the service api.
    28  func NewClient(st *api.State) *Client {
    29  	frontend, backend := base.NewClientFacade(st, "Service")
    30  	return &Client{ClientFacade: frontend, st: st, facade: backend}
    31  }
    32  
    33  // SetMetricCredentials sets the metric credentials for the service specified.
    34  func (c *Client) SetMetricCredentials(service string, credentials []byte) error {
    35  	creds := []params.ServiceMetricCredential{
    36  		{service, credentials},
    37  	}
    38  	p := params.ServiceMetricCredentials{creds}
    39  	results := new(params.ErrorResults)
    40  	err := c.facade.FacadeCall("SetMetricCredentials", p, results)
    41  	if err != nil {
    42  		return errors.Trace(err)
    43  	}
    44  	return errors.Trace(results.OneError())
    45  }
    46  
    47  // ServiceDeploy obtains the charm, either locally or from
    48  // the charm store, and deploys it. It allows the specification of
    49  // requested networks that must be present on the machines where the
    50  // service is deployed. Another way to specify networks to include/exclude
    51  // is using constraints.
    52  func (c *Client) ServiceDeploy(
    53  	charmURL string,
    54  	serviceName string,
    55  	numUnits int,
    56  	configYAML string,
    57  	cons constraints.Value,
    58  	toMachineSpec string,
    59  	networks []string,
    60  	storage map[string]storage.Constraints,
    61  ) error {
    62  	args := params.ServicesDeploy{
    63  		Services: []params.ServiceDeploy{{
    64  			ServiceName:   serviceName,
    65  			CharmUrl:      charmURL,
    66  			NumUnits:      numUnits,
    67  			ConfigYAML:    configYAML,
    68  			Constraints:   cons,
    69  			ToMachineSpec: toMachineSpec,
    70  			Networks:      networks,
    71  			Storage:       storage,
    72  		}},
    73  	}
    74  	var results params.ErrorResults
    75  	err := c.facade.FacadeCall("ServicesDeploy", args, &results)
    76  	if err != nil {
    77  		return err
    78  	}
    79  	return results.OneError()
    80  }