Skip to content

Commit c4c3320

Browse files
committed
aws delete by tag
1 parent 137a99d commit c4c3320

File tree

6 files changed

+100
-95
lines changed

6 files changed

+100
-95
lines changed

examples/main.tf

Lines changed: 22 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,18 @@
11
terraform {
22
required_providers {
3-
# iterative = {
4-
# versions = ["0.6"]
5-
# source = "github.com/iterative/iterative"
6-
# }
7-
83
iterative = {
9-
source = "iterative/iterative"
10-
version = "0.5.7"
4+
versions = ["0.6"]
5+
source = "github.com/iterative/iterative"
116
}
7+
8+
# iterative = {
9+
# source = "iterative/iterative"
10+
# version = "0.5.7"
11+
# }
1212
}
1313
}
1414

1515
provider "iterative" {}
16-
17-
/*
18-
resource "iterative_machine" "machine-az" {
19-
cloud = "azure"
20-
region = "us-east"
21-
instance_type = "m"
22-
}
23-
*/
24-
25-
2616
resource "iterative_runner" "runner-az" {
2717
name = "holy-moly56"
2818
token = "arszDpb3xtNdKaXmQ6vN"
@@ -35,14 +25,6 @@ resource "iterative_runner" "runner-az" {
3525
instance_type = "m"
3626
}
3727

38-
/*
39-
resource "iterative_machine" "machine-aws" {
40-
cloud = "aws"
41-
region = "us-west"
42-
instance_type = "t2.micro" //fallback to known instance type
43-
}
44-
*/
45-
4628

4729
/*
4830
resource "iterative_machine" "machine-az" {
@@ -83,4 +65,18 @@ resource "iterative_machine" "machine-az" {
8365
host = "${self.instance_ip}"
8466
}
8567
}
86-
} */
68+
} */
69+
/*
70+
resource "iterative_machine" "machine-aws" {
71+
name = "cml-aws4"
72+
cloud = "aws"
73+
region = "us-west"
74+
instance_type = "t2.micro" //fallback to known instance type
75+
} */
76+
77+
/* resource "iterative_machine" "machine-az" {
78+
name = "cml-azure"
79+
cloud = "azure"
80+
region = "us-west"
81+
instance_type = "m"
82+
} */

iterative/aws/provider.go

