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 Resource azurerm_network_manager_static_member #20077

Merged
merged 3 commits into from
Jan 19, 2023
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
5 changes: 5 additions & 0 deletions internal/services/network/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ type Client struct {
ManagerManagementGroupConnectionsClient *network.ManagementGroupNetworkManagerConnectionsClient
ManagerNetworkGroupsClient *network.GroupsClient
ManagerScopeConnectionsClient *network.ScopeConnectionsClient
ManagerStaticMembersClient *network.StaticMembersClient
ManagerSubscriptionConnectionsClient *network.SubscriptionNetworkManagerConnectionsClient
NatRuleClient *network.NatRulesClient
PointToSiteVpnGatewaysClient *network.P2sVpnGatewaysClient
Expand Down Expand Up @@ -140,6 +141,9 @@ func NewClient(o *common.ClientOptions) *Client {
ManagerNetworkGroupsClient := network.NewGroupsClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId)
o.ConfigureClient(&ManagerNetworkGroupsClient.Client, o.ResourceManagerAuthorizer)

ManagerStaticMembersClient := network.NewStaticMembersClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId)
o.ConfigureClient(&ManagerStaticMembersClient.Client, o.ResourceManagerAuthorizer)

ManagerSubscriptionConnectionsClient := network.NewSubscriptionNetworkManagerConnectionsClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId)
o.ConfigureClient(&ManagerSubscriptionConnectionsClient.Client, o.ResourceManagerAuthorizer)

Expand Down Expand Up @@ -281,6 +285,7 @@ func NewClient(o *common.ClientOptions) *Client {
ManagerManagementGroupConnectionsClient: &ManagerManagementGroupConnectionsClient,
ManagerNetworkGroupsClient: &ManagerNetworkGroupsClient,
ManagerScopeConnectionsClient: &ManagerScopeConnectionsClient,
ManagerStaticMembersClient: &ManagerStaticMembersClient,
ManagerSubscriptionConnectionsClient: &ManagerSubscriptionConnectionsClient,
NatRuleClient: &NatRuleClient,
PointToSiteVpnGatewaysClient: &pointToSiteVpnGatewaysClient,
Expand Down
4 changes: 4 additions & 0 deletions internal/services/network/network_manager_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ func TestAccNetworkManager(t *testing.T) {
"update": testAccNetworkManagerScopeConnection_update,
"requiresImport": testAccNetworkManagerScopeConnection_requiresImport,
},
"StaticMember": {
"basic": testAccNetworkManagerStaticMember_basic,
"requiresImport": testAccNetworkManagerStaticMember_requiresImport,
},
}

for group, m := range testCases {
Expand Down
176 changes: 176 additions & 0 deletions internal/services/network/network_manager_static_member_resource.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
package network

import (
"context"
"fmt"
"time"

"github.com/hashicorp/terraform-provider-azurerm/internal/sdk"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/network/parse"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/network/validate"
"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"
"github.com/tombuildsstuff/kermit/sdk/network/2022-05-01/network"
)

type ManagerStaticMemberModel struct {
Name string `tfschema:"name"`
NetworkGroupId string `tfschema:"network_group_id"`
TargetVNetId string `tfschema:"target_virtual_network_id"`
Region string `tfschema:"region"`
}

type ManagerStaticMemberResource struct{}

var _ sdk.Resource = ManagerStaticMemberResource{}

func (r ManagerStaticMemberResource) ResourceType() string {
return "azurerm_network_manager_static_member"
}

func (r ManagerStaticMemberResource) ModelObject() interface{} {
return &ManagerStaticMemberModel{}
}

func (r ManagerStaticMemberResource) IDValidationFunc() pluginsdk.SchemaValidateFunc {
return validate.NetworkManagerStaticMemberID
}

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

"network_group_id": {
Type: pluginsdk.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validate.NetworkManagerNetworkGroupID,
},

"target_virtual_network_id": {
Type: pluginsdk.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validate.VirtualNetworkID,
},
}
}

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

func (r ManagerStaticMemberResource) Create() sdk.ResourceFunc {
return sdk.ResourceFunc{
Timeout: 30 * time.Minute,
Func: func(ctx context.Context, metadata sdk.ResourceMetaData) error {
var model ManagerStaticMemberModel
if err := metadata.Decode(&model); err != nil {
return fmt.Errorf("decoding: %+v", err)
}

client := metadata.Client.Network.ManagerStaticMembersClient
networkGroupId, err := parse.NetworkManagerNetworkGroupID(model.NetworkGroupId)
if err != nil {
return err
}

id := parse.NewNetworkManagerStaticMemberID(networkGroupId.SubscriptionId, networkGroupId.ResourceGroup, networkGroupId.NetworkManagerName, networkGroupId.NetworkGroupName, model.Name)
existing, err := client.Get(ctx, id.ResourceGroup, id.NetworkManagerName, id.NetworkGroupName, id.StaticMemberName)
if err != nil && !utils.ResponseWasNotFound(existing.Response) {
return fmt.Errorf("checking for existing %s: %+v", id, err)
}

if !utils.ResponseWasNotFound(existing.Response) {
return metadata.ResourceRequiresImport(r.ResourceType(), id)
}

staticMember := &network.StaticMember{
StaticMemberProperties: &network.StaticMemberProperties{
ResourceID: &model.TargetVNetId,
},
}

if _, err := client.CreateOrUpdate(ctx, *staticMember, id.ResourceGroup, id.NetworkManagerName, id.NetworkGroupName, id.StaticMemberName); err != nil {
return fmt.Errorf("creating %s: %+v", id, err)
}

metadata.SetID(id)
return nil
},
}
}

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

