Skip to content

Commit

Permalink
Merge pull request #16704 from orgito/ds/ec2_instance_type_offering-l…
Browse files Browse the repository at this point in the history
…ocations

ds/ec2_instance_type_offering locations: Add locations attribute
  • Loading branch information
ewbankkit committed Jul 23, 2021
2 parents 0d5f9db + 383202e commit 2cff58c
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 21 deletions.
3 changes: 3 additions & 0 deletions .changelog/16704.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
data-source/aws_ec2_instance_type_offerings: Add `locations` and `location_types` attributes
```
52 changes: 31 additions & 21 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.TypeSet,
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.TypeSet,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
},
}
Expand All @@ -47,36 +53,40 @@ 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: %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
24 changes: 24 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 @@ -42,6 +42,7 @@ func TestAccAWSEc2InstanceTypeOfferingsDataSource_LocationType(t *testing.T) {
Config: testAccAWSEc2InstanceTypeOfferingsDataSourceConfigLocationType(),
Check: resource.ComposeTestCheckFunc(
testAccCheckEc2InstanceTypeOfferingsInstanceTypes(dataSourceName),
testAccCheckEc2InstanceTypeOfferingsLocations(dataSourceName),
),
},
},
Expand All @@ -59,6 +60,29 @@ 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
}
}

func testAccCheckEc2InstanceTypeOfferingsLocations(dataSourceName string) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[dataSourceName]
if !ok {
return fmt.Errorf("Not found: %s", dataSourceName)
}

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

return nil
}
}
Expand Down
2 changes: 2 additions & 0 deletions website/docs/d/ec2_instance_type_offerings.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,5 @@ In addition to all arguments above, the following attributes are exported:

* `id` - AWS Region.
* `instance_types` - Set of EC2 Instance Types.
* `locations` - Set of locations.
* `location_types` - Set of location types.

0 comments on commit 2cff58c

Please sign in to comment.