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

web_app_hybrid_connection_resource and function_app_hybrid_connection_resource should retrieve the sendKey by either hybrid connection ID or the relay ID #25379

Merged
merged 14 commits into from
Mar 27, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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,
},
}

Expand Down
22 changes: 17 additions & 5 deletions internal/services/appservice/helpers/hybrid_connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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,
},
}

Expand Down Expand Up @@ -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)
Expand Down
Loading