Skip to content

Commit 4f2db44

Browse files
committed
hdd and ami
1 parent 8a4b77a commit 4f2db44

File tree

2 files changed

+98
-37
lines changed

2 files changed

+98
-37
lines changed

examples/main.tf

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,26 @@ terraform {
99

1010
provider "iterative" {}
1111

12-
resource "iterative_machine" "machine" {
12+
/* resource "iterative_machine" "machine" {
1313
region = "us-west-1"
1414
instance_ami = "ami-03ba3948f6c37a4b0"
1515
aws_security_group = "default"
16+
key_public = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAACAQCcztk+/ibMWJH7LTjcw5KhlDKW1y/gJVB3ivk3D0YMa84ylL5pc8/3zd4PkAvX4eJX0Yi/dE9r4dY+8+ws/1pIuAO9y9Fu3ev+Cj8CKbFbgxaLDlaWHCV/y295rhmgfArtTB4KCbQ2EihXgodzddA9FGiyPoeYWilUUNDsi9wBTsouGahaFpiDVeAkexkUgGtGUWTW7OcTgvagGmKoNogWEpo9VBU3gGEoWj/I1TecQmOs09NFMyj1DdtRfsKYhQUfYz1W38ht0zCuPHKOnVGLDK4vd3nI2KzKJu0/CcFbjlJNqPrHHooIGJtmQRQIsSyI6hCUPK3ZCI7o+viaGqP+Awbo7XSKyexqd81bhgha98jqy6304jOG5qSUewgeK7VNq2FEXQ0D7ox0Yci/TgM7w+XVpjOf6XEUjkUyoLoL1xkxcINdZozWzeXK/dykvfXo+nwALT4UhjMx7fk46e2lRyExBuD4L0ah8rDT1ZUORsDkEVvmCx/tJqO1drrUPLT846Cb0E6oebcYCUCN9r8qo2BeipG44VkX0jL9BLB2IZeP5BpXFT+bo3zjXqEtX3l/5iJ42jOJodmw70gaf/7c2NWumydR2STuDQLUSvJC2Xtka5M/CHfNX3ShssrJaR/oKacU8F5DaPqTH9RSJ3oSs8Kr247E20i1BzDTwrWicQ== g.ortega.david@gmail.com"
17+
} */
18+
/*
19+
resource "iterative_machine" "machine2" {
20+
region = "us-west-1"
21+
instance_ami = "ami-03ba3948f6c37a4b0"
22+
aws_security_group = "default"
23+
} */
24+
25+
26+
# resource "iterative_machine" "machine3" {
27+
# region = "us-west-1"
28+
# }
29+
30+
resource "iterative_machine" "machine4" {
31+
region = "us-west-2"
32+
instance_hdd_size = 110
1633
}
1734

iterative/resource_machine.go

Lines changed: 80 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package iterative
22

33
import (
44
"context"
5+
"sort"
56
"time"
67

78
"github.com/aws/aws-sdk-go/aws"
@@ -23,19 +24,19 @@ func resourceMachine() *schema.Resource {
2324
Type: schema.TypeString,
2425
Optional: true,
2526
ForceNew: true,
26-
Default: "us-west-2",
27+
Default: "us-east-1",
2728
},
28-
"instance_ami": &schema.Schema{
29+
"instance_type": &schema.Schema{
2930
Type: schema.TypeString,
3031
Optional: true,
3132
ForceNew: true,
32-
Default: "ami-e7527ed7",
33+
Default: "t2.micro",
3334
},
34-
"instance_type": &schema.Schema{
35-
Type: schema.TypeString,
35+
"instance_hdd_size": &schema.Schema{
36+
Type: schema.TypeInt,
3637
Optional: true,
3738
ForceNew: true,
38-
Default: "t2.micro",
39+
Default: 100,
3940
},
4041
"instance_id": &schema.Schema{
4142
Type: schema.TypeString,
@@ -89,11 +90,43 @@ func resourceMachineCreate(ctx context.Context, d *schema.ResourceData, m interf
8990
sid, err := shortid.New(1, shortid.DefaultABC, 2342)
9091
id, _ := sid.Generate()
9192

92-
instanceAmi := d.Get("instance_ami").(string)
93+
amiParams := &ec2.DescribeImagesInput{
94+
Filters: []*ec2.Filter{
95+
{
96+
Name: aws.String("name"),
97+
Values: []*string{aws.String("Deep*Ubuntu 18.04*")},
98+
},
99+
{
100+
Name: aws.String("architecture"),
101+
Values: []*string{aws.String("x86_64")},
102+
},
103+
},
104+
}
105+
imagesRes, imagesErr := svc.DescribeImages(amiParams)
106+
if imagesErr != nil {
107+
diag.FromErr(imagesErr)
108+
}
109+
110+
sort.Slice(imagesRes.Images, func(i, j int) bool {
111+
itime, _ := time.Parse(time.RFC3339, aws.StringValue(imagesRes.Images[i].CreationDate))
112+
jtime, _ := time.Parse(time.RFC3339, aws.StringValue(imagesRes.Images[j].CreationDate))
113+
return itime.Unix() > jtime.Unix()
114+
})
115+
116+
/* diags = append(diags, diag.Diagnostic{
117+
Severity: diag.Error,
118+
Summary: "Unable to create HashiCups client",
119+
Detail: fmt.Sprint(len(imagesRes.Images)),
120+
})
121+
122+
return diags */
123+
124+
instanceAmi := *imagesRes.Images[0].ImageId
93125
instanceType := d.Get("instance_type").(string)
94126
keyPublic := d.Get("key_public").(string)
95127

96128
securityGroup := d.Get("aws_security_group").(string)
129+
hddSize := d.Get("instance_hdd_size").(int)
97130

98131
pairName := "cml_" + id
99132
var keyMaterial string
@@ -131,33 +164,32 @@ func resourceMachineCreate(ctx context.Context, d *schema.ResourceData, m interf
131164
VpcId: aws.String(vpcID),
132165
})
133166

134-
svc.AuthorizeSecurityGroupIngress(&ec2.AuthorizeSecurityGroupIngressInput{
135-
GroupId: aws.String(*gpResult.GroupId),
136-
IpPermissions: []*ec2.IpPermission{
137-
(&ec2.IpPermission{}).
138-
SetIpProtocol("-1").
139-
SetFromPort(-1).
140-
SetToPort(-1).
141-
SetIpRanges([]*ec2.IpRange{
142-
{CidrIp: aws.String("0.0.0.0/0")},
143-
}),
144-
},
145-
})
146-
147-
svc.AuthorizeSecurityGroupEgress(&ec2.AuthorizeSecurityGroupEgressInput{
148-
GroupId: aws.String(*gpResult.GroupId),
149-
IpPermissions: []*ec2.IpPermission{
150-
(&ec2.IpPermission{}).
151-
SetIpProtocol("-1").
152-
SetFromPort(-1).
153-
SetToPort(-1).
154-
SetIpRanges([]*ec2.IpRange{
155-
{CidrIp: aws.String("0.0.0.0/0")},
156-
}),
157-
},
158-
})
159-
if ee != nil {
160-
return diag.FromErr(err)
167+
if ee == nil {
168+
svc.AuthorizeSecurityGroupIngress(&ec2.AuthorizeSecurityGroupIngressInput{
169+
GroupId: aws.String(*gpResult.GroupId),
170+
IpPermissions: []*ec2.IpPermission{
171+
(&ec2.IpPermission{}).
172+
SetIpProtocol("-1").
173+
SetFromPort(-1).
174+
SetToPort(-1).
175+
SetIpRanges([]*ec2.IpRange{
176+
{CidrIp: aws.String("0.0.0.0/0")},
177+
}),
178+
},
179+
})
180+
181+
svc.AuthorizeSecurityGroupEgress(&ec2.AuthorizeSecurityGroupEgressInput{
182+
GroupId: aws.String(*gpResult.GroupId),
183+
IpPermissions: []*ec2.IpPermission{
184+
(&ec2.IpPermission{}).
185+
SetIpProtocol("-1").
186+
SetFromPort(-1).
187+
SetToPort(-1).
188+
SetIpRanges([]*ec2.IpRange{
189+
{CidrIp: aws.String("0.0.0.0/0")},
190+
}),
191+
},
192+
})
161193
}
162194
}
163195

@@ -170,7 +202,19 @@ func resourceMachineCreate(ctx context.Context, d *schema.ResourceData, m interf
170202
SecurityGroups: []*string{
171203
aws.String(securityGroup),
172204
},
173-
//CpuOptions: instanceOpts.CpuOptions,
205+
BlockDeviceMappings: []*ec2.BlockDeviceMapping{
206+
{
207+
//VirtualName: aws.String("Root"),
208+
DeviceName: aws.String("/dev/sda1"),
209+
Ebs: &ec2.EbsBlockDevice{
210+
DeleteOnTermination: aws.Bool(true),
211+
Encrypted: aws.Bool(false),
212+
//Iops: aws.Int64(0),
213+
VolumeSize: aws.Int64(int64(hddSize)),
214+
VolumeType: aws.String("gp2"),
215+
},
216+
},
217+
},
174218
})
175219
if err != nil {
176220
return diag.FromErr(err)
@@ -232,8 +276,8 @@ func resourceMachineDelete(ctx context.Context, d *schema.ResourceData, m interf
232276

233277
svc, _ := awsClient(d)
234278

235-
instanceID := d.Get("instance_id").(string)
236279
pairName := d.Get("key_name").(string)
280+
instanceID := d.Get("instance_id").(string)
237281

238282
_, erro := svc.DeleteKeyPair(&ec2.DeleteKeyPairInput{
239283
KeyName: aws.String(pairName),

0 commit comments

Comments
 (0)