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_app_service_hybrid_connection - support relays in different namespaces #8370

Merged
merged 2 commits into from
Sep 12, 2020
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
@@ -1,10 +1,12 @@
package web

import (
"context"
"fmt"
"log"
"time"

relayMngt "github.com/Azure/azure-sdk-for-go/services/relay/mgmt/2017-04-01/relay"
"github.com/Azure/azure-sdk-for-go/services/web/mgmt/2019-08-01/web"
"github.com/hashicorp/go-azure-helpers/response"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
Expand Down Expand Up @@ -195,12 +197,18 @@ func resourceArmAppServiceHybridConnectionRead(d *schema.ResourceData, meta inte
}

// key values are not returned in the response, so we get the primary key from the relay namespace ListKeys func
relayNSClient := meta.(*clients.Client).Relay.NamespacesClient
accessKeys, err := relayNSClient.ListKeys(ctx, resourceGroup, *resp.ServiceBusNamespace, *resp.SendKeyName)
if err != nil {
return fmt.Errorf("unable to List Access Keys for Namespace %q (Resource Group %q): %+v", *resp.ServiceBusNamespace, resourceGroup, err)
} else {
d.Set("send_key_value", accessKeys.PrimaryKey)
if resp.ServiceBusNamespace != nil && resp.SendKeyName != nil {
relayNSClient := meta.(*clients.Client).Relay.NamespacesClient
relayNamespaceRG, err := findRelayNamespace(relayNSClient, ctx, *resp.ServiceBusNamespace)
if err != nil {
return err
}
accessKeys, err := relayNSClient.ListKeys(ctx, relayNamespaceRG, *resp.ServiceBusNamespace, *resp.SendKeyName)
if err != nil {
return fmt.Errorf("unable to List Access Keys for Namespace %q (Resource Group %q): %+v", *resp.ServiceBusNamespace, resourceGroup, err)
} else {
d.Set("send_key_value", accessKeys.PrimaryKey)
}
}

return nil
Expand Down Expand Up @@ -230,3 +238,32 @@ func resourceArmAppServiceHybridConnectionDelete(d *schema.ResourceData, meta in

return nil
}

func findRelayNamespace(client *relayMngt.NamespacesClient, ctx context.Context, name string) (string, error) {
relayNSIterator, err := client.ListComplete(ctx)
if err != nil {
return "", fmt.Errorf("listing Relay Namespaces: %+v", err)
}

var found *relayMngt.Namespace
for relayNSIterator.NotDone() {
namespace := relayNSIterator.Value()
if namespace.Name != nil && *namespace.Name == name {
found = &namespace
break
}
if err := relayNSIterator.NextWithContext(ctx); err != nil {
return "", fmt.Errorf("listing Relay Namespaces: %+v", err)
}
}

if found == nil || found.ID == nil {
return "", fmt.Errorf("could not find Relay Namespace with name: %q", name)
}

id, err := relay.ParseNamespaceID(*found.ID)
if err != nil {
return "", fmt.Errorf("relay Namespace id not valid: %+v", err)
}
return id.ResourceGroup, nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,25 @@ func TestAccAzureRMAppServiceHybridConnection_requiresImport(t *testing.T) {
})
}

func TestAccAzureRMAppServiceHybridConnection_differentResourceGroup(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_app_service_hybrid_connection", "test")

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acceptance.PreCheck(t) },
Providers: acceptance.SupportedProviders,
CheckDestroy: testCheckAzureRMAppServiceHybridConnectionDestroy,
Steps: []resource.TestStep{
{
Config: testAccAzureRMAppServiceHybridConnection_differentResourceGroup(data),
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMAppServiceHybridConnectionExists(data.ResourceName),
),
},
data.ImportStep(),
},
})
}

func testCheckAzureRMAppServiceHybridConnectionDestroy(s *terraform.State) error {
client := acceptance.AzureProvider.Meta().(*clients.Client).Web.AppServicesClient

Expand Down Expand Up @@ -221,3 +240,63 @@ resource "azurerm_app_service_hybrid_connection" "import" {
}
`, template)
}

func testAccAzureRMAppServiceHybridConnection_differentResourceGroup(data acceptance.TestData) string {
return fmt.Sprintf(`
provider "azurerm" {
features {}
}

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

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

resource "azurerm_app_service_plan" "test" {
name = "acctest-ASP-%d"
location = azurerm_resource_group.test.location
resource_group_name = azurerm_resource_group.test.name

sku {
tier = "Standard"
size = "S1"
}
}

resource "azurerm_app_service" "test" {
name = "acctest-AS-%d"
location = azurerm_resource_group.test.location
resource_group_name = azurerm_resource_group.test.name
app_service_plan_id = azurerm_app_service_plan.test.id
}

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

sku_name = "Standard"
}

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

resource "azurerm_app_service_hybrid_connection" "test" {
app_service_name = azurerm_app_service.test.name
resource_group_name = azurerm_resource_group.test.name
relay_id = azurerm_relay_hybrid_connection.test.id
hostname = "testhostname.azuretest"
port = 443
send_key_name = "RootManageSharedAccessKey"
}
`, data.RandomInteger, data.Locations.Primary, data.RandomInteger, data.Locations.Primary, data.RandomInteger, data.RandomInteger, data.RandomInteger, data.RandomInteger)
}