Lines changed: 39 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ import (
1414

1515
//ResourceMachineCreate creates AWS instance
1616
func ResourceMachineCreate(ctx context.Context, d *schema.ResourceData, m interface{}) error {
17+
userData := d.Get("startup_script").(string)
1718
instanceName := d.Get("name").(string)
19+
pairName := d.Id()
1820
hddSize := d.Get("instance_hdd_size").(int)
1921
region := getRegion(d.Get("region").(string))
2022
instanceType := getInstanceType(d.Get("instance_type").(string), d.Get("instance_gpu").(string))
@@ -57,13 +59,15 @@ func ResourceMachineCreate(ctx context.Context, d *schema.ResourceData, m interf
5759
})
5860

5961
instanceAmi := *imagesRes.Images[0].ImageId
60-
pairName := instanceName
6162

6263
// key-pair
6364
svc.ImportKeyPair(&ec2.ImportKeyPairInput{
6465
KeyName: aws.String(pairName),
6566
PublicKeyMaterial: []byte(keyPublic),
6667
})
68+
//if err != nil {
69+
//return err
70+
//}
6771

6872
// securityGroup
6973
var vpcID, sgID string
@@ -128,6 +132,7 @@ func ResourceMachineCreate(ctx context.Context, d *schema.ResourceData, m interf
128132

129133
//launch instance
130134
runResult, err := svc.RunInstancesWithContext(ctx, &ec2.RunInstancesInput{
135+
UserData: aws.String(userData),
131136
ImageId: aws.String(instanceAmi),
132137
KeyName: aws.String(pairName),
133138
InstanceType: aws.String(instanceType),
@@ -163,6 +168,10 @@ func ResourceMachineCreate(ctx context.Context, d *schema.ResourceData, m interf
163168
Key: aws.String("Name"),
164169
Value: aws.String(instanceName),
165170
},
171+
{
172+
Key: aws.String("Id"),
173+
Value: aws.String(d.Id()),
174+
},
166175
},
167176
})
168177
if err != nil {
@@ -182,43 +191,50 @@ func ResourceMachineCreate(ctx context.Context, d *schema.ResourceData, m interf
182191
svc.WaitUntilInstanceExistsWithContext(ctx, &statusInput)
183192

184193
descResult, err := svc.DescribeInstancesWithContext(ctx, &statusInput)
185-
instanceDesc := descResult.Reservations[0].Instances[0]
186-
187-
d.SetId(instanceID)
188-
d.Set("instance_id", instanceID)
189-
d.Set("instance_ip", instanceDesc.PublicIpAddress)
190-
d.Set("instance_launch_time", instanceDesc.LaunchTime.Format(time.RFC3339))
191-
192-
d.Set("key_name", pairName)
193-
194194
if err != nil {
195195
return err
196196
}
197197

198+
instanceDesc := descResult.Reservations[0].Instances[0]
199+
d.Set("instance_ip", instanceDesc.PublicIpAddress)
200+
d.Set("instance_launch_time", instanceDesc.LaunchTime.Format(time.RFC3339))
201+
198202
return nil
199203
}
200204

201205
//ResourceMachineDelete deletes AWS instance
202206
func ResourceMachineDelete(ctx context.Context, d *schema.ResourceData, m interface{}) error {
203-
svc, _ := awsClient(getRegion(d.Get("region").(string)))
204-
instanceID := d.Get("instance_id").(string)
207+
id := aws.String(d.Id())
208+
region := getRegion(d.Get("region").(string))
205209

206-
/*
207-
pairName := d.Get("key_name").(string)
208-
svc.DeleteKeyPair(&ec2.DeleteKeyPairInput{
209-
KeyName: aws.String(pairName),
210-
})
211-
*/
210+
svc, err := awsClient(region)
211+
if err != nil {
212+
return err
213+
}
212214

213-
input := &ec2.TerminateInstancesInput{
214-
InstanceIds: []*string{
215-
aws.String(instanceID),
215+
descResult, err := svc.DescribeInstancesWithContext(ctx, &ec2.DescribeInstancesInput{
216+
Filters: []*ec2.Filter{
217+
{
218+
Name: aws.String("tag:Id"),
219+
Values: []*string{id},
220+
},
216221
},
217-
DryRun: aws.Bool(false),
222+
})
223+
if err != nil {
224+
return err
218225
}
219226

220-
_, err := svc.TerminateInstances(input)
227+
svc.DeleteKeyPair(&ec2.DeleteKeyPairInput{
228+
KeyName: id,
229+
})
221230

231+
instanceID := *descResult.Reservations[0].Instances[0].InstanceId
232+
_, err = svc.TerminateInstances(&ec2.TerminateInstancesInput{
233+
InstanceIds: []*string{
234+
aws.String(instanceID),
235+
},
236+
DryRun: aws.Bool(false),
237+
})
222238
if err != nil {
223239
return err
224240
}

iterative/azure/provider.go

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package azure
22

33
import (
44
"context"
5-
"encoding/base64"
65
"fmt"
76
"os"
87
"strings"
@@ -25,9 +24,7 @@ func ResourceMachineCreate(ctx context.Context, d *schema.ResourceData, m interf
2524
username := "ubuntu"
2625
//username := d.Get("ssh_user").(string)
2726

28-
vmName := d.Get("name").(string)
29-
30-
customData := base64.StdEncoding.EncodeToString([]byte(d.Get("custom_data").(string)))
27+
customData := d.Get("startup_script").(string)
3128
region := getRegion(d.Get("region").(string))
3229
instanceType := getInstanceType(d.Get("instance_type").(string), d.Get("instance_gpu").(string))
3330
keyPublic := d.Get("ssh_public").(string)
@@ -45,13 +42,14 @@ func ResourceMachineCreate(ctx context.Context, d *schema.ResourceData, m interf
4542
sku := imageParts[2]
4643
version := imageParts[3]
4744

48-
gpName := vmName
49-
nsgName := vmName + "-nsg"
50-
vnetName := vmName + "-vnet"
51-
ipName := vmName + "-ip"
52-
subnetName := vmName + "-sn"
53-
nicName := vmName + "-nic"
54-
ipConfigName := vmName + "-ipc"
45+
vmName := d.Get("name").(string)
46+
gpName := d.Id()
47+
nsgName := gpName + "-nsg"
48+
vnetName := gpName + "-vnet"
49+
ipName := gpName + "-ip"
50+
subnetName := gpName + "-sn"
51+
nicName := gpName + "-nic"
52+
ipConfigName := gpName + "-ipc"
5553

5654
groupsClient, err := getGroupsClient(subscriptionID)
5755
_, err = groupsClient.CreateOrUpdate(
@@ -64,9 +62,6 @@ func ResourceMachineCreate(ctx context.Context, d *schema.ResourceData, m interf
6462
return err
6563
}
6664

67-
d.SetId(gpName)
68-
d.Set("instance_id", gpName)
69-
7065
// securityGroup
7166
nsgClient, _ := getNsgClient(subscriptionID)
7267
futureNsg, _ := nsgClient.CreateOrUpdate(
@@ -268,14 +263,16 @@ func ResourceMachineDelete(ctx context.Context, d *schema.ResourceData, m interf
268263
if err != nil {
269264
return err
270265
}
271-
future, err := groupsClient.Delete(context.Background(), d.Id())
272-
if err != nil {
273-
return err
274-
}
275-
err = future.WaitForCompletionRef(ctx, groupsClient.Client)
266+
_, err = groupsClient.Delete(context.Background(), d.Id())
276267
if err != nil {
277268
return err
278269
}
270+
/*
271+
err = future.WaitForCompletionRef(ctx, groupsClient.Client)
272+
if err != nil {
273+
return err
274+
}
275+
*/
279276
return err
280277
}
281278

iterative/resource_machine.go

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

33
import (
44
"context"
5+
"encoding/base64"
56
"fmt"
67

78
"terraform-provider-iterative/iterative/aws"
@@ -65,10 +66,6 @@ func machineSchema() *map[string]*schema.Schema {
6566
Optional: true,
6667
Default: "",
6768
},
68-
"instance_id": &schema.Schema{
69-
Type: schema.TypeString,
70-
Computed: true,
71-
},
7269
"instance_ip": &schema.Schema{
7370
Type: schema.TypeString,
7471
Computed: true,
@@ -95,7 +92,7 @@ func machineSchema() *map[string]*schema.Schema {
9592
Optional: true,
9693
Default: "ubuntu",
9794
},
98-
"custom_data": &schema.Schema{
95+
"startup_script": &schema.Schema{
9996
Type: schema.TypeString,
10097
ForceNew: true,
10198
Optional: true,
@@ -113,7 +110,10 @@ func machineSchema() *map[string]*schema.Schema {
113110
func resourceMachineCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
114111
var diags diag.Diagnostics
115112

116-
utils.SetName(d)
113+
utils.SetId(d)
114+
115+
script64 := base64.StdEncoding.EncodeToString([]byte(d.Get("startup_script").(string)))
116+
d.Set("startup_script", script64)
117117

118118
keyPublic := d.Get("ssh_public").(string)
119119
if len(keyPublic) == 0 {

iterative/resource_runner.go

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -89,10 +89,6 @@ func resourceRunner() *schema.Resource {
8989
Optional: true,
9090
Default: "",
9191
},
92-
"instance_id": &schema.Schema{
93-
Type: schema.TypeString,
94-
Computed: true,
95-
},
9692
"instance_ip": &schema.Schema{
9793
Type: schema.TypeString,
9894
Computed: true,
@@ -119,7 +115,7 @@ func resourceRunner() *schema.Resource {
119115
Optional: true,
120116
Default: "ubuntu",
121117
},
122-
"custom_data": &schema.Schema{
118+
"startup_script": &schema.Schema{
123119
Type: schema.TypeString,
124120
Computed: true,
125121
},
@@ -136,17 +132,17 @@ func resourceRunner() *schema.Resource {
136132
func resourceRunnerCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
137133
var diags diag.Diagnostics
138134

139-
utils.SetName(d)
135+
utils.SetId(d)
140136

141-
customData, err := provisionerCode(d)
137+
startupScript, err := provisionerCode(d)
142138
if err != nil {
143139
diags = append(diags, diag.Diagnostic{
144140
Severity: diag.Error,
145-
Summary: fmt.Sprintf("Error generating provisioner code: %s", err),
141+
Summary: fmt.Sprintf("Error generating startup script: %s", err),
146142
})
147143
return diags
148144
}
149-
d.Set("custom_data", customData)
145+
d.Set("startup_script", startupScript)
150146

151147
/*
152148
diags = append(diags, diag.Diagnostic{
@@ -187,9 +183,10 @@ func provisionerCode(d *schema.ResourceData) (string, error) {
187183
InstanceType{
188184
SchemaVersion: 0,
189185
Attributes: AttributesType{
190-
ID: d.Get("name").(string),
186+
ID: d.Id(),
191187
Cloud: d.Get("cloud").(string),
192-
Name: "",
188+
Region: d.Get("region").(string),
189+
Name: d.Get("name").(string),
193190
Labels: "",
194191
IdleTimeout: d.Get("idle_timeout").(int),
195192
Repo: "",
@@ -200,11 +197,9 @@ func provisionerCode(d *schema.ResourceData) (string, error) {
200197
Image: "",
201198
InstanceGpu: "",
202199
InstanceHddSize: d.Get("instance_hdd_size").(int),
203-
InstanceID: "",
204200
InstanceIP: "",
205201
InstanceLaunchTime: "",
206202
InstanceType: "",
207-
Region: "",
208203
SSHName: "",
209204
SSHPrivate: "",
210205
SSHPublic: "",
@@ -267,21 +262,20 @@ type AttributesType struct {
267262
Repo string `json:"repo"`
268263
Token string `json:"token"`
269264
Driver string `json:"driver"`
270-
AwsSecurityGroup interface{} `json:"aws_security_group"`
271265
Cloud string `json:"cloud"`
272266
CustomData string `json:"custom_data"`
273267
ID string `json:"id"`
274268
Image interface{} `json:"image"`
275269
InstanceGpu interface{} `json:"instance_gpu"`
276270
InstanceHddSize int `json:"instance_hdd_size"`
277-
InstanceID string `json:"instance_id"`
278271
InstanceIP string `json:"instance_ip"`
279272
InstanceLaunchTime string `json:"instance_launch_time"`
280273
InstanceType string `json:"instance_type"`
281274
Region string `json:"region"`
282275
SSHName string `json:"ssh_name"`
283276
SSHPrivate string `json:"ssh_private"`
284277
SSHPublic string `json:"ssh_public"`
278+
AwsSecurityGroup interface{} `json:"aws_security_group"`
285279
}
286280
type InstanceType struct {
287281
Private string `json:"private"`

0 commit comments

Comments
 (0)