Skip to content

Commit

Permalink
New Data Source azurerm_network_manager_network_group (#22277)
Browse files Browse the repository at this point in the history
* new data source `azurerm_network_manager_network_group`

* review comment

* always write description to state
  • Loading branch information
teowa authored Jun 28, 2023
1 parent f83b644 commit 2518162
Show file tree
Hide file tree
Showing 6 changed files with 206 additions and 5 deletions.
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
1 change: 1 addition & 0 deletions internal/services/network/network_manager_resource_test.go
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"
}
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.

0 comments on commit 2518162

Please sign in to comment.