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

Add the ability to do basic filtering with azurerm_subscriptions #2429

Merged
merged 4 commits into from
Dec 4, 2018
Merged
Show file tree
Hide file tree
Changes from 2 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
39 changes: 38 additions & 1 deletion azurerm/data_source_subscriptions.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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,
Expand All @@ -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) {
Copy link
Contributor

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?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

see above.

//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) {
Copy link
Contributor

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?

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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 {
Expand Down
5 changes: 4 additions & 1 deletion website/docs/d/subscriptions.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* `display_name_prefix` - (Optional) This argument can be used to only return subscriptions that start with this given string.
* `display_name_prefix` - (Optional) A case-insensitive prefix which can be used to filter on the `display_name` field

* `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

Expand Down