-
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 Resource: `azurerm_custom_hostname_bindings #1087
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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, | ||
}, | ||
}, | ||
}) | ||
} |
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
124 changes: 124 additions & 0 deletions
124
azurerm/resource_arm_app_service_custom_hostname_binding.go
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,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
134
azurerm/resource_arm_app_service_custom_hostname_binding_test.go
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,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) | ||
} | ||
|
||
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) | ||
} |
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
%q
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.
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?