github.com/makyo/juju@v0.0.0-20160425123129-2608902037e9/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  	"github.com/juju/names"
     9  
    10  	apiwatcher "github.com/juju/juju/api/watcher"
    11  	"github.com/juju/juju/apiserver/params"
    12  	"github.com/juju/juju/instance"
    13  	"github.com/juju/juju/network"
    14  	"github.com/juju/juju/watcher"
    15  )
    16  
    17  // Machine represents a juju machine as seen by the firewaller worker.
    18  type Machine struct {
    19  	st   *State
    20  	tag  names.MachineTag
    21  	life params.Life
    22  }
    23  
    24  // Tag returns the machine tag.
    25  func (m *Machine) Tag() names.MachineTag {
    26  	return m.tag
    27  }
    28  
    29  // WatchUnits starts a StringsWatcher to watch all units assigned to
    30  // the machine.
    31  func (m *Machine) WatchUnits() (watcher.StringsWatcher, error) {
    32  	var results params.StringsWatchResults
    33  	args := params.Entities{
    34  		Entities: []params.Entity{{Tag: m.tag.String()}},
    35  	}
    36  	err := m.st.facade.FacadeCall("WatchUnits", args, &results)
    37  	if err != nil {
    38  		return nil, err
    39  	}
    40  	if len(results.Results) != 1 {
    41  		return nil, fmt.Errorf("expected 1 result, got %d", len(results.Results))
    42  	}
    43  	result := results.Results[0]
    44  	if result.Error != nil {
    45  		return nil, result.Error
    46  	}
    47  	w := apiwatcher.NewStringsWatcher(m.st.facade.RawAPICaller(), result)
    48  	return w, nil
    49  }
    50  
    51  // InstanceId returns the provider specific instance id for this
    52  // machine, or a CodeNotProvisioned error, if not set.
    53  func (m *Machine) InstanceId() (instance.Id, error) {
    54  	var results params.StringResults
    55  	args := params.Entities{
    56  		Entities: []params.Entity{{Tag: m.tag.String()}},
    57  	}
    58  	err := m.st.facade.FacadeCall("InstanceId", args, &results)
    59  	if err != nil {
    60  		return "", err
    61  	}
    62  	if len(results.Results) != 1 {
    63  		return "", fmt.Errorf("expected 1 result, got %d", len(results.Results))
    64  	}
    65  	result := results.Results[0]
    66  	if result.Error != nil {
    67  		return "", result.Error
    68  	}
    69  	return instance.Id(result.Result), nil
    70  }
    71  
    72  // Life returns the machine's life cycle value.
    73  func (m *Machine) Life() params.Life {
    74  	return m.life
    75  }
    76  
    77  // ActiveSubnets returns a list of subnet tags for which the machine has opened
    78  // ports.
    79  func (m *Machine) ActiveSubnets() ([]names.SubnetTag, error) {
    80  	var results params.StringsResults
    81  	args := params.Entities{
    82  		Entities: []params.Entity{{Tag: m.tag.String()}},
    83  	}
    84  	err := m.st.facade.FacadeCall("GetMachineActiveSubnets", args, &results)
    85  	if err != nil {
    86  		return nil, err
    87  	}
    88  	if len(results.Results) != 1 {
    89  		return nil, fmt.Errorf("expected 1 result, got %d", len(results.Results))
    90  	}
    91  	result := results.Results[0]
    92  	if result.Error != nil {
    93  		return nil, result.Error
    94  	}
    95  	// Convert string tags to names.SubnetTag before returning.
    96  	tags := make([]names.SubnetTag, len(result.Result))
    97  	for i, tag := range result.Result {
    98  		var subnetTag names.SubnetTag
    99  		if tag != "" {
   100  			subnetTag, err = names.ParseSubnetTag(tag)
   101  			if err != nil {
   102  				return nil, err
   103  			}
   104  		}
   105  		tags[i] = subnetTag
   106  	}
   107  	return tags, nil
   108  }
   109  
   110  // OpenedPorts returns a map of network.PortRange to unit tag for all opened
   111  // port ranges on the machine for the subnet matching given subnetTag.
   112  func (m *Machine) OpenedPorts(subnetTag names.SubnetTag) (map[network.PortRange]names.UnitTag, error) {
   113  	var results params.MachinePortsResults
   114  	var subnetTagAsString string
   115  	if subnetTag.Id() != "" {
   116  		subnetTagAsString = subnetTag.String()
   117  	}
   118  	args := params.MachinePortsParams{
   119  		Params: []params.MachinePorts{
   120  			{MachineTag: m.tag.String(), SubnetTag: subnetTagAsString},
   121  		},
   122  	}
   123  	err := m.st.facade.FacadeCall("GetMachinePorts", args, &results)
   124  	if err != nil {
   125  		return nil, err
   126  	}
   127  	if len(results.Results) != 1 {
   128  		return nil, fmt.Errorf("expected 1 result, got %d", len(results.Results))
   129  	}
   130  	result := results.Results[0]
   131  	if result.Error != nil {
   132  		return nil, result.Error
   133  	}
   134  	// Convert string tags to names.UnitTag before returning.
   135  	endResult := make(map[network.PortRange]names.UnitTag)
   136  	for _, ports := range result.Ports {
   137  		unitTag, err := names.ParseUnitTag(ports.UnitTag)
   138  		if err != nil {
   139  			return nil, err
   140  		}
   141  		endResult[ports.PortRange.NetworkPortRange()] = unitTag
   142  	}
   143  	return endResult, nil
   144  }