github.com/axw/juju@v0.0.0-20161005053422-4bd6544d08d4/api/modelconfig/modelconfig.go (about)

     1  // Copyright 2016 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package modelconfig
     5  
     6  import (
     7  	"github.com/juju/errors"
     8  
     9  	"github.com/juju/juju/api/base"
    10  	"github.com/juju/juju/apiserver/params"
    11  	"github.com/juju/juju/environs/config"
    12  )
    13  
    14  // Client provides methods that the Juju client command uses to interact
    15  // with models stored in the Juju Server.
    16  type Client struct {
    17  	base.ClientFacade
    18  	facade base.FacadeCaller
    19  }
    20  
    21  // NewClient creates a new `Client` based on an existing authenticated API
    22  // connection.
    23  func NewClient(st base.APICallCloser) *Client {
    24  	frontend, backend := base.NewClientFacade(st, "ModelConfig")
    25  	return &Client{ClientFacade: frontend, facade: backend}
    26  }
    27  
    28  // Close closes the api connection.
    29  func (c *Client) Close() error {
    30  	return c.ClientFacade.Close()
    31  }
    32  
    33  // ModelGet returns all model settings.
    34  func (c *Client) ModelGet() (map[string]interface{}, error) {
    35  	result := params.ModelConfigResults{}
    36  	err := c.facade.FacadeCall("ModelGet", nil, &result)
    37  	if err != nil {
    38  		return nil, errors.Trace(err)
    39  	}
    40  	values := make(map[string]interface{})
    41  	for name, val := range result.Config {
    42  		values[name] = val.Value
    43  	}
    44  	return values, nil
    45  }
    46  
    47  // ModelGetWithMetadata returns all model settings along with extra
    48  // metadata like the source of the setting value.
    49  func (c *Client) ModelGetWithMetadata() (config.ConfigValues, error) {
    50  	result := params.ModelConfigResults{}
    51  	err := c.facade.FacadeCall("ModelGet", nil, &result)
    52  	if err != nil {
    53  		return nil, errors.Trace(err)
    54  	}
    55  	values := make(config.ConfigValues)
    56  	for name, val := range result.Config {
    57  		values[name] = config.ConfigValue{
    58  			Value:  val.Value,
    59  			Source: val.Source,
    60  		}
    61  	}
    62  	return values, nil
    63  }
    64  
    65  // ModelSet sets the given key-value pairs in the model.
    66  func (c *Client) ModelSet(config map[string]interface{}) error {
    67  	args := params.ModelSet{Config: config}
    68  	return c.facade.FacadeCall("ModelSet", args, nil)
    69  }
    70  
    71  // ModelUnset sets the given key-value pairs in the model.
    72  func (c *Client) ModelUnset(keys ...string) error {
    73  	args := params.ModelUnset{Keys: keys}
    74  	return c.facade.FacadeCall("ModelUnset", args, nil)
    75  }