/
environ_availzones.go
162 lines (147 loc) · 5.3 KB
/
environ_availzones.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
// Copyright 2014 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package gce
import (
"github.com/juju/errors"
"github.com/juju/juju/core/instance"
"github.com/juju/juju/core/network"
"github.com/juju/juju/environs"
"github.com/juju/juju/environs/context"
"github.com/juju/juju/provider/gce/google"
"github.com/juju/juju/storage"
)
// AvailabilityZones returns all availability zones in the environment.
func (env *environ) AvailabilityZones(ctx context.ProviderCallContext) (network.AvailabilityZones, error) {
zones, err := env.gce.AvailabilityZones(env.cloud.Region)
if err != nil {
return nil, google.HandleCredentialError(errors.Trace(err), ctx)
}
var result network.AvailabilityZones
for _, zone := range zones {
if zone.Deprecated() {
continue
}
// We make a copy since the loop var keeps the same pointer.
zoneCopy := zone
result = append(result, &zoneCopy)
}
return result, nil
}
// InstanceAvailabilityZoneNames returns the names of the availability
// zones for the specified instances. The error returned follows the same
// rules as Environ.Instances.
func (env *environ) InstanceAvailabilityZoneNames(ctx context.ProviderCallContext, ids []instance.Id) (map[instance.Id]string, error) {
instances, err := env.Instances(ctx, ids)
if err != nil && err != environs.ErrPartialInstances && err != environs.ErrNoInstances {
return nil, errors.Trace(err)
}
// We let the two environs errors pass on through. However, we do
// not use errors.Trace in that case since callers may not call
// errors.Cause.
results := make(map[instance.Id]string, len(ids))
for _, inst := range instances {
if eInst, ok := inst.(*environInstance); ok && eInst != nil {
results[inst.Id()] = eInst.base.ZoneName
}
}
return results, nil
}
// DeriveAvailabilityZones is part of the common.ZonedEnviron interface.
func (env *environ) DeriveAvailabilityZones(ctx context.ProviderCallContext, args environs.StartInstanceParams) ([]string, error) {
zone, err := env.deriveAvailabilityZones(ctx, args.Placement, args.VolumeAttachments)
if zone != "" {
return []string{zone}, errors.Trace(err)
}
return nil, errors.Trace(err)
}
func (env *environ) availZone(ctx context.ProviderCallContext, name string) (*google.AvailabilityZone, error) {
zones, err := env.gce.AvailabilityZones(env.cloud.Region)
if err != nil {
return nil, google.HandleCredentialError(errors.Trace(err), ctx)
}
for _, z := range zones {
if z.Name() == name {
return &z, nil
}
}
return nil, errors.NotFoundf("invalid availability zone %q", name)
}
func (env *environ) availZoneUp(ctx context.ProviderCallContext, name string) (*google.AvailabilityZone, error) {
zone, err := env.availZone(ctx, name)
if err != nil {
return nil, errors.Trace(err)
}
if !zone.Available() {
return nil, errors.Errorf("availability zone %q is %s", zone.Name(), zone.Status())
}
return zone, nil
}
// volumeAttachmentsZone determines the availability zone for each volume
// identified in the volume attachment parameters, checking that they are
// all the same, and returns the availability zone name.
func volumeAttachmentsZone(volumeAttachments []storage.VolumeAttachmentParams) (string, error) {
var zone string
for i, a := range volumeAttachments {
volumeZone, _, err := parseVolumeId(a.VolumeId)
if err != nil {
return "", errors.Trace(err)
}
if zone == "" {
zone = volumeZone
} else if zone != volumeZone {
return "", errors.Errorf(
"cannot attach volumes from multiple availability zones: %s is in %s, %s is in %s",
volumeAttachments[i-1].VolumeId, zone, a.VolumeId, volumeZone,
)
}
}
return zone, nil
}
func (env *environ) instancePlacementZone(ctx context.ProviderCallContext, placement string, volumeAttachmentsZone string) (string, error) {
if placement == "" {
return volumeAttachmentsZone, nil
}
// placement will always be a zone name or empty.
instPlacement, err := env.parsePlacement(ctx, placement)
if err != nil {
return "", errors.Trace(err)
}
if volumeAttachmentsZone != "" && instPlacement.Zone.Name() != volumeAttachmentsZone {
return "", errors.Errorf(
"cannot create instance with placement %q, as this will prevent attaching the requested disks in zone %q",
placement, volumeAttachmentsZone,
)
}
return instPlacement.Zone.Name(), nil
}
func (e *environ) deriveAvailabilityZones(
ctx context.ProviderCallContext,
placement string,
volumeAttachments []storage.VolumeAttachmentParams,
) (string, error) {
volumeAttachmentsZone, err := volumeAttachmentsZone(volumeAttachments)
if err != nil {
return "", errors.Trace(err)
}
if placement == "" {
return volumeAttachmentsZone, nil
}
instPlacement, err := e.parsePlacement(ctx, placement)
if err != nil {
return "", err
}
instanceZone := instPlacement.Zone.Name()
if err := validateAvailabilityZoneConsistency(instanceZone, volumeAttachmentsZone); err != nil {
return "", errors.Annotatef(err, "cannot create instance with placement %q", placement)
}
return instanceZone, nil
}
func validateAvailabilityZoneConsistency(instanceZone, volumeAttachmentsZone string) error {
if volumeAttachmentsZone != "" && instanceZone != volumeAttachmentsZone {
return errors.Errorf(
"cannot create instance in zone %q, as this will prevent attaching the requested disks in zone %q",
instanceZone, volumeAttachmentsZone,
)
}
return nil
}