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

azurerm_function_app_hybrid_connection and azurerm_web_app_hybrid_connection: allow use of an authorization rule for a relay in a different resource group #25541

Merged
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())
}

sendKeyValue, err := helpers.GetSendKeyValue(ctx, metadata, id, appHybridConn.SendKeyName)
sendKeyValue, err := helpers.GetSendKeyValue(ctx, metadata, *relayId, appHybridConn.SendKeyName)
if err != nil {
return err
}
Expand Down Expand Up @@ -312,7 +312,12 @@ func (r FunctionAppHybridConnectionResource) Update() sdk.ResourceFunc {
}

if metadata.ResourceData.HasChange("send_key_name") {
key, err := helpers.GetSendKeyValue(ctx, metadata, *id, appHybridConn.SendKeyName)
relayId, err := hybridconnections.ParseHybridConnectionID(appHybridConn.RelayId)
if err != nil {
return err
}

key, err := helpers.GetSendKeyValue(ctx, metadata, *relayId, appHybridConn.SendKeyName)
if err != nil {
return err
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,63 @@ resource "azurerm_windows_function_app" "test" {
`, data.RandomInteger, data.Locations.Primary, SkuBasicPlan, data.RandomString)
}

func (r FunctionAppHybridConnectionResource) templateRelayInOtherResourceGroup(data acceptance.TestData) string {
return fmt.Sprintf(`
resource "azurerm_resource_group" "test" {
name = "acctestRG-%[1]d"
location = "%[2]s"
}

resource "azurerm_service_plan" "test" {
name = "acctestASP-%[1]d"
location = azurerm_resource_group.test.location
resource_group_name = azurerm_resource_group.test.name
os_type = "Windows"
sku_name = "%[3]s"
}

resource "azurerm_storage_account" "test" {
name = "acctestsa%[4]s"
resource_group_name = azurerm_resource_group.test.name
location = azurerm_resource_group.test.location
account_tier = "Standard"
account_replication_type = "LRS"
}

resource "azurerm_resource_group" "rg-test-relay" {
name = "acctestRG-relay-%[1]d"
location = "%[2]s"
}

resource "azurerm_relay_namespace" "test" {
name = "acctest-RN-%[1]d"
location = azurerm_resource_group.rg-test-relay.location
resource_group_name = azurerm_resource_group.rg-test-relay.name

sku_name = "Standard"
}

resource "azurerm_relay_hybrid_connection" "test" {
name = "acctest-RHC-%[1]d"
resource_group_name = azurerm_resource_group.rg-test-relay.name
relay_namespace_name = azurerm_relay_namespace.test.name
user_metadata = "metadatatest"
}

resource "azurerm_windows_function_app" "test" {
name = "acctest-WFA-%[1]d"
location = azurerm_resource_group.test.location
resource_group_name = azurerm_resource_group.test.name
service_plan_id = azurerm_service_plan.test.id

storage_account_name = azurerm_storage_account.test.name
storage_account_access_key = azurerm_storage_account.test.primary_access_key

site_config {}
}
`, data.RandomInteger, data.Locations.Primary, SkuBasicPlan, data.RandomString)
}

func (r FunctionAppHybridConnectionResource) authRuleTemplate(data acceptance.TestData) string {
return fmt.Sprintf(`
%s
Expand All @@ -276,14 +333,9 @@ func (r FunctionAppHybridConnectionResource) authRuleInRemoteResourceGroupTempla
return fmt.Sprintf(`
%s

resource "azurerm_resource_group" "relay" {
name = "acctestRG-%d"
location = "%s"
}

resource "azurerm_relay_hybrid_connection_authorization_rule" "test" {
name = "sendKey"
resource_group_name = azurerm_resource_group.relay.name
resource_group_name = azurerm_resource_group.rg-test-relay.name
hybrid_connection_name = azurerm_relay_hybrid_connection.test.name
namespace_name = azurerm_relay_namespace.test.name

Expand All @@ -292,5 +344,5 @@ resource "azurerm_relay_hybrid_connection_authorization_rule" "test" {
manage = false
}

`, r.template(data), data.RandomInteger, data.Locations.Primary)
`, r.templateRelayInOtherResourceGroup(data))
}
7 changes: 3 additions & 4 deletions internal/services/appservice/helpers/hybrid_connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,12 @@ import (
"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) {
func GetSendKeyValue(ctx context.Context, metadata sdk.ResourceMetaData, id hybridconnections.HybridConnectionId, sendKeyName string) (*string, error) {
relayNamespaceClient := metadata.Client.Relay.NamespacesClient
relayConnectionId := namespaces.NewAuthorizationRuleID(id.SubscriptionId, id.ResourceGroupName, id.HybridConnectionNamespaceName, sendKeyName)
relayConnectionId := namespaces.NewAuthorizationRuleID(id.SubscriptionId, id.ResourceGroupName, id.NamespaceName, 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)
Expand All @@ -26,7 +25,7 @@ func GetSendKeyValue(ctx context.Context, metadata sdk.ResourceMetaData, id weba
}

hybridConnectionsClient := metadata.Client.Relay.HybridConnectionsClient
connectionId := hybridconnections.NewHybridConnectionAuthorizationRuleID(id.SubscriptionId, id.ResourceGroupName, id.HybridConnectionNamespaceName, id.RelayName, sendKeyName)
connectionId := hybridconnections.NewHybridConnectionAuthorizationRuleID(id.SubscriptionId, id.ResourceGroupName, id.NamespaceName, id.HybridConnectionName, 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)
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())
}

sendKeyValue, err := helpers.GetSendKeyValue(ctx, metadata, id, appHybridConn.SendKeyName)
sendKeyValue, err := helpers.GetSendKeyValue(ctx, metadata, *relayId, appHybridConn.SendKeyName)
if err != nil {
return err
}
Expand Down Expand Up @@ -311,7 +311,12 @@ func (r WebAppHybridConnectionResource) Update() sdk.ResourceFunc {
}

if metadata.ResourceData.HasChange("send_key_name") {
sendKeyValue, err := helpers.GetSendKeyValue(ctx, metadata, *id, appHybridConn.SendKeyName)
relayId, err := hybridconnections.ParseHybridConnectionID(appHybridConn.RelayId)
if err != nil {
return err
}

sendKeyValue, err := helpers.GetSendKeyValue(ctx, metadata, *relayId, appHybridConn.SendKeyName)
if err != nil {
return err
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,52 @@ resource "azurerm_windows_web_app" "test" {
`, data.RandomInteger, data.Locations.Primary, SkuBasicPlan)
}

func (r WebAppHybridConnectionResource) templateRelayInOtherResourceGroup(data acceptance.TestData) string {
return fmt.Sprintf(`
resource "azurerm_resource_group" "rg-test-relay" {
name = "acctestRG-%[1]d-relay"
location = "%[2]s"
}

resource "azurerm_relay_namespace" "test" {
name = "acctest-RN-%[1]d"
location = azurerm_resource_group.rg-test-relay.location
resource_group_name = azurerm_resource_group.rg-test-relay.name

sku_name = "Standard"
}

resource "azurerm_relay_hybrid_connection" "test" {
name = "acctest-RHC-%[1]d"
resource_group_name = azurerm_resource_group.rg-test-relay.name
relay_namespace_name = azurerm_relay_namespace.test.name
user_metadata = "metadatatest"
}

resource "azurerm_resource_group" "test" {
name = "acctestRG-%[1]d"
location = "%[2]s"
}

resource "azurerm_service_plan" "test" {
name = "acctestASP-%[1]d"
location = azurerm_resource_group.test.location
resource_group_name = azurerm_resource_group.test.name
os_type = "Windows"
sku_name = "%[3]s"
}

resource "azurerm_windows_web_app" "test" {
name = "acctestWA-%[1]d"
location = azurerm_resource_group.test.location
resource_group_name = azurerm_resource_group.test.name
service_plan_id = azurerm_service_plan.test.id

site_config {}
}
`, data.RandomInteger, data.Locations.Primary, SkuBasicPlan)
}

func (r WebAppHybridConnectionResource) authRuleTemplate(data acceptance.TestData) string {
return fmt.Sprintf(`
%s
Expand All @@ -266,14 +312,9 @@ func (r WebAppHybridConnectionResource) authRuleInRemoteResourceGroupTemplate(da
return fmt.Sprintf(`
%s

resource "azurerm_resource_group" "relay" {
name = "acctestRG-%d"
location = "%s"
}

resource "azurerm_relay_hybrid_connection_authorization_rule" "test" {
name = "sendKey"
resource_group_name = azurerm_resource_group.relay.name
resource_group_name = azurerm_resource_group.rg-test-relay.name
hybrid_connection_name = azurerm_relay_hybrid_connection.test.name
namespace_name = azurerm_relay_namespace.test.name

Expand All @@ -282,5 +323,5 @@ resource "azurerm_relay_hybrid_connection_authorization_rule" "test" {
manage = false
}

`, r.template(data), data.RandomInteger, data.Locations.Primary)
`, r.templateRelayInOtherResourceGroup(data))
}
Loading