/
agent_env_factory.go
72 lines (50 loc) · 1.43 KB
/
agent_env_factory.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
package apiv1
import (
"encoding/json"
bosherr "github.com/cloudfoundry/bosh-utils/errors"
)
type AgentEnvFactory struct{}
func NewAgentEnvFactory() AgentEnvFactory {
return AgentEnvFactory{}
}
func (f AgentEnvFactory) FromBytes(bytes []byte) (AgentEnv, error) {
var agentEnvSpec agentEnvSpec
err := json.Unmarshal(bytes, &agentEnvSpec)
if err != nil {
return nil, bosherr.WrapError(err, "Unmarshalling agent env")
}
return &AgentEnvImpl{agentEnvSpec}, nil
}
func (f AgentEnvFactory) ForVM(
agentID AgentID, cid VMCID, networks Networks, env VMEnv, agentOptions AgentOptions) AgentEnv {
networksSpec := NetworksSpec{}
for netName, network := range networks {
typedNet := network.(*NetworkImpl)
networksSpec[netName] = NetworkSpec{
Type: typedNet.spec.Type,
IP: typedNet.IP(),
Netmask: typedNet.Netmask(),
Gateway: typedNet.Gateway(),
DNS: typedNet.spec.DNS,
Default: typedNet.spec.Default,
MAC: typedNet.mac,
Preconfigured: typedNet.preconfigured,
}
}
agentEnvSpec := agentEnvSpec{
AgentID: agentID.AsString(),
VM: VMSpec{
Name: cid.AsString(), // id for name and id
ID: cid.AsString(),
},
Mbus: agentOptions.Mbus,
NTP: agentOptions.NTP,
Blobstore: BlobstoreSpec{
Provider: agentOptions.Blobstore.Type,
Options: agentOptions.Blobstore.Options,
},
Networks: networksSpec,
Env: EnvSpec(env.val), // todo deep copy env?
}
return &AgentEnvImpl{agentEnvSpec}
}