Skip to content

Commit f65064f

Browse files
authored
Delete error and other non existant resurces check (#22)
1 parent 6223b7a commit f65064f

File tree

2 files changed

+29
-25
lines changed

2 files changed

+29
-25
lines changed

iterative/aws/provider.go

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -65,21 +65,21 @@ func ResourceMachineCreate(ctx context.Context, d *schema.ResourceData, m interf
6565
KeyName: aws.String(pairName),
6666
PublicKeyMaterial: []byte(keyPublic),
6767
})
68-
//if err != nil {
69-
//return err
70-
//}
7168

7269
// securityGroup
7370
var vpcID, sgID string
7471
if len(securityGroup) == 0 {
7572
securityGroup = "cml"
7673

7774
vpcsDesc, _ := svc.DescribeVpcs(&ec2.DescribeVpcsInput{})
75+
if len(vpcsDesc.Vpcs) == 0 {
76+
return errors.New("no VPCs found")
77+
}
7878
vpcID = *vpcsDesc.Vpcs[0].VpcId
7979

8080
gpResult, err := svc.CreateSecurityGroup(&ec2.CreateSecurityGroupInput{
8181
GroupName: aws.String(securityGroup),
82-
Description: aws.String("CML security group"),
82+
Description: aws.String("Iterative security group"),
8383
VpcId: aws.String(vpcID),
8484
})
8585

@@ -117,18 +117,27 @@ func ResourceMachineCreate(ctx context.Context, d *schema.ResourceData, m interf
117117
if err != nil {
118118
return err
119119
}
120+
if len(sgDesc.SecurityGroups) == 0 {
121+
return errors.New("no Security Groups found")
122+
}
120123

121124
sgID = *sgDesc.SecurityGroups[0].GroupId
122125
vpcID = *sgDesc.SecurityGroups[0].VpcId
123126

124-
subDesc, _ := svc.DescribeSubnetsWithContext(ctx, &ec2.DescribeSubnetsInput{
127+
subDesc, err := svc.DescribeSubnetsWithContext(ctx, &ec2.DescribeSubnetsInput{
125128
Filters: []*ec2.Filter{
126129
{
127130
Name: aws.String("vpc-id"),
128131
Values: []*string{aws.String(vpcID)},
129132
},
130133
},
131134
})
135+
if err != nil {
136+
return err
137+
}
138+
if len(subDesc.Subnets) == 0 {
139+
return errors.New("no subnets found")
140+
}
132141

133142
//launch instance
134143
runResult, err := svc.RunInstancesWithContext(ctx, &ec2.RunInstancesInput{
@@ -212,6 +221,10 @@ func ResourceMachineDelete(ctx context.Context, d *schema.ResourceData, m interf
212221
return err
213222
}
214223

224+
svc.DeleteKeyPair(&ec2.DeleteKeyPairInput{
225+
KeyName: id,
226+
})
227+
215228
descResult, err := svc.DescribeInstancesWithContext(ctx, &ec2.DescribeInstancesInput{
216229
Filters: []*ec2.Filter{
217230
{
@@ -223,20 +236,16 @@ func ResourceMachineDelete(ctx context.Context, d *schema.ResourceData, m interf
223236
if err != nil {
224237
return err
225238
}
226-
227-
svc.DeleteKeyPair(&ec2.DeleteKeyPairInput{
228-
KeyName: id,
229-
})
230-
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-
})
238-
if err != nil {
239-
return err
239+
if len(descResult.Reservations) > 0 && len(descResult.Reservations[0].Instances) > 0 {
240+
instanceID := *descResult.Reservations[0].Instances[0].InstanceId
241+
_, err = svc.TerminateInstances(&ec2.TerminateInstancesInput{
242+
InstanceIds: []*string{
243+
aws.String(instanceID),
244+
},
245+
})
246+
if err != nil {
247+
return err
248+
}
240249
}
241250

242251
return nil

iterative/azure/provider.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -265,12 +265,7 @@ func ResourceMachineDelete(ctx context.Context, d *schema.ResourceData, m interf
265265
if err != nil {
266266
return err
267267
}
268-
/*
269-
err = future.WaitForCompletionRef(ctx, groupsClient.Client)
270-
if err != nil {
271-
return err
272-
}
273-
*/
268+
274269
return err
275270
}
276271

0 commit comments

Comments
 (0)