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 Resource: `azurerm_custom_hostname_bindings #1087

Merged
merged 1 commit into from
Apr 6, 2018
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
68 changes: 68 additions & 0 deletions azurerm/import_arm_app_service_hostname_binding_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package azurerm

import (
"os"
"testing"

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

func TestAccAzureRMAppServiceCustomHostnameBinding(t *testing.T) {
// NOTE: this is a combined test rather than separate split out tests due to
// the app service name being shared (so the tests don't conflict with each other)
testCases := map[string]map[string]func(t *testing.T){
"basic": {
"basic": testAccAzureRMAppServiceCustomHostnameBinding_basic,
"import": testAccAzureRMAppServiceCustomHostnameBinding_import,
},
}

for group, m := range testCases {
m := m
t.Run(group, func(t *testing.T) {
for name, tc := range m {
tc := tc
t.Run(name, func(t *testing.T) {
tc(t)
})
}
})
}
}

func testAccAzureRMAppServiceCustomHostnameBinding_import(t *testing.T) {
appServiceEnvVariable := "ARM_TEST_APP_SERVICE"
appServiceEnv := os.Getenv(appServiceEnvVariable)
if appServiceEnv == "" {
t.Skipf("Skipping as %q is not specified", appServiceEnvVariable)
}

domainEnvVariable := "ARM_TEST_DOMAIN"
domainEnv := os.Getenv(domainEnvVariable)
if domainEnv == "" {
t.Skipf("Skipping as %q is not specified", domainEnvVariable)
}

resourceName := "azurerm_app_service_custom_hostname_binding.test"

ri := acctest.RandInt()
location := testLocation()
config := testAccAzureRMAppServiceCustomHostnameBinding_basicConfig(ri, location, appServiceEnv, domainEnv)

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testCheckAzureRMAppServiceCustomHostnameBindingDestroy,
Steps: []resource.TestStep{
{
Config: config,
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
},
})
}
1 change: 1 addition & 0 deletions azurerm/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ func Provider() terraform.ResourceProvider {
"azurerm_app_service": resourceArmAppService(),
"azurerm_app_service_plan": resourceArmAppServicePlan(),
"azurerm_app_service_active_slot": resourceArmAppServiceActiveSlot(),
"azurerm_app_service_custom_hostname_binding": resourceArmAppServiceCustomHostnameBinding(),
"azurerm_app_service_slot": resourceArmAppServiceSlot(),
"azurerm_automation_account": resourceArmAutomationAccount(),
"azurerm_automation_credential": resourceArmAutomationCredential(),
Expand Down
124 changes: 124 additions & 0 deletions azurerm/resource_arm_app_service_custom_hostname_binding.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
package azurerm

import (
"fmt"
"log"

"github.com/Azure/azure-sdk-for-go/services/web/mgmt/2016-09-01/web"
"github.com/hashicorp/terraform/helper/schema"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
)

func resourceArmAppServiceCustomHostnameBinding() *schema.Resource {
return &schema.Resource{
Create: resourceArmAppServiceCustomHostnameBindingCreate,
Read: resourceArmAppServiceCustomHostnameBindingRead,
Delete: resourceArmAppServiceCustomHostnameBindingDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},

Schema: map[string]*schema.Schema{
"hostname": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},

"resource_group_name": resourceGroupNameSchema(),

"app_service_name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
},
}
}

func resourceArmAppServiceCustomHostnameBindingCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient).appServicesClient
ctx := meta.(*ArmClient).StopContext

log.Printf("[INFO] preparing arguments for App Service Hostname Binding creation.")

resourceGroup := d.Get("resource_group_name").(string)
appServiceName := d.Get("app_service_name").(string)
hostname := d.Get("hostname").(string)

properties := web.HostNameBinding{
HostNameBindingProperties: &web.HostNameBindingProperties{
SiteName: utils.String(appServiceName),
},
}
_, err := client.CreateOrUpdateHostNameBinding(ctx, resourceGroup, appServiceName, hostname, properties)
if err != nil {
return err
}

read, err := client.GetHostNameBinding(ctx, resourceGroup, appServiceName, hostname)
if err != nil {
return err
}
if read.ID == nil {
return fmt.Errorf("Cannot read Hostname Binding %q (App Service %q / Resource Group %q) ID", hostname, appServiceName, resourceGroup)
}

d.SetId(*read.ID)

return resourceArmAppServiceCustomHostnameBindingRead(d, meta)
}

func resourceArmAppServiceCustomHostnameBindingRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient).appServicesClient

id, err := parseAzureResourceID(d.Id())
if err != nil {
return err
}

resourceGroup := id.ResourceGroup
appServiceName := id.Path["sites"]
hostname := id.Path["hostNameBindings"]

ctx := meta.(*ArmClient).StopContext
resp, err := client.GetHostNameBinding(ctx, resourceGroup, appServiceName, hostname)
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
log.Printf("[DEBUG] App Service Hostname Binding %q (App Service %q / Resource Group %q) was not found - removing from state", hostname, appServiceName, resourceGroup)
d.SetId("")
return nil
}
return fmt.Errorf("Error making Read request on App Service Hostname Binding %q (App Service %q / Resource Group %q): %+v", hostname, appServiceName, resourceGroup, err)
}

d.Set("hostname", hostname)
d.Set("app_service_name", appServiceName)
d.Set("resource_group_name", resourceGroup)

return nil
}

func resourceArmAppServiceCustomHostnameBindingDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient).appServicesClient

id, err := parseAzureResourceID(d.Id())
if err != nil {
return err
}
resGroup := id.ResourceGroup
appServiceName := id.Path["sites"]
hostname := id.Path["hostNameBindings"]

log.Printf("[DEBUG] Deleting App Service Hostname Binding %q (App Service %q / Resource Group %q)", hostname, appServiceName, resGroup)

ctx := meta.(*ArmClient).StopContext
resp, err := client.DeleteHostNameBinding(ctx, resGroup, appServiceName, hostname)
if err != nil {
if !utils.ResponseWasNotFound(resp) {
return err
}
}

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

import (
"fmt"
"testing"

"os"

"github.com/hashicorp/terraform/helper/acctest"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
)

func testAccAzureRMAppServiceCustomHostnameBinding_basic(t *testing.T) {
appServiceEnvVariable := "ARM_TEST_APP_SERVICE"
appServiceEnv := os.Getenv(appServiceEnvVariable)
if appServiceEnv == "" {
t.Skipf("Skipping as %q is not specified", appServiceEnvVariable)
}

domainEnvVariable := "ARM_TEST_DOMAIN"
domainEnv := os.Getenv(domainEnvVariable)
if domainEnv == "" {
t.Skipf("Skipping as %q is not specified", domainEnvVariable)
}

resourceName := "azurerm_app_service_custom_hostname_binding.test"
ri := acctest.RandInt()
location := testLocation()
config := testAccAzureRMAppServiceCustomHostnameBinding_basicConfig(ri, location, appServiceEnv, domainEnv)

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testCheckAzureRMAppServiceCustomHostnameBindingDestroy,
Steps: []resource.TestStep{
{
Config: config,
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMAppServiceCustomHostnameBindingExists(resourceName),
),
},
},
})
}

func testCheckAzureRMAppServiceCustomHostnameBindingDestroy(s *terraform.State) error {
client := testAccProvider.Meta().(*ArmClient).appServicesClient

for _, rs := range s.RootModule().Resources {
if rs.Type != "azurerm_app_service_custom_hostname_binding" {
continue
}

resourceGroup := rs.Primary.Attributes["resource_group_name"]
appServiceName := rs.Primary.Attributes["app_service_name"]
hostname := rs.Primary.Attributes["hostname"]

ctx := testAccProvider.Meta().(*ArmClient).StopContext
resp, err := client.GetHostNameBinding(ctx, resourceGroup, appServiceName, hostname)

if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
return nil
}
return err
}

return nil
}

return nil
}

func testCheckAzureRMAppServiceCustomHostnameBindingExists(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)
Copy link
Member

Choose a reason for hiding this comment

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

%q 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.

in general we don’t for these since this is the resource name (eg “azurerm_resource_group”) rather than the name property of the resource so generally we just output those raw (since it means the resource is missing from the state) - but I can update this?

}

resourceGroup := rs.Primary.Attributes["resource_group_name"]
appServiceName := rs.Primary.Attributes["app_service_name"]
hostname := rs.Primary.Attributes["hostname"]

client := testAccProvider.Meta().(*ArmClient).appServicesClient
ctx := testAccProvider.Meta().(*ArmClient).StopContext
resp, err := client.GetHostNameBinding(ctx, resourceGroup, appServiceName, hostname)
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
return fmt.Errorf("Bad: Hostname Binding %q (App Service %q / Resource Group: %q) does not exist", hostname, appServiceName, resourceGroup)
}

return fmt.Errorf("Bad: Get on appServicesClient: %+v", err)
}

return nil
}
}

func testAccAzureRMAppServiceCustomHostnameBinding_basicConfig(rInt int, location string, appServiceName string, domain 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 = "Standard"
size = "S1"
}
}

resource "azurerm_app_service" "test" {
name = "%s"
location = "${azurerm_resource_group.test.location}"
resource_group_name = "${azurerm_resource_group.test.name}"
app_service_plan_id = "${azurerm_app_service_plan.test.id}"
}

resource "azurerm_app_service_custom_hostname_binding" "test" {
hostname = "%s"
app_service_name = "${azurerm_app_service.test.name}"
resource_group_name = "${azurerm_resource_group.test.name}"
}
`, rInt, location, rInt, appServiceName, domain)
}
6 changes: 5 additions & 1 deletion website/azurerm.erb
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,11 @@
</li>

<li<%= sidebar_current("docs-azurerm-resource-app-service-active-slot") %>>
<a href="/docs/providers/azurerm/r/app_service_active_slot.html">azurerm_app_service_active_slot</a>
<a href="/docs/providers/azurerm/r/app_service_active_slot.html">azurerm_app_service_active_slot</a>
</li>

<li<%= sidebar_current("docs-azurerm-resource-app-service-custom-hostname-binding") %>>
<a href="/docs/providers/azurerm/r/app_service_custom_hostname_binding.html">azurerm_app_service_custom_hostname_binding</a>
</li>

<li<%= sidebar_current("docs-azurerm-resource-app-service-slot") %>>
Expand Down
Loading