From 9ce417d8a2b53ed0ee7137398b9eae3b5d787b77 Mon Sep 17 00:00:00 2001 From: Stuart Brown Date: Thu, 2 Nov 2023 14:10:57 +0100 Subject: [PATCH 01/10] fix: populate the sendKeyValue when creating the WebApp hybrid connection --- .../appservice/web_app_hybrid_connection_resource.go | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/internal/services/appservice/web_app_hybrid_connection_resource.go b/internal/services/appservice/web_app_hybrid_connection_resource.go index 481e32836a16..688639dd474a 100644 --- a/internal/services/appservice/web_app_hybrid_connection_resource.go +++ b/internal/services/appservice/web_app_hybrid_connection_resource.go @@ -163,13 +163,18 @@ func (r WebAppHybridConnectionResource) Create() sdk.ResourceFunc { return tf.ImportAsExistsError(r.ResourceType(), *existing.ID) } + key, err := helpers.GetSendKeyValue(ctx, metadata, id, appHybridConn.SendKeyName) + if err != nil { + return err + } + envelope := web.HybridConnection{ HybridConnectionProperties: &web.HybridConnectionProperties{ RelayArmURI: utils.String(relayId.ID()), Hostname: utils.String(appHybridConn.HostName), Port: utils.Int32(int32(appHybridConn.HostPort)), SendKeyName: utils.String(appHybridConn.SendKeyName), - SendKeyValue: utils.String(""), + SendKeyValue: utils.String(*key), }, } From 76eabfe7e9b0028c80edf2a2a52be07fb454ea8c Mon Sep 17 00:00:00 2001 From: kt Date: Thu, 8 Feb 2024 17:36:43 -0800 Subject: [PATCH 02/10] Update web_app_hybrid_connection_resource.go --- .../web_app_hybrid_connection_resource.go | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/internal/services/appservice/web_app_hybrid_connection_resource.go b/internal/services/appservice/web_app_hybrid_connection_resource.go index 688639dd474a..fb26599b6001 100644 --- a/internal/services/appservice/web_app_hybrid_connection_resource.go +++ b/internal/services/appservice/web_app_hybrid_connection_resource.go @@ -168,13 +168,13 @@ func (r WebAppHybridConnectionResource) Create() sdk.ResourceFunc { return err } - envelope := web.HybridConnection{ - HybridConnectionProperties: &web.HybridConnectionProperties{ - RelayArmURI: utils.String(relayId.ID()), - Hostname: utils.String(appHybridConn.HostName), - Port: utils.Int32(int32(appHybridConn.HostPort)), - SendKeyName: utils.String(appHybridConn.SendKeyName), - SendKeyValue: utils.String(*key), + envelope := webapps.HybridConnection{ + Properties: &webapps.HybridConnectionProperties{ + RelayArmUri: pointer.To(relayId.ID()), + Hostname: pointer.To(appHybridConn.HostName), + Port: pointer.To(appHybridConn.HostPort), + SendKeyName: pointer.To(appHybridConn.SendKeyName), + SendKeyValue: key, }, } From 4e2b8cd89b2731a8944cce3b8e01e2903b94e0bd Mon Sep 17 00:00:00 2001 From: Stuart Brown Date: Mon, 12 Feb 2024 08:31:46 +0100 Subject: [PATCH 03/10] fix: the send key value is already a string pointer --- .../services/appservice/web_app_hybrid_connection_resource.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/services/appservice/web_app_hybrid_connection_resource.go b/internal/services/appservice/web_app_hybrid_connection_resource.go index 1e1d435f6c75..9cce3876315f 100644 --- a/internal/services/appservice/web_app_hybrid_connection_resource.go +++ b/internal/services/appservice/web_app_hybrid_connection_resource.go @@ -173,7 +173,7 @@ func (r WebAppHybridConnectionResource) Create() sdk.ResourceFunc { Hostname: pointer.To(appHybridConn.HostName), Port: pointer.To(appHybridConn.HostPort), SendKeyName: pointer.To(appHybridConn.SendKeyName), - SendKeyValue: pointer.To(key), + SendKeyValue: key, }, } From 6cbb1b0f073f8cf2716e48dd8bfe0901b2be4c97 Mon Sep 17 00:00:00 2001 From: Stuart Brown Date: Mon, 12 Feb 2024 08:38:10 +0100 Subject: [PATCH 04/10] feat: retrieve and use sendKeyValue for function app hybrid connections --- .../appservice/function_app_hybrid_connection_resource.go | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/internal/services/appservice/function_app_hybrid_connection_resource.go b/internal/services/appservice/function_app_hybrid_connection_resource.go index b799ed9907fb..dda027467831 100644 --- a/internal/services/appservice/function_app_hybrid_connection_resource.go +++ b/internal/services/appservice/function_app_hybrid_connection_resource.go @@ -162,13 +162,18 @@ func (r FunctionAppHybridConnectionResource) Create() sdk.ResourceFunc { return tf.ImportAsExistsError(r.ResourceType(), id.ID()) } + key, err := helpers.GetSendKeyValue(ctx, metadata, id, appHybridConn.SendKeyName) + if err != nil { + return err + } + envelope := webapps.HybridConnection{ Properties: &webapps.HybridConnectionProperties{ RelayArmUri: pointer.To(relayId.ID()), Hostname: pointer.To(appHybridConn.HostName), Port: pointer.To(appHybridConn.HostPort), SendKeyName: pointer.To(appHybridConn.SendKeyName), - SendKeyValue: pointer.To(""), + SendKeyValue: key, }, } From 411c4172f2b5015023d7e02c11416c0cf242a5f0 Mon Sep 17 00:00:00 2001 From: Stuart Brown Date: Mon, 12 Feb 2024 08:39:09 +0100 Subject: [PATCH 05/10] chore: formatting --- .../services/appservice/web_app_hybrid_connection_resource.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/services/appservice/web_app_hybrid_connection_resource.go b/internal/services/appservice/web_app_hybrid_connection_resource.go index 9cce3876315f..6720398c7b1a 100644 --- a/internal/services/appservice/web_app_hybrid_connection_resource.go +++ b/internal/services/appservice/web_app_hybrid_connection_resource.go @@ -173,7 +173,7 @@ func (r WebAppHybridConnectionResource) Create() sdk.ResourceFunc { Hostname: pointer.To(appHybridConn.HostName), Port: pointer.To(appHybridConn.HostPort), SendKeyName: pointer.To(appHybridConn.SendKeyName), - SendKeyValue: key, + SendKeyValue: key, }, } From d359c48d9324f274449e421074f6e2e1e12d723d Mon Sep 17 00:00:00 2001 From: Stuart Brown Date: Mon, 18 Mar 2024 11:28:48 +0100 Subject: [PATCH 06/10] fix: hybrid connection uses authorization rule of the hybrid connection not the namespace --- internal/services/appservice/helpers/hybrid_connection.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/internal/services/appservice/helpers/hybrid_connection.go b/internal/services/appservice/helpers/hybrid_connection.go index 0b25c1272954..0187bc478f1a 100644 --- a/internal/services/appservice/helpers/hybrid_connection.go +++ b/internal/services/appservice/helpers/hybrid_connection.go @@ -7,14 +7,14 @@ import ( "context" "fmt" - "github.com/hashicorp/go-azure-sdk/resource-manager/relay/2021-11-01/namespaces" + "github.com/hashicorp/go-azure-sdk/resource-manager/relay/2021-11-01/hybridconnections" "github.com/hashicorp/go-azure-sdk/resource-manager/web/2023-01-01/webapps" "github.com/hashicorp/terraform-provider-azurerm/internal/sdk" ) func GetSendKeyValue(ctx context.Context, metadata sdk.ResourceMetaData, id webapps.RelayId, sendKeyName string) (*string, error) { - relayClient := metadata.Client.Relay.NamespacesClient - connectionId := namespaces.NewAuthorizationRuleID(id.SubscriptionId, id.ResourceGroupName, id.HybridConnectionNamespaceName, sendKeyName) + relayClient := metadata.Client.Relay.HybridConnectionsClient + connectionId := hybridconnections.NewHybridConnectionAuthorizationRuleID(id.SubscriptionId, id.ResourceGroupName, id.HybridConnectionNamespaceName, id.RelayName, sendKeyName) keys, err := relayClient.ListKeys(ctx, connectionId) if err != nil { return nil, fmt.Errorf("listing Send Keys for %s in %s: %+v", connectionId, id, err) From a2aeebb9b0f5ca44c2ebf975a8a83e260f18cefc Mon Sep 17 00:00:00 2001 From: Stuart Brown Date: Fri, 22 Mar 2024 16:01:01 +0100 Subject: [PATCH 07/10] chore: fmt --- internal/services/appservice/helpers/hybrid_connection.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/services/appservice/helpers/hybrid_connection.go b/internal/services/appservice/helpers/hybrid_connection.go index 0187bc478f1a..40d3a9f0fd8f 100644 --- a/internal/services/appservice/helpers/hybrid_connection.go +++ b/internal/services/appservice/helpers/hybrid_connection.go @@ -14,7 +14,7 @@ import ( func GetSendKeyValue(ctx context.Context, metadata sdk.ResourceMetaData, id webapps.RelayId, sendKeyName string) (*string, error) { relayClient := metadata.Client.Relay.HybridConnectionsClient - connectionId := hybridconnections.NewHybridConnectionAuthorizationRuleID(id.SubscriptionId, id.ResourceGroupName, id.HybridConnectionNamespaceName, id.RelayName, sendKeyName) + connectionId := hybridconnections.NewHybridConnectionAuthorizationRuleID(id.SubscriptionId, id.ResourceGroupName, id.HybridConnectionNamespaceName, id.RelayName, sendKeyName) keys, err := relayClient.ListKeys(ctx, connectionId) if err != nil { return nil, fmt.Errorf("listing Send Keys for %s in %s: %+v", connectionId, id, err) From febea3a53be308613f8adbd437fd2b97fe473403 Mon Sep 17 00:00:00 2001 From: Stuart Brown Date: Wed, 27 Mar 2024 12:34:21 +0100 Subject: [PATCH 08/10] fix: try to get the sendKeyValue on both the HC and the Relay authorization rule --- ...function_app_hybrid_connection_resource.go | 14 ++++++----- .../appservice/helpers/hybrid_connection.go | 24 ++++++++++++++----- .../web_app_hybrid_connection_resource.go | 8 +++---- 3 files changed, 30 insertions(+), 16 deletions(-) diff --git a/internal/services/appservice/function_app_hybrid_connection_resource.go b/internal/services/appservice/function_app_hybrid_connection_resource.go index dda027467831..4c35b9874f95 100644 --- a/internal/services/appservice/function_app_hybrid_connection_resource.go +++ b/internal/services/appservice/function_app_hybrid_connection_resource.go @@ -162,21 +162,23 @@ func (r FunctionAppHybridConnectionResource) Create() sdk.ResourceFunc { return tf.ImportAsExistsError(r.ResourceType(), id.ID()) } - key, err := helpers.GetSendKeyValue(ctx, metadata, id, appHybridConn.SendKeyName) + sendKeyValue, err := helpers.GetSendKeyValue(ctx, metadata, id, appHybridConn.SendKeyName) if err != nil { return err } envelope := webapps.HybridConnection{ Properties: &webapps.HybridConnectionProperties{ - RelayArmUri: pointer.To(relayId.ID()), - Hostname: pointer.To(appHybridConn.HostName), - Port: pointer.To(appHybridConn.HostPort), - SendKeyName: pointer.To(appHybridConn.SendKeyName), - SendKeyValue: key, + RelayArmUri: pointer.To(relayId.ID()), + Hostname: pointer.To(appHybridConn.HostName), + Port: pointer.To(appHybridConn.HostPort), + SendKeyName: pointer.To(appHybridConn.SendKeyName), + SendKeyValue: sendKeyValue, }, } + + _, err = client.CreateOrUpdateHybridConnection(ctx, id, envelope) if err != nil { return fmt.Errorf("creating %s: %+v", id, err) diff --git a/internal/services/appservice/helpers/hybrid_connection.go b/internal/services/appservice/helpers/hybrid_connection.go index 40d3a9f0fd8f..2f4790c2c9a7 100644 --- a/internal/services/appservice/helpers/hybrid_connection.go +++ b/internal/services/appservice/helpers/hybrid_connection.go @@ -7,20 +7,32 @@ import ( "context" "fmt" + "github.com/hashicorp/go-azure-helpers/lang/response" "github.com/hashicorp/go-azure-sdk/resource-manager/relay/2021-11-01/hybridconnections" + "github.com/hashicorp/go-azure-sdk/resource-manager/relay/2021-11-01/namespaces" "github.com/hashicorp/go-azure-sdk/resource-manager/web/2023-01-01/webapps" "github.com/hashicorp/terraform-provider-azurerm/internal/sdk" ) func GetSendKeyValue(ctx context.Context, metadata sdk.ResourceMetaData, id webapps.RelayId, sendKeyName string) (*string, error) { - relayClient := metadata.Client.Relay.HybridConnectionsClient + hybridConnectionsClient := metadata.Client.Relay.HybridConnectionsClient connectionId := hybridconnections.NewHybridConnectionAuthorizationRuleID(id.SubscriptionId, id.ResourceGroupName, id.HybridConnectionNamespaceName, id.RelayName, sendKeyName) - keys, err := relayClient.ListKeys(ctx, connectionId) + keys, err := hybridConnectionsClient.ListKeys(ctx, connectionId) + if err != nil && !response.WasNotFound(keys.HttpResponse) { + return nil, fmt.Errorf("listing Send Keys for name %s for %s in %s: %+v", sendKeyName, connectionId, id, err) + } + if keys.Model != nil && keys.Model.PrimaryKey != nil { + return keys.Model.PrimaryKey, nil + } + + relayNamespaceClient := metadata.Client.Relay.NamespacesClient + relayConnectionId := namespaces.NewAuthorizationRuleID(id.SubscriptionId, id.ResourceGroupName, id.HybridConnectionNamespaceName, sendKeyName) + relayKeys, err := relayNamespaceClient.ListKeys(ctx, relayConnectionId) if err != nil { - return nil, fmt.Errorf("listing Send Keys for %s in %s: %+v", connectionId, id, err) + return nil, fmt.Errorf("listing Send Keys for name %s for %s in %s: %+v", sendKeyName, relayConnectionId, id, err) } - if err != nil || keys.Model == nil || keys.Model.PrimaryKey == nil { - return nil, fmt.Errorf("reading Send Key Value for %s in %s", connectionId.AuthorizationRuleName, id) + if relayKeys.Model == nil || relayKeys.Model.PrimaryKey == nil { + return nil, fmt.Errorf("reading Send Key Value for %s in %s", relayConnectionId.AuthorizationRuleName, id) } - return keys.Model.PrimaryKey, nil + return relayKeys.Model.PrimaryKey, nil } diff --git a/internal/services/appservice/web_app_hybrid_connection_resource.go b/internal/services/appservice/web_app_hybrid_connection_resource.go index 6720398c7b1a..478f995d4bb6 100644 --- a/internal/services/appservice/web_app_hybrid_connection_resource.go +++ b/internal/services/appservice/web_app_hybrid_connection_resource.go @@ -162,7 +162,7 @@ func (r WebAppHybridConnectionResource) Create() sdk.ResourceFunc { return tf.ImportAsExistsError(r.ResourceType(), id.ID()) } - key, err := helpers.GetSendKeyValue(ctx, metadata, id, appHybridConn.SendKeyName) + sendKeyValue, err := helpers.GetSendKeyValue(ctx, metadata, id, appHybridConn.SendKeyName) if err != nil { return err } @@ -173,7 +173,7 @@ func (r WebAppHybridConnectionResource) Create() sdk.ResourceFunc { Hostname: pointer.To(appHybridConn.HostName), Port: pointer.To(appHybridConn.HostPort), SendKeyName: pointer.To(appHybridConn.SendKeyName), - SendKeyValue: key, + SendKeyValue: sendKeyValue, }, } @@ -311,11 +311,11 @@ func (r WebAppHybridConnectionResource) Update() sdk.ResourceFunc { } if metadata.ResourceData.HasChange("send_key_name") { - key, err := helpers.GetSendKeyValue(ctx, metadata, *id, appHybridConn.SendKeyName) + sendKeyValue, err := helpers.GetSendKeyValue(ctx, metadata, *id, appHybridConn.SendKeyName) if err != nil { return err } - model.Properties.SendKeyValue = key + model.Properties.SendKeyValue = sendKeyValue } _, err = client.CreateOrUpdateHybridConnection(ctx, *id, model) From a1d29574e2250b7b21a108d41c18f431d0c16b30 Mon Sep 17 00:00:00 2001 From: Stuart Brown Date: Wed, 27 Mar 2024 12:38:08 +0100 Subject: [PATCH 09/10] chore: format fix --- .../function_app_hybrid_connection_resource.go | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/internal/services/appservice/function_app_hybrid_connection_resource.go b/internal/services/appservice/function_app_hybrid_connection_resource.go index 4c35b9874f95..dd4a015e9e80 100644 --- a/internal/services/appservice/function_app_hybrid_connection_resource.go +++ b/internal/services/appservice/function_app_hybrid_connection_resource.go @@ -169,16 +169,14 @@ func (r FunctionAppHybridConnectionResource) Create() sdk.ResourceFunc { envelope := webapps.HybridConnection{ Properties: &webapps.HybridConnectionProperties{ - RelayArmUri: pointer.To(relayId.ID()), - Hostname: pointer.To(appHybridConn.HostName), - Port: pointer.To(appHybridConn.HostPort), - SendKeyName: pointer.To(appHybridConn.SendKeyName), + RelayArmUri: pointer.To(relayId.ID()), + Hostname: pointer.To(appHybridConn.HostName), + Port: pointer.To(appHybridConn.HostPort), + SendKeyName: pointer.To(appHybridConn.SendKeyName), SendKeyValue: sendKeyValue, }, } - - _, err = client.CreateOrUpdateHybridConnection(ctx, id, envelope) if err != nil { return fmt.Errorf("creating %s: %+v", id, err) From ec135de9ef84b2eb9766efd05440ab6ae58a5bfe Mon Sep 17 00:00:00 2001 From: Stuart Brown Date: Wed, 27 Mar 2024 13:49:30 +0100 Subject: [PATCH 10/10] chore: align GetSendKeyValue helper logic with that of Read() --- .../appservice/helpers/hybrid_connection.go | 28 +++++++++---------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/internal/services/appservice/helpers/hybrid_connection.go b/internal/services/appservice/helpers/hybrid_connection.go index 2f4790c2c9a7..20a0e8cb2fec 100644 --- a/internal/services/appservice/helpers/hybrid_connection.go +++ b/internal/services/appservice/helpers/hybrid_connection.go @@ -15,24 +15,24 @@ import ( ) func GetSendKeyValue(ctx context.Context, metadata sdk.ResourceMetaData, id webapps.RelayId, sendKeyName string) (*string, error) { - hybridConnectionsClient := metadata.Client.Relay.HybridConnectionsClient - connectionId := hybridconnections.NewHybridConnectionAuthorizationRuleID(id.SubscriptionId, id.ResourceGroupName, id.HybridConnectionNamespaceName, id.RelayName, sendKeyName) - keys, err := hybridConnectionsClient.ListKeys(ctx, connectionId) - if err != nil && !response.WasNotFound(keys.HttpResponse) { - return nil, fmt.Errorf("listing Send Keys for name %s for %s in %s: %+v", sendKeyName, connectionId, id, err) - } - if keys.Model != nil && keys.Model.PrimaryKey != nil { - return keys.Model.PrimaryKey, nil - } - relayNamespaceClient := metadata.Client.Relay.NamespacesClient relayConnectionId := namespaces.NewAuthorizationRuleID(id.SubscriptionId, id.ResourceGroupName, id.HybridConnectionNamespaceName, sendKeyName) relayKeys, err := relayNamespaceClient.ListKeys(ctx, relayConnectionId) - if err != nil { + if err != nil && !response.WasNotFound(relayKeys.HttpResponse) { return nil, fmt.Errorf("listing Send Keys for name %s for %s in %s: %+v", sendKeyName, relayConnectionId, id, err) } - if relayKeys.Model == nil || relayKeys.Model.PrimaryKey == nil { - return nil, fmt.Errorf("reading Send Key Value for %s in %s", relayConnectionId.AuthorizationRuleName, id) + if relayKeys.Model != nil && relayKeys.Model.PrimaryKey != nil { + return relayKeys.Model.PrimaryKey, nil + } + + hybridConnectionsClient := metadata.Client.Relay.HybridConnectionsClient + connectionId := hybridconnections.NewHybridConnectionAuthorizationRuleID(id.SubscriptionId, id.ResourceGroupName, id.HybridConnectionNamespaceName, id.RelayName, sendKeyName) + keys, err := hybridConnectionsClient.ListKeys(ctx, connectionId) + if err != nil { + return nil, fmt.Errorf("listing Send Keys for name %s for %s in %s: %+v", sendKeyName, connectionId, id, err) + } + if keys.Model == nil || keys.Model.PrimaryKey == nil { + return nil, fmt.Errorf("reading Send Key Value for %s in %s", connectionId.AuthorizationRuleName, id) } - return relayKeys.Model.PrimaryKey, nil + return keys.Model.PrimaryKey, nil }