forked from juju/juju
/
instance.go
208 lines (183 loc) · 5.97 KB
/
instance.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
// Copyright 2014 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package google
import (
"fmt"
"path"
"google.golang.org/api/compute/v1"
"github.com/juju/juju/network"
)
// InstanceSpec holds all the information needed to create a new GCE
// instance within some zone.
// TODO(ericsnow) Validate the invariants?
type InstanceSpec struct {
// ID is the "name" of the instance.
ID string
// Type is the name of the GCE instance type. The value is resolved
// relative to an availability zone when the API request is sent.
// The type must match one of the GCE-recognized types.
Type string
// Disks holds the information needed to request each of the disks
// that should be attached to a new instance. This must include a
// single root disk.
Disks []DiskSpec
// Network identifies the information for the network that a new
// instance should use. If the network does not exist then it will
// be added when the instance is. At least the network's name must
// be set.
Network NetworkSpec
// NetworkInterfaces is the names of the network interfaces to
// associate with the instance. They will be connected to the the
// network identified by the instance spec. At least one name must
// be provided.
NetworkInterfaces []string
// Metadata is the GCE instance "user-specified" metadata that will
// be initialized on the new instance.
Metadata map[string]string
// Tags are the labels to associate with the instance. This is
// useful when making bulk calls or in relation to some API methods
// (e.g. related to firewalls access rules).
Tags []string
}
func (is InstanceSpec) raw() *compute.Instance {
return &compute.Instance{
Name: is.ID,
Disks: is.disks(),
NetworkInterfaces: is.networkInterfaces(),
Metadata: packMetadata(is.Metadata),
Tags: &compute.Tags{Items: is.Tags},
// MachineType is set in the addInstance call.
}
}
// Summary builds an InstanceSummary based on the spec and returns it.
func (is InstanceSpec) Summary() InstanceSummary {
raw := is.raw()
return newInstanceSummary(raw)
}
func (is InstanceSpec) disks() []*compute.AttachedDisk {
var result []*compute.AttachedDisk
for _, spec := range is.Disks {
result = append(result, spec.newAttached())
}
return result
}
func (is InstanceSpec) networkInterfaces() []*compute.NetworkInterface {
var result []*compute.NetworkInterface
for _, name := range is.NetworkInterfaces {
result = append(result, is.Network.newInterface(name))
}
return result
}
// RootDisk identifies the root disk for a given instance (or instance
// spec) and returns it. If the root disk could not be determined then
// nil is returned.
// TODO(ericsnow) Return an error?
func (is InstanceSpec) RootDisk() *compute.AttachedDisk {
return is.Disks[0].newAttached()
}
// InstanceSummary captures all the data needed by Instance.
type InstanceSummary struct {
// ID is the "name" of the instance.
ID string
// ZoneName is the unqualified name of the zone in which the
// instance was provisioned.
ZoneName string
// Status holds the status of the instance at a certain point in time.
Status string
// Metadata is the instance metadata.
Metadata map[string]string
// Addresses are the IP Addresses associated with the instance.
Addresses []network.Address
}
func newInstanceSummary(raw *compute.Instance) InstanceSummary {
return InstanceSummary{
ID: raw.Name,
ZoneName: path.Base(raw.Zone),
Status: raw.Status,
Metadata: unpackMetadata(raw.Metadata),
Addresses: extractAddresses(raw.NetworkInterfaces...),
}
}
// Instance represents a single realized GCE compute instance.
type Instance struct {
InstanceSummary
// spec is the InstanceSpec used to create this instance.
spec *InstanceSpec
}
func newInstance(raw *compute.Instance, spec *InstanceSpec) *Instance {
summary := newInstanceSummary(raw)
return NewInstance(summary, spec)
}
// NewInstance builds an instance from the provided summary and spec
// and returns it.
func NewInstance(summary InstanceSummary, spec *InstanceSpec) *Instance {
if spec != nil {
// Make a copy.
val := *spec
spec = &val
}
return &Instance{
InstanceSummary: summary,
spec: spec,
}
}
// RootDisk returns an AttachedDisk
func (gi Instance) RootDisk() *compute.AttachedDisk {
if gi.spec == nil {
return nil
}
return gi.spec.RootDisk()
}
// RootDiskGB returns the size of the instance's root disk. If it
// cannot be determined then 0 is returned.
func (gi Instance) RootDiskGB() uint64 {
if gi.spec == nil {
return 0
}
attached := gi.RootDisk()
return uint64(attached.InitializeParams.DiskSizeGb)
}
// Status returns a string identifying the status of the instance. The
// value will match one of the Status* constants in the package.
func (gi Instance) Status() string {
return gi.InstanceSummary.Status
}
// Addresses identifies information about the network addresses
// associated with the instance and returns it.
func (gi Instance) Addresses() []network.Address {
// TODO*ericsnow) return a copy?
return gi.InstanceSummary.Addresses
}
// Metadata returns the user-specified metadata for the instance.
func (gi Instance) Metadata() map[string]string {
// TODO*ericsnow) return a copy?
return gi.InstanceSummary.Metadata
}
// packMetadata composes the provided data into the format required
// by the GCE API.
func packMetadata(data map[string]string) *compute.Metadata {
var items []*compute.MetadataItems
for key, value := range data {
item := compute.MetadataItems{
Key: key,
Value: value,
}
items = append(items, &item)
}
return &compute.Metadata{Items: items}
}
// unpackMetadata decomposes the provided data from the format used
// in the GCE API.
func unpackMetadata(data *compute.Metadata) map[string]string {
if data == nil {
return nil
}
result := make(map[string]string)
for _, item := range data.Items {
result[item.Key] = item.Value
}
return result
}
func formatMachineType(zone, name string) string {
return fmt.Sprintf("zones/%s/machineTypes/%s", zone, name)
}