github.com/cloudbase/juju-core@v0.0.0-20140504232958-a7271ac7912f/state/api/firewaller/unit.go (about) 1 // Copyright 2013 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package firewaller 5 6 import ( 7 "fmt" 8 9 "launchpad.net/juju-core/instance" 10 "launchpad.net/juju-core/names" 11 "launchpad.net/juju-core/state/api/params" 12 "launchpad.net/juju-core/state/api/watcher" 13 ) 14 15 // Unit represents a juju unit as seen by a firewaller worker. 16 type Unit struct { 17 st *State 18 tag string 19 life params.Life 20 } 21 22 // Name returns the name of the unit. 23 func (u *Unit) Name() string { 24 _, unitName, err := names.ParseTag(u.tag, names.UnitTagKind) 25 if err != nil { 26 panic(fmt.Sprintf("%q is not a valid unit tag", u.tag)) 27 } 28 return unitName 29 } 30 31 // Life returns the unit's life cycle value. 32 func (u *Unit) Life() params.Life { 33 return u.life 34 } 35 36 // Refresh updates the cached local copy of the unit's data. 37 func (u *Unit) Refresh() error { 38 life, err := u.st.life(u.tag) 39 if err != nil { 40 return err 41 } 42 u.life = life 43 return nil 44 } 45 46 // Watch returns a watcher for observing changes to the unit. 47 func (u *Unit) Watch() (watcher.NotifyWatcher, error) { 48 var results params.NotifyWatchResults 49 args := params.Entities{ 50 Entities: []params.Entity{{Tag: u.tag}}, 51 } 52 err := u.st.caller.Call("Firewaller", "", "Watch", args, &results) 53 if err != nil { 54 return nil, err 55 } 56 if len(results.Results) != 1 { 57 return nil, fmt.Errorf("expected one result, got %d", len(results.Results)) 58 } 59 result := results.Results[0] 60 if result.Error != nil { 61 return nil, result.Error 62 } 63 w := watcher.NewNotifyWatcher(u.st.caller, result) 64 return w, nil 65 } 66 67 // Service returns the service. 68 func (u *Unit) Service() (*Service, error) { 69 serviceTag := names.ServiceTag(names.UnitService(u.Name())) 70 service := &Service{ 71 st: u.st, 72 tag: serviceTag, 73 } 74 // Call Refresh() immediately to get the up-to-date 75 // life and other needed locally cached fields. 76 err := service.Refresh() 77 if err != nil { 78 return nil, err 79 } 80 return service, nil 81 } 82 83 // OpenedPorts returns the list of opened ports for this unit. 84 // 85 // NOTE: This differs from state.Unit.OpenedPorts() by returning 86 // an error as well, because it needs to make an API call. 87 func (u *Unit) OpenedPorts() ([]instance.Port, error) { 88 var results params.PortsResults 89 args := params.Entities{ 90 Entities: []params.Entity{{Tag: u.tag}}, 91 } 92 err := u.st.caller.Call("Firewaller", "", "OpenedPorts", args, &results) 93 if err != nil { 94 return nil, err 95 } 96 if len(results.Results) != 1 { 97 return nil, fmt.Errorf("expected one result, got %d", len(results.Results)) 98 } 99 result := results.Results[0] 100 if result.Error != nil { 101 return nil, result.Error 102 } 103 return result.Ports, nil 104 } 105 106 // AssignedMachine returns the tag of this unit's assigned machine (if 107 // any), or a CodeNotAssigned error. 108 func (u *Unit) AssignedMachine() (string, error) { 109 var results params.StringResults 110 args := params.Entities{ 111 Entities: []params.Entity{{Tag: u.tag}}, 112 } 113 err := u.st.caller.Call("Firewaller", "", "GetAssignedMachine", args, &results) 114 if err != nil { 115 return "", err 116 } 117 if len(results.Results) != 1 { 118 return "", fmt.Errorf("expected one result, got %d", len(results.Results)) 119 } 120 result := results.Results[0] 121 if result.Error != nil { 122 return "", result.Error 123 } 124 return result.Result, nil 125 }