github.com/wallyworld/juju@v0.0.0-20161013125918-6cf1bc9d917a/api/machiner/machine.go (about)

     1  // Copyright 2012, 2013 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package machiner
     5  
     6  import (
     7  	"github.com/juju/errors"
     8  	"gopkg.in/juju/names.v2"
     9  
    10  	"github.com/juju/juju/api/common"
    11  	"github.com/juju/juju/apiserver/params"
    12  	"github.com/juju/juju/network"
    13  	"github.com/juju/juju/status"
    14  	"github.com/juju/juju/watcher"
    15  )
    16  
    17  // Machine represents a juju machine as seen by a machiner worker.
    18  type Machine struct {
    19  	tag  names.MachineTag
    20  	life params.Life
    21  	st   *State
    22  }
    23  
    24  // Tag returns the machine's tag.
    25  func (m *Machine) Tag() names.Tag {
    26  	return m.tag
    27  }
    28  
    29  // Life returns the machine's lifecycle value.
    30  func (m *Machine) Life() params.Life {
    31  	return m.life
    32  }
    33  
    34  // Refresh updates the cached local copy of the machine's data.
    35  func (m *Machine) Refresh() error {
    36  	life, err := m.st.machineLife(m.tag)
    37  	if err != nil {
    38  		return err
    39  	}
    40  	m.life = life
    41  	return nil
    42  }
    43  
    44  // SetStatus sets the status of the machine.
    45  func (m *Machine) SetStatus(status status.Status, info string, data map[string]interface{}) error {
    46  	var result params.ErrorResults
    47  	args := params.SetStatus{
    48  		Entities: []params.EntityStatusArgs{
    49  			{Tag: m.tag.String(), Status: status.String(), Info: info, Data: data},
    50  		},
    51  	}
    52  	err := m.st.facade.FacadeCall("SetStatus", args, &result)
    53  	if err != nil {
    54  		return err
    55  	}
    56  	return result.OneError()
    57  }
    58  
    59  // SetMachineAddresses sets the machine determined addresses of the machine.
    60  func (m *Machine) SetMachineAddresses(addresses []network.Address) error {
    61  	var result params.ErrorResults
    62  	args := params.SetMachinesAddresses{
    63  		MachineAddresses: []params.MachineAddresses{
    64  			{Tag: m.Tag().String(), Addresses: params.FromNetworkAddresses(addresses...)},
    65  		},
    66  	}
    67  	err := m.st.facade.FacadeCall("SetMachineAddresses", args, &result)
    68  	if err != nil {
    69  		return err
    70  	}
    71  	return result.OneError()
    72  }
    73  
    74  // EnsureDead sets the machine lifecycle to Dead if it is Alive or
    75  // Dying. It does nothing otherwise.
    76  func (m *Machine) EnsureDead() error {
    77  	var result params.ErrorResults
    78  	args := params.Entities{
    79  		Entities: []params.Entity{{Tag: m.tag.String()}},
    80  	}
    81  	err := m.st.facade.FacadeCall("EnsureDead", args, &result)
    82  	if err != nil {
    83  		return err
    84  	}
    85  	return result.OneError()
    86  }
    87  
    88  // Watch returns a watcher for observing changes to the machine.
    89  func (m *Machine) Watch() (watcher.NotifyWatcher, error) {
    90  	return common.Watch(m.st.facade, m.tag)
    91  }
    92  
    93  // Jobs returns a list of jobs for the machine.
    94  func (m *Machine) Jobs() (*params.JobsResult, error) {
    95  	var results params.JobsResults
    96  	args := params.Entities{
    97  		Entities: []params.Entity{{Tag: m.Tag().String()}},
    98  	}
    99  	err := m.st.facade.FacadeCall("Jobs", args, &results)
   100  	if err != nil {
   101  		return nil, errors.Annotate(err, "error from FacadeCall")
   102  	}
   103  	if len(results.Results) != 1 {
   104  		return nil, errors.Errorf("expected 1 result, got %d", len(results.Results))
   105  	}
   106  	result := results.Results[0]
   107  	if result.Error != nil {
   108  		return nil, result.Error
   109  	}
   110  	return &result, nil
   111  }
   112  
   113  // SetObservedNetworkConfig sets the machine network config as observed on the
   114  // machine.
   115  func (m *Machine) SetObservedNetworkConfig(netConfig []params.NetworkConfig) error {
   116  	args := params.SetMachineNetworkConfig{
   117  		Tag:    m.Tag().String(),
   118  		Config: netConfig,
   119  	}
   120  	err := m.st.facade.FacadeCall("SetObservedNetworkConfig", args, nil)
   121  	if err != nil {
   122  		return errors.Trace(err)
   123  	}
   124  	return nil
   125  }
   126  
   127  // SetProviderNetworkConfig sets the machine network config as seen by the
   128  // provider.
   129  func (m *Machine) SetProviderNetworkConfig() error {
   130  	var result params.ErrorResults
   131  	args := params.Entities{
   132  		Entities: []params.Entity{{Tag: m.tag.String()}},
   133  	}
   134  	err := m.st.facade.FacadeCall("SetProviderNetworkConfig", args, &result)
   135  	if err != nil {
   136  		return err
   137  	}
   138  	return result.OneError()
   139  }