@@ -2,6 +2,7 @@ package iterative
2
2
3
3
import (
4
4
"context"
5
+ "sort"
5
6
"time"
6
7
7
8
"github.com/aws/aws-sdk-go/aws"
@@ -23,19 +24,19 @@ func resourceMachine() *schema.Resource {
23
24
Type : schema .TypeString ,
24
25
Optional : true ,
25
26
ForceNew : true ,
26
- Default : "us-west-2 " ,
27
+ Default : "us-east-1 " ,
27
28
},
28
- "instance_ami " : & schema.Schema {
29
+ "instance_type " : & schema.Schema {
29
30
Type : schema .TypeString ,
30
31
Optional : true ,
31
32
ForceNew : true ,
32
- Default : "ami-e7527ed7 " ,
33
+ Default : "t2.micro " ,
33
34
},
34
- "instance_type " : & schema.Schema {
35
- Type : schema .TypeString ,
35
+ "instance_hdd_size " : & schema.Schema {
36
+ Type : schema .TypeInt ,
36
37
Optional : true ,
37
38
ForceNew : true ,
38
- Default : "t2.micro" ,
39
+ Default : 100 ,
39
40
},
40
41
"instance_id" : & schema.Schema {
41
42
Type : schema .TypeString ,
@@ -89,11 +90,43 @@ func resourceMachineCreate(ctx context.Context, d *schema.ResourceData, m interf
89
90
sid , err := shortid .New (1 , shortid .DefaultABC , 2342 )
90
91
id , _ := sid .Generate ()
91
92
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
93
125
instanceType := d .Get ("instance_type" ).(string )
94
126
keyPublic := d .Get ("key_public" ).(string )
95
127
96
128
securityGroup := d .Get ("aws_security_group" ).(string )
129
+ hddSize := d .Get ("instance_hdd_size" ).(int )
97
130
98
131
pairName := "cml_" + id
99
132
var keyMaterial string
@@ -131,33 +164,32 @@ func resourceMachineCreate(ctx context.Context, d *schema.ResourceData, m interf
131
164
VpcId : aws .String (vpcID ),
132
165
})
133
166
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
+ })
161
193
}
162
194
}
163
195
@@ -170,7 +202,19 @@ func resourceMachineCreate(ctx context.Context, d *schema.ResourceData, m interf
170
202
SecurityGroups : []* string {
171
203
aws .String (securityGroup ),
172
204
},
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
+ },
174
218
})
175
219
if err != nil {
176
220
return diag .FromErr (err )
@@ -232,8 +276,8 @@ func resourceMachineDelete(ctx context.Context, d *schema.ResourceData, m interf
232
276
233
277
svc , _ := awsClient (d )
234
278
235
- instanceID := d .Get ("instance_id" ).(string )
236
279
pairName := d .Get ("key_name" ).(string )
280
+ instanceID := d .Get ("instance_id" ).(string )
237
281
238
282
_ , erro := svc .DeleteKeyPair (& ec2.DeleteKeyPairInput {
239
283
KeyName : aws .String (pairName ),
0 commit comments