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

New Data Source azurerm_network_manager_network_group #22277

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
@@ -0,0 +1,98 @@
package network

import (
"context"
"fmt"
"time"

"github.com/hashicorp/go-azure-helpers/lang/pointer"
"github.com/hashicorp/go-azure-helpers/lang/response"
"github.com/hashicorp/go-azure-sdk/resource-manager/network/2022-09-01/networkgroups"
"github.com/hashicorp/terraform-provider-azurerm/internal/sdk"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/validation"
)

type ManagerNetworkGroupDataSource struct{}

var _ sdk.DataSource = ManagerNetworkGroupDataSource{}

func (r ManagerNetworkGroupDataSource) ResourceType() string {
return "azurerm_network_manager_network_group"
}

func (r ManagerNetworkGroupDataSource) ModelObject() interface{} {
return &ManagerNetworkGroupModel{}
}

func (r ManagerNetworkGroupDataSource) Arguments() map[string]*pluginsdk.Schema {
return map[string]*pluginsdk.Schema{
"name": {
Type: pluginsdk.TypeString,
Required: true,
ValidateFunc: validation.StringIsNotEmpty,
},

"network_manager_id": {
Type: pluginsdk.TypeString,
Required: true,
ValidateFunc: networkgroups.ValidateNetworkManagerID,
},
}
}

func (r ManagerNetworkGroupDataSource) Attributes() map[string]*pluginsdk.Schema {
return map[string]*pluginsdk.Schema{
"description": {
Type: pluginsdk.TypeString,
Computed: true,
},
}
}

