-
Notifications
You must be signed in to change notification settings - Fork 28
/
resource_launch_template.go
168 lines (146 loc) · 4.68 KB
/
resource_launch_template.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
package resources
import (
"context"
"encoding/base64"
"errors"
"github.com/aws/smithy-go"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/ec2"
"github.com/aws/aws-sdk-go-v2/service/ec2/types"
"terraform-provider-iterative/task/aws/client"
"terraform-provider-iterative/task/common"
"terraform-provider-iterative/task/common/machine"
)
func NewLaunchTemplate(client *client.Client, identifier common.Identifier, securityGroup *SecurityGroup, permissionSet *PermissionSet, image *Image, keyPair *KeyPair, credentials *Credentials, task common.Task) *LaunchTemplate {
l := new(LaunchTemplate)
l.Client = client
l.Identifier = identifier.Long()
l.Attributes = task
l.Dependencies.SecurityGroup = securityGroup
l.Dependencies.Image = image
l.Dependencies.KeyPair = keyPair
l.Dependencies.Credentials = credentials
l.Dependencies.PermissionSet = permissionSet
return l
}
type LaunchTemplate struct {
Client *client.Client
Identifier string
Attributes common.Task
Dependencies struct {
*KeyPair
*SecurityGroup
*Image
*Credentials
*PermissionSet
}
Resource *types.LaunchTemplate
}
func (l *LaunchTemplate) Create(ctx context.Context) error {
if l.Attributes.Environment.Variables == nil {
l.Attributes.Environment.Variables = make(map[string]*string)
}
for name, value := range *l.Dependencies.Credentials.Resource {
valueCopy := value
l.Attributes.Environment.Variables[name] = &valueCopy
}
script := machine.Script(l.Attributes.Environment.Script, l.Attributes.Environment.Variables, l.Attributes.Environment.Timeout)
userData := base64.StdEncoding.EncodeToString([]byte(script))
size := l.Attributes.Size.Machine
sizes := map[string]string{
"s": "t2.micro",
"m": "m5.2xlarge",
"l": "m5.8xlarge",
"xl": "m5.16xlarge",
"m+t4": "g4dn.xlarge",
"m+k80": "p2.xlarge",
"l+k80": "p2.8xlarge",
"xl+k80": "p2.16xlarge",
"m+v100": "p3.xlarge",
"l+v100": "p3.8xlarge",
"xl+v100": "p3.16xlarge",
}
if val, ok := sizes[size]; ok {
size = val
}
input := ec2.CreateLaunchTemplateInput{
LaunchTemplateName: aws.String(l.Identifier),
LaunchTemplateData: &types.RequestLaunchTemplateData{
UserData: aws.String(userData),
ImageId: l.Dependencies.Image.Resource.ImageId,
KeyName: l.Dependencies.KeyPair.Resource.KeyName,
InstanceType: types.InstanceType(size),
SecurityGroupIds: []string{aws.ToString(l.Dependencies.SecurityGroup.Resource.GroupId)},
IamInstanceProfile: l.Dependencies.PermissionSet.Resource,
BlockDeviceMappings: []types.LaunchTemplateBlockDeviceMappingRequest{
{
DeviceName: aws.String("/dev/sda1"),
Ebs: &types.LaunchTemplateEbsBlockDeviceRequest{
DeleteOnTermination: aws.Bool(true),
Encrypted: aws.Bool(false),
VolumeType: types.VolumeType("gp2"),
},
},
},
TagSpecifications: []types.LaunchTemplateTagSpecificationRequest{
{
ResourceType: types.ResourceTypeInstance,
Tags: makeTagSlice(l.Identifier, l.Client.Tags),
},
{
ResourceType: types.ResourceTypeVolume,
Tags: makeTagSlice(l.Identifier, l.Client.Tags),
},
},
},
TagSpecifications: []types.TagSpecification{
{
ResourceType: types.ResourceTypeLaunchTemplate,
Tags: makeTagSlice(l.Identifier, l.Client.Tags),
},
},
}
if size := l.Attributes.Size.Storage; size > 0 {
input.LaunchTemplateData.BlockDeviceMappings[0].Ebs.VolumeSize = aws.Int32(int32(size))
}
if _, err := l.Client.Services.EC2.CreateLaunchTemplate(ctx, &input); err != nil {
var e smithy.APIError
if errors.As(err, &e) && e.ErrorCode() == "InvalidLaunchTemplateName.AlreadyExistsException" {
return l.Read(ctx)
}
return err
}
return l.Read(ctx)
}
func (l *LaunchTemplate) Read(ctx context.Context) error {
input := ec2.DescribeLaunchTemplatesInput{
LaunchTemplateNames: []string{l.Identifier},
}
templates, err := l.Client.Services.EC2.DescribeLaunchTemplates(ctx, &input)
if err != nil {
return err
}
if len(templates.LaunchTemplates) == 0 {
return common.NotFoundError
}
l.Resource = &templates.LaunchTemplates[0]
return nil
}
func (l *LaunchTemplate) Update(ctx context.Context) error {
return common.NotImplementedError
}
func (l *LaunchTemplate) Delete(ctx context.Context) error {
input := ec2.DeleteLaunchTemplateInput{
LaunchTemplateName: aws.String(l.Identifier),
}
if _, err := l.Client.Services.EC2.DeleteLaunchTemplate(ctx, &input); err != nil {
var e smithy.APIError
if errors.As(err, &e) && e.ErrorCode() == "InvalidLaunchTemplateName.NotFoundException" {
l.Resource = nil
return nil
}
return err
}
l.Resource = nil
return nil
}