-
Notifications
You must be signed in to change notification settings - Fork 4.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
new datasource
azurerm_network_manager
(#24398)
* new datasource network manager * recover test * go generate
- Loading branch information
Showing
8 changed files
with
325 additions
and
5 deletions.
There are no files selected for viewing
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
169 changes: 169 additions & 0 deletions
169
internal/services/network/network_manager_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,169 @@ | ||
package network | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"time" | ||
|
||
"github.com/hashicorp/go-azure-helpers/lang/response" | ||
"github.com/hashicorp/go-azure-helpers/resourcemanager/commonschema" | ||
"github.com/hashicorp/go-azure-helpers/resourcemanager/location" | ||
"github.com/hashicorp/go-azure-sdk/resource-manager/network/2023-06-01/networkmanagers" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
"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" | ||
"github.com/hashicorp/terraform-provider-azurerm/utils" | ||
) | ||
|
||
type ManagerDataSource struct{} | ||
|
||
var _ sdk.DataSource = ManagerDataSource{} | ||
|
||
func (r ManagerDataSource) ResourceType() string { | ||
return "azurerm_network_manager" | ||
} | ||
|
||
func (r ManagerDataSource) ModelObject() interface{} { | ||
return &ManagerModel{} | ||
} | ||
|
||
func (r ManagerDataSource) Arguments() map[string]*pluginsdk.Schema { | ||
return map[string]*pluginsdk.Schema{ | ||
"name": { | ||
Type: pluginsdk.TypeString, | ||
Required: true, | ||
ValidateFunc: validation.StringIsNotEmpty, | ||
}, | ||
|
||
"resource_group_name": commonschema.ResourceGroupNameForDataSource(), | ||
} | ||
} | ||
|
||
func (r ManagerDataSource) Attributes() map[string]*pluginsdk.Schema { | ||
return map[string]*pluginsdk.Schema{ | ||
"location": commonschema.LocationComputed(), | ||
|
||
"scope": { | ||
Type: pluginsdk.TypeList, | ||
Computed: true, | ||
Elem: &pluginsdk.Resource{ | ||
Schema: map[string]*schema.Schema{ | ||
"management_group_ids": { | ||
Type: pluginsdk.TypeList, | ||
Computed: true, | ||
Elem: &pluginsdk.Schema{ | ||
Type: pluginsdk.TypeString, | ||
}, | ||
}, | ||
"subscription_ids": { | ||
Type: pluginsdk.TypeList, | ||
Computed: true, | ||
Elem: &pluginsdk.Schema{ | ||
Type: pluginsdk.TypeString, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
|
||
"scope_accesses": { | ||
Type: pluginsdk.TypeList, | ||
Computed: true, | ||
Elem: &pluginsdk.Schema{ | ||
Type: pluginsdk.TypeString, | ||
}, | ||
}, | ||
|
||
"cross_tenant_scopes": { | ||
Type: pluginsdk.TypeList, | ||
Computed: true, | ||
Elem: &pluginsdk.Resource{ | ||
Schema: map[string]*pluginsdk.Schema{ | ||
"tenant_id": { | ||
Type: pluginsdk.TypeString, | ||
Computed: true, | ||
}, | ||
"subscriptions": { | ||
Type: pluginsdk.TypeList, | ||
Computed: true, | ||
Elem: &pluginsdk.Schema{ | ||
Type: pluginsdk.TypeString, | ||
}, | ||
}, | ||
"management_groups": { | ||
Type: pluginsdk.TypeList, | ||
Computed: true, | ||
Elem: &pluginsdk.Schema{ | ||
Type: pluginsdk.TypeString, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
|
||
"description": { | ||
Type: pluginsdk.TypeString, | ||
Computed: true, | ||
}, | ||
|
||
"tags": commonschema.TagsDataSource(), | ||
} | ||
} | ||
|
||
func (r ManagerDataSource) Read() sdk.ResourceFunc { | ||
return sdk.ResourceFunc{ | ||
Timeout: 5 * time.Minute, | ||
Func: func(ctx context.Context, metadata sdk.ResourceMetaData) error { | ||
client := metadata.Client.Network.NetworkManagers | ||
subscriptionId := metadata.Client.Account.SubscriptionId | ||
|
||
var model ManagerModel | ||
if err := metadata.Decode(&model); err != nil { | ||
return fmt.Errorf("decoding: %+v", err) | ||
} | ||
|
||
id := networkmanagers.NewNetworkManagerID(subscriptionId, model.ResourceGroupName, 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 | ||
var description string | ||
var scope []ManagerScopeModel | ||
var ScopeAccesses []string | ||
if properties.Description != nil { | ||
description = *properties.Description | ||
} | ||
scope = flattenNetworkManagerScope(properties.NetworkManagerScopes) | ||
ScopeAccesses = flattenNetworkManagerScopeAccesses(properties.NetworkManagerScopeAccesses) | ||
|
||
state := ManagerModel{ | ||
CrossTenantScopes: flattenNetworkManagerCrossTenantScopes(properties.NetworkManagerScopes.CrossTenantScopes), | ||
Description: description, | ||
Location: location.NormalizeNilable(existing.Model.Location), | ||
Name: id.NetworkManagerName, | ||
ResourceGroupName: id.ResourceGroupName, | ||
ScopeAccesses: ScopeAccesses, | ||
Scope: scope, | ||
Tags: utils.FlattenPtrMapStringString(existing.Model.Tags), | ||
} | ||
|
||
metadata.SetID(id) | ||
|
||
return metadata.Encode(&state) | ||
}, | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
internal/services/network/network_manager_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,40 @@ | ||
package network_test | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform-provider-azurerm/internal/acceptance" | ||
"github.com/hashicorp/terraform-provider-azurerm/internal/acceptance/check" | ||
) | ||
|
||
type ManagerDataSource struct{} | ||
|
||
func testAccNetworkManagerDataSource_complete(t *testing.T) { | ||
data := acceptance.BuildTestData(t, "data.azurerm_network_manager", "test") | ||
d := ManagerDataSource{} | ||
data.DataSourceTestInSequence(t, []acceptance.TestStep{ | ||
{ | ||
Config: d.complete(data), | ||
Check: acceptance.ComposeTestCheckFunc( | ||
check.That(data.ResourceName).Key("location").IsNotEmpty(), | ||
check.That(data.ResourceName).Key("description").IsNotEmpty(), | ||
check.That(data.ResourceName).Key("scope_accesses.#").HasValue("2"), | ||
check.That(data.ResourceName).Key("scope_accesses.0").HasValue("Connectivity"), | ||
check.That(data.ResourceName).Key("scope.#").HasValue("1"), | ||
check.That(data.ResourceName).Key("scope.0.subscription_ids.#").HasValue("1"), | ||
), | ||
}, | ||
}) | ||
} | ||
|
||
func (d ManagerDataSource) complete(data acceptance.TestData) string { | ||
return fmt.Sprintf(` | ||
%s | ||
data "azurerm_network_manager" "test" { | ||
name = azurerm_network_manager.test.name | ||
resource_group_name = azurerm_network_manager.test.resource_group_name | ||
} | ||
`, ManagerResource{}.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
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,91 @@ | ||
--- | ||
subcategory: "Network" | ||
layout: "azurerm" | ||
page_title: "Azure Resource Manager: azurerm_network_manager" | ||
description: |- | ||
Get information about an existing Network Manager. | ||
--- | ||
|
||
# azurerm_network_manager | ||
|
||
Use this data source to access information about a Network Manager. | ||
|
||
## 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" | ||
} | ||
data "azurerm_network_manager" "example" { | ||
name = azurerm_network_manager.example.name | ||
resource_group_name = azurerm_network_manager.example.resource_group_name | ||
} | ||
``` | ||
|
||
## Arguments Reference | ||
|
||
The following arguments are supported: | ||
|
||
* `name` - (Required) The name of the Network Manager. | ||
|
||
* `resource_group_name` - (Required) The Name of the Resource Group where the Network Manager exists. | ||
|
||
|
||
## Attributes Reference | ||
|
||
In addition to the Arguments listed above - the following Attributes are exported: | ||
|
||
* `id` - The ID of the Network Manager. | ||
|
||
* `cross_tenant_scopes` - One or more `cross_tenant_scopes` blocks as defined below. | ||
|
||
* `description` - A description of the Network Manager. | ||
|
||
* `location` - The Azure Region where the Network Manager exists. | ||
|
||
* `scope` - A `scope` block as defined below. | ||
|
||
* `scope_accesses` - A list of configuration deployment type configured on the Network Manager. | ||
|
||
* `tags` - A mapping of tags assigned to the Network Manager. | ||
|
||
--- | ||
|
||
A `scope` block exports the following: | ||
|
||
* `management_group_ids` - A list of management group IDs used a scope for the Network Manager. | ||
|
||
* `subscription_ids` - A list of subscription IDs used as the scope for the Network Manager. | ||
|
||
--- | ||
|
||
A `cross_tenant_scopes` block exports the following: | ||
|
||
* `management_groups` - A list of management groups used as cross tenant scope for the Network Manager. | ||
|
||
* `subscriptions` - A list of subscriptions used as cross tenant scope for the Network Manager. | ||
|
||
* `tenant_id` - The tenant ID of the cross tenant scope. | ||
|
||
|
||
## 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. |
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