id, err := parse.NetworkManagerStaticMemberID(metadata.ResourceData.Id())
if err != nil {
return err
}

existing, err := client.Get(ctx, id.ResourceGroup, id.NetworkManagerName, id.NetworkGroupName, id.StaticMemberName)
if err != nil {
if utils.ResponseWasNotFound(existing.Response) {
return metadata.MarkAsGone(id)
}

return fmt.Errorf("retrieving %s: %+v", *id, err)
}

properties := existing.StaticMemberProperties
if properties == nil {
return fmt.Errorf("retrieving %s: properties was nil", id)
}

state := ManagerStaticMemberModel{
Name: id.StaticMemberName,
NetworkGroupId: parse.NewNetworkManagerNetworkGroupID(id.SubscriptionId, id.ResourceGroup, id.NetworkManagerName, id.NetworkGroupName).ID(),
}

if properties.Region != nil {
state.Region = *properties.Region
}

if properties.ResourceID != nil {
state.TargetVNetId = *properties.ResourceID
}

return metadata.Encode(&state)
},
}
}

func (r ManagerStaticMemberResource) Delete() sdk.ResourceFunc {
return sdk.ResourceFunc{
Timeout: 30 * time.Minute,
Func: func(ctx context.Context, metadata sdk.ResourceMetaData) error {
client := metadata.Client.Network.ManagerStaticMembersClient

id, err := parse.NetworkManagerStaticMemberID(metadata.ResourceData.Id())
if err != nil {
return err
}

if _, err := client.Delete(ctx, id.ResourceGroup, id.NetworkManagerName, id.NetworkGroupName, id.StaticMemberName); err != nil {
return fmt.Errorf("deleting %s: %+v", id, err)
}

return nil
},
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
package network_test

import (
"context"
"fmt"
"testing"

"github.com/hashicorp/terraform-provider-azurerm/internal/acceptance"
"github.com/hashicorp/terraform-provider-azurerm/internal/acceptance/check"
"github.com/hashicorp/terraform-provider-azurerm/internal/clients"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/network/parse"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
"github.com/hashicorp/terraform-provider-azurerm/utils"
)

type ManagerStaticMemberResource struct{}

func testAccNetworkManagerStaticMember_basic(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_network_manager_static_member", "test")
r := ManagerStaticMemberResource{}
data.ResourceSequentialTest(t, r, []acceptance.TestStep{
{
Config: r.basic(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
),
},
data.ImportStep(),
})
}

func testAccNetworkManagerStaticMember_requiresImport(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_network_manager_static_member", "test")
r := ManagerStaticMemberResource{}
data.ResourceSequentialTest(t, r, []acceptance.TestStep{
{
Config: r.basic(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
),
},
data.RequiresImportErrorStep(r.requiresImport),
})
}

func (r ManagerStaticMemberResource) Exists(ctx context.Context, clients *clients.Client, state *pluginsdk.InstanceState) (*bool, error) {
id, err := parse.NetworkManagerStaticMemberID(state.ID)
if err != nil {
return nil, err
}

client := clients.Network.ManagerStaticMembersClient
resp, err := client.Get(ctx, id.ResourceGroup, id.NetworkManagerName, id.NetworkGroupName, id.StaticMemberName)
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
return utils.Bool(false), nil
}
return nil, fmt.Errorf("retrieving %s: %+v", id, err)
}
return utils.Bool(resp.StaticMemberProperties != nil), nil
}

func (r ManagerStaticMemberResource) template(data acceptance.TestData) string {
return fmt.Sprintf(`
provider "azurerm" {
features {}
}

resource "azurerm_resource_group" "test" {
name = "acctest-rg-network-manager-%d"
location = "%s"
}

data "azurerm_subscription" "current" {
}

resource "azurerm_network_manager" "test" {
name = "acctest-nm-%d"
resource_group_name = azurerm_resource_group.test.name
location = azurerm_resource_group.test.location
scope {
subscription_ids = [data.azurerm_subscription.current.id]
}
scope_accesses = ["SecurityAdmin"]
}

resource "azurerm_network_manager_network_group" "test" {
name = "acctest-nmng-%d"
network_manager_id = azurerm_network_manager.test.id
}

resource "azurerm_virtual_network" "test" {
name = "acctest-vnet-%d"
location = azurerm_resource_group.test.location
resource_group_name = azurerm_resource_group.test.name
address_space = ["10.0.0.0/22"]
}
`, data.RandomInteger, data.Locations.Primary, data.RandomInteger, data.RandomInteger, data.RandomInteger)
}

func (r ManagerStaticMemberResource) basic(data acceptance.TestData) string {
template := r.template(data)
return fmt.Sprintf(`
%s

resource "azurerm_network_manager_static_member" "test" {
name = "acctest-nmsm-%d"
network_group_id = azurerm_network_manager_network_group.test.id
target_virtual_network_id = azurerm_virtual_network.test.id
}
`, template, data.RandomInteger)
}

func (r ManagerStaticMemberResource) requiresImport(data acceptance.TestData) string {
config := r.basic(data)
return fmt.Sprintf(`
%s

resource "azurerm_network_manager_static_member" "import" {
name = azurerm_network_manager_static_member.test.name
network_group_id = azurerm_network_manager_static_member.test.network_group_id
target_virtual_network_id = azurerm_network_manager_static_member.test.target_virtual_network_id
}
`, config)
}
Loading