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_network_group #19593

Merged
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 @@ -26,6 +26,7 @@ type Client struct {
IPGroupsClient *network.IPGroupsClient
LocalNetworkGatewaysClient *network.LocalNetworkGatewaysClient
ManagersClient *network.ManagersClient
ManagerNetworkGroupsClient *network.GroupsClient
NatRuleClient *network.NatRulesClient
PointToSiteVpnGatewaysClient *network.P2sVpnGatewaysClient
ProfileClient *network.ProfilesClient
Expand Down Expand Up @@ -127,6 +128,9 @@ func NewClient(o *common.ClientOptions) *Client {
ManagersClient := network.NewManagersClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId)
o.ConfigureClient(&ManagersClient.Client, o.ResourceManagerAuthorizer)

ManagerNetworkGroupsClient := network.NewGroupsClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId)
o.ConfigureClient(&ManagerNetworkGroupsClient.Client, o.ResourceManagerAuthorizer)

NatRuleClient := network.NewNatRulesClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId)
o.ConfigureClient(&NatRuleClient.Client, o.ResourceManagerAuthorizer)

Expand Down Expand Up @@ -262,6 +266,7 @@ func NewClient(o *common.ClientOptions) *Client {
IPGroupsClient: &IpGroupsClient,
LocalNetworkGatewaysClient: &LocalNetworkGatewaysClient,
ManagersClient: &ManagersClient,
ManagerNetworkGroupsClient: &ManagerNetworkGroupsClient,
NatRuleClient: &NatRuleClient,
PointToSiteVpnGatewaysClient: &pointToSiteVpnGatewaysClient,
ProfileClient: &ProfileClient,
Expand Down
216 changes: 216 additions & 0 deletions internal/services/network/network_manager_network_group_resource.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,216 @@
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 ManagerNetworkGroupModel struct {
Name string `tfschema:"name"`
NetworkManagerId string `tfschema:"network_manager_id"`
Description string `tfschema:"description"`
}

type ManagerNetworkGroupResource struct{}

var _ sdk.ResourceWithUpdate = ManagerNetworkGroupResource{}

func (r ManagerNetworkGroupResource) ResourceType() string {
return "azurerm_network_manager_network_group"
}

func (r ManagerNetworkGroupResource) ModelObject() interface{} {
return &ManagerNetworkGroupModel{}
}

func (r ManagerNetworkGroupResource) IDValidationFunc() pluginsdk.SchemaValidateFunc {
return validate.NetworkManagerNetworkGroupID
}

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

"network_manager_id": {
Type: pluginsdk.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validate.NetworkManagerID,
},

"description": {
Type: pluginsdk.TypeString,
Optional: true,
},
}
}

func (r ManagerNetworkGroupResource) Attributes() map[string]*pluginsdk.Schema {
return map[string]*pluginsdk.Schema{}
}

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

client := metadata.Client.Network.ManagerNetworkGroupsClient
networkManagerId, err := parse.NetworkManagerID(model.NetworkManagerId)
if err != nil {
return err
}

id := parse.NewNetworkManagerNetworkGroupID(networkManagerId.SubscriptionId, networkManagerId.ResourceGroup, networkManagerId.Name, model.Name)
existing, err := client.Get(ctx, id.ResourceGroup, id.NetworkManagerName, id.NetworkGroupName)
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)
}

group := &network.Group{
GroupProperties: &network.GroupProperties{},
}

if model.Description != "" {
group.GroupProperties.Description = &model.Description
}

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

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

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

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

var model ManagerNetworkGroupModel
if err := metadata.Decode(&model); err != nil {
return fmt.Errorf("decoding: %+v", err)
}

existing, err := client.Get(ctx, id.ResourceGroup, id.NetworkManagerName, id.NetworkGroupName)
if err != nil {
return fmt.Errorf("retrieving %s: %+v", *id, err)
}

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

if metadata.ResourceData.HasChange("description") {
if model.Description != "" {
properties.Description = &model.Description
} else {
properties.Description = nil
}
}

existing.SystemData = nil

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

return nil
},
}
}

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

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

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

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

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

state := ManagerNetworkGroupModel{
Name: id.NetworkGroupName,
NetworkManagerId: parse.NewNetworkManagerID(id.SubscriptionId, id.ResourceGroup, id.NetworkManagerName).ID(),
}

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

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

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

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

future, err := client.Delete(ctx, id.ResourceGroup, id.NetworkManagerName, id.NetworkGroupName, utils.Bool(true))
if err != nil {
return fmt.Errorf("deleting %s: %+v", *id, err)
}

if err = future.WaitForCompletionRef(ctx, client.Client); err != nil {
return fmt.Errorf("waiting for deletion of %s: %+v", *id, err)
}

return nil
},
}
}
Loading