From ea104e090a7e5a5346845032745014d8388fa15a Mon Sep 17 00:00:00 2001 From: tombuildsstuff Date: Mon, 8 Jan 2018 16:21:16 +0000 Subject: [PATCH] New Data Source: `azurerm_app_service_plan` Tests pass: ``` $ acctests azurerm TestAccDataSourceAzureRMAppServicePlan_ === RUN TestAccDataSourceAzureRMAppServicePlan_basic --- PASS: TestAccDataSourceAzureRMAppServicePlan_basic (79.79s) === RUN TestAccDataSourceAzureRMAppServicePlan_complete --- PASS: TestAccDataSourceAzureRMAppServicePlan_complete (74.04s) PASS ok github.com/terraform-providers/terraform-provider-azurerm/azurerm 153.860s ``` --- azurerm/data_source_app_service_plan.go | 118 +++++++++++++++++ azurerm/data_source_app_service_plan_test.go | 119 ++++++++++++++++++ azurerm/provider.go | 1 + website/azurerm.erb | 9 +- website/docs/d/app_service_plan.html.markdown | 61 +++++++++ 5 files changed, 306 insertions(+), 2 deletions(-) create mode 100644 azurerm/data_source_app_service_plan.go create mode 100644 azurerm/data_source_app_service_plan_test.go create mode 100644 website/docs/d/app_service_plan.html.markdown diff --git a/azurerm/data_source_app_service_plan.go b/azurerm/data_source_app_service_plan.go new file mode 100644 index 000000000000..a1c0442eff04 --- /dev/null +++ b/azurerm/data_source_app_service_plan.go @@ -0,0 +1,118 @@ +package azurerm + +import ( + "fmt" + + "github.com/hashicorp/terraform/helper/schema" + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils" +) + +func dataSourceAppServicePlan() *schema.Resource { + return &schema.Resource{ + Read: dataSourceAppServicePlanRead, + Importer: &schema.ResourceImporter{ + State: schema.ImportStatePassthrough, + }, + + Schema: map[string]*schema.Schema{ + "name": { + Type: schema.TypeString, + Required: true, + }, + + "resource_group_name": resourceGroupNameForDataSourceSchema(), + + "location": locationForDataSourceSchema(), + + "kind": { + Type: schema.TypeString, + Computed: true, + }, + + "sku": { + Type: schema.TypeList, + Computed: true, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "tier": { + Type: schema.TypeString, + Computed: true, + }, + "size": { + Type: schema.TypeString, + Computed: true, + }, + "capacity": { + Type: schema.TypeInt, + Computed: true, + }, + }, + }, + }, + + "properties": { + Type: schema.TypeList, + Computed: true, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "reserved": { + Type: schema.TypeBool, + Computed: true, + }, + "per_site_scaling": { + Type: schema.TypeBool, + Computed: true, + }, + }, + }, + }, + + "maximum_number_of_workers": { + Type: schema.TypeInt, + Computed: true, + }, + + "tags": tagsForDataSourceSchema(), + }, + } +} + +func dataSourceAppServicePlanRead(d *schema.ResourceData, meta interface{}) error { + client := meta.(*ArmClient).appServicePlansClient + + name := d.Get("name").(string) + resourceGroup := d.Get("resource_group_name").(string) + + resp, err := client.Get(resourceGroup, name) + if err != nil { + if utils.ResponseWasNotFound(resp.Response) { + d.SetId("") + return nil + } + + return fmt.Errorf("Error making Read request on App Service Plan %q (Resource Group %q): %+v", name, resourceGroup, err) + } + + d.SetId(*resp.ID) + + d.Set("name", name) + d.Set("resource_group_name", resourceGroup) + d.Set("location", azureRMNormalizeLocation(*resp.Location)) + d.Set("kind", resp.Kind) + + if props := resp.AppServicePlanProperties; props != nil { + d.Set("properties", flattenAppServiceProperties(props)) + + if props.MaximumNumberOfWorkers != nil { + d.Set("maximum_number_of_workers", int(*props.MaximumNumberOfWorkers)) + } + } + + if sku := resp.Sku; sku != nil { + d.Set("sku", flattenAppServicePlanSku(sku)) + } + + flattenAndSetTags(d, resp.Tags) + + return nil +} diff --git a/azurerm/data_source_app_service_plan_test.go b/azurerm/data_source_app_service_plan_test.go new file mode 100644 index 000000000000..7ccbbeb5481e --- /dev/null +++ b/azurerm/data_source_app_service_plan_test.go @@ -0,0 +1,119 @@ +package azurerm + +import ( + "fmt" + "testing" + + "github.com/hashicorp/terraform/helper/acctest" + "github.com/hashicorp/terraform/helper/resource" +) + +func TestAccDataSourceAzureRMAppServicePlan_basic(t *testing.T) { + dataSourceName := "data.azurerm_app_service_plan.test" + rInt := acctest.RandInt() + location := testLocation() + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + Steps: []resource.TestStep{ + { + Config: testAccDataSourceAppServicePlan_basic(rInt, location), + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr(dataSourceName, "kind", "Windows"), + resource.TestCheckResourceAttr(dataSourceName, "sku.#", "1"), + resource.TestCheckResourceAttr(dataSourceName, "sku.0.tier", "Basic"), + resource.TestCheckResourceAttr(dataSourceName, "sku.0.size", "B1"), + resource.TestCheckResourceAttr(dataSourceName, "properties.#", "1"), + resource.TestCheckResourceAttr(dataSourceName, "properties.0.per_site_scaling", "false"), + resource.TestCheckResourceAttr(dataSourceName, "tags.%", "0"), + ), + }, + }, + }) +} + +func TestAccDataSourceAzureRMAppServicePlan_complete(t *testing.T) { + dataSourceName := "data.azurerm_app_service_plan.test" + rInt := acctest.RandInt() + location := testLocation() + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + Steps: []resource.TestStep{ + { + Config: testAccDataSourceAppServicePlan_complete(rInt, location), + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr(dataSourceName, "kind", "Windows"), + resource.TestCheckResourceAttr(dataSourceName, "sku.#", "1"), + resource.TestCheckResourceAttr(dataSourceName, "sku.0.tier", "Standard"), + resource.TestCheckResourceAttr(dataSourceName, "sku.0.size", "S1"), + resource.TestCheckResourceAttr(dataSourceName, "properties.#", "1"), + resource.TestCheckResourceAttr(dataSourceName, "properties.0.per_site_scaling", "true"), + resource.TestCheckResourceAttr(dataSourceName, "tags.%", "1"), + resource.TestCheckResourceAttr(dataSourceName, "tags.environment", "Test"), + ), + }, + }, + }) +} + +func testAccDataSourceAppServicePlan_basic(rInt int, location string) string { + return fmt.Sprintf(` +resource "azurerm_resource_group" "test" { + name = "acctestRG-%d" + location = "%s" +} + +resource "azurerm_app_service_plan" "test" { + name = "acctestASP-%d" + location = "${azurerm_resource_group.test.location}" + resource_group_name = "${azurerm_resource_group.test.name}" + + sku { + tier = "Basic" + size = "B1" + } +} + +data "azurerm_app_service_plan" "test" { + name = "${azurerm_app_service_plan.test.name}" + resource_group_name = "${azurerm_app_service_plan.test.resource_group_name}" +} +`, rInt, location, rInt) +} + +func testAccDataSourceAppServicePlan_complete(rInt int, location string) string { + return fmt.Sprintf(` +resource "azurerm_resource_group" "test" { + name = "acctestRG-%d" + location = "%s" +} + +resource "azurerm_app_service_plan" "test" { + name = "acctestASP-%d" + location = "${azurerm_resource_group.test.location}" + resource_group_name = "${azurerm_resource_group.test.name}" + kind = "Windows" + + sku { + tier = "Standard" + size = "S1" + } + + properties { + per_site_scaling = true + } + + tags { + environment = "Test" + } +} + +data "azurerm_app_service_plan" "test" { + name = "${azurerm_app_service_plan.test.name}" + resource_group_name = "${azurerm_app_service_plan.test.resource_group_name}" +} +`, rInt, location, rInt) +} diff --git a/azurerm/provider.go b/azurerm/provider.go index eea804a46705..3bc5301343e6 100644 --- a/azurerm/provider.go +++ b/azurerm/provider.go @@ -65,6 +65,7 @@ func Provider() terraform.ResourceProvider { }, DataSourcesMap: map[string]*schema.Resource{ + "azurerm_app_service_plan": dataSourceAppServicePlan(), "azurerm_builtin_role_definition": dataSourceArmBuiltInRoleDefinition(), "azurerm_client_config": dataSourceArmClientConfig(), "azurerm_image": dataSourceArmImage(), diff --git a/website/azurerm.erb b/website/azurerm.erb index e03f1261ab57..676300865e72 100644 --- a/website/azurerm.erb +++ b/website/azurerm.erb @@ -23,10 +23,15 @@ > Data Sources