launchpad.net/~rogpeppe/juju-core/500-errgo-fix@v0.0.0-20140213181702-000000002356/state/api/provisioner/provisioner.go (about)

     1  // Copyright 2013 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package provisioner
     5  
     6  import (
     7  	"launchpad.net/errgo/errors"
     8  	"launchpad.net/juju-core/state/api/base"
     9  	"launchpad.net/juju-core/state/api/common"
    10  	"launchpad.net/juju-core/state/api/params"
    11  	"launchpad.net/juju-core/state/api/watcher"
    12  	"launchpad.net/juju-core/tools"
    13  )
    14  
    15  const provisioner = "Provisioner"
    16  
    17  // State provides access to the Machiner API facade.
    18  type State struct {
    19  	*common.EnvironWatcher
    20  
    21  	caller base.Caller
    22  }
    23  
    24  // NewState creates a new client-side Machiner facade.
    25  func NewState(caller base.Caller) *State {
    26  	return &State{
    27  		EnvironWatcher: common.NewEnvironWatcher(provisioner, caller),
    28  
    29  		caller: caller}
    30  }
    31  
    32  // machineLife requests the lifecycle of the given machine from the server.
    33  func (st *State) machineLife(tag string) (params.Life, error) {
    34  	return common.Life(st.caller, provisioner, tag)
    35  }
    36  
    37  // Machine provides access to methods of a state.Machine through the facade.
    38  func (st *State) Machine(tag string) (*Machine, error) {
    39  	life, err := st.machineLife(tag)
    40  	if err != nil {
    41  		return nil, base.WrapError(err)
    42  	}
    43  	return &Machine{
    44  		tag:  tag,
    45  		life: life,
    46  		st:   st,
    47  	}, nil
    48  }
    49  
    50  // WatchEnvironMachines returns a StringsWatcher that notifies of
    51  // changes to the lifecycles of the machines (but not containers) in
    52  // the current environment.
    53  func (st *State) WatchEnvironMachines() (watcher.StringsWatcher, error) {
    54  	var result params.StringsWatchResult
    55  	err := st.caller.Call(provisioner, "", "WatchEnvironMachines", nil, &result)
    56  	if err != nil {
    57  		return nil, base.WrapError(err)
    58  	}
    59  	if err := result.Error; err != nil {
    60  		return nil, result.Error
    61  	}
    62  	w := watcher.NewStringsWatcher(st.caller, result)
    63  	return w, nil
    64  }
    65  
    66  // StateAddresses returns the list of addresses used to connect to the state.
    67  func (st *State) StateAddresses() ([]string, error) {
    68  	var result params.StringsResult
    69  	err := st.caller.Call(provisioner, "", "StateAddresses", nil, &result)
    70  	if err != nil {
    71  		return nil, base.WrapError(err)
    72  	}
    73  	return result.Result, nil
    74  }
    75  
    76  // APIAddresses returns the list of addresses used to connect to the API.
    77  func (st *State) APIAddresses() ([]string, error) {
    78  	var result params.StringsResult
    79  	err := st.caller.Call(provisioner, "", "APIAddresses", nil, &result)
    80  	if err != nil {
    81  		return nil, base.WrapError(err)
    82  	}
    83  	return result.Result, nil
    84  }
    85  
    86  // CACert returns the certificate used to validate the state connection.
    87  func (st *State) CACert() ([]byte, error) {
    88  	var result params.BytesResult
    89  	err := st.caller.Call(provisioner, "", "CACert", nil, &result)
    90  	if err != nil {
    91  		return nil, base.WrapError(err)
    92  	}
    93  	return result.Result, nil
    94  }
    95  
    96  // Tools returns the agent tools for the given entity.
    97  func (st *State) Tools(tag string) (*tools.Tools, error) {
    98  	var results params.ToolsResults
    99  	args := params.Entities{
   100  		Entities: []params.Entity{{Tag: tag}},
   101  	}
   102  	err := st.caller.Call(provisioner, "", "Tools", args, &results)
   103  	if err != nil {
   104  		// TODO: Not directly tested
   105  		return nil, base.WrapError(err)
   106  	}
   107  	if len(results.Results) != 1 {
   108  		// TODO: Not directly tested
   109  		return nil, errors.Newf("expected one result, got %d", len(results.Results))
   110  	}
   111  	result := results.Results[0]
   112  	if err := result.Error; err != nil {
   113  		return nil, base.WrapError(err)
   114  	}
   115  	return result.Tools, nil
   116  }
   117  
   118  // ContainerConfig returns information from the environment config that are
   119  // needed for container cloud-init.
   120  func (st *State) ContainerConfig() (result params.ContainerConfig, err error) {
   121  	err = st.caller.Call(provisioner, "", "ContainerConfig", nil, &result)
   122  	return result, base.WrapError(err)
   123  }