github.com/cloud-green/juju@v0.0.0-20151002100041-a00291338d3d/worker/uniter/runner/context/export_test.go (about) 1 // Copyright 2012-2015 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package context 5 6 import ( 7 "github.com/juju/errors" 8 "github.com/juju/names" 9 "github.com/juju/utils/proxy" 10 "gopkg.in/juju/charm.v6-unstable" 11 12 "github.com/juju/juju/api/uniter" 13 "github.com/juju/juju/apiserver/params" 14 "github.com/juju/juju/worker/leadership" 15 "github.com/juju/juju/worker/uniter/runner/jujuc" 16 ) 17 18 var ( 19 ValidatePortRange = validatePortRange 20 TryOpenPorts = tryOpenPorts 21 TryClosePorts = tryClosePorts 22 ) 23 24 func NewHookContext( 25 unit *uniter.Unit, 26 state *uniter.State, 27 id, 28 uuid, 29 envName string, 30 relationId int, 31 remoteUnitName string, 32 relations map[int]*ContextRelation, 33 apiAddrs []string, 34 proxySettings proxy.Settings, 35 canAddMetrics bool, 36 charmMetrics *charm.Metrics, 37 actionData *ActionData, 38 assignedMachineTag names.MachineTag, 39 paths Paths, 40 ) (*HookContext, error) { 41 ctx := &HookContext{ 42 unit: unit, 43 state: state, 44 id: id, 45 uuid: uuid, 46 envName: envName, 47 unitName: unit.Name(), 48 relationId: relationId, 49 remoteUnitName: remoteUnitName, 50 relations: relations, 51 apiAddrs: apiAddrs, 52 proxySettings: proxySettings, 53 actionData: actionData, 54 pendingPorts: make(map[PortRange]PortRangeInfo), 55 assignedMachineTag: assignedMachineTag, 56 } 57 // Get and cache the addresses. 58 var err error 59 ctx.publicAddress, err = unit.PublicAddress() 60 if err != nil && !params.IsCodeNoAddressSet(err) { 61 return nil, err 62 } 63 ctx.privateAddress, err = unit.PrivateAddress() 64 if err != nil && !params.IsCodeNoAddressSet(err) { 65 return nil, err 66 } 67 ctx.availabilityzone, err = unit.AvailabilityZone() 68 if err != nil { 69 return nil, err 70 } 71 ctx.machinePorts, err = state.AllMachinePorts(ctx.assignedMachineTag) 72 if err != nil { 73 return nil, errors.Trace(err) 74 } 75 76 statusCode, statusInfo, err := unit.MeterStatus() 77 if err != nil { 78 return nil, errors.Annotate(err, "could not retrieve meter status for unit") 79 } 80 ctx.meterStatus = &meterStatus{ 81 code: statusCode, 82 info: statusInfo, 83 } 84 return ctx, nil 85 } 86 87 // SetEnvironmentHookContextRelation exists purely to set the fields used in hookVars. 88 // It makes no assumptions about the validity of context. 89 func SetEnvironmentHookContextRelation( 90 context *HookContext, 91 relationId int, endpointName, remoteUnitName string, 92 ) { 93 context.relationId = relationId 94 context.remoteUnitName = remoteUnitName 95 context.relations = map[int]*ContextRelation{ 96 relationId: { 97 endpointName: endpointName, 98 relationId: relationId, 99 }, 100 } 101 } 102 103 func PatchCachedStatus(ctx jujuc.Context, status, info string, data map[string]interface{}) func() { 104 hctx := ctx.(*HookContext) 105 oldStatus := hctx.status 106 hctx.status = &jujuc.StatusInfo{ 107 Status: status, 108 Info: info, 109 Data: data, 110 } 111 return func() { 112 hctx.status = oldStatus 113 } 114 } 115 116 func GetStubActionContext(in map[string]interface{}) *HookContext { 117 return &HookContext{ 118 actionData: &ActionData{ 119 ResultsMap: in, 120 }, 121 } 122 } 123 124 type LeadershipContextFunc func(LeadershipSettingsAccessor, leadership.Tracker) LeadershipContext 125 126 func PatchNewLeadershipContext(f LeadershipContextFunc) func() { 127 var old LeadershipContextFunc 128 old, newLeadershipContext = newLeadershipContext, f 129 return func() { newLeadershipContext = old } 130 } 131 132 func StorageAddConstraints(ctx *HookContext) map[string][]params.StorageConstraints { 133 return ctx.storageAddConstraints 134 } 135 136 // NewEnvironmentHookContext exists purely to set the fields used in rs. 137 // The returned value is not otherwise valid. 138 func NewEnvironmentHookContext( 139 id, envUUID, envName, unitName, meterCode, meterInfo, availZone string, 140 apiAddresses []string, proxySettings proxy.Settings, 141 machineTag names.MachineTag, 142 ) *HookContext { 143 return &HookContext{ 144 id: id, 145 unitName: unitName, 146 uuid: envUUID, 147 envName: envName, 148 apiAddrs: apiAddresses, 149 proxySettings: proxySettings, 150 meterStatus: &meterStatus{ 151 code: meterCode, 152 info: meterInfo, 153 }, 154 relationId: -1, 155 assignedMachineTag: machineTag, 156 availabilityzone: availZone, 157 } 158 } 159 160 func ContextEnvInfo(hctx *HookContext) (name, uuid string) { 161 return hctx.envName, hctx.uuid 162 } 163 164 func ContextMachineTag(hctx *HookContext) names.MachineTag { 165 return hctx.assignedMachineTag 166 } 167 168 func UpdateCachedSettings(cf0 ContextFactory, relId int, unitName string, settings params.Settings) { 169 cf := cf0.(*contextFactory) 170 members := cf.relationCaches[relId].members 171 if members[unitName] == nil { 172 members[unitName] = params.Settings{} 173 } 174 for key, value := range settings { 175 members[unitName][key] = value 176 } 177 } 178 179 func CachedSettings(cf0 ContextFactory, relId int, unitName string) (params.Settings, bool) { 180 cf := cf0.(*contextFactory) 181 settings, found := cf.relationCaches[relId].members[unitName] 182 return settings, found 183 }