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_network_manager - support management group register provider #19853

Closed
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
18 changes: 18 additions & 0 deletions internal/services/network/network_manager_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-provider-azurerm/helpers/azure"
"github.com/hashicorp/terraform-provider-azurerm/internal/sdk"
managementGroupParse "github.com/hashicorp/terraform-provider-azurerm/internal/services/managementgroup/parse"
managementGroupValidate "github.com/hashicorp/terraform-provider-azurerm/internal/services/managementgroup/validate"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/network/parse"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/network/validate"
Expand Down Expand Up @@ -175,6 +176,23 @@ func (r ManagerResource) Create() sdk.ResourceFunc {
return metadata.ResourceRequiresImport(r.ResourceType(), id)
}

if len(state.Scope[0].ManagementGroups) > 0 {
metadata.Logger.Info("try to register 'Microsoft.Network' provider to the Management Group scope")
resourceProvidersClient := metadata.Client.Resource.ResourceProvidersClient
for _, group := range state.Scope[0].ManagementGroups {
groupId, err := managementGroupParse.ManagementGroupID(group)
if err != nil {
return err
}
_, err = resourceProvidersClient.RegisterAtManagementGroupScope(ctx, "Microsoft.Network", groupId.Name)
if err != nil {
return fmt.Errorf("registering the 'Microsoft.Network' provider with Management Group %s: %+v", groupId, err)
}
}
Comment on lines +179 to +191
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you give some more context on the issue here? Terraform shouldn't be silently registering resource providers at management group scopes which generally require extra-permissions

// https://github.com/Azure/azure-rest-api-specs/issues/22041
time.Sleep(10 * time.Second)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Resource Provider registration takes up to 15 minutes, and unfortunately time.Sleep's aren't something we can ship/rely on - as such I think we can remove all of this RP auto-registration logic here?

}

input := network.Manager{
Location: utils.String(azure.NormalizeLocation(state.Location)),
Name: utils.String(state.Name),
Expand Down
59 changes: 21 additions & 38 deletions internal/services/network/network_manager_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,34 +15,7 @@ import (

type ManagerResource struct{}

func TestAccNetworkManager(t *testing.T) {
// NOTE: this is a combined test rather than separate split out tests due to
// Azure only being happy about provisioning one network manager per subscription at once
// (which our test suite can't easily work around)

testCases := map[string]map[string]func(t *testing.T){
"Manager": {
"basic": testAccNetworkManager_basic,
"complete": testAccNetworkManager_complete,
"update": testAccNetworkManager_update,
"requiresImport": testAccNetworkManager_requiresImport,
},
}

for group, m := range testCases {
m := m
t.Run(group, func(t *testing.T) {
for name, tc := range m {
tc := tc
t.Run(name, func(t *testing.T) {
tc(t)
})
}
})
}
}

func testAccNetworkManager_basic(t *testing.T) {
func TestAccNetworkManager_basic(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_network_manager", "test")
r := ManagerResource{}

Expand All @@ -57,7 +30,7 @@ func testAccNetworkManager_basic(t *testing.T) {
})
}

func testAccNetworkManager_complete(t *testing.T) {
func TestAccNetworkManager_complete(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_network_manager", "test")
r := ManagerResource{}

Expand All @@ -72,7 +45,7 @@ func testAccNetworkManager_complete(t *testing.T) {
})
}

func testAccNetworkManager_update(t *testing.T) {
func TestAccNetworkManager_update(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_network_manager", "test")
r := ManagerResource{}

Expand Down Expand Up @@ -101,7 +74,7 @@ func testAccNetworkManager_update(t *testing.T) {
})
}

func testAccNetworkManager_requiresImport(t *testing.T) {
func TestAccNetworkManager_requiresImport(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_network_manager", "test")
r := ManagerResource{}

Expand Down Expand Up @@ -135,14 +108,16 @@ func (r ManagerResource) Exists(ctx context.Context, clients *clients.Client, st
func (r ManagerResource) basic(data acceptance.TestData) string {
return fmt.Sprintf(`
%s

resource "azurerm_network_manager" "test" {
name = "acctest-networkmanager-%d"
location = azurerm_resource_group.test.location
resource_group_name = azurerm_resource_group.test.name
scope_accesses = ["SecurityAdmin"]

scope {
subscription_ids = [data.azurerm_subscription.current.id]
management_group_ids = [azurerm_management_group.test.id]
}
scope_accesses = ["SecurityAdmin"]
}
`, r.template(data), data.RandomInteger)
}
Expand All @@ -154,10 +129,11 @@ resource "azurerm_network_manager" "import" {
name = azurerm_network_manager.test.name
location = azurerm_network_manager.test.location
resource_group_name = azurerm_network_manager.test.resource_group_name
scope_accesses = azurerm_network_manager.test.scope_accesses

scope {
subscription_ids = azurerm_network_manager.test.scope.0.subscription_ids
management_group_ids = azurerm_network_manager.test.scope.0.management_group_ids
}
scope_accesses = azurerm_network_manager.test.scope_accesses
}
`, r.basic(data))
}
Expand All @@ -168,12 +144,14 @@ func (r ManagerResource) complete(data acceptance.TestData) string {
resource "azurerm_network_manager" "test" {
name = "acctest-networkmanager-%d"
location = azurerm_resource_group.test.location
scope_accesses = ["Connectivity", "SecurityAdmin"]
description = "test network manager"
resource_group_name = azurerm_resource_group.test.name

scope {
subscription_ids = [data.azurerm_subscription.current.id]
management_group_ids = [azurerm_management_group.test.id]
}
scope_accesses = ["Connectivity", "SecurityAdmin"]
description = "test network manager"

tags = {
foo = "bar"
}
Expand All @@ -186,11 +164,16 @@ func (ManagerResource) template(data acceptance.TestData) string {
provider "azurerm" {
features {}
}

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

data "azurerm_subscription" "current" {
}

resource "azurerm_management_group" "test" {
}
`, data.RandomInteger, data.Locations.Primary)
}
2 changes: 2 additions & 0 deletions website/docs/r/network_manager.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ A `scope` block supports the following:

* `management_group_ids` - (Optional) A list of management group IDs.

-> **NOTE:** Network Manager provisioned using Management Group as scope will try to register the `Microsoft.Network` provider to the Management Group.

* `subscription_ids` - (Optional) A list of subscription IDs.

## Attributes Reference
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,4 +128,4 @@ Spring Cloud Customized Accelerators can be imported using the `resource id`, e.

```shell
terraform import azurerm_spring_cloud_customized_accelerator.example /subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.AppPlatform/Spring/spring1/applicationAccelerators/default/customizedAccelerators/customizedAccelerator1
```
```