From 752ff6d4033804fd4f79a23743c6842e0c0c51b6 Mon Sep 17 00:00:00 2001 From: tombuildsstuff Date: Mon, 21 Sep 2020 09:04:07 +0200 Subject: [PATCH 1/3] r/signalr: support for delta updates --- .../signalr/resource_arm_signalr_service.go | 128 ++++++++++++------ 1 file changed, 90 insertions(+), 38 deletions(-) diff --git a/azurerm/internal/services/signalr/resource_arm_signalr_service.go b/azurerm/internal/services/signalr/resource_arm_signalr_service.go index dfe592abb325..cee3cb98f18f 100644 --- a/azurerm/internal/services/signalr/resource_arm_signalr_service.go +++ b/azurerm/internal/services/signalr/resource_arm_signalr_service.go @@ -21,9 +21,9 @@ import ( func resourceArmSignalRService() *schema.Resource { return &schema.Resource{ - Create: resourceArmSignalRServiceCreateUpdate, + Create: resourceArmSignalRServiceCreate, Read: resourceArmSignalRServiceRead, - Update: resourceArmSignalRServiceCreateUpdate, + Update: resourceArmSignalRServiceUpdate, Delete: resourceArmSignalRServiceDelete, Timeouts: &schema.ResourceTimeout{ @@ -164,34 +164,32 @@ func resourceArmSignalRService() *schema.Resource { } } -func resourceArmSignalRServiceCreateUpdate(d *schema.ResourceData, meta interface{}) error { +func resourceArmSignalRServiceCreate(d *schema.ResourceData, meta interface{}) error { client := meta.(*clients.Client).SignalR.Client - ctx, cancel := timeouts.ForCreateUpdate(meta.(*clients.Client).StopContext, d) + ctx, cancel := timeouts.ForCreate(meta.(*clients.Client).StopContext, d) defer cancel() name := d.Get("name").(string) location := azure.NormalizeLocation(d.Get("location").(string)) resourceGroup := d.Get("resource_group_name").(string) + existing, err := client.Get(ctx, resourceGroup, name) + if err != nil { + if !utils.ResponseWasNotFound(existing.Response) { + return fmt.Errorf("Error checking for presence of existing SignalR %q (Resource Group %q): %+v", name, resourceGroup, err) + } + } + + if existing.ID != nil && *existing.ID != "" { + return tf.ImportAsExistsError("azurerm_signalr_service", *existing.ID) + } + sku := d.Get("sku").([]interface{}) t := d.Get("tags").(map[string]interface{}) featureFlags := d.Get("features").([]interface{}) cors := d.Get("cors").([]interface{}) expandedTags := tags.Expand(t) - if d.IsNewResource() { - existing, err := client.Get(ctx, resourceGroup, name) - if err != nil { - if !utils.ResponseWasNotFound(existing.Response) { - return fmt.Errorf("Error checking for presence of existing SignalR %q (Resource Group %q): %+v", name, resourceGroup, err) - } - } - - if existing.ID != nil && *existing.ID != "" { - return tf.ImportAsExistsError("azurerm_signalr_service", *existing.ID) - } - } - properties := &signalr.CreateOrUpdateProperties{ Cors: expandSignalRCors(cors), Features: expandSignalRFeatures(featureFlags), @@ -217,11 +215,11 @@ func resourceArmSignalRServiceCreateUpdate(d *schema.ResourceData, meta interfac return err } if read.ID == nil { - return fmt.Errorf("SignalR %q (Resource Group %q) ID is empty", name, resourceGroup) + return fmt.Errorf("SignalR Service %q (Resource Group %q) ID is empty", name, resourceGroup) } d.SetId(*read.ID) - return resourceArmSignalRServiceRead(d, meta) + return resourceArmSignalRServiceUpdate(d, meta) } func resourceArmSignalRServiceRead(d *schema.ResourceData, meta interface{}) error { @@ -283,6 +281,53 @@ func resourceArmSignalRServiceRead(d *schema.ResourceData, meta interface{}) err return tags.FlattenAndSet(d, resp.Tags) } +func resourceArmSignalRServiceUpdate(d *schema.ResourceData, meta interface{}) error { + client := meta.(*clients.Client).SignalR.Client + ctx, cancel := timeouts.ForUpdate(meta.(*clients.Client).StopContext, d) + defer cancel() + + id, err := parse.SignalRServiceID(d.Id()) + if err != nil { + return err + } + + parameters := &signalr.UpdateParameters{} + + if d.HasChanges("cors", "features") { + parameters.Properties = &signalr.CreateOrUpdateProperties{} + + if d.HasChange("cors") { + corsRaw := d.Get("cors").([]interface{}) + parameters.Properties.Cors = expandSignalRCors(corsRaw) + } + + if d.HasChange("features") { + featuresRaw := d.Get("features").([]interface{}) + parameters.Properties.Features = expandSignalRFeatures(featuresRaw) + } + } + + if d.HasChange("sku") { + sku := d.Get("sku").([]interface{}) + parameters.Sku = expandSignalRServiceSku(sku) + } + + if d.HasChange("tags") { + tagsRaw := d.Get("tags").(map[string]interface{}) + parameters.Tags = tags.Expand(tagsRaw) + } + + future, err := client.Update(ctx, id.ResourceGroup, id.Name, parameters) + if err != nil { + return fmt.Errorf("updating SignalR Service %q (Resource Group %q): %+v", id.Name, id.ResourceGroup, err) + } + if err = future.WaitForCompletionRef(ctx, client.Client); err != nil { + return fmt.Errorf("waiting for the update of SignalR Service %q (Resource Group %q): %+v", id.Name, id.ResourceGroup, err) + } + + return resourceArmSignalRServiceRead(d, meta) +} + func resourceArmSignalRServiceDelete(d *schema.ResourceData, meta interface{}) error { client := meta.(*clients.Client).SignalR.Client ctx, cancel := timeouts.ForDelete(meta.(*clients.Client).StopContext, d) @@ -296,13 +341,13 @@ func resourceArmSignalRServiceDelete(d *schema.ResourceData, meta interface{}) e future, err := client.Delete(ctx, id.ResourceGroup, id.Name) if err != nil { if !response.WasNotFound(future.Response()) { - return fmt.Errorf("Error deleting SignalR %q (Resource Group %q): %+v", id.Name, id.ResourceGroup, err) + return fmt.Errorf("deleting SignalR Service %q (Resource Group %q): %+v", id.Name, id.ResourceGroup, err) } return nil } if err := future.WaitForCompletionRef(ctx, client.Client); err != nil { if !response.WasNotFound(future.Response()) { - return fmt.Errorf("Error waiting for the deletion of SignalR %q (Resource Group %q): %+v", id.Name, id.ResourceGroup, err) + return fmt.Errorf("waiting for the deletion of SignalR Service %q (Resource Group %q): %+v", id.Name, id.ResourceGroup, err) } } @@ -326,19 +371,21 @@ func expandSignalRFeatures(input []interface{}) *[]signalr.Feature { } func flattenSignalRFeatures(features *[]signalr.Feature) []interface{} { + if features == nil { + return []interface{}{} + } + result := make([]interface{}, 0) - if features != nil { - for _, feature := range *features { - value := "" - if feature.Value != nil { - value = *feature.Value - } - - result = append(result, map[string]interface{}{ - "flag": string(feature.Flag), - "value": value, - }) + for _, feature := range *features { + value := "" + if feature.Value != nil { + value = *feature.Value } + + result = append(result, map[string]interface{}{ + "flag": string(feature.Flag), + "value": value, + }) } return result } @@ -395,15 +442,20 @@ func flattenSignalRServiceSku(input *signalr.ResourceSku) []interface{} { return []interface{}{} } - result := make(map[string]interface{}) + capacity := 0 + if input.Capacity != nil { + capacity = int(*input.Capacity) + } + name := "" if input.Name != nil { - result["name"] = *input.Name + name = *input.Name } - if input.Capacity != nil { - result["capacity"] = *input.Capacity + return []interface{}{ + map[string]interface{}{ + "capacity": capacity, + "name": name, + }, } - - return []interface{}{result} } From b2b9efd081061b400dc7f2b07e36f1e55b6410d9 Mon Sep 17 00:00:00 2001 From: tombuildsstuff Date: Mon, 21 Sep 2020 14:43:29 +0200 Subject: [PATCH 2/3] r/signalr_service: adding missing features The API adds features in the backgorund which can't be removed - this adds them in. --- .../signalr/tests/resource_arm_signalr_service_test.go | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/azurerm/internal/services/signalr/tests/resource_arm_signalr_service_test.go b/azurerm/internal/services/signalr/tests/resource_arm_signalr_service_test.go index 079c9b4cd367..0f681234e0bb 100644 --- a/azurerm/internal/services/signalr/tests/resource_arm_signalr_service_test.go +++ b/azurerm/internal/services/signalr/tests/resource_arm_signalr_service_test.go @@ -483,6 +483,16 @@ resource "azurerm_signalr_service" "test" { capacity = 1 } + features { + flag = "EnableConnectivityLogs" + value = "False" + } + + features { + flag = "EnableMessagingLogs" + value = "False" + } + features { flag = "ServiceMode" value = "%s" From 02c8b3f734f689fcadeca57ed28c5824c0ae26d4 Mon Sep 17 00:00:00 2001 From: tombuildsstuff Date: Mon, 21 Sep 2020 16:56:22 +0200 Subject: [PATCH 3/3] fixing the tests --- .../signalr/tests/resource_arm_signalr_service_test.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/azurerm/internal/services/signalr/tests/resource_arm_signalr_service_test.go b/azurerm/internal/services/signalr/tests/resource_arm_signalr_service_test.go index 0f681234e0bb..d018b8b8dd05 100644 --- a/azurerm/internal/services/signalr/tests/resource_arm_signalr_service_test.go +++ b/azurerm/internal/services/signalr/tests/resource_arm_signalr_service_test.go @@ -484,18 +484,18 @@ resource "azurerm_signalr_service" "test" { } features { - flag = "EnableConnectivityLogs" - value = "False" + flag = "ServiceMode" + value = "%s" } features { - flag = "EnableMessagingLogs" + flag = "EnableConnectivityLogs" value = "False" } features { - flag = "ServiceMode" - value = "%s" + flag = "EnableMessagingLogs" + value = "False" } } `, data.RandomInteger, data.Locations.Primary, data.RandomInteger, serviceMode)