-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #940 from terraform-providers/f-ds-subscriptions
New data source: azurerm_subscriptions
- Loading branch information
Showing
7 changed files
with
184 additions
and
34 deletions.
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. |