/
spotrequest.go
92 lines (76 loc) · 2.48 KB
/
spotrequest.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
// Copyright © 2018 Banzai Cloud
//
// 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 ec2
import (
"github.com/aws/aws-sdk-go/service/ec2"
)
const instancePipelineCreatedTag = "pipeline-created"
// SpotInstanceRequest extends ec2.SpotInstanceRequest
type SpotInstanceRequest struct {
*ec2.SpotInstanceRequest
}
// NewSpotInstanceRequest initialises and gives back a SpotInstanceRequest
func NewSpotInstanceRequest(request *ec2.SpotInstanceRequest) *SpotInstanceRequest {
return &SpotInstanceRequest{
SpotInstanceRequest: request,
}
}
// IsPipelineRelated checks whether the spot request has a tag which indicates that it was created by Pipeline
func (r *SpotInstanceRequest) IsPipelineRelated() bool {
if len(r.Tags) == 0 {
return false
}
for _, tag := range r.Tags {
if tag.Key != nil && tag.Value != nil && *tag.Key == instancePipelineCreatedTag && *tag.Value == "true" {
return true
}
}
return false
}
// IsActive is true if the request is an active one
func (r *SpotInstanceRequest) IsActive() bool {
state := r.GetState()
return state == ec2.SpotInstanceStateOpen || state == ec2.SpotInstanceStateActive
}
// IsFullfilled is true if the request is fulfilled
func (r *SpotInstanceRequest) IsFulfilled() bool {
return r.GetStatusCode() == "fulfilled"
}
// IsPending is true if the request is in a pending state
func (r *SpotInstanceRequest) IsPending() bool {
if !r.IsActive() {
return false
}
switch r.GetStatusCode() {
case "pending-evaluation", "not-scheduled-yet", "pending-fulfillment", "capacity-not-available", "capacity-oversubscribed":
return true
}
return false
}
// GetState gives back the state of the request
func (r *SpotInstanceRequest) GetState() string {
var state string
if r.State != nil {
state = *r.State
}
return state
}
// GetStatusCode gives back the status code of the request
func (r *SpotInstanceRequest) GetStatusCode() string {
var status string
if r.Status != nil && r.Status.Code != nil {
status = *r.Status.Code
}
return status
}