From 3633361ae9806d7aa35692277de173f8bc07ca25 Mon Sep 17 00:00:00 2001 From: kt Date: Tue, 6 Mar 2018 08:36:07 -0800 Subject: [PATCH 1/3] Added new data source azurerm_subscriptions --- azurerm/data_source_subscription.go | 37 +----------- azurerm/data_source_subscriptions.go | 62 ++++++++++++++++++++ azurerm/data_source_subscriptions_test.go | 26 ++++++++ azurerm/helpers/subscription/subscription.go | 40 +++++++++++++ azurerm/provider.go | 1 + website/azurerm.erb | 4 ++ website/docs/d/subscriptions.html.markdown | 37 ++++++++++++ 7 files changed, 173 insertions(+), 34 deletions(-) create mode 100644 azurerm/data_source_subscriptions.go create mode 100644 azurerm/data_source_subscriptions_test.go create mode 100644 azurerm/helpers/subscription/subscription.go create mode 100644 website/docs/d/subscriptions.html.markdown diff --git a/azurerm/data_source_subscription.go b/azurerm/data_source_subscription.go index dcf01b1d0f0d..80e0e620dc6f 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(), } } diff --git a/azurerm/data_source_subscriptions.go b/azurerm/data_source_subscriptions.go new file mode 100644 index 000000000000..d975e64f3ce5 --- /dev/null +++ b/azurerm/data_source_subscriptions.go @@ -0,0 +1,62 @@ +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(), + }, + }, + }, + } +} + +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{}) + + s["subscription_id"] = *val.SubscriptionID + s["display_name"] = *val.DisplayName + s["state"] = val.State + if policies := val.SubscriptionPolicies; policies != nil { + s["location_placement_id"] = *policies.LocationPlacementID + s["quota_id"] = *policies.QuotaID + s["spending_limit"] = 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..ecb18ad685b0 --- /dev/null +++ b/azurerm/helpers/subscription/subscription.go @@ -0,0 +1,40 @@ +package subscription + +import ( + "github.com/hashicorp/terraform/helper/schema" +) + +func SubscriptionSchema() map[string]*schema.Schema { + return 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, + }, + } +} 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 + > + azurerm_subscriptions + + > azurerm_virtual_network diff --git a/website/docs/d/subscriptions.html.markdown b/website/docs/d/subscriptions.html.markdown new file mode 100644 index 000000000000..fe6867958897 --- /dev/null +++ b/website/docs/d/subscriptions.html.markdown @@ -0,0 +1,37 @@ +--- +layout: "azurerm" +page_title: "Azure Resource Manager: azurerm_subscriptions" +sidebar_current: "docs-azurerm-datasource-subscriptions" +description: |- + Get information about the available subscriptions. +--- + +# Data Source: azurerm_subscription + +Use this data source to access a list of all Azure subscription currently available. + +## Example Usage + +```hcl +data "azurerm_subscriptions" "availible" {} + +output "availible_subscriptions" { + value = "${data.azurerm_subscriptions.current.subscriptions}" +} + +output "first_availible_subscription_display_name" { + value = "${data.azurerm_subscriptions.current.subscriptions.0.display_name}" +} +``` + +## Attributes Reference + +* `subscriptions` - One or more `subscription` blocks as defined below. + + +The `subscription` block contains: +* `display_name` - The subscription display name. +* `state` - The subscription state. Possible values are Enabled, Warned, PastDue, Disabled, and Deleted. +* `location_placement_id` - The subscription location placement ID. +* `quota_id` - The subscription quota ID. +* `spending_limit` - The subscription spending limit. From afb88c6ed159bea106977578554f82b10fe6e16d Mon Sep 17 00:00:00 2001 From: kt Date: Tue, 6 Mar 2018 10:48:23 -0800 Subject: [PATCH 2/3] In the Subscriptions datasource: Fixed some typos, added some nil checks and corrected schema --- azurerm/data_source_subscription.go | 2 +- azurerm/data_source_subscriptions.go | 25 +++++++++++++------- azurerm/helpers/subscription/subscription.go | 8 ++++--- website/docs/d/subscriptions.html.markdown | 6 ++--- 4 files changed, 26 insertions(+), 15 deletions(-) diff --git a/azurerm/data_source_subscription.go b/azurerm/data_source_subscription.go index 80e0e620dc6f..e28320863fde 100644 --- a/azurerm/data_source_subscription.go +++ b/azurerm/data_source_subscription.go @@ -10,7 +10,7 @@ import ( func dataSourceArmSubscription() *schema.Resource { return &schema.Resource{ Read: dataSourceArmSubscriptionRead, - Schema: subscription.SubscriptionSchema(), + Schema: subscription.SubscriptionSchema(true), } } diff --git a/azurerm/data_source_subscriptions.go b/azurerm/data_source_subscriptions.go index d975e64f3ce5..de95b7035ba9 100644 --- a/azurerm/data_source_subscriptions.go +++ b/azurerm/data_source_subscriptions.go @@ -16,7 +16,7 @@ func dataSourceArmSubscriptions() *schema.Resource { Type: schema.TypeList, Computed: true, Elem: &schema.Resource{ - Schema: subscription.SubscriptionSchema(), + Schema: subscription.SubscriptionSchema(false), }, }, }, @@ -41,15 +41,24 @@ func dataSourceArmSubscriptionsRead(d *schema.ResourceData, meta interface{}) er s := make(map[string]interface{}) - s["subscription_id"] = *val.SubscriptionID - s["display_name"] = *val.DisplayName - s["state"] = val.State + 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 { - s["location_placement_id"] = *policies.LocationPlacementID - s["quota_id"] = *policies.QuotaID - s["spending_limit"] = policies.SpendingLimit + 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) } diff --git a/azurerm/helpers/subscription/subscription.go b/azurerm/helpers/subscription/subscription.go index ecb18ad685b0..fc94f3986a12 100644 --- a/azurerm/helpers/subscription/subscription.go +++ b/azurerm/helpers/subscription/subscription.go @@ -4,11 +4,11 @@ import ( "github.com/hashicorp/terraform/helper/schema" ) -func SubscriptionSchema() map[string]*schema.Schema { - return map[string]*schema.Schema{ +func SubscriptionSchema(subscriptionIdOptional bool) map[string]*schema.Schema { + s := map[string]*schema.Schema{ "subscription_id": { Type: schema.TypeString, - Optional: true, + Optional: subscriptionIdOptional, Computed: true, }, @@ -37,4 +37,6 @@ func SubscriptionSchema() map[string]*schema.Schema { Computed: true, }, } + + return s } diff --git a/website/docs/d/subscriptions.html.markdown b/website/docs/d/subscriptions.html.markdown index fe6867958897..a553c5a21b4e 100644 --- a/website/docs/d/subscriptions.html.markdown +++ b/website/docs/d/subscriptions.html.markdown @@ -13,13 +13,13 @@ Use this data source to access a list of all Azure subscription currently availa ## Example Usage ```hcl -data "azurerm_subscriptions" "availible" {} +data "azurerm_subscriptions" "available" {} -output "availible_subscriptions" { +output "available_subscriptions" { value = "${data.azurerm_subscriptions.current.subscriptions}" } -output "first_availible_subscription_display_name" { +output "first_available_subscription_display_name" { value = "${data.azurerm_subscriptions.current.subscriptions.0.display_name}" } ``` From 73053ead7b9b3f3c8d2c9cf326464fc5cf084251 Mon Sep 17 00:00:00 2001 From: kt Date: Tue, 6 Mar 2018 11:08:16 -0800 Subject: [PATCH 3/3] Subscriptions datasource: go naming convention fix --- azurerm/helpers/subscription/subscription.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/azurerm/helpers/subscription/subscription.go b/azurerm/helpers/subscription/subscription.go index fc94f3986a12..59f1331bd662 100644 --- a/azurerm/helpers/subscription/subscription.go +++ b/azurerm/helpers/subscription/subscription.go @@ -4,11 +4,11 @@ import ( "github.com/hashicorp/terraform/helper/schema" ) -func SubscriptionSchema(subscriptionIdOptional bool) map[string]*schema.Schema { +func SubscriptionSchema(subscriptionIDOptional bool) map[string]*schema.Schema { s := map[string]*schema.Schema{ "subscription_id": { Type: schema.TypeString, - Optional: subscriptionIdOptional, + Optional: subscriptionIDOptional, Computed: true, },