-
Notifications
You must be signed in to change notification settings - Fork 23
/
cloudservice.go
119 lines (101 loc) · 2.97 KB
/
cloudservice.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
// Copyright 2018 Canonical Ltd.
// Licensed under the LGPLv3, see LICENCE file for details.
package description
import (
"github.com/juju/errors"
"github.com/juju/schema"
)
// CloudService represents the state of a CAAS service.
type CloudService interface {
ProviderId() string
Addresses() []Address
SetAddresses(addresses []AddressArgs)
}
type cloudService struct {
Version int `yaml:"version"`
ProviderId_ string `yaml:"provider-id,omitempty"`
Addresses_ []*address `yaml:"addresses,omitempty"`
}
// ProviderId implements cloudService.
func (c *cloudService) ProviderId() string {
return c.ProviderId_
}
// Addresses implements cloudService.
func (c *cloudService) Addresses() []Address {
var result []Address
for _, addr := range c.Addresses_ {
result = append(result, addr)
}
return result
}
// SetAddresses implements cloudService.
func (m *cloudService) SetAddresses(args []AddressArgs) {
m.Addresses_ = nil
for _, args := range args {
if args.Value != "" {
m.Addresses_ = append(m.Addresses_, newAddress(args))
}
}
}
// CloudServiceArgs is an argument struct used to create a
// new internal cloudService type that supports the cloudService interface.
type CloudServiceArgs struct {
ProviderId string
Addresses []AddressArgs
}
func newCloudService(args *CloudServiceArgs) *cloudService {
if args == nil {
return nil
}
cloudService := &cloudService{
Version: 1,
ProviderId_: args.ProviderId,
}
cloudService.SetAddresses(args.Addresses)
return cloudService
}
func importCloudService(source map[string]interface{}) (*cloudService, error) {
version, err := getVersion(source)
if err != nil {
return nil, errors.Annotate(err, "cloudService version schema check failed")
}
importFunc, ok := cloudServiceDeserializationFuncs[version]
if !ok {
return nil, errors.NotValidf("version %d", version)
}
return importFunc(source)
}
type cloudServiceDeserializationFunc func(map[string]interface{}) (*cloudService, error)
var cloudServiceDeserializationFuncs = map[int]cloudServiceDeserializationFunc{
1: importCloudServiceV1,
}
func importCloudServiceV1(source map[string]interface{}) (*cloudService, error) {
fields := schema.Fields{
"provider-id": schema.String(),
"addresses": schema.List(schema.StringMap(schema.Any())),
}
// Some values don't have to be there.
defaults := schema.Defaults{
"provider-id": schema.Omit,
"addresses": schema.Omit,
}
checker := schema.FieldMap(fields, defaults)
coerced, err := checker.Coerce(source, nil)
if err != nil {
return nil, errors.Annotatef(err, "cloudService v1 schema check failed")
}
valid := coerced.(map[string]interface{})
providerId, _ := valid["provider-id"].(string)
cloudService := &cloudService{
Version: 1,
ProviderId_: providerId,
}
if addresses, ok := valid["addresses"]; ok {
serviceAddresses, err := importAddresses(addresses.([]interface{}))
if err != nil {
return nil, errors.Trace(err)
}
cloudService.Addresses_ = serviceAddresses
}
return cloudService, nil
}