/
boshup.go
134 lines (101 loc) · 3.58 KB
/
boshup.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package boshup
import (
"strings"
"github.com/cloudfoundry/bosh-cli/director/template"
"github.com/cloudfoundry/bosh-utils/errors"
"github.com/cppforlife/go-patch/patch"
"github.com/pivotal-cf/on-demand-services-sdk/bosh"
"github.com/pivotal-cf/on-demand-services-sdk/serviceadapter"
"gopkg.in/yaml.v2"
)
func getOpsFromBytes(opsBytes []byte) (patch.Ops, error) {
var opDefinitions []patch.OpDefinition
err := yaml.Unmarshal(opsBytes, &opDefinitions)
if err != nil {
return nil, errors.WrapError(err, "failed to unmarshal ops")
}
ops, err := patch.NewOpsFromDefinitions(opDefinitions)
if err != nil {
return nil, errors.WrapError(err, "failed to create ops from definitions")
}
return ops, nil
}
func getStaticVariablesFromMap(variables map[string]interface{}) (template.Variables, error) {
staticVariables := template.StaticVariables(variables)
return staticVariables, nil
}
func GetPath(manifestBytes []byte, path string) (string, error) {
tpl := template.NewTemplate(manifestBytes)
pathPointer, err := patch.NewPointerFromString(path)
if err != nil {
return "", errors.WrapError(err, "failed to create pointer from path")
}
evaluated, err := tpl.Evaluate(template.StaticVariables{}, patch.Ops{}, template.EvaluateOpts{
PostVarSubstitutionOp: patch.FindOp{Path: pathPointer},
UnescapedMultiline: true,
})
if err != nil {
return "", errors.WrapError(err, "failed to evaluate get path")
}
trimmedEvaluated := strings.TrimSpace(string(evaluated))
return trimmedEvaluated, nil
}
func SetPath(manifestBytes []byte, path string, value interface{}) ([]byte, error) {
tpl := template.NewTemplate(manifestBytes)
pathPointer, err := patch.NewPointerFromString(path)
if err != nil {
return nil, errors.WrapError(err, "failed to create pointer from path")
}
evaluated, err := tpl.Evaluate(template.StaticVariables{}, patch.Ops{}, template.EvaluateOpts{
PostVarSubstitutionOp: patch.ReplaceOp{
Path: pathPointer,
Value: value,
},
UnescapedMultiline: true,
})
if err != nil {
return nil, errors.WrapError(err, "failed to evaluate get path")
}
return evaluated, nil
}
func Interpolate(manifestBytes []byte, opsBytes []byte, variables map[string]interface{}) ([]byte, error) {
tpl := template.NewTemplate(manifestBytes)
ops, err := getOpsFromBytes(opsBytes)
if err != nil {
return nil, err
}
staticVariables, err := getStaticVariablesFromMap(variables)
if err != nil {
return nil, err
}
evaluated, err := tpl.Evaluate(staticVariables, ops, template.EvaluateOpts{})
if err != nil {
return nil, errors.WrapError(err, "failed to evaluate template")
}
return evaluated, nil
}
func UpdateFromServiceDeployment(manifestBytes []byte, serviceDeployment serviceadapter.ServiceDeployment) ([]byte, error) {
var boshManifest bosh.BoshManifest
err := yaml.Unmarshal(manifestBytes, &boshManifest)
if err != nil {
return nil, errors.WrapError(err, "failed to unmarshal boshManifest")
}
boshManifest.Name = serviceDeployment.DeploymentName
var stemcell bosh.Stemcell
if len(boshManifest.Stemcells) == 1 {
stemcell = boshManifest.Stemcells[0]
}
stemcell.Version = serviceDeployment.Stemcell.Version
stemcell.OS = serviceDeployment.Stemcell.OS
boshManifest.Stemcells = []bosh.Stemcell{stemcell}
var releases []bosh.Release
for _, serviceDeploymentRelease := range serviceDeployment.Releases {
releases = append(releases, bosh.Release{
Name: serviceDeploymentRelease.Name,
Version: serviceDeploymentRelease.Version,
})
}
boshManifest.Releases = releases
updatedManifestBytes, _ := yaml.Marshal(boshManifest)
return updatedManifestBytes, nil
}