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 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
32 changes: 30 additions & 2 deletions azurerm/data_source_subscriptions.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package azurerm

import (
"fmt"
"strings"

"github.com/hashicorp/terraform/helper/schema"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure"
Expand All @@ -12,6 +13,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,6 +37,9 @@ 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 {
Expand Down Expand Up @@ -59,11 +71,27 @@ func dataSourceArmSubscriptionsRead(d *schema.ResourceData, meta interface{}) er
s["spending_limit"] = string(policies.SpendingLimit)
}

subscriptions = append(subscriptions, s)

if err = results.Next(); err != nil {
return fmt.Errorf("Error going to next subscriptions value: %+v", err)
}

//check if the display name prefix matches the given input
if displayNamePrefix != "" {
if !strings.HasPrefix(strings.ToLower(s["display_name"].(string)), displayNamePrefix) {
//the display name does not match the given prefix
continue
}
}

//check if the display name matches the 'contains' comparison
if displayNameContains != "" {
if !strings.Contains(strings.ToLower(s["display_name"].(string)), displayNameContains) {
//the display name does not match the contains check
continue
}
}

subscriptions = append(subscriptions, s)
}

d.SetId("subscriptions-" + armClient.tenantId)
Expand Down
3 changes: 2 additions & 1 deletion website/docs/d/subscriptions.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ output "first_available_subscription_display_name" {

## Argument Reference

There are no arguments available for this data source.
* `display_name_prefix` - (Optional) A case-insensitive prefix which can be used to filter on the `display_name` field
* `display_name_contains` - (Optional) A case-insensitive value which must be contained within the `display_name` field, used to filter the results

## Attributes Reference

Expand Down