Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ds/ec2_instance_type_offering locations: Add locations attribute #16704

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.