-
Notifications
You must be signed in to change notification settings - Fork 4.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
Add the ability to do basic filtering with azurerm_subscriptions #2429
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,8 @@ package azurerm | |
|
||
import ( | ||
"fmt" | ||
"log" | ||
"strings" | ||
|
||
"github.com/hashicorp/terraform/helper/schema" | ||
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure" | ||
|
@@ -12,6 +14,14 @@ func dataSourceArmSubscriptions() *schema.Resource { | |
Read: dataSourceArmSubscriptionsRead, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"display_name_prefix": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
}, | ||
"display_name_contains": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
}, | ||
"subscriptions": { | ||
Type: schema.TypeList, | ||
Computed: true, | ||
|
@@ -28,17 +38,44 @@ func dataSourceArmSubscriptionsRead(d *schema.ResourceData, meta interface{}) er | |
subClient := armClient.subscriptionsClient | ||
ctx := armClient.StopContext | ||
|
||
displayNamePrefix := strings.ToLower(d.Get("display_name_prefix").(string)) | ||
displayNameContains := strings.ToLower(d.Get("display_name_contains").(string)) | ||
|
||
//ListComplete returns an iterator struct | ||
results, err := subClient.ListComplete(ctx) | ||
if err != nil { | ||
return fmt.Errorf("Error listing subscriptions: %+v", err) | ||
} | ||
|
||
//iterate across each subscriptions and append them to slice | ||
//check if the display name matches the 'prefix' comparison | ||
subscriptions := make([]map[string]interface{}, 0) | ||
for results.NotDone() { | ||
val := results.Value() | ||
|
||
//check if the display name prefix matches the given input | ||
if displayNamePrefix != "" { | ||
if !strings.HasPrefix(strings.ToLower(*val.DisplayName), displayNamePrefix) { | ||
//the display name does not match the given prefix | ||
log.Printf("[DEBUG][data_azurerm_subscriptions] %q does not match the prefix check %q", *val.DisplayName, displayNamePrefix) | ||
tombuildsstuff marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if err = results.Next(); err != nil { | ||
return fmt.Errorf("Error going to next subscriptions value: %+v", err) | ||
} | ||
continue | ||
} | ||
} | ||
|
||
//check if the display name matches the 'contains' comparison | ||
if displayNameContains != "" { | ||
if !strings.Contains(strings.ToLower(*val.DisplayName), displayNameContains) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. rather than iterating over the results multiple times, I'd suggest it'd be easiest to do this check here - what do you think? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I moved the check to the bottom and simplified it a bit (check against the slice val). This saved a few lines of code, but I'm not sure if this is what you meant. 🤓️ |
||
//the display name does not match the contains check | ||
log.Printf("[DEBUG][data_azurerm_subscriptions] %q does not match the contains check %q", *val.DisplayName, displayNameContains) | ||
tombuildsstuff marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if err = results.Next(); err != nil { | ||
return fmt.Errorf("Error going to next subscriptions value: %+v", err) | ||
} | ||
continue | ||
} | ||
} | ||
|
||
s := make(map[string]interface{}) | ||
|
||
if v := val.SubscriptionID; v != nil { | ||
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -26,7 +26,10 @@ output "first_available_subscription_display_name" { | |||||
|
||||||
## Argument Reference | ||||||
|
||||||
There are no arguments available for this data source. | ||||||
-> **NOTE** The comparisons are case-insensitive. | ||||||
tiwood marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
|
||||||
* `display_name_prefix` - (Optional) This argument can be used to only return subscriptions that start with this given string. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
* `display_name_contains` - (Optional) This argument can be used to only return subscriptions that contain the given string. | ||||||
tombuildsstuff marked this conversation as resolved.
Show resolved
Hide resolved
tombuildsstuff marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
|
||||||
## Attributes Reference | ||||||
|
||||||
|
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.
rather than iterating over the results multiple times, I'd suggest it'd be easiest to do this check here - what do you think?
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.
see above.