diff --git a/internal/services/network/network_manager_network_group_data_source.go b/internal/services/network/network_manager_network_group_data_source.go new file mode 100644 index 000000000000..e2fc8f2f6c2e --- /dev/null +++ b/internal/services/network/network_manager_network_group_data_source.go @@ -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) + }, + } +} diff --git a/internal/services/network/network_manager_network_group_data_source_test.go b/internal/services/network/network_manager_network_group_data_source_test.go new file mode 100644 index 000000000000..49e43dc215db --- /dev/null +++ b/internal/services/network/network_manager_network_group_data_source_test.go @@ -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)) +} diff --git a/internal/services/network/network_manager_network_group_resource.go b/internal/services/network/network_manager_network_group_resource.go index f52591e2f5de..c6523e28ab36 100644 --- a/internal/services/network/network_manager_network_group_resource.go +++ b/internal/services/network/network_manager_network_group_resource.go @@ -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" @@ -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) diff --git a/internal/services/network/network_manager_resource_test.go b/internal/services/network/network_manager_resource_test.go index 0b72836a83f8..bb269798a752 100644 --- a/internal/services/network/network_manager_resource_test.go +++ b/internal/services/network/network_manager_resource_test.go @@ -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, diff --git a/internal/services/network/registration.go b/internal/services/network/registration.go index 27434d73384f..80de86893f3e 100644 --- a/internal/services/network/registration.go +++ b/internal/services/network/registration.go @@ -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 { diff --git a/website/docs/d/network_manager_network_group.html.markdown b/website/docs/d/network_manager_network_group.html.markdown new file mode 100644 index 000000000000..577f8a2eb708 --- /dev/null +++ b/website/docs/d/network_manager_network_group.html.markdown @@ -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.