Skip to content
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 3 commits into from
Mar 6, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 3 additions & 34 deletions azurerm/data_source_subscription.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
}
}

Expand Down
71 changes: 71 additions & 0 deletions azurerm/data_source_subscriptions.go
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
}
26 changes: 26 additions & 0 deletions azurerm/data_source_subscriptions_test.go
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"),
),
},
},
})
}
42 changes: 42 additions & 0 deletions azurerm/helpers/subscription/subscription.go
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,
},
Copy link
Contributor

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?

Copy link
Collaborator Author

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.

}

return s
}
1 change: 1 addition & 0 deletions azurerm/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
},
Expand Down
4 changes: 4 additions & 0 deletions website/azurerm.erb
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,10 @@
<a href="/docs/providers/azurerm/d/subscription.html">azurerm_subscription</a>
</li>

<li<%= sidebar_current("docs-azurerm-datasource-subscriptions") %>>
<a href="/docs/providers/azurerm/d/subscriptions.html">azurerm_subscriptions</a>
</li>

<li<%= sidebar_current("docs-azurerm-datasource-virtual-network-x") %>>
<a href="/docs/providers/azurerm/d/virtual_network.html">azurerm_virtual_network</a>
</li>
Expand Down
37 changes: 37 additions & 0 deletions website/docs/d/subscriptions.html.markdown
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.