diff --git a/internal/services/appservice/function_app_hybrid_connection_resource.go b/internal/services/appservice/function_app_hybrid_connection_resource.go index dda027467831..dd4a015e9e80 100644 --- a/internal/services/appservice/function_app_hybrid_connection_resource.go +++ b/internal/services/appservice/function_app_hybrid_connection_resource.go @@ -162,7 +162,7 @@ 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 } @@ -173,7 +173,7 @@ func (r FunctionAppHybridConnectionResource) Create() sdk.ResourceFunc { Hostname: pointer.To(appHybridConn.HostName), Port: pointer.To(appHybridConn.HostPort), SendKeyName: pointer.To(appHybridConn.SendKeyName), - SendKeyValue: key, + SendKeyValue: sendKeyValue, }, } diff --git a/internal/services/appservice/helpers/hybrid_connection.go b/internal/services/appservice/helpers/hybrid_connection.go index 0b25c1272954..20a0e8cb2fec 100644 --- a/internal/services/appservice/helpers/hybrid_connection.go +++ b/internal/services/appservice/helpers/hybrid_connection.go @@ -7,19 +7,31 @@ 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.NamespacesClient - connectionId := namespaces.NewAuthorizationRuleID(id.SubscriptionId, id.ResourceGroupName, id.HybridConnectionNamespaceName, sendKeyName) - keys, err := relayClient.ListKeys(ctx, connectionId) + relayNamespaceClient := metadata.Client.Relay.NamespacesClient + relayConnectionId := namespaces.NewAuthorizationRuleID(id.SubscriptionId, id.ResourceGroupName, id.HybridConnectionNamespaceName, sendKeyName) + relayKeys, err := relayNamespaceClient.ListKeys(ctx, relayConnectionId) + 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 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 %s in %s: %+v", connectionId, id, err) + return nil, fmt.Errorf("listing Send Keys for name %s for %s in %s: %+v", sendKeyName, connectionId, id, err) } - if err != nil || keys.Model == nil || keys.Model.PrimaryKey == nil { + if keys.Model == nil || keys.Model.PrimaryKey == nil { return nil, fmt.Errorf("reading Send Key Value for %s in %s", connectionId.AuthorizationRuleName, id) } return keys.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)