-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #285 from ewbankkit/azurerm_subscription-data-source
New data source: azurerm_subscription
- Loading branch information
Showing
12 changed files
with
801 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
vendor/github.com/Azure/azure-sdk-for-go/arm/resources/subscriptions/client.go
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
132 changes: 132 additions & 0 deletions
132
vendor/github.com/Azure/azure-sdk-for-go/arm/resources/subscriptions/models.go
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.