github.com/cloudbase/juju-core@v0.0.0-20140504232958-a7271ac7912f/state/api/firewaller/machine.go (about)

     1  // Licensed under the AGPLv3, see LICENCE file for details.
     2  
     3  package firewaller
     4  
     5  import (
     6  	"fmt"
     7  
     8  	"launchpad.net/juju-core/instance"
     9  	"launchpad.net/juju-core/state/api/params"
    10  	"launchpad.net/juju-core/state/api/watcher"
    11  )
    12  
    13  // Machine represents a juju machine as seen by the firewaller worker.
    14  type Machine struct {
    15  	st   *State
    16  	tag  string
    17  	life params.Life
    18  }
    19  
    20  // WatchUnits starts a StringsWatcher to watch all units assigned to
    21  // the machine.
    22  func (m *Machine) WatchUnits() (watcher.StringsWatcher, error) {
    23  	var results params.StringsWatchResults
    24  	args := params.Entities{
    25  		Entities: []params.Entity{{Tag: m.tag}},
    26  	}
    27  	err := m.st.caller.Call("Firewaller", "", "WatchUnits", args, &results)
    28  	if err != nil {
    29  		return nil, err
    30  	}
    31  	if len(results.Results) != 1 {
    32  		return nil, fmt.Errorf("expected one result, got %d", len(results.Results))
    33  	}
    34  	result := results.Results[0]
    35  	if result.Error != nil {
    36  		return nil, result.Error
    37  	}
    38  	w := watcher.NewStringsWatcher(m.st.caller, result)
    39  	return w, nil
    40  }
    41  
    42  // InstanceId returns the provider specific instance id for this
    43  // machine, or a CodeNotProvisioned error, if not set.
    44  func (m *Machine) InstanceId() (instance.Id, error) {
    45  	var results params.StringResults
    46  	args := params.Entities{
    47  		Entities: []params.Entity{{Tag: m.tag}},
    48  	}
    49  	err := m.st.caller.Call("Firewaller", "", "InstanceId", args, &results)
    50  	if err != nil {
    51  		return "", err
    52  	}
    53  	if len(results.Results) != 1 {
    54  		return "", fmt.Errorf("expected one result, got %d", len(results.Results))
    55  	}
    56  	result := results.Results[0]
    57  	if result.Error != nil {
    58  		return "", result.Error
    59  	}
    60  	return instance.Id(result.Result), nil
    61  }
    62  
    63  // Life returns the machine's life cycle value.
    64  func (m *Machine) Life() params.Life {
    65  	return m.life
    66  }