-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
feat: add data source google_compute_addresses #6648
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
e02ec79
feat: add data_source google_compute_addresses
xabufr 06a378f
tests: data_source google_compute_addresses
xabufr e0101eb
tests: template google_compute_addresses
xabufr d14996f
docs: add descriptions on some fields
xabufr ff29b12
fix: add missing header in tests
xabufr b4e771d
tests: condition labels for compute addresses
xabufr 72753b6
docs: add for data google_compute_addresse
xabufr File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
205 changes: 205 additions & 0 deletions
205
mmv1/third_party/terraform/data_sources/data_source_google_compute_addresses.go.erb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,205 @@ | ||
<% autogen_exception -%> | ||
package google | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/v2/diag" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
<% if version == "ga" -%> | ||
"google.golang.org/api/compute/v1" | ||
<% else -%> | ||
compute "google.golang.org/api/compute/v0.beta" | ||
<% end -%> | ||
) | ||
|
||
func dataSourceGoogleComputeAddresses() *schema.Resource { | ||
return &schema.Resource{ | ||
ReadContext: dataSourceGoogleComputeAddressesRead, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"addresses": { | ||
Type: schema.TypeList, | ||
Computed: true, | ||
Elem: &schema.Resource{ | ||
Schema: map[string]*schema.Schema{ | ||
"name": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
Description: `The name of the IP address.`, | ||
}, | ||
"address": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
Description: `The IP address.`, | ||
}, | ||
"address_type": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
Description: `The IP address type.`, | ||
}, | ||
"description": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"status": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"region": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
Optional: true, | ||
}, | ||
"self_link": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
<% unless version == 'ga' -%> | ||
"labels": { | ||
Type: schema.TypeMap, | ||
Elem: &schema.Schema{ | ||
Type: schema.TypeString, | ||
}, | ||
Description: `Labels attached to this address`, | ||
Computed: true, | ||
}, | ||
<% end -%> | ||
}, | ||
}, | ||
}, | ||
|
||
"filter": { | ||
Type: schema.TypeString, | ||
Description: `Filter sets the optional parameter "filter": A filter expression that | ||
filters resources listed in the response. The expression must specify | ||
the field name, an operator, and the value that you want to use for | ||
filtering. The value must be a string, a number, or a boolean. The | ||
operator must be either "=", "!=", ">", "<", "<=", ">=" or ":". For | ||
example, if you are filtering Compute Engine instances, you can | ||
exclude instances named "example-instance" by specifying "name != | ||
example-instance". The ":" operator can be used with string fields to | ||
match substrings. For non-string fields it is equivalent to the "=" | ||
operator. The ":*" comparison can be used to test whether a key has | ||
been defined. For example, to find all objects with "owner" label | ||
use: """ labels.owner:* """ You can also filter nested fields. For | ||
example, you could specify "scheduling.automaticRestart = false" to | ||
include instances only if they are not scheduled for automatic | ||
restarts. You can use filtering on nested fields to filter based on | ||
resource labels. To filter on multiple expressions, provide each | ||
separate expression within parentheses. For example: """ | ||
(scheduling.automaticRestart = true) (cpuPlatform = "Intel Skylake") | ||
""" By default, each expression is an "AND" expression. However, you | ||
can include "AND" and "OR" expressions explicitly. For example: """ | ||
(cpuPlatform = "Intel Skylake") OR (cpuPlatform = "Intel Broadwell") | ||
AND (scheduling.automaticRestart = true) """`, | ||
Optional: true, | ||
}, | ||
|
||
"region": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
Description: `Region that should be considered to search addresses. All regions are considered if missing.`, | ||
}, | ||
|
||
"project": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
Optional: true, | ||
Description: `The google project in which addresses are listed. Defaults to provider's configuration if missing.`, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func dataSourceGoogleComputeAddressesRead(context context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { | ||
config := meta.(*Config) | ||
userAgent, err := generateUserAgentString(d, config.userAgent) | ||
if err != nil { | ||
return diag.FromErr(err) | ||
} | ||
|
||
project, err := getProject(d, config) | ||
if err != nil { | ||
return diag.FromErr(err) | ||
} | ||
|
||
allAddresses := make([]map[string]interface{}, 0) | ||
|
||
client := config.NewComputeClient(userAgent).Addresses | ||
if region, has_region := d.GetOk("region"); has_region { | ||
request := client.List(project, region.(string)) | ||
if filter, has_filter := d.GetOk("filter"); has_filter { | ||
request = request.Filter(filter.(string)) | ||
} | ||
err = request.Pages(context, func(addresses *compute.AddressList) error { | ||
for _, address := range addresses.Items { | ||
allAddresses = append(allAddresses, generateTfAddress(address)) | ||
} | ||
return nil | ||
}) | ||
} else { | ||
request := client.AggregatedList(project) | ||
if filter, has_filter := d.GetOk("filter"); has_filter { | ||
request = request.Filter(filter.(string)) | ||
} | ||
err = request.Pages(context, func(addresses *compute.AddressAggregatedList) error { | ||
for _, items := range addresses.Items { | ||
for _, address := range items.Addresses { | ||
allAddresses = append(allAddresses, generateTfAddress(address)) | ||
} | ||
} | ||
return nil | ||
}) | ||
} | ||
if err != nil { | ||
return diag.FromErr(err) | ||
} | ||
|
||
if err := d.Set("addresses", allAddresses); err != nil { | ||
return diag.FromErr(fmt.Errorf("error setting addresses: %s", err)) | ||
} | ||
|
||
if err := d.Set("project", project); err != nil { | ||
return diag.FromErr(fmt.Errorf("error setting project: %s", err)) | ||
} | ||
d.SetId(computeId(project, d)) | ||
return nil | ||
} | ||
|
||
func generateTfAddress(address *compute.Address) map[string]interface{} { | ||
return map[string]interface{}{ | ||
"name": address.Name, | ||
"address": address.Address, | ||
"address_type": address.AddressType, | ||
"description": address.Description, | ||
"region": regionFromUrl(address.Region), | ||
"status": address.Status, | ||
"self_link": address.SelfLink, | ||
<% unless version == 'ga' -%> | ||
"labels": address.Labels, | ||
<% end -%> | ||
} | ||
} | ||
|
||
func computeId(project string, d *schema.ResourceData) string { | ||
region := "ALL" | ||
filter := "ALL" | ||
if p_region, has_region := d.GetOk("region"); has_region { | ||
region = p_region.(string) | ||
} | ||
if p_filter, has_filter := d.GetOk("filter"); has_filter { | ||
filter = p_filter.(string) | ||
} | ||
return fmt.Sprintf("%s-%s-%s", project, region, filter) | ||
} | ||
|
||
func regionFromUrl(url string) string { | ||
parts := strings.Split(url, "/") | ||
if count := len(parts); count > 0 { | ||
return parts[count-1] | ||
} | ||
return "" | ||
} |
189 changes: 189 additions & 0 deletions
189
mmv1/third_party/terraform/tests/data_source_google_compute_addresses_test.go.erb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,189 @@ | ||
<% autogen_exception -%> | ||
package google | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform" | ||
) | ||
|
||
func TestAccDataSourceComputeAddresses(t *testing.T) { | ||
t.Parallel() | ||
|
||
addressName := fmt.Sprintf("tf-test-%s", randString(t, 10)) | ||
|
||
region := "europe-west8" | ||
region_bis := "asia-east1" | ||
dsName := "regional_addresses" | ||
dsFullName := fmt.Sprintf("data.google_compute_addresses.%s", dsName) | ||
dsAllName := "all_addresses" | ||
dsAllFullName := fmt.Sprintf("data.google_compute_addresses.%s", dsAllName) | ||
|
||
vcrTest(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccDataSourceComputeAddressesConfig(addressName, region, region_bis), | ||
Check: resource.ComposeTestCheckFunc( | ||
testAccDataSourceComputeAddressesRegionSpecificCheck(t, addressName, dsFullName, region), | ||
testAccDataSourceComputeAddressesAllRegionsCheck(t, addressName, dsAllFullName, region, region_bis), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func testAccDataSourceComputeAddressesAllRegionsCheck(t *testing.T, address_name string, data_source_name string, expected_region string, expected_region_bis string) resource.TestCheckFunc { | ||
return func(s *terraform.State) error { | ||
expected_addresses := buildAddressesList(3, address_name, expected_region) | ||
expected_addresses = append(expected_addresses, buildAddressesList(3, address_name, expected_region_bis)...) | ||
|
||
return testDataSourceAdressContains(s, data_source_name, expected_addresses) | ||
} | ||
} | ||
|
||
func testAccDataSourceComputeAddressesRegionSpecificCheck(t *testing.T, address_name string, data_source_name string, expected_region string) resource.TestCheckFunc { | ||
return func(s *terraform.State) error { | ||
expected_addresses := buildAddressesList(3, address_name, expected_region) | ||
return testDataSourceAdressContains(s, data_source_name, expected_addresses) | ||
} | ||
} | ||
|
||
func testAccDataSourceComputeAddressesConfig(addressName, region, region_bis string) string { | ||
return fmt.Sprintf(` | ||
locals { | ||
region = "%s" | ||
region_bis = "%s" | ||
address_name = "%s" | ||
} | ||
|
||
resource "google_compute_address" "address" { | ||
count = 3 | ||
|
||
region = local.region | ||
name = "${local.address_name}-${local.region}-${count.index}" | ||
<% unless version == 'ga' -%> | ||
labels = { | ||
mykey = "myvalue" | ||
} | ||
<% end -%> | ||
} | ||
|
||
resource "google_compute_address" "address_region_bis" { | ||
count = 3 | ||
|
||
region = local.region_bis | ||
name = "${local.address_name}-${local.region_bis}-${count.index}" | ||
<% unless version == 'ga' -%> | ||
labels = { | ||
mykey = "myvalue" | ||
} | ||
<% end -%> | ||
} | ||
|
||
data "google_compute_addresses" "regional_addresses" { | ||
filter = "name:${local.address_name}-*" | ||
depends_on = [google_compute_address.address] | ||
region = local.region | ||
} | ||
|
||
data "google_compute_addresses" "all_addresses" { | ||
filter = "name:${local.address_name}-*" | ||
depends_on = [google_compute_address.address, google_compute_address.address_region_bis] | ||
} | ||
`, region, region_bis, addressName) | ||
} | ||
|
||
type expectedAddress struct { | ||
name string | ||
region string | ||
} | ||
|
||
func (r expectedAddress) checkAddressMatch(index int, attrs map[string]string) (bool, error) { | ||
map_name := fmt.Sprintf("addresses.%d.name", index) | ||
address_name := attrs[map_name] | ||
|
||
if address_name != r.name { | ||
return false, nil | ||
} | ||
|
||
map_region := fmt.Sprintf("addresses.%d.region", index) | ||
region, found := attrs[map_region] | ||
if !found { | ||
return false, fmt.Errorf("%s doesn't exists", map_region) | ||
} | ||
if region != r.region { | ||
return false, fmt.Errorf("Unexpected region: got %s expected %s", region, r.region) | ||
} | ||
|
||
<% unless version == 'ga' -%> | ||
map_label := fmt.Sprintf("addresses.%d.labels.mykey", index) | ||
label_value, found := attrs[map_label] | ||
if !found { | ||
return false, fmt.Errorf("label with key 'mykey' not found for %s", address_name) | ||
} | ||
if label_value != "myvalue" { | ||
return false, fmt.Errorf("label value of 'mykey' not equal to 'myvalue' for %s, got %s", address_name, label_value) | ||
} | ||
<% end -%> | ||
|
||
return true, nil | ||
} | ||
|
||
func testDataSourceAdressContains(state *terraform.State, data_source_name string, addresses []expectedAddress) error { | ||
ds, ok := state.RootModule().Resources[data_source_name] | ||
if !ok { | ||
return fmt.Errorf("root module has no resource called %s", data_source_name) | ||
} | ||
|
||
ds_attr := ds.Primary.Attributes | ||
|
||
addresses_length := len(addresses) | ||
|
||
if ds_attr["addresses.#"] != fmt.Sprintf("%d", addresses_length) { | ||
return fmt.Errorf("addresses.# is not equal to %d", addresses_length) | ||
} | ||
|
||
for address_index := 0; address_index < addresses_length; address_index++ { | ||
has_match := false | ||
for j := 0; j < len(addresses); j++ { | ||
match, err := addresses[j].checkAddressMatch(address_index, ds_attr) | ||
if err != nil { | ||
return err | ||
} else { | ||
if match { | ||
has_match = true | ||
addresses = removeExpectedAddress(addresses, j) | ||
break | ||
} | ||
} | ||
} | ||
if !has_match { | ||
return fmt.Errorf("unexpected address at index %d", address_index) // TODO improve | ||
} | ||
} | ||
|
||
if len(addresses) != 0 { | ||
return fmt.Errorf("%+v not found in data source", addresses) | ||
} | ||
return nil | ||
} | ||
|
||
func buildAddressesList(numberofAddresses int, addressName string, region string) []expectedAddress { | ||
var addresses []expectedAddress | ||
for i := 0; i < numberofAddresses; i++ { | ||
addresses = append(addresses, expectedAddress{ | ||
name: fmt.Sprintf("%s-%s-%d", addressName, region, i), | ||
region: region, | ||
}) | ||
} | ||
return addresses | ||
} | ||
|
||
func removeExpectedAddress(s []expectedAddress, i int) []expectedAddress { | ||
s[i] = s[len(s)-1] | ||
return s[:len(s)-1] | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is labels beta-only? The tests include labels for addresses, will this cause problems?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Labels for compute addresses are still in beta yes, yes I forgot to condition labels in tests resource, I'll fix thanks!