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 Resources: azurerm_app_service_slot & azurerm_app_service_active_slot #818

Merged
merged 13 commits into from
Feb 13, 2018
Merged
31 changes: 31 additions & 0 deletions azurerm/import_arm_app_service_slot_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package azurerm

import (
"testing"

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

func TestAccAzureRMAppServiceSlot_importBasic(t *testing.T) {
resourceName := "azurerm_app_service_slot.test"

ri := acctest.RandInt()
config := testAccAzureRMAppServiceSlot_basic(ri, testLocation())

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testCheckAzureRMAppServiceSlotDestroy,
Steps: []resource.TestStep{
{
Config: config,
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
},
})
}
2 changes: 2 additions & 0 deletions azurerm/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ func Provider() terraform.ResourceProvider {
"azurerm_application_insights": resourceArmApplicationInsights(),
"azurerm_app_service": resourceArmAppService(),
"azurerm_app_service_plan": resourceArmAppServicePlan(),
"azurerm_app_service_active_slot": resourceArmAppServiceActiveSlot(),
"azurerm_app_service_slot": resourceArmAppServiceSlot(),
"azurerm_automation_account": resourceArmAutomationAccount(),
"azurerm_automation_credential": resourceArmAutomationCredential(),
"azurerm_automation_runbook": resourceArmAutomationRunbook(),
Expand Down
112 changes: 112 additions & 0 deletions azurerm/resource_arm_app_service_active_slot.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
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 resourceArmAppServiceActiveSlot() *schema.Resource {
return &schema.Resource{
Create: resourceArmAppServiceActiveSlotCreate,
Read: resourceArmAppServiceActiveSlotRead,
Update: resourceArmAppServiceActiveSlotCreate,
Delete: resourceArmAppServiceActiveSlotDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{

"resource_group_name": resourceGroupNameSchema(),

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

"app_service_slot_name": {
Type: schema.TypeString,
Required: true,
},
},
}
}

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

appServiceName := d.Get("app_service_name").(string)
resGroup := d.Get("resource_group_name").(string)
targetSlot := d.Get("app_service_slot_name").(string)
preserveVnet := true

resp, err := client.Get(ctx, resGroup, appServiceName)
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
return fmt.Errorf("[DEBUG] App Service %q (resource group %q) was not found.", appServiceName, resGroup)
}
return fmt.Errorf("Error making Read request on AzureRM App Service %q: %+v", appServiceName, err)
}

_, err = client.Get(ctx, resGroup, targetSlot)
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
return fmt.Errorf("[DEBUG] App Service Target Active Slot %q/%q (resource group %q) was not found.", appServiceName, targetSlot, resGroup)
}
return fmt.Errorf("Error making Read request on AzureRM App Service Slot %q/%q: %+v", appServiceName, targetSlot, err)
}

cmsSlotEntity := web.CsmSlotEntity{
TargetSlot: &targetSlot,
PreserveVnet: &preserveVnet,
}

future, err := client.SwapSlotWithProduction(ctx, resGroup, appServiceName, cmsSlotEntity)
if err != nil {
return fmt.Errorf("Error swapping App Service Slot %q/%q: %+v", appServiceName, targetSlot, err)
}
err = future.WaitForCompletion(ctx, client.Client)
if err != nil {
return fmt.Errorf("Error swapping App Service Slot %q/%q: %+v", appServiceName, targetSlot, err)
}
d.SetId(*resp.ID)
return resourceArmAppServiceActiveSlotRead(d, meta)
}

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

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

resGroup := id.ResourceGroup
name := id.Path["sites"]

resp, err := client.Get(ctx, resGroup, name)
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
log.Printf("[DEBUG] App Service %q (resource group %q) was not found - removing from state", name, resGroup)
d.SetId("")
return nil
}
return fmt.Errorf("Error making Read request on AzureRM App Service %q: %+v", name, err)
}

