diff --git a/azurerm/data_source_subscription.go b/azurerm/data_source_subscription.go index dcf01b1d0f0d..e28320863fde 100644 --- a/azurerm/data_source_subscription.go +++ b/azurerm/data_source_subscription.go @@ -4,44 +4,13 @@ import ( "fmt" "github.com/hashicorp/terraform/helper/schema" + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/subscription" ) func dataSourceArmSubscription() *schema.Resource { return &schema.Resource{ - Read: dataSourceArmSubscriptionRead, - Schema: map[string]*schema.Schema{ - - "subscription_id": { - Type: schema.TypeString, - Optional: true, - Computed: true, - }, - - "display_name": { - Type: schema.TypeString, - Computed: true, - }, - - "state": { - Type: schema.TypeString, - Computed: true, - }, - - "location_placement_id": { - Type: schema.TypeString, - Computed: true, - }, - - "quota_id": { - Type: schema.TypeString, - Computed: true, - }, - - "spending_limit": { - Type: schema.TypeString, - Computed: true, - }, - }, + Read: dataSourceArmSubscriptionRead, + Schema: subscription.SubscriptionSchema(true), } } diff --git a/azurerm/data_source_subscriptions.go b/azurerm/data_source_subscriptions.go new file mode 100644 index 000000000000..de95b7035ba9 --- /dev/null +++ b/azurerm/data_source_subscriptions.go @@ -0,0 +1,71 @@ +package azurerm + +import ( + "fmt" + + "github.com/hashicorp/terraform/helper/schema" + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/subscription" +) + +func dataSourceArmSubscriptions() *schema.Resource { + return &schema.Resource{ + Read: dataSourceArmSubscriptionsRead, + + Schema: map[string]*schema.Schema{ + "subscriptions": { + Type: schema.TypeList, + Computed: true, + Elem: &schema.Resource{ + Schema: subscription.SubscriptionSchema(false), + }, + }, + }, + } +} + +func dataSourceArmSubscriptionsRead(d *schema.ResourceData, meta interface{}) error { + armClient := meta.(*ArmClient) + subClient := armClient.subscriptionsClient + ctx := armClient.StopContext + + //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 + subscriptions := make([]map[string]interface{}, 0) + for err = nil; results.NotDone(); err = results.Next() { + val := results.Value() + + s := make(map[string]interface{}) + + if v := val.SubscriptionID; v != nil { + s["subscription_id"] = *v + } + if v := val.DisplayName; v != nil { + s["display_name"] = *v + } + s["state"] = string(val.State) + + if policies := val.SubscriptionPolicies; policies != nil { + if v := policies.LocationPlacementID; v != nil { + s["location_placement_id"] = *v + } + if v := policies.QuotaID; v != nil { + s["quota_id"] = *v + } + s["spending_limit"] = string(policies.SpendingLimit) + } + + subscriptions = append(subscriptions, s) + } + + d.SetId("subscriptions-" + armClient.tenantId) + if err := d.Set("subscriptions", subscriptions); err != nil { + return fmt.Errorf("Error flattening `subscriptions`: %+v", err) + } + + return nil +} diff --git a/azurerm/data_source_subscriptions_test.go b/azurerm/data_source_subscriptions_test.go new file mode 100644 index 000000000000..6ea35934e64f --- /dev/null +++ b/azurerm/data_source_subscriptions_test.go @@ -0,0 +1,26 @@ +package azurerm + +import ( + "testing" + + "github.com/hashicorp/terraform/helper/resource" +) + +func TestAccDataSourceAzureRMSubscriptions_basic(t *testing.T) { + resourceName := "data.azurerm_subscriptions.current" + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + Steps: []resource.TestStep{ + { + Config: `data "azurerm_subscriptions" "current" {}`, + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttrSet(resourceName, "subscriptions.0.subscription_id"), + resource.TestCheckResourceAttrSet(resourceName, "subscriptions.0.display_name"), + resource.TestCheckResourceAttrSet(resourceName, "subscriptions.0.state"), + ), + }, + }, + }) +} diff --git a/azurerm/helpers/subscription/subscription.go b/azurerm/helpers/subscription/subscription.go new file mode 100644 index 000000000000..59f1331bd662 --- /dev/null +++ b/azurerm/helpers/subscription/subscription.go @@ -0,0 +1,42 @@ +package subscription + +import ( + "github.com/hashicorp/terraform/helper/schema" +) + +func SubscriptionSchema(subscriptionIDOptional bool) map[string]*schema.Schema { + s := map[string]*schema.Schema{ + "subscription_id": { + Type: schema.TypeString, + Optional: subscriptionIDOptional, + Computed: true, + }, + + "display_name": { + Type: schema.TypeString, + Computed: true, + }, + + "state": { + Type: schema.TypeString, + Computed: true, + }, + + "location_placement_id": { + Type: schema.TypeString, + Computed: true, + }, + + "quota_id": { + Type: schema.TypeString, + Computed: true, + }, + + "spending_limit": { + Type: schema.TypeString, + Computed: true, + }, + } + + return s +} diff --git a/azurerm/provider.go b/azurerm/provider.go index 623de61ed6eb..bb69450ada7d 100644 --- a/azurerm/provider.go +++ b/azurerm/provider.go @@ -96,6 +96,7 @@ func Provider() terraform.ResourceProvider { "azurerm_snapshot": dataSourceArmSnapshot(), "azurerm_subnet": dataSourceArmSubnet(), "azurerm_subscription": dataSourceArmSubscription(), + "azurerm_subscriptions": dataSourceArmSubscriptions(), "azurerm_virtual_network": dataSourceArmVirtualNetwork(), "azurerm_virtual_network_gateway": dataSourceArmVirtualNetworkGateway(), }, diff --git a/website/azurerm.erb b/website/azurerm.erb index d79d4923e665..d355562194ef 100644 --- a/website/azurerm.erb +++ b/website/azurerm.erb @@ -99,6 +99,10 @@ azurerm_subscription +