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