Skip to content

Commit

Permalink
New Resource azurerm_network_manager_scope_connection (#19610)
Browse files Browse the repository at this point in the history
  • Loading branch information
teowa authored Jan 17, 2023
1 parent f4f79c0 commit 5d4775e
Show file tree
Hide file tree
Showing 11 changed files with 854 additions and 0 deletions.
5 changes: 5 additions & 0 deletions internal/services/network/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ type Client struct {
ManagersClient *network.ManagersClient
ManagerManagementGroupConnectionsClient *network.ManagementGroupNetworkManagerConnectionsClient
ManagerNetworkGroupsClient *network.GroupsClient
ManagerScopeConnectionsClient *network.ScopeConnectionsClient
ManagerSubscriptionConnectionsClient *network.SubscriptionNetworkManagerConnectionsClient
NatRuleClient *network.NatRulesClient
PointToSiteVpnGatewaysClient *network.P2sVpnGatewaysClient
Expand Down Expand Up @@ -130,6 +131,9 @@ func NewClient(o *common.ClientOptions) *Client {
ManagersClient := network.NewManagersClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId)
o.ConfigureClient(&ManagersClient.Client, o.ResourceManagerAuthorizer)

ManagerScopeConnectionsClient := network.NewScopeConnectionsClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId)
o.ConfigureClient(&ManagerScopeConnectionsClient.Client, o.ResourceManagerAuthorizer)

ManagerManagementGroupConnectionsClient := network.NewManagementGroupNetworkManagerConnectionsClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId)
o.ConfigureClient(&ManagerManagementGroupConnectionsClient.Client, o.ResourceManagerAuthorizer)

Expand Down Expand Up @@ -276,6 +280,7 @@ func NewClient(o *common.ClientOptions) *Client {
ManagersClient: &ManagersClient,
ManagerManagementGroupConnectionsClient: &ManagerManagementGroupConnectionsClient,
ManagerNetworkGroupsClient: &ManagerNetworkGroupsClient,
ManagerScopeConnectionsClient: &ManagerScopeConnectionsClient,
ManagerSubscriptionConnectionsClient: &ManagerSubscriptionConnectionsClient,
NatRuleClient: &NatRuleClient,
PointToSiteVpnGatewaysClient: &pointToSiteVpnGatewaysClient,
Expand Down
6 changes: 6 additions & 0 deletions internal/services/network/network_manager_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,12 @@ func TestAccNetworkManager(t *testing.T) {
"update": testAccNetworkManagerManagementGroupConnection_update,
"requiresImport": testAccNetworkManagerManagementGroupConnection_requiresImport,
},
"ScopeConnection": {
"basic": testAccNetworkManagerScopeConnection_basic,
"complete": testAccNetworkManagerScopeConnection_complete,
"update": testAccNetworkManagerScopeConnection_update,
"requiresImport": testAccNetworkManagerScopeConnection_requiresImport,
},
}

for group, m := range testCases {
Expand Down
259 changes: 259 additions & 0 deletions internal/services/network/network_manager_scope_connection_resource.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,259 @@
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 ManagerScopeConnectionModel struct {
Name string `tfschema:"name"`
NetworkManagerId string `tfschema:"network_manager_id"`
ConnectionState string `tfschema:"connection_state"`
Description string `tfschema:"description"`
ResourceId string `tfschema:"target_scope_id"`
TenantId string `tfschema:"tenant_id"`
}

type ManagerScopeConnectionResource struct{}

var _ sdk.ResourceWithUpdate = ManagerScopeConnectionResource{}

func (r ManagerScopeConnectionResource) ResourceType() string {
return "azurerm_network_manager_scope_connection"
}

func (r ManagerScopeConnectionResource) ModelObject() interface{} {
return &ManagerScopeConnectionModel{}
}

func (r ManagerScopeConnectionResource) IDValidationFunc() pluginsdk.SchemaValidateFunc {
return validate.NetworkManagerScopeConnectionID
}

func (r ManagerScopeConnectionResource) 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,
},

"target_scope_id": {
Type: pluginsdk.TypeString,
Required: true,
ValidateFunc: validation.StringIsNotEmpty,
},

"tenant_id": {
Type: pluginsdk.TypeString,
Required: true,
ValidateFunc: validation.IsUUID,
},

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

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

"connection_state": {
Type: pluginsdk.TypeString,
Computed: true,
},
}
}

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

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

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

scopeConnection := &network.ScopeConnection{
ScopeConnectionProperties: &network.ScopeConnectionProperties{},
}

if model.Description != "" {
scopeConnection.ScopeConnectionProperties.Description = &model.Description
}

if model.ResourceId != "" {
scopeConnection.ScopeConnectionProperties.ResourceID = &model.ResourceId
}

if model.TenantId != "" {
scopeConnection.ScopeConnectionProperties.TenantID = &model.TenantId
}

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

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

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

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

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

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

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

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

if metadata.ResourceData.HasChange("target_scope_id") {
if model.ResourceId != "" {
properties.ResourceID = &model.ResourceId
}
}

if metadata.ResourceData.HasChange("tenant_id") {
if model.TenantId != "" {
properties.TenantID = &model.TenantId
}
}

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

return nil
},
}
}

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

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

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

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

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

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

state.ConnectionState = string(properties.ConnectionState)

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

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

if properties.TenantID != nil {
state.TenantId = *properties.TenantID
}

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

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

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

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

return nil
},
}
}
Loading

0 comments on commit 5d4775e

Please sign in to comment.