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

Allow updating identity on app_service_slot without recreating the resource #3702

Merged
merged 4 commits into from
Jun 27, 2019
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
90 changes: 9 additions & 81 deletions azurerm/resource_arm_app_service_slot.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ import (

func resourceArmAppServiceSlot() *schema.Resource {
return &schema.Resource{
Create: resourceArmAppServiceSlotCreate,
Create: resourceArmAppServiceSlotCreateUpdate,
Read: resourceArmAppServiceSlotRead,
Update: resourceArmAppServiceSlotUpdate,
Update: resourceArmAppServiceSlotCreateUpdate,
Delete: resourceArmAppServiceSlotDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
Expand All @@ -37,7 +37,6 @@ func resourceArmAppServiceSlot() *schema.Resource {
"identity": {
Type: schema.TypeList,
Optional: true,
ForceNew: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
Expand Down Expand Up @@ -165,12 +164,10 @@ func resourceArmAppServiceSlot() *schema.Resource {
}
}

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

log.Printf("[INFO] preparing arguments for AzureRM App Service Slot creation.")

slot := d.Get("name").(string)
resGroup := d.Get("resource_group_name").(string)
appServiceName := d.Get("app_service_name").(string)
Expand All @@ -193,16 +190,18 @@ func resourceArmAppServiceSlotCreate(d *schema.ResourceData, meta interface{}) e
enabled := d.Get("enabled").(bool)
httpsOnly := d.Get("https_only").(bool)
tags := d.Get("tags").(map[string]interface{})
affinity := d.Get("client_affinity_enabled").(bool)

siteConfig := azure.ExpandAppServiceSiteConfig(d.Get("site_config"))
siteEnvelope := web.Site{
Location: &location,
Tags: expandTags(tags),
SiteProperties: &web.SiteProperties{
ServerFarmID: utils.String(appServicePlanId),
Enabled: utils.Bool(enabled),
HTTPSOnly: utils.Bool(httpsOnly),
SiteConfig: &siteConfig,
ServerFarmID: utils.String(appServicePlanId),
Enabled: utils.Bool(enabled),
HTTPSOnly: utils.Bool(httpsOnly),
SiteConfig: &siteConfig,
ClientAffinityEnabled: &affinity,
Copy link
Collaborator

Choose a reason for hiding this comment

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

We are setting this below, what's the reasoning behind adding it 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.

My bad, I'll remove the assignment here

},
}

Expand All @@ -211,19 +210,6 @@ func resourceArmAppServiceSlotCreate(d *schema.ResourceData, meta interface{}) e
siteEnvelope.Identity = appServiceIdentity
}

if v, ok := d.GetOk("client_affinity_enabled"); ok {
enabled := v.(bool)
siteEnvelope.SiteProperties.ClientAffinityEnabled = utils.Bool(enabled)
}

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)
}

createFuture, err := client.CreateOrUpdateSlot(ctx, resGroup, appServiceName, siteEnvelope, slot)
if err != nil {
return err
Expand All @@ -245,50 +231,6 @@ func resourceArmAppServiceSlotCreate(d *schema.ResourceData, meta interface{}) e

d.SetId(*read.ID)

return resourceArmAppServiceSlotUpdate(d, meta)
}

func resourceArmAppServiceSlotUpdate(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
appServiceName := id.Path["sites"]
location := azure.NormalizeLocation(d.Get("location").(string))
appServicePlanId := d.Get("app_service_plan_id").(string)
slot := id.Path["slots"]
siteConfig := azure.ExpandAppServiceSiteConfig(d.Get("site_config"))
enabled := d.Get("enabled").(bool)
httpsOnly := d.Get("https_only").(bool)
tags := d.Get("tags").(map[string]interface{})
siteEnvelope := web.Site{
Location: &location,
Tags: expandTags(tags),
SiteProperties: &web.SiteProperties{
ServerFarmID: utils.String(appServicePlanId),
Enabled: utils.Bool(enabled),
HTTPSOnly: utils.Bool(httpsOnly),
SiteConfig: &siteConfig,
},
}
if v, ok := d.GetOk("client_affinity_enabled"); ok {
enabled := v.(bool)
siteEnvelope.SiteProperties.ClientAffinityEnabled = utils.Bool(enabled)
}
createFuture, err := client.CreateOrUpdateSlot(ctx, resGroup, appServiceName, siteEnvelope, slot)
if err != nil {
return err
}

err = createFuture.WaitForCompletionRef(ctx, client.Client)
if err != nil {
return err
}
if d.HasChange("site_config") {
// update the main configuration
siteConfig := azure.ExpandAppServiceSiteConfig(d.Get("site_config"))
Expand All @@ -300,20 +242,6 @@ func resourceArmAppServiceSlotUpdate(d *schema.ResourceData, meta interface{}) e
}
}

if d.HasChange("client_affinity_enabled") {
affinity := d.Get("client_affinity_enabled").(bool)
sitePatchResource := web.SitePatchResource{
ID: utils.String(d.Id()),
SitePatchResourceProperties: &web.SitePatchResourceProperties{
ClientAffinityEnabled: &affinity,
},
}
_, err := client.UpdateSlot(ctx, resGroup, appServiceName, sitePatchResource, slot)
if err != nil {
return fmt.Errorf("Error updating App Service ARR Affinity setting %q: %+v", slot, err)
}
}

if d.HasChange("app_settings") {
// update the AppSettings
appSettings := expandAppServiceAppSettings(d)
Expand Down
28 changes: 28 additions & 0 deletions azurerm/resource_arm_app_service_slot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -911,6 +911,34 @@ func TestAccAzureRMAppServiceSlot_webSockets(t *testing.T) {
})
}

func TestAccAzureRMAppServiceSlot_updateManageServiceIdentity(t *testing.T) {
resourceName := "azurerm_app_service_slot.test"
ri := tf.AccRandTimeInt()

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testCheckAzureRMAppServiceSlotDestroy,
Steps: []resource.TestStep{
{
Config: testAccAzureRMAppServiceSlot_basic(ri, testLocation()),
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMAppServiceSlotExists(resourceName),
),
},
{
Config: testAccAzureRMAppServiceSlot_enableManageServiceIdentity(ri, testLocation()),
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMAppServiceSlotExists(resourceName),
resource.TestCheckResourceAttr(resourceName, "identity.0.type", "SystemAssigned"),
resource.TestMatchResourceAttr(resourceName, "identity.0.principal_id", validate.UUIDRegExp),
resource.TestMatchResourceAttr(resourceName, "identity.0.tenant_id", validate.UUIDRegExp),
),
},
},
})
}

func TestAccAzureRMAppServiceSlot_enableManageServiceIdentity(t *testing.T) {
resourceName := "azurerm_app_service_slot.test"
ri := tf.AccRandTimeInt()
Expand Down