github.com/altoros/juju-vmware@v0.0.0-20150312064031-f19ae857ccca/apiserver/service/service.go (about)

     1  // Copyright 2014 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  // Package service contains api calls for accessing service functionality.
     5  package service
     6  
     7  import (
     8  	"github.com/juju/loggo"
     9  
    10  	"github.com/juju/juju/apiserver/common"
    11  	"github.com/juju/juju/apiserver/params"
    12  	"github.com/juju/juju/state"
    13  )
    14  
    15  var logger = loggo.GetLogger("juju.apiserver.service")
    16  
    17  func init() {
    18  	common.RegisterStandardFacade("Service", 1, NewAPI)
    19  }
    20  
    21  // Service defines the methods on the service API end point.
    22  type Service interface {
    23  	SetMetricCredentials(args params.ServiceMetricCredentials) (params.ErrorResults, error)
    24  }
    25  
    26  // API implements the service interface and is the concrete
    27  // implementation of the api end point.
    28  type API struct {
    29  	state      *state.State
    30  	authorizer common.Authorizer
    31  }
    32  
    33  // NewAPI returns a new service API facade.
    34  func NewAPI(
    35  	st *state.State,
    36  	resources *common.Resources,
    37  	authorizer common.Authorizer,
    38  ) (*API, error) {
    39  	if !authorizer.AuthClient() {
    40  		return nil, common.ErrPerm
    41  	}
    42  
    43  	return &API{
    44  		state:      st,
    45  		authorizer: authorizer,
    46  	}, nil
    47  }
    48  
    49  // SetMetricCredentials sets credentials on the service.
    50  func (api *API) SetMetricCredentials(args params.ServiceMetricCredentials) (params.ErrorResults, error) {
    51  	result := params.ErrorResults{
    52  		Results: make([]params.ErrorResult, len(args.Creds)),
    53  	}
    54  	if len(args.Creds) == 0 {
    55  		return result, nil
    56  	}
    57  	for i, a := range args.Creds {
    58  		service, err := api.state.Service(a.ServiceName)
    59  		if err != nil {
    60  			result.Results[i].Error = common.ServerError(err)
    61  			continue
    62  		}
    63  		err = service.SetMetricCredentials(a.MetricCredentials)
    64  		if err != nil {
    65  			result.Results[i].Error = common.ServerError(err)
    66  		}
    67  	}
    68  	return result, nil
    69  }