Skip to content

Commit

Permalink
Added new data source azurerm_subscriptions
Browse files Browse the repository at this point in the history
  • Loading branch information
katbyte committed Mar 6, 2018
1 parent 3359ff5 commit 87229fb
Show file tree
Hide file tree
Showing 7 changed files with 174 additions and 34 deletions.
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(),
}
}

Expand Down
63 changes: 63 additions & 0 deletions azurerm/data_source_subscriptions.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
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)
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"),
),
},
},
})
}
40 changes: 40 additions & 0 deletions azurerm/helpers/subscription/subscription.go
Original file line number Diff line number Diff line change
@@ -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,
},
}
}
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" "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.

0 comments on commit 87229fb

Please sign in to comment.