Skip to content

Commit

Permalink
resource/app_service_slot: Add support for enabling MSI (#1579)
Browse files Browse the repository at this point in the history
* resource/app_service_slot: Add support for enabling MSI

Fixes: #1268

* App Service -> App Service Slot
  • Loading branch information
stack72 authored and tombuildsstuff committed Jul 16, 2018
1 parent 84dc8e4 commit e992995
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 1 deletion.
39 changes: 38 additions & 1 deletion azurerm/resource_arm_app_service_slot.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,33 @@ func resourceArmAppServiceSlot() *schema.Resource {

"location": locationSchema(),

"identity": {
Type: schema.TypeList,
Optional: true,
ForceNew: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"type": {
Type: schema.TypeString,
Required: true,
DiffSuppressFunc: ignoreCaseDiffSuppressFunc,
ValidateFunc: validation.StringInSlice([]string{
"SystemAssigned",
}, true),
},
"principal_id": {
Type: schema.TypeString,
Computed: true,
},
"tenant_id": {
Type: schema.TypeString,
Computed: true,
},
},
},
},

"app_service_name": {
Type: schema.TypeString,
Required: true,
Expand Down Expand Up @@ -144,6 +171,11 @@ func resourceArmAppServiceSlotCreate(d *schema.ResourceData, meta interface{}) e
},
}

if _, ok := d.GetOk("identity"); ok {
appServiceIdentity := expandAzureRmAppServiceIdentity(d)
siteEnvelope.Identity = appServiceIdentity
}

if v, ok := d.GetOk("client_affinity_enabled"); ok {
enabled := v.(bool)
siteEnvelope.SiteProperties.ClientAffinityEnabled = utils.Bool(enabled)
Expand Down Expand Up @@ -273,7 +305,7 @@ func resourceArmAppServiceSlotUpdate(d *schema.ResourceData, meta interface{}) e

_, err := client.UpdateConnectionStringsSlot(ctx, resGroup, appServiceName, properties, slot)
if err != nil {
return fmt.Errorf("Error updating Connection Strings for App Service %q/%q: %+v", appServiceName, slot, err)
return fmt.Errorf("Error updating Connection Strings for App Service Slot %q/%q: %+v", appServiceName, slot, err)
}
}

Expand Down Expand Up @@ -345,6 +377,11 @@ func resourceArmAppServiceSlotRead(d *schema.ResourceData, meta interface{}) err
return err
}

identity := flattenAzureRmAppServiceMachineIdentity(resp.Identity)
if err := d.Set("identity", identity); err != nil {
return err
}

flattenAndSetTags(d, resp.Tags)

return nil
Expand Down
65 changes: 65 additions & 0 deletions azurerm/resource_arm_app_service_slot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"fmt"
"testing"

"regexp"

"github.com/hashicorp/terraform/helper/acctest"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
Expand Down Expand Up @@ -681,6 +683,31 @@ func TestAccAzureRMAppServiceSlot_webSockets(t *testing.T) {
})
}

func TestAccAzureRMAppServiceSlot_enableManageServiceIdentity(t *testing.T) {
resourceName := "azurerm_app_service_slot.test"
ri := acctest.RandInt()
config := testAccAzureRMAppServiceSlot_enableManageServiceIdentity(ri, testLocation())

uuidMatch := regexp.MustCompile("^[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-4[a-fA-F0-9]{3}-[8|9|aA|bB][a-fA-F0-9]{3}-[a-fA-F0-9]{12}$")

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testCheckAzureRMAppServiceSlotDestroy,
Steps: []resource.TestStep{
{
Config: config,
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMAppServiceSlotExists(resourceName),
resource.TestCheckResourceAttr(resourceName, "identity.0.type", "SystemAssigned"),
resource.TestMatchResourceAttr(resourceName, "identity.0.principal_id", uuidMatch),
resource.TestMatchResourceAttr(resourceName, "identity.0.tenant_id", uuidMatch),
),
},
},
})
}

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

Expand Down Expand Up @@ -1615,3 +1642,41 @@ resource "azurerm_app_service_slot" "test" {
}
`, rInt, location, rInt, rInt, rInt)
}

func testAccAzureRMAppServiceSlot_enableManageServiceIdentity(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"
location = "${azurerm_resource_group.test.location}"
resource_group_name = "${azurerm_resource_group.test.name}"
app_service_plan_id = "${azurerm_app_service_plan.test.id}"
app_service_name = "${azurerm_app_service.test.name}"
identity = {
type = "SystemAssigned"
}
}
`, rInt, location, rInt, rInt, rInt)
}
8 changes: 8 additions & 0 deletions website/docs/r/app_service_slot.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,8 @@ The following arguments are supported:

* `site_config` - (Optional) A `site_config` object as defined below.

* `identity` - (Optional) A Managed Service Identity block as defined below.

* `tags` - (Optional) A mapping of tags to assign to the resource.

---
Expand Down Expand Up @@ -212,6 +214,12 @@ The following arguments are supported:

* `subnet_mask` - (Optional) The Subnet mask used for this IP Restriction. Defaults to `255.255.255.255`.

`identity` supports the following:

* `type` - (Required) Specifies the identity type of the App Service. At this time the only allowed value is `SystemAssigned`.

~> The assigned `principal_id` and `tenant_id` can be retrieved after the App Service Slot has been created.

## Attributes Reference

The following attributes are exported:
Expand Down

0 comments on commit e992995

Please sign in to comment.