forked from cloudfoundry-attic/bosh-init
-
Notifications
You must be signed in to change notification settings - Fork 0
/
job_evaluation_context.go
103 lines (87 loc) · 3.18 KB
/
job_evaluation_context.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package templatescompiler
import (
"encoding/json"
bosherr "github.com/cloudfoundry/bosh-init/internal/github.com/cloudfoundry/bosh-utils/errors"
boshlog "github.com/cloudfoundry/bosh-init/internal/github.com/cloudfoundry/bosh-utils/logger"
biproperty "github.com/cloudfoundry/bosh-init/internal/github.com/cloudfoundry/bosh-utils/property"
bireljob "github.com/cloudfoundry/bosh-init/release/job"
bierbrenderer "github.com/cloudfoundry/bosh-init/templatescompiler/erbrenderer"
)
type jobEvaluationContext struct {
releaseJob bireljob.Job
jobProperties biproperty.Map
globalProperties biproperty.Map
deploymentName string
logger boshlog.Logger
logTag string
}
// RootContext is exposed as an open struct in ERB templates.
// It must stay same to provide backwards compatible API.
type RootContext struct {
Index int `json:"index"`
JobContext jobContext `json:"job"`
Deployment string `json:"deployment"`
// Usually is accessed with <%= spec.networks.default.ip %>
NetworkContexts map[string]networkContext `json:"networks"`
//TODO: this should be a map[string]interface{}
GlobalProperties biproperty.Map `json:"global_properties"` // values from manifest's top-level properties
ClusterProperties biproperty.Map `json:"cluster_properties"` // values from manifest's jobs[].properties
DefaultProperties biproperty.Map `json:"default_properties"` // values from release's job's spec
}
type jobContext struct {
Name string `json:"name"`
}
type networkContext struct {
IP string `json:"ip"`
Netmask string `json:"netmask"`
Gateway string `json:"gateway"`
}
func NewJobEvaluationContext(
releaseJob bireljob.Job,
jobProperties biproperty.Map,
globalProperties biproperty.Map,
deploymentName string,
logger boshlog.Logger,
) bierbrenderer.TemplateEvaluationContext {
return jobEvaluationContext{
releaseJob: releaseJob,
jobProperties: jobProperties,
globalProperties: globalProperties,
deploymentName: deploymentName,
logger: logger,
logTag: "jobEvaluationContext",
}
}
func (ec jobEvaluationContext) MarshalJSON() ([]byte, error) {
defaultProperties := ec.propertyDefaults(ec.releaseJob.Properties)
context := RootContext{
Index: 0,
JobContext: jobContext{Name: ec.releaseJob.Name},
Deployment: ec.deploymentName,
NetworkContexts: ec.buildNetworkContexts(),
GlobalProperties: ec.globalProperties,
ClusterProperties: ec.jobProperties,
DefaultProperties: defaultProperties,
}
ec.logger.Debug(ec.logTag, "Marshalling context %#v", context)
jsonBytes, err := json.Marshal(context)
if err != nil {
return []byte{}, bosherr.WrapErrorf(err, "Marshalling job eval context: %#v", context)
}
return jsonBytes, nil
}
func (ec jobEvaluationContext) propertyDefaults(properties map[string]bireljob.PropertyDefinition) biproperty.Map {
result := biproperty.Map{}
for propertyKey, property := range properties {
result[propertyKey] = property.Default
}
return result
}
func (ec jobEvaluationContext) buildNetworkContexts() map[string]networkContext {
// IP is being returned by agent
return map[string]networkContext{
"default": networkContext{
IP: "",
},
}
}