Skip to content

Commit

Permalink
Added locations and location_types to the output. #15272
Browse files Browse the repository at this point in the history
  • Loading branch information
mjulianotq authored and ewbankkit committed Jul 23, 2021
1 parent 1a15bd2 commit abc67dc
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 23 deletions.
50 changes: 27 additions & 23 deletions aws/data_source_aws_ec2_instance_type_offerings.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,20 @@ func dataSourceAwsEc2InstanceTypeOfferings() *schema.Resource {
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"locations": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"location_type": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.StringInSlice([]string{
ec2.LocationTypeAvailabilityZone,
ec2.LocationTypeAvailabilityZoneId,
ec2.LocationTypeRegion,
}, false),
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.StringInSlice(ec2.LocationType_Values(), false),
},
"location_types": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
},
}
Expand All @@ -48,40 +54,38 @@ func dataSourceAwsEc2InstanceTypeOfferingsRead(d *schema.ResourceData, meta inte

var instanceTypes []string
var locations []string
var locationTypes []string

for {
output, err := conn.DescribeInstanceTypeOfferings(input)

if err != nil {
return fmt.Errorf("error reading EC2 Instance Type Offerings: %w", err)
}

if output == nil {
break
err := conn.DescribeInstanceTypeOfferingsPages(input, func(page *ec2.DescribeInstanceTypeOfferingsOutput, lastPage bool) bool {
if page == nil {
return !lastPage
}

for _, instanceTypeOffering := range output.InstanceTypeOfferings {
for _, instanceTypeOffering := range page.InstanceTypeOfferings {
if instanceTypeOffering == nil {
continue
}

instanceTypes = append(instanceTypes, aws.StringValue(instanceTypeOffering.InstanceType))
locations = append(locations, aws.StringValue(instanceTypeOffering.Location))
locationTypes = append(locationTypes, aws.StringValue(instanceTypeOffering.LocationType))
}

if aws.StringValue(output.NextToken) == "" {
break
}
return !lastPage
})

input.NextToken = output.NextToken
if err != nil {
return fmt.Errorf("error reading EC2 Instance Type Offerings: %w", err)
}

if err := d.Set("instance_types", instanceTypes); err != nil {
return fmt.Errorf("error setting instance_types: %w", err)
}

if err := d.Set("locations", locations); err != nil {
return fmt.Errorf("error setting locations: %s", err)
return fmt.Errorf("error setting locations: %w", err)
}
if err := d.Set("location_types", locationTypes); err != nil {
return fmt.Errorf("error setting location_types: %w", err)
}

d.SetId(meta.(*AWSClient).region)
Expand Down
8 changes: 8 additions & 0 deletions aws/data_source_aws_ec2_instance_type_offerings_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,14 @@ func testAccCheckEc2InstanceTypeOfferingsInstanceTypes(dataSourceName string) re
return fmt.Errorf("expected at least one instance_types result, got none")
}

if v := rs.Primary.Attributes["locations.#"]; v == "0" {
return fmt.Errorf("expected at least one locations result, got none")
}

if v := rs.Primary.Attributes["location_types.#"]; v == "0" {
return fmt.Errorf("expected at least one location_types result, got none")
}

return nil
}
}
Expand Down

0 comments on commit abc67dc

Please sign in to comment.