-
Notifications
You must be signed in to change notification settings - Fork 23
/
cloudcontainer.go
116 lines (97 loc) · 2.97 KB
/
cloudcontainer.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
// Copyright 2018 Canonical Ltd.
// Licensed under the LGPLv3, see LICENCE file for details.
package description
import (
"github.com/juju/errors"
"github.com/juju/schema"
)
// CloudContainer represents the state of a CAAS container, eg pod.
type CloudContainer interface {
ProviderId() string
Address() Address
Ports() []string
}
type cloudContainer struct {
Version int `yaml:"version"`
ProviderId_ string `yaml:"provider-id,omitempty"`
Address_ *address `yaml:"address,omitempty"`
Ports_ []string `yaml:"ports,omitempty"`
}
// ProviderId implements CloudContainer.
func (c *cloudContainer) ProviderId() string {
return c.ProviderId_
}
// Address implements CloudContainer.
func (c *cloudContainer) Address() Address {
return c.Address_
}
// Ports implements CloudContainer.
func (c *cloudContainer) Ports() []string {
return c.Ports_
}
// CloudContainerArgs is an argument struct used to create a
// new internal cloudContainer type that supports the CloudContainer interface.
type CloudContainerArgs struct {
ProviderId string
Address AddressArgs
Ports []string
}
func newCloudContainer(args *CloudContainerArgs) *cloudContainer {
if args == nil {
return nil
}
cloudcontainer := &cloudContainer{
Version: 1,
ProviderId_: args.ProviderId,
Address_: newAddress(args.Address),
Ports_: args.Ports,
}
return cloudcontainer
}
func importCloudContainer(source map[string]interface{}) (*cloudContainer, error) {
version, err := getVersion(source)
if err != nil {
return nil, errors.Annotate(err, "cloudContainer version schema check failed")
}
importFunc, ok := cloudContainerDeserializationFuncs[version]
if !ok {
return nil, errors.NotValidf("version %d", version)
}
return importFunc(source)
}
type cloudContainerDeserializationFunc func(map[string]interface{}) (*cloudContainer, error)
var cloudContainerDeserializationFuncs = map[int]cloudContainerDeserializationFunc{
1: importCloudContainerV1,
}
func importCloudContainerV1(source map[string]interface{}) (*cloudContainer, error) {
fields := schema.Fields{
"provider-id": schema.String(),
"address": schema.StringMap(schema.Any()),
"ports": schema.List(schema.String()),
}
// Some values don't have to be there.
defaults := schema.Defaults{
"provider-id": schema.Omit,
"address": schema.Omit,
"ports": schema.Omit,
}
checker := schema.FieldMap(fields, defaults)
coerced, err := checker.Coerce(source, nil)
if err != nil {
return nil, errors.Annotatef(err, "cloudContainer v1 schema check failed")
}
valid := coerced.(map[string]interface{})
cloudContainer := &cloudContainer{
Version: 1,
ProviderId_: valid["provider-id"].(string),
Ports_: convertToStringSlice(valid["ports"]),
}
if address, ok := valid["address"]; ok {
containerAddresses, err := importAddress(address.(map[string]interface{}))
if err != nil {
return nil, errors.Trace(err)
}
cloudContainer.Address_ = containerAddresses
}
return cloudContainer, nil
}