forked from kubernetes/kops
-
Notifications
You must be signed in to change notification settings - Fork 0
/
aws.go
153 lines (117 loc) · 4.08 KB
/
aws.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
/*
Copyright 2018 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package spotinst
import (
"context"
"github.com/spotinst/spotinst-sdk-go/service/elastigroup/providers/aws"
"k8s.io/kops/upup/pkg/fi"
)
type awsService struct {
svc aws.Service
}
// List returns a list of Elastigroups.
func (s *awsService) List(ctx context.Context) ([]Elastigroup, error) {
output, err := s.svc.List(ctx, nil)
if err != nil {
return nil, err
}
groups := make([]Elastigroup, len(output.Groups))
for i, group := range output.Groups {
groups[i] = &awsElastigroup{group}
}
return groups, nil
}
// Create creates a new Elastigroup and returns its ID.
func (s *awsService) Create(ctx context.Context, group Elastigroup) (string, error) {
input := &aws.CreateGroupInput{
Group: group.Obj().(*aws.Group),
}
output, err := s.svc.Create(ctx, input)
if err != nil {
return "", err
}
return fi.StringValue(output.Group.ID), nil
}
// Read returns an existing Elastigroup by ID.
func (s *awsService) Read(ctx context.Context, groupID string) (Elastigroup, error) {
input := &aws.ReadGroupInput{
GroupID: fi.String(groupID),
}
output, err := s.svc.Read(ctx, input)
if err != nil {
return nil, err
}
return &awsElastigroup{output.Group}, nil
}
// Update updates an existing Elastigroup.
func (s *awsService) Update(ctx context.Context, group Elastigroup) error {
input := &aws.UpdateGroupInput{
Group: group.Obj().(*aws.Group),
}
_, err := s.svc.Update(ctx, input)
return err
}
// Delete deletes an existing Elastigroup by ID.
func (s *awsService) Delete(ctx context.Context, groupID string) error {
input := &aws.DeleteGroupInput{
GroupID: fi.String(groupID),
}
_, err := s.svc.Delete(ctx, input)
return err
}
// Detach removes one or more instances from the specified Elastigroup.
func (s *awsService) Detach(ctx context.Context, groupID string, instanceIDs []string) error {
input := &aws.DetachGroupInput{
GroupID: fi.String(groupID),
InstanceIDs: instanceIDs,
ShouldDecrementTargetCapacity: fi.Bool(false),
ShouldTerminateInstances: fi.Bool(true),
}
_, err := s.svc.Detach(ctx, input)
return err
}
// Instances returns a list of all instances that belong to specified Elastigroup.
func (s *awsService) Instances(ctx context.Context, groupID string) ([]Instance, error) {
input := &aws.StatusGroupInput{
GroupID: fi.String(groupID),
}
output, err := s.svc.Status(ctx, input)
if err != nil {
return nil, err
}
instances := make([]Instance, len(output.Instances))
for i, instance := range output.Instances {
instances[i] = &awsInstance{instance}
}
return instances, err
}
type awsElastigroup struct {
obj *aws.Group
}
// Id returns the ID of the Elastigroup.
func (e *awsElastigroup) Id() string { return fi.StringValue(e.obj.ID) }
// Name returns the name of the Elastigroup.
func (e *awsElastigroup) Name() string { return fi.StringValue(e.obj.Name) }
// MinSize returns the minimum size of the Elastigroup.
func (e *awsElastigroup) MinSize() int { return fi.IntValue(e.obj.Capacity.Minimum) }
// MaxSize returns the maximum size of the Elastigroup.
func (e *awsElastigroup) MaxSize() int { return fi.IntValue(e.obj.Capacity.Maximum) }
// Obj returns the raw object which is a cloud-specific implementation.
func (e *awsElastigroup) Obj() interface{} { return e.obj }
type awsInstance struct {
obj *aws.Instance
}
// Id returns the ID of the instance.
func (i *awsInstance) Id() string { return fi.StringValue(i.obj.ID) }
// Obj returns the raw object which is a cloud-specific implementation.
func (i *awsInstance) Obj() interface{} { return i.obj }