github.com/cloudbase/juju-core@v0.0.0-20140504232958-a7271ac7912f/state/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  	"launchpad.net/juju-core/charm"
     8  	"launchpad.net/juju-core/constraints"
     9  	"launchpad.net/juju-core/state/api/params"
    10  )
    11  
    12  // ServiceGet returns the configuration for a service.
    13  func (c *Client) ServiceGet(args params.ServiceGet) (params.ServiceGetResults, error) {
    14  	service, err := c.api.state.Service(args.ServiceName)
    15  	if err != nil {
    16  		return params.ServiceGetResults{}, err
    17  	}
    18  	settings, err := service.ConfigSettings()
    19  	if err != nil {
    20  		return params.ServiceGetResults{}, err
    21  	}
    22  	charm, _, err := service.Charm()
    23  	if err != nil {
    24  		return params.ServiceGetResults{}, err
    25  	}
    26  	configInfo := describe(settings, charm.Config())
    27  	var constraints constraints.Value
    28  	if service.IsPrincipal() {
    29  		constraints, err = service.Constraints()
    30  		if err != nil {
    31  			return params.ServiceGetResults{}, err
    32  		}
    33  	}
    34  	return params.ServiceGetResults{
    35  		Service:     args.ServiceName,
    36  		Charm:       charm.Meta().Name,
    37  		Config:      configInfo,
    38  		Constraints: constraints,
    39  	}, nil
    40  }
    41  
    42  func describe(settings charm.Settings, config *charm.Config) map[string]interface{} {
    43  	results := make(map[string]interface{})
    44  	for name, option := range config.Options {
    45  		info := map[string]interface{}{
    46  			"description": option.Description,
    47  			"type":        option.Type,
    48  		}
    49  		if value := settings[name]; value != nil {
    50  			info["value"] = value
    51  		} else {
    52  			if option.Default != nil {
    53  				info["value"] = option.Default
    54  			}
    55  			info["default"] = true
    56  		}
    57  		results[name] = info
    58  	}
    59  	return results
    60  }
    61  
    62  // ServiceGetCharmURL returns the charm URL the given service is
    63  // running at present.
    64  func (c *Client) ServiceGetCharmURL(args params.ServiceGet) (params.StringResult, error) {
    65  	service, err := c.api.state.Service(args.ServiceName)
    66  	if err != nil {
    67  		return params.StringResult{}, err
    68  	}
    69  	charmURL, _ := service.CharmURL()
    70  	return params.StringResult{Result: charmURL.String()}, nil
    71  }