github.com/cloud-green/juju@v0.0.0-20151002100041-a00291338d3d/apiserver/client/get.go (about)

     1  // Copyright 2013 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package client
     5  
     6  import (
     7  	"gopkg.in/juju/charm.v6-unstable"
     8  
     9  	"github.com/juju/juju/apiserver/params"
    10  	"github.com/juju/juju/constraints"
    11  )
    12  
    13  // ServiceGet returns the configuration for a service.
    14  func (c *Client) ServiceGet(args params.ServiceGet) (params.ServiceGetResults, error) {
    15  	service, err := c.api.stateAccessor.Service(args.ServiceName)
    16  	if err != nil {
    17  		return params.ServiceGetResults{}, err
    18  	}
    19  	settings, err := service.ConfigSettings()
    20  	if err != nil {
    21  		return params.ServiceGetResults{}, err
    22  	}
    23  	charm, _, err := service.Charm()
    24  	if err != nil {
    25  		return params.ServiceGetResults{}, err
    26  	}
    27  	configInfo := describe(settings, charm.Config())
    28  	var constraints constraints.Value
    29  	if service.IsPrincipal() {
    30  		constraints, err = service.Constraints()
    31  		if err != nil {
    32  			return params.ServiceGetResults{}, err
    33  		}
    34  	}
    35  	return params.ServiceGetResults{
    36  		Service:     args.ServiceName,
    37  		Charm:       charm.Meta().Name,
    38  		Config:      configInfo,
    39  		Constraints: constraints,
    40  	}, nil
    41  }
    42  
    43  func describe(settings charm.Settings, config *charm.Config) map[string]interface{} {
    44  	results := make(map[string]interface{})
    45  	for name, option := range config.Options {
    46  		info := map[string]interface{}{
    47  			"description": option.Description,
    48  			"type":        option.Type,
    49  		}
    50  		if value := settings[name]; value != nil {
    51  			info["value"] = value
    52  		} else {
    53  			if option.Default != nil {
    54  				info["value"] = option.Default
    55  			}
    56  			info["default"] = true
    57  		}
    58  		results[name] = info
    59  	}
    60  	return results
    61  }
    62  
    63  // ServiceGetCharmURL returns the charm URL the given service is
    64  // running at present.
    65  func (c *Client) ServiceGetCharmURL(args params.ServiceGet) (params.StringResult, error) {
    66  	service, err := c.api.stateAccessor.Service(args.ServiceName)
    67  	if err != nil {
    68  		return params.StringResult{}, err
    69  	}
    70  	charmURL, _ := service.CharmURL()
    71  	return params.StringResult{Result: charmURL.String()}, nil
    72  }