github.com/cloudbase/juju-core@v0.0.0-20140504232958-a7271ac7912f/state/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  	"fmt"
     8  
     9  	"launchpad.net/juju-core/instance"
    10  	"launchpad.net/juju-core/state/api/params"
    11  	"launchpad.net/juju-core/state/api/watcher"
    12  )
    13  
    14  // Machine represents a juju machine as seen by a machiner worker.
    15  type Machine struct {
    16  	tag  string
    17  	life params.Life
    18  	st   *State
    19  }
    20  
    21  // Tag returns the machine's tag.
    22  func (m *Machine) Tag() string {
    23  	return m.tag
    24  }
    25  
    26  // Life returns the machine's lifecycle value.
    27  func (m *Machine) Life() params.Life {
    28  	return m.life
    29  }
    30  
    31  // Refresh updates the cached local copy of the machine's data.
    32  func (m *Machine) Refresh() error {
    33  	life, err := m.st.machineLife(m.tag)
    34  	if err != nil {
    35  		return err
    36  	}
    37  	m.life = life
    38  	return nil
    39  }
    40  
    41  // SetStatus sets the status of the machine.
    42  func (m *Machine) SetStatus(status params.Status, info string, data params.StatusData) error {
    43  	var result params.ErrorResults
    44  	args := params.SetStatus{
    45  		Entities: []params.SetEntityStatus{
    46  			{Tag: m.tag, Status: status, Info: info, Data: data},
    47  		},
    48  	}
    49  	err := m.st.caller.Call("Machiner", "", "SetStatus", args, &result)
    50  	if err != nil {
    51  		return err
    52  	}
    53  	return result.OneError()
    54  }
    55  
    56  // SetMachineAddresses sets the machine determined addresses of the machine.
    57  func (m *Machine) SetMachineAddresses(addresses []instance.Address) error {
    58  	var result params.ErrorResults
    59  	args := params.SetMachinesAddresses{
    60  		MachineAddresses: []params.MachineAddresses{
    61  			{Tag: m.Tag(), Addresses: addresses},
    62  		},
    63  	}
    64  	err := m.st.caller.Call("Machiner", "", "SetMachineAddresses", args, &result)
    65  	if err != nil {
    66  		return err
    67  	}
    68  	return result.OneError()
    69  }
    70  
    71  // EnsureDead sets the machine lifecycle to Dead if it is Alive or
    72  // Dying. It does nothing otherwise.
    73  func (m *Machine) EnsureDead() error {
    74  	var result params.ErrorResults
    75  	args := params.Entities{
    76  		Entities: []params.Entity{{Tag: m.tag}},
    77  	}
    78  	err := m.st.caller.Call("Machiner", "", "EnsureDead", args, &result)
    79  	if err != nil {
    80  		return err
    81  	}
    82  	return result.OneError()
    83  }
    84  
    85  // Watch returns a watcher for observing changes to the machine.
    86  func (m *Machine) Watch() (watcher.NotifyWatcher, error) {
    87  	var results params.NotifyWatchResults
    88  	args := params.Entities{
    89  		Entities: []params.Entity{{Tag: m.tag}},
    90  	}
    91  	err := m.st.caller.Call("Machiner", "", "Watch", args, &results)
    92  	if err != nil {
    93  		return nil, err
    94  	}
    95  	if len(results.Results) != 1 {
    96  		return nil, fmt.Errorf("expected one result, got %d", len(results.Results))
    97  	}
    98  	result := results.Results[0]
    99  	if result.Error != nil {
   100  		return nil, result.Error
   101  	}
   102  	w := watcher.NewNotifyWatcher(m.st.caller, result)
   103  	return w, nil
   104  }