Skip to content

Commit

Permalink
Merge pull request #285 from ewbankkit/azurerm_subscription-data-source
Browse files Browse the repository at this point in the history
New data source: azurerm_subscription
  • Loading branch information
tombuildsstuff authored Aug 30, 2017
2 parents b632e57 + d891bd4 commit 0d450a1
Show file tree
Hide file tree
Showing 12 changed files with 801 additions and 0 deletions.
9 changes: 9 additions & 0 deletions azurerm/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"github.com/Azure/azure-sdk-for-go/arm/network"
"github.com/Azure/azure-sdk-for-go/arm/redis"
"github.com/Azure/azure-sdk-for-go/arm/resources/resources"
"github.com/Azure/azure-sdk-for-go/arm/resources/subscriptions"
"github.com/Azure/azure-sdk-for-go/arm/scheduler"
"github.com/Azure/azure-sdk-for-go/arm/search"
"github.com/Azure/azure-sdk-for-go/arm/servicebus"
Expand Down Expand Up @@ -94,6 +95,8 @@ type ArmClient struct {
tagsClient resources.TagsClient
resourceFindClient resources.GroupClient

subscriptionsGroupClient subscriptions.GroupClient

jobsClient scheduler.JobsClient
jobsCollectionsClient scheduler.JobCollectionsClient

Expand Down Expand Up @@ -446,6 +449,12 @@ func (c *Config) getArmClient() (*ArmClient, error) {
rf.Sender = autorest.CreateSender(withRequestLogging())
client.resourceFindClient = rf

subgc := subscriptions.NewGroupClientWithBaseURI(endpoint)
setUserAgent(&subgc.Client)
subgc.Authorizer = auth
subgc.Sender = autorest.CreateSender(withRequestLogging())
client.subscriptionsGroupClient = subgc

jc := scheduler.NewJobsClientWithBaseURI(endpoint, c.SubscriptionID)
setUserAgent(&jc.Client)
jc.Authorizer = auth
Expand Down
73 changes: 73 additions & 0 deletions azurerm/data_source_subscription.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package azurerm

import (
"fmt"

"github.com/hashicorp/terraform/helper/schema"
)

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,
},
},
}
}

func dataSourceArmSubscriptionRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient)
groupClient := client.subscriptionsGroupClient

subscriptionId := d.Get("subscription_id").(string)
if subscriptionId == "" {
subscriptionId = client.subscriptionId
}

resp, err := groupClient.Get(subscriptionId)
if err != nil {
return fmt.Errorf("Error reading subscription: %+v", err)
}

d.SetId(*resp.ID)
d.Set("subscription_id", resp.SubscriptionID)
d.Set("display_name", resp.DisplayName)
d.Set("state", resp.State)
if resp.SubscriptionPolicies != nil {
d.Set("location_placement_id", resp.SubscriptionPolicies.LocationPlacementID)
d.Set("quota_id", resp.SubscriptionPolicies.QuotaID)
d.Set("spending_limit", resp.SubscriptionPolicies.SpendingLimit)
}

return nil
}
83 changes: 83 additions & 0 deletions azurerm/data_source_subscription_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package azurerm

import (
"fmt"
"os"
"testing"

"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
)

func TestAccDataSourceAzureRMSubscription_current(t *testing.T) {
resourceName := "data.azurerm_subscription.current"

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccDataSourceAzureRMSubscription_currentConfig,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrSet(resourceName, "subscription_id"),
testCheckAzureRMSubscriptionId(resourceName),
resource.TestCheckResourceAttrSet(resourceName, "display_name"),
resource.TestCheckResourceAttr(resourceName, "state", "Enabled"),
),
},
},
})
}

func TestAccDataSourceAzureRMSubscription_specific(t *testing.T) {
resourceName := "data.azurerm_subscription.specific"

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccDataSourceAzureRMSubscription_specificConfig(os.Getenv("ARM_SUBSCRIPTION_ID")),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrSet(resourceName, "subscription_id"),
testCheckAzureRMSubscriptionId(resourceName),
resource.TestCheckResourceAttrSet(resourceName, "display_name"),
resource.TestCheckResourceAttrSet(resourceName, "location_placement_id"),
resource.TestCheckResourceAttrSet(resourceName, "quota_id"),
resource.TestCheckResourceAttrSet(resourceName, "spending_limit"),
),
},
},
})
}

func testCheckAzureRMSubscriptionId(name string) resource.TestCheckFunc {
return func(s *terraform.State) error {
// Ensure we have enough information in state to look up in API
rs, ok := s.RootModule().Resources[name]
if !ok {
return fmt.Errorf("Not found: %s", name)
}

attributeName := "subscription_id"
subscriptionId := rs.Primary.Attributes[attributeName]
client := testAccProvider.Meta().(*ArmClient)
if subscriptionId != client.subscriptionId {
return fmt.Errorf("%s: Attribute '%s' expected \"%s\", got \"%s\"", name, attributeName, client.subscriptionId, subscriptionId)
}

return nil
}
}

const testAccDataSourceAzureRMSubscription_currentConfig = `
data "azurerm_subscription" "current" {}
`

func testAccDataSourceAzureRMSubscription_specificConfig(subscriptionId string) string {
return fmt.Sprintf(`
data "azurerm_subscription" "specific" {
subscription_id = "%s"
}
`, subscriptionId)
}
1 change: 1 addition & 0 deletions azurerm/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ func Provider() terraform.ResourceProvider {
"azurerm_resource_group": dataSourceArmResourceGroup(),
"azurerm_public_ip": dataSourceArmPublicIP(),
"azurerm_managed_disk": dataSourceArmManagedDisk(),
"azurerm_subscription": dataSourceArmSubscription(),
},

ResourcesMap: map[string]*schema.Resource{
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 0d450a1

Please sign in to comment.