-
Notifications
You must be signed in to change notification settings - Fork 4.6k
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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"), | ||
), | ||
}, | ||
}, | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}, | ||
} | ||
|
||
return s | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
do you think it's also worth exposing the TenantID here?
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.
The tenant will always be what the provider is using and accessible from
client_config
data source.