d.Set("app_service_name", resp.Name)
d.Set("resource_group_name", resp.ResourceGroup)
d.Set("app_service_slot_name", resp.SiteProperties.SlotSwapStatus.SourceSlotName)
return nil
}

func resourceArmAppServiceActiveSlotDelete(d *schema.ResourceData, meta interface{}) error {
// There is nothing to delete so return nil
return nil
}
197 changes: 197 additions & 0 deletions azurerm/resource_arm_app_service_active_slot_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,197 @@
package azurerm

import (
"fmt"
"testing"

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

func TestAccAzureRMAppServiceActiveSlot_basic(t *testing.T) {
resourceName := "azurerm_app_service_active_slot.test"
ri := acctest.RandInt()
config := testAccAzureRMAppServiceActiveSlot_basic(ri, testLocation())

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
// Destroy actually does nothing so we just return nil
CheckDestroy: nil,
Steps: []resource.TestStep{
{
Config: config,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(resourceName, "app_service_slot_name", fmt.Sprintf("acctestASSlot-%d", ri)),
),
},
},
})
}

func TestAccAzureRMAppServiceActiveSlot_update(t *testing.T) {
resourceName := "azurerm_app_service_active_slot.test"
ri := acctest.RandInt()
config := testAccAzureRMAppServiceActiveSlot_update(ri, testLocation())
config2 := testAccAzureRMAppServiceActiveSlot_updated(ri, testLocation())

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
// Destroy actually does nothing so we just return nil
CheckDestroy: nil,
Steps: []resource.TestStep{
{
Config: config,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(resourceName, "app_service_slot_name", fmt.Sprintf("acctestASSlot-%d", ri)),
),
},
{
Config: config2,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(resourceName, "app_service_slot_name", fmt.Sprintf("acctestASSlot2-%d", ri)),
),
},
},
})
}

func testAccAzureRMAppServiceActiveSlot_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 = "Standard"
size = "S1"
}
}

resource "azurerm_app_service" "test" {
name = "acctestAS-%d"
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_slot" "test" {
name = "acctestASSlot-%d"
app_service_name = "${azurerm_app_service.test.name}"
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_active_slot" "test" {
resource_group_name = "${azurerm_resource_group.test.name}"
app_service_name = "${azurerm_app_service.test.name}"
app_service_slot_name = "${azurerm_app_service_slot.test.name}"
}
`, rInt, location, rInt, rInt, rInt)
}

func testAccAzureRMAppServiceActiveSlot_update(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 = "Standard"
size = "S1"
}
}

resource "azurerm_app_service" "test" {
name = "acctestAS-%d"
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_slot" "test" {
name = "acctestASSlot-%d"
app_service_name = "${azurerm_app_service.test.name}"
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_slot" "test2" {
name = "acctestASSlot2-%d"
app_service_name = "${azurerm_app_service.test.name}"
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_active_slot" "test" {
resource_group_name = "${azurerm_resource_group.test.name}"
app_service_name = "${azurerm_app_service.test.name}"
app_service_slot_name = "${azurerm_app_service_slot.test.name}"
}
`, rInt, location, rInt, rInt, rInt, rInt)
}

func testAccAzureRMAppServiceActiveSlot_updated(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 = "Standard"
size = "S1"
}
}

resource "azurerm_app_service" "test" {
name = "acctestAS-%d"
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_slot" "test" {
name = "acctestASSlot-%d"
app_service_name = "${azurerm_app_service.test.name}"
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_slot" "test2" {
name = "acctestASSlot2-%d"
app_service_name = "${azurerm_app_service.test.name}"
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_active_slot" "test" {
resource_group_name = "${azurerm_resource_group.test.name}"
app_service_name = "${azurerm_app_service.test.name}"
app_service_slot_name = "${azurerm_app_service_slot.test2.name}"
}
`, rInt, location, rInt, rInt, rInt, rInt)
}
Loading