Skip to content

Commit d7e00bb

Browse files
AWS better check and election (#143)
Co-authored-by: DavidGOrtega <g.ortega.david@gmail.com> Co-authored-by: 0x2b3bfa0 <0x2b3bfa0+git@googlemail.com>
1 parent 0c60583 commit d7e00bb

File tree

4 files changed

+58
-8
lines changed

4 files changed

+58
-8
lines changed

iterative/aws/provider.go

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ func ResourceMachineCreate(ctx context.Context, d *schema.ResourceData, m interf
2626
instanceName := d.Get("name").(string)
2727
pairName := d.Id()
2828
hddSize := d.Get("instance_hdd_size").(int)
29-
region := getRegion(d.Get("region").(string))
29+
region := GetRegion(d.Get("region").(string))
3030
instanceType := getInstanceType(d.Get("instance_type").(string), d.Get("instance_gpu").(string))
3131
ami := d.Get("image").(string)
3232
keyPublic := d.Get("ssh_public").(string)
@@ -38,7 +38,6 @@ func ResourceMachineCreate(ctx context.Context, d *schema.ResourceData, m interf
3838
}
3939

4040
svc, err := awsClient(region)
41-
4241
if err != nil {
4342
return decodeAWSError(region, err)
4443
}
@@ -70,6 +69,10 @@ func ResourceMachineCreate(ctx context.Context, d *schema.ResourceData, m interf
7069
Name: aws.String("architecture"),
7170
Values: []*string{aws.String("x86_64")},
7271
},
72+
{
73+
Name: aws.String("owner-id"),
74+
Values: []*string{aws.String("099720109477")},
75+
},
7376
},
7477
})
7578

