github.com/altoros/juju-vmware@v0.0.0-20150312064031-f19ae857ccca/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  	"github.com/juju/names"
     8  
     9  	"github.com/juju/juju/api/base"
    10  	"github.com/juju/juju/api/common"
    11  	"github.com/juju/juju/api/watcher"
    12  	"github.com/juju/juju/apiserver/params"
    13  	"github.com/juju/juju/tools"
    14  	"github.com/juju/juju/version"
    15  )
    16  
    17  // State provides access to the Machiner API facade.
    18  type State struct {
    19  	*common.EnvironWatcher
    20  	*common.APIAddresser
    21  
    22  	facade base.FacadeCaller
    23  }
    24  
    25  const provisionerFacade = "Provisioner"
    26  
    27  // NewState creates a new client-side Machiner facade.
    28  func NewState(caller base.APICaller) *State {
    29  	facadeCaller := base.NewFacadeCaller(caller, provisionerFacade)
    30  	return &State{
    31  		EnvironWatcher: common.NewEnvironWatcher(facadeCaller),
    32  		APIAddresser:   common.NewAPIAddresser(facadeCaller),
    33  		facade:         facadeCaller}
    34  }
    35  
    36  // machineLife requests the lifecycle of the given machine from the server.
    37  func (st *State) machineLife(tag names.MachineTag) (params.Life, error) {
    38  	return common.Life(st.facade, tag)
    39  }
    40  
    41  // Machine provides access to methods of a state.Machine through the facade.
    42  func (st *State) Machine(tag names.MachineTag) (*Machine, error) {
    43  	life, err := st.machineLife(tag)
    44  	if err != nil {
    45  		return nil, err
    46  	}
    47  	return &Machine{
    48  		tag:  tag,
    49  		life: life,
    50  		st:   st,
    51  	}, nil
    52  }
    53  
    54  // WatchEnvironMachines returns a StringsWatcher that notifies of
    55  // changes to the lifecycles of the machines (but not containers) in
    56  // the current environment.
    57  func (st *State) WatchEnvironMachines() (watcher.StringsWatcher, error) {
    58  	var result params.StringsWatchResult
    59  	err := st.facade.FacadeCall("WatchEnvironMachines", nil, &result)
    60  	if err != nil {
    61  		return nil, err
    62  	}
    63  	if err := result.Error; err != nil {
    64  		return nil, result.Error
    65  	}
    66  	w := watcher.NewStringsWatcher(st.facade.RawAPICaller(), result)
    67  	return w, nil
    68  }
    69  
    70  func (st *State) WatchMachineErrorRetry() (watcher.NotifyWatcher, error) {
    71  	var result params.NotifyWatchResult
    72  	err := st.facade.FacadeCall("WatchMachineErrorRetry", nil, &result)
    73  	if err != nil {
    74  		return nil, err
    75  	}
    76  	if err := result.Error; err != nil {
    77  		return nil, result.Error
    78  	}
    79  	w := watcher.NewNotifyWatcher(st.facade.RawAPICaller(), result)
    80  	return w, nil
    81  }
    82  
    83  // StateAddresses returns the list of addresses used to connect to the state.
    84  func (st *State) StateAddresses() ([]string, error) {
    85  	var result params.StringsResult
    86  	err := st.facade.FacadeCall("StateAddresses", nil, &result)
    87  	if err != nil {
    88  		return nil, err
    89  	}
    90  	return result.Result, nil
    91  }
    92  
    93  // ContainerManagerConfig returns information from the environment config that is
    94  // needed for configuring the container manager.
    95  func (st *State) ContainerManagerConfig(args params.ContainerManagerConfigParams) (result params.ContainerManagerConfig, err error) {
    96  	err = st.facade.FacadeCall("ContainerManagerConfig", args, &result)
    97  	return result, err
    98  }
    99  
   100  // ContainerConfig returns information from the environment config that is
   101  // needed for container cloud-init.
   102  func (st *State) ContainerConfig() (result params.ContainerConfig, err error) {
   103  	err = st.facade.FacadeCall("ContainerConfig", nil, &result)
   104  	return result, err
   105  }
   106  
   107  // MachinesWithTransientErrors returns a slice of machines and corresponding status information
   108  // for those machines which have transient provisioning errors.
   109  func (st *State) MachinesWithTransientErrors() ([]*Machine, []params.StatusResult, error) {
   110  	var results params.StatusResults
   111  	err := st.facade.FacadeCall("MachinesWithTransientErrors", nil, &results)
   112  	if err != nil {
   113  		return nil, nil, err
   114  	}
   115  	machines := make([]*Machine, len(results.Results))
   116  	for i, status := range results.Results {
   117  		if status.Error != nil {
   118  			continue
   119  		}
   120  		machines[i] = &Machine{
   121  			tag:  names.NewMachineTag(status.Id),
   122  			life: status.Life,
   123  			st:   st,
   124  		}
   125  	}
   126  	return machines, results.Results, nil
   127  }
   128  
   129  // FindTools returns al ist of tools matching the specified version number and
   130  // series, and, if non-empty, arch.
   131  func (st *State) FindTools(v version.Number, series string, arch *string) (tools.List, error) {
   132  	args := params.FindToolsParams{
   133  		Number:       v,
   134  		Series:       series,
   135  		MajorVersion: -1,
   136  		MinorVersion: -1,
   137  	}
   138  	if arch != nil {
   139  		args.Arch = *arch
   140  	}
   141  	var result params.FindToolsResult
   142  	if err := st.facade.FacadeCall("FindTools", args, &result); err != nil {
   143  		return nil, err
   144  	}
   145  	if result.Error != nil {
   146  		return nil, result.Error
   147  	}
   148  	return result.List, nil
   149  }