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_subscription #285

Merged
merged 1 commit into from
Aug 30, 2017
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
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,
},
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

are there any other fields (e.g. Tenant ID?) that would make sense to be exposed here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have added the subscription state and possible subscription policies.
I'm not adding the tenant as the tenant API only works for the "current" account.


"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),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can remove this in favour of checking via:

resource.TestCheckResourceAttr(resourceName, "subscription_id", "{Subscription ID Value}")

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The reason we can't do as suggested is that {Subscription ID Value} is obtained from the ArmClient for the running test step and this can't be done when the steps are declared (testAccProvider.Meta().(*ArmClient) is nil at this point).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

true, but we could instead check this against the ARM_SUBSCRIPTION_ID environment variable? It's not a blocker by any means and this otherwise LGTM :)

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