@@ -101,11 +104,19 @@ func ResourceMachineCreate(ctx context.Context, d *schema.ResourceData, m interf
101104
securityGroup = "iterative"
102105

103106
vpcsDesc, _ := svc.DescribeVpcs(&ec2.DescribeVpcsInput{})
107+
104108
if len(vpcsDesc.Vpcs) == 0 {
105109
return errors.New("no VPCs found")
106110
}
107111
vpcID = *vpcsDesc.Vpcs[0].VpcId
108112

113+
for _, vpc := range vpcsDesc.Vpcs {
114+
if *vpc.IsDefault {
115+
vpcID = *vpc.VpcId
116+
break
117+
}
118+
}
119+
109120
gpResult, err := svc.CreateSecurityGroup(&ec2.CreateSecurityGroupInput{
110121
GroupName: aws.String(securityGroup),
111122
Description: aws.String("Iterative security group"),
@@ -177,6 +188,16 @@ func ResourceMachineCreate(ctx context.Context, d *schema.ResourceData, m interf
177188
if len(subDesc.Subnets) == 0 {
178189
return errors.New("no subnets found")
179190
}
191+
var subnetID string
192+
for _, subnet := range subDesc.Subnets {
193+
if *subnet.AvailableIpAddressCount > 0 && *subnet.MapPublicIpOnLaunch {
194+
subnetID = *subnet.SubnetId
195+
break
196+
}
197+
}
198+
if subnetID == "" {
199+
return errors.New("No subnet found with public IPs available or able to create new public IPs on creation")
200+
}
180201

181202
blockDeviceMappings := []*ec2.BlockDeviceMapping{
182203
{
@@ -200,7 +221,7 @@ func ResourceMachineCreate(ctx context.Context, d *schema.ResourceData, m interf
200221
KeyName: aws.String(pairName),
201222
InstanceType: aws.String(instanceType),
202223
SecurityGroupIds: []*string{aws.String(sgID)},
203-
SubnetId: aws.String(*subDesc.Subnets[0].SubnetId),
224+
SubnetId: aws.String(subnetID),
204225
BlockDeviceMappings: blockDeviceMappings,
205226
},
206227
InstanceCount: aws.Int64(1),
@@ -287,14 +308,15 @@ func ResourceMachineCreate(ctx context.Context, d *schema.ResourceData, m interf
287308
instanceDesc := descResult.Reservations[0].Instances[0]
288309
d.Set("instance_ip", instanceDesc.PublicIpAddress)
289310
d.Set("instance_launch_time", instanceDesc.LaunchTime.Format(time.RFC3339))
311+
d.Set("image", *imagesRes.Images[0].Name)
290312

291313
return nil
292314
}
293315

294316
//ResourceMachineDelete deletes AWS instance
295317
func ResourceMachineDelete(ctx context.Context, d *schema.ResourceData, m interface{}) error {
296318
id := aws.String(d.Id())
297-
region := getRegion(d.Get("region").(string))
319+
region := GetRegion(d.Get("region").(string))
298320

299321
svc, err := awsClient(region)
300322
if err != nil {
@@ -359,7 +381,8 @@ var ImageRegions = []string{
359381
"sa-east-1",
360382
}
361383

362-
func getRegion(region string) string {
384+
//GetRegion maps region to real cloud regions
385+
func GetRegion(region string) string {
363386
instanceRegions := make(map[string]string)
364387
instanceRegions["us-east"] = "us-east-1"
365388
instanceRegions["us-west"] = "us-west-1"

iterative/azure/provider.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ func ResourceMachineCreate(ctx context.Context, d *schema.ResourceData, m interf
3030

3131
username := "ubuntu"
3232
customData := d.Get("startup_script").(string)
33-
region := getRegion(d.Get("region").(string))
33+
region := GetRegion(d.Get("region").(string))
3434
instanceType := getInstanceType(d.Get("instance_type").(string), d.Get("instance_gpu").(string))
3535
keyPublic := d.Get("ssh_public").(string)
3636
hddSize := int32(d.Get("instance_hdd_size").(int))
@@ -354,7 +354,8 @@ func getVMClient(subscriptionID string) (compute.VirtualMachinesClient, error) {
354354
//ImageRegions provider available image regions
355355
var ImageRegions = []string{}
356356

357-
func getRegion(region string) string {
357+
//GetRegion maps region to real cloud regions
358+
func GetRegion(region string) string {
358359
instanceRegions := make(map[string]string)
359360
instanceRegions["us-east"] = "eastus"
360361
instanceRegions["us-west"] = "westus2"

iterative/resource_runner.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -437,8 +437,16 @@ func isAMIAvailable(cloud string, region string) bool {
437437
regions = azure.ImageRegions
438438
}
439439

440+
var cloudRegion string
441+
switch cloud {
442+
case "aws":
443+
cloudRegion = aws.GetRegion(region)
444+
case "azure":
445+
cloudRegion = azure.GetRegion(region)
446+
}
447+
440448
for _, item := range regions {
441-
if item == region {
449+
if item == cloudRegion {
442450
return true
443451
}
444452
}

iterative/resource_runner_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,15 @@ func TestScript(t *testing.T) {
2121
assert.NotContains(t, script, "sudo ubuntu-drivers autoinstall")
2222
})
2323

24+
t.Run("AWS known generic region should not add the NVIDA drivers", func(t *testing.T) {
25+
data := make(map[string]interface{})
26+
data["ami"] = isAMIAvailable("aws", "us-west")
27+
28+
script, err := renderScript(data)
29+
assert.Nil(t, err)
30+
assert.NotContains(t, script, "sudo ubuntu-drivers autoinstall")
31+
})
32+
2433
t.Run("AWS unknown region should add the NVIDA drivers", func(t *testing.T) {
2534
data := make(map[string]interface{})
2635
data["ami"] = isAMIAvailable("aws", "us-east-99")
@@ -39,6 +48,15 @@ func TestScript(t *testing.T) {
3948
assert.Contains(t, script, "sudo ubuntu-drivers autoinstall")
4049
})
4150

51+
t.Run("Azure known generic region should add the NVIDA drivers", func(t *testing.T) {
52+
data := make(map[string]interface{})
53+
data["ami"] = isAMIAvailable("azure", "us-west")
54+
55+
script, err := renderScript(data)
56+
assert.Nil(t, err)
57+
assert.Contains(t, script, "sudo ubuntu-drivers autoinstall")
58+
})
59+
4260
t.Run("Azure unknown region should add the NVIDA drivers", func(t *testing.T) {
4361
data := make(map[string]interface{})
4462
data["ami"] = isAMIAvailable("azure", "us-east-99")

0 commit comments

Comments
 (0)