This repository has been archived by the owner on Oct 8, 2021. It is now read-only.
forked from cloudfoundry/bosh-cpi-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
agent_env.go
126 lines (90 loc) · 2.49 KB
/
agent_env.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
package apiv1
import (
"encoding/json"
bosherr "github.com/cloudfoundry/bosh-utils/errors"
)
type AgentEnv interface {
AttachSystemDisk(DiskHint)
AttachEphemeralDisk(DiskHint)
AttachPersistentDisk(DiskCID, DiskHint)
DetachPersistentDisk(DiskCID)
AsBytes() ([]byte, error)
_final() // interface unimplementable from outside
}
type AgentEnvImpl struct {
spec agentEnvSpec
}
var _ AgentEnv = &AgentEnvImpl{}
type agentEnvSpec struct {
AgentID string `json:"agent_id"`
VM VMSpec `json:"vm"`
Mbus string `json:"mbus"`
NTP []string `json:"ntp"`
Blobstore BlobstoreSpec `json:"blobstore"`
Networks NetworksSpec `json:"networks"`
Disks DisksSpec `json:"disks"`
Env EnvSpec `json:"env"`
}
type VMSpec struct {
Name string `json:"name"`
ID string `json:"id"`
}
type NetworksSpec map[string]NetworkSpec
type NetworkSpec struct {
Type string `json:"type"`
IP string `json:"ip"`
Netmask string `json:"netmask"`
Gateway string `json:"gateway"`
DNS []string `json:"dns"`
Default []string `json:"default"`
Routes []Route `json:"routes"`
Alias string `json:"alias,omitempty"`
MAC string `json:"mac"`
Preconfigured bool `json:"preconfigured"`
}
type DisksSpec struct {
System DiskHint `json:"system"`
Ephemeral DiskHint `json:"ephemeral"`
Persistent PersistentSpec `json:"persistent"`
}
type PersistentSpec map[string]DiskHint
type EnvSpec map[string]interface{}
type BlobstoreSpec struct {
Provider string `json:"provider"`
Options map[string]interface{} `json:"options"`
}
func (ae *AgentEnvImpl) AttachSystemDisk(hint DiskHint) {
ae.spec.Disks.System = hint
}
func (ae *AgentEnvImpl) AttachEphemeralDisk(hint DiskHint) {
ae.spec.Disks.Ephemeral = hint
}
// todo better type for hint
func (ae *AgentEnvImpl) AttachPersistentDisk(cid DiskCID, hint DiskHint) {
spec := PersistentSpec{}
if ae.spec.Disks.Persistent != nil {
for k, v := range ae.spec.Disks.Persistent {
spec[k] = v
}
}
spec[cid.AsString()] = hint
ae.spec.Disks.Persistent = spec
}
func (ae *AgentEnvImpl) DetachPersistentDisk(cid DiskCID) {
spec := PersistentSpec{}
if ae.spec.Disks.Persistent != nil {
for k, v := range ae.spec.Disks.Persistent {
spec[k] = v
}
}
delete(spec, cid.AsString())
ae.spec.Disks.Persistent = spec
}
func (ae AgentEnvImpl) AsBytes() ([]byte, error) {
bytes, err := json.Marshal(ae.spec)
if err != nil {
return nil, bosherr.WrapError(err, "Marshalling agent env")
}
return bytes, nil
}
func (ae AgentEnvImpl) _final() {}