-
Notifications
You must be signed in to change notification settings - Fork 4.7k
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 | ||
} |
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), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we can remove this in favour of checking via:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The reason we can't do as suggested is that There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. true, but we could instead check this against the |
||
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) | ||
} |
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.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.