-
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
New data source: azurerm_subscriptions #940
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 |
---|---|---|
@@ -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 | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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"), | ||
), | ||
}, | ||
}, | ||
}) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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, | ||
}, | ||
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. do you think it's also worth exposing the TenantID here? 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. The tenant will always be what the provider is using and accessible from |
||
} | ||
|
||
return s | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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" "available" {} | ||
|
||
output "available_subscriptions" { | ||
value = "${data.azurerm_subscriptions.current.subscriptions}" | ||
} | ||
|
||
output "first_available_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. |
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.
super minor, but use
ID
(see https://github.com/golang/go/wiki/CodeReviewComments#initialisms)