func (r ManagerNetworkGroupDataSource) Read() sdk.ResourceFunc {
return sdk.ResourceFunc{
Timeout: 5 * time.Minute,
Func: func(ctx context.Context, metadata sdk.ResourceMetaData) error {
client := metadata.Client.Network.ManagerNetworkGroupsClient

var model ManagerNetworkGroupModel
if err := metadata.Decode(&model); err != nil {
return fmt.Errorf("decoding: %+v", err)
}

networkManagerId, err := networkgroups.ParseNetworkManagerID(model.NetworkManagerId)
if err != nil {
return err
}

id := networkgroups.NewNetworkGroupID(networkManagerId.SubscriptionId, networkManagerId.ResourceGroupName, networkManagerId.NetworkManagerName, model.Name)

existing, err := client.Get(ctx, id)
if err != nil {
if response.WasNotFound(existing.HttpResponse) {
return fmt.Errorf("%s does not exist", id)
}

return fmt.Errorf("retrieving %s: %+v", id, err)
}
if existing.Model == nil {
return fmt.Errorf("retrieving %s: model was nil", id)
}
if existing.Model.Properties == nil {
return fmt.Errorf("retrieving %s: model properties was nil", id)
}

properties := existing.Model.Properties
state := ManagerNetworkGroupModel{
Name: id.NetworkGroupName,
NetworkManagerId: networkgroups.NewNetworkManagerID(id.SubscriptionId, id.ResourceGroupName, id.NetworkManagerName).ID(),
Description: pointer.From(properties.Description),
}

metadata.SetID(id)

return metadata.Encode(&state)
},
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package network_test

import (
"fmt"
"testing"

"github.com/hashicorp/terraform-provider-azurerm/internal/acceptance"
"github.com/hashicorp/terraform-provider-azurerm/internal/acceptance/check"
)

type ManagerNetworkGroupDataSource struct{}

func testAccNetworkManagerNetworkGroupDataSource_complete(t *testing.T) {
data := acceptance.BuildTestData(t, "data.azurerm_network_manager_network_group", "test")
d := ManagerNetworkGroupDataSource{}
data.DataSourceTestInSequence(t, []acceptance.TestStep{
{
Config: d.complete(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).Key("description").HasValue("test complete"),
),
},
})
}

func (d ManagerNetworkGroupDataSource) complete(data acceptance.TestData) string {
return fmt.Sprintf(`
%s

data "azurerm_network_manager_network_group" "test" {
name = azurerm_network_manager_network_group.test.name
network_manager_id = azurerm_network_manager_network_group.test.network_manager_id
}
`, ManagerNetworkGroupResource{}.complete(data))
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"time"

"github.com/hashicorp/go-azure-helpers/lang/pointer"
"github.com/hashicorp/go-azure-helpers/lang/response"
"github.com/hashicorp/go-azure-sdk/resource-manager/network/2022-09-01/networkgroups"
"github.com/hashicorp/terraform-provider-azurerm/internal/sdk"
Expand Down Expand Up @@ -180,10 +181,7 @@ func (r ManagerNetworkGroupResource) Read() sdk.ResourceFunc {
state := ManagerNetworkGroupModel{
Name: id.NetworkGroupName,
NetworkManagerId: networkgroups.NewNetworkManagerID(id.SubscriptionId, id.ResourceGroupName, id.NetworkManagerName).ID(),
}

if properties.Description != nil {
state.Description = *properties.Description
Description: pointer.From(properties.Description),
}

return metadata.Encode(&state)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ func TestAccNetworkManager(t *testing.T) {
"complete": testAccNetworkManagerNetworkGroup_complete,
"update": testAccNetworkManagerNetworkGroup_update,
"requiresImport": testAccNetworkManagerNetworkGroup_requiresImport,
"dataSource": testAccNetworkManagerNetworkGroupDataSource_complete,
},
"SubscriptionConnection": {
"basic": testAccNetworkSubscriptionNetworkManagerConnection_basic,
Expand Down
4 changes: 3 additions & 1 deletion internal/services/network/registration.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ func (r Registration) WebsiteCategories() []string {
}

func (r Registration) DataSources() []sdk.DataSource {
return []sdk.DataSource{}
return []sdk.DataSource{
ManagerNetworkGroupDataSource{},
}
}

func (r Registration) Resources() []sdk.Resource {
Expand Down
67 changes: 67 additions & 0 deletions website/docs/d/network_manager_network_group.html.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
---
subcategory: "Network"
layout: "azurerm"
page_title: "Azure Resource Manager: azurerm_network_manager_network_group"
description: |-
Get information about a Network Manager Network Group.
---

# azurerm_network_manager_network_group

Use this data source to access information about a Network Manager Network Group.

## Example Usage

```hcl
resource "azurerm_resource_group" "example" {
name = "example-resources"
location = "West Europe"
}

data "azurerm_subscription" "current" {
}

resource "azurerm_network_manager" "example" {
name = "example-network-manager"
location = azurerm_resource_group.example.location
resource_group_name = azurerm_resource_group.example.name
scope {
subscription_ids = [data.azurerm_subscription.current.id]
}
scope_accesses = ["Connectivity", "SecurityAdmin"]
description = "example network manager"
}

teowa marked this conversation as resolved.
Show resolved Hide resolved
resource "azurerm_network_manager_network_group" "example" {
name = "example-group"
network_manager_id = azurerm_network_manager.example.id
}

data "azurerm_network_manager_network_group" "example" {
name = azurerm_network_manager_network_group.example.name
network_manager_id = azurerm_network_manager.example.id
}
```

## Arguments Reference

The following arguments are supported:

* `name` - (Required) Specifies the name of the Network Manager Network Group.

* `network_manager_id` - (Required) Specifies the ID of the Network Manager.


## Attributes Reference

In addition to the Arguments listed above - the following Attributes are exported:

* `id` - The ID of the Network Manager Network Group.

* `description` - A description of the Network Manager Network Group.

## Timeouts

The `timeouts` block allows you to specify [timeouts](https://www.terraform.io/language/resources/syntax#operation-timeouts) for certain actions:

* `read` - (Defaults to 5 minutes) Used when retrieving the Network Manager Network Group.