Skip to content

Commit

Permalink
resource/app_service_slot: Add support for enabling MSI
Browse files Browse the repository at this point in the history
Fixes: #1268
  • Loading branch information
stack72 committed Jul 15, 2018
1 parent c5a039f commit a63edf2
Show file tree
Hide file tree
Showing 3 changed files with 166 additions and 1 deletion.
61 changes: 60 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,
Computed: 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,29 @@ 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)
}
}

if d.HasChange("identity") {
site, err := client.GetSlot(ctx, resGroup, appServiceName, slot)
if err != nil {
return fmt.Errorf("Error getting configuration for App Service Slot %q: %+v", appServiceName, err)
}

appServiceIdentity := expandAzureRmAppServiceIdentity(d)
site.Identity = appServiceIdentity

future, err := client.CreateOrUpdateSlot(ctx, resGroup, appServiceName, siteEnvelope, slot)

if err != nil {
return fmt.Errorf("Error updating Managed Service Identity for App Service Slot %q: %+v", appServiceName, err)
}

err = future.WaitForCompletion(ctx, client.Client)

if err != nil {
return fmt.Errorf("Error updating Managed Service Identity for App Service Slot %q: %+v", appServiceName, err)
}
}

Expand Down Expand Up @@ -345,6 +399,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
98 changes: 98 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,64 @@ 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 TestAccAzureRMAppServiceSlot_updateResourceByEnablingManageServiceIdentity(t *testing.T) {
resourceName := "azurerm_app_service_slot.test"
ri := acctest.RandInt()
config := testAccAzureRMAppServiceSlot_basic(ri, testLocation())
updatedConfig := 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"),
),
},
{
Config: updatedConfig,
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 +1675,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 has been created.

## Attributes Reference

The following attributes are exported:
Expand Down

0 comments on commit a63edf2

Please sign in to comment.