Skip to content

Commit

Permalink
Merge pull request #25379 from stuartjbrown/main
Browse files Browse the repository at this point in the history
`web_app_hybrid_connection_resource` and `function_app_hybrid_connection_resource` should retrieve the sendKey by either hybrid connection ID or the relay ID
  • Loading branch information
manicminer authored Mar 27, 2024
2 parents c836611 + 1914f6d commit 1b47379
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 11 deletions.
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

0 comments on commit 1b47379

Please sign in to comment.