forked from cloudfoundry-attic/bosh-init
-
Notifications
You must be signed in to change notification settings - Fork 0
/
manifest.go
117 lines (99 loc) · 3.06 KB
/
manifest.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
package manifest
import (
bosherr "github.com/cloudfoundry/bosh-agent/errors"
biproperty "github.com/cloudfoundry/bosh-init/common/property"
)
type Manifest struct {
Name string
Properties biproperty.Map
Jobs []Job
Networks []Network
DiskPools []DiskPool
ResourcePools []ResourcePool
Update Update
}
type Update struct {
UpdateWatchTime WatchTime
}
// NetworkInterfaces returns a map of network names to network interfaces.
// We can't use map[string]NetworkInterface, because it's impossible to down-cast to what the cloud client requires.
//TODO: refactor to NetworkInterfaces(Job) and use FindJobByName before using (then remove error)
func (d Manifest) NetworkInterfaces(jobName string) (map[string]biproperty.Map, error) {
job, found := d.FindJobByName(jobName)
if !found {
return map[string]biproperty.Map{}, bosherr.Errorf("Could not find job with name: %s", jobName)
}
networkMap := d.networkMap()
ifaceMap := map[string]biproperty.Map{}
var err error
for _, jobNetwork := range job.Networks {
network := networkMap[jobNetwork.Name]
ifaceMap[jobNetwork.Name], err = network.Interface(jobNetwork.StaticIPs)
if err != nil {
return map[string]biproperty.Map{}, bosherr.WrapError(err, "Building network interface")
}
}
return ifaceMap, nil
}
func (d Manifest) JobName() string {
// Currently we deploy only one job
return d.Jobs[0].Name
}
func (d Manifest) Stemcell(jobName string) (StemcellRef, error) {
resourcePool, err := d.ResourcePool(jobName)
if err != nil {
return StemcellRef{}, err
}
return resourcePool.Stemcell, nil
}
func (d Manifest) ResourcePool(jobName string) (ResourcePool, error) {
job, found := d.FindJobByName(jobName)
if !found {
return ResourcePool{}, bosherr.Errorf("Could not find job with name: %s", jobName)
}
for _, resourcePool := range d.ResourcePools {
if resourcePool.Name == job.ResourcePool {
return resourcePool, nil
}
}
err := bosherr.Errorf("Could not find resource pool '%s' for job '%s'", job.ResourcePool, jobName)
return ResourcePool{}, err
}
func (d Manifest) DiskPool(jobName string) (DiskPool, error) {
job, found := d.FindJobByName(jobName)
if !found {
return DiskPool{}, bosherr.Errorf("Could not find job with name: %s", jobName)
}
if job.PersistentDiskPool != "" {
for _, diskPool := range d.DiskPools {
if diskPool.Name == job.PersistentDiskPool {
return diskPool, nil
}
}
err := bosherr.Errorf("Could not find persistent disk pool '%s' for job '%s'", job.PersistentDiskPool, jobName)
return DiskPool{}, err
}
if job.PersistentDisk > 0 {
diskPool := DiskPool{
DiskSize: job.PersistentDisk,
CloudProperties: biproperty.Map{},
}
return diskPool, nil
}
return DiskPool{}, nil
}
func (d Manifest) networkMap() map[string]Network {
result := map[string]Network{}
for _, network := range d.Networks {
result[network.Name] = network
}
return result
}
func (d Manifest) FindJobByName(jobName string) (Job, bool) {
for _, job := range d.Jobs {
if job.Name == jobName {
return job, true
}
}
return Job{}, false
}