-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New Data Source
azurerm_network_manager_network_group
(#22277)
* new data source `azurerm_network_manager_network_group` * review comment * always write description to state
- Loading branch information
Showing
6 changed files
with
206 additions
and
5 deletions.
There are no files selected for viewing
98 changes: 98 additions & 0 deletions
98
internal/services/network/network_manager_network_group_data_source.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}, | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
internal/services/network/network_manager_network_group_data_source_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
website/docs/d/network_manager_network_group.html.markdown
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |