~nskaggs/+junk/xenial-test

« back to all changes in this revision

Viewing changes to src/github.com/juju/juju/worker/uniter/runner/context/export_test.go

  • Committer: Nicholas Skaggs
  • Date: 2016-10-24 20:56:05 UTC
  • Revision ID: nicholas.skaggs@canonical.com-20161024205605-z8lta0uvuhtxwzwl
Initi with beta15

Show diffs side-by-side

added added

removed removed

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