-
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 Resource:
azurerm_network_manager_network_group
(#19593)
- Loading branch information
Showing
11 changed files
with
780 additions
and
0 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
216 changes: 216 additions & 0 deletions
216
internal/services/network/network_manager_network_group_resource.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,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 | ||
}, | ||
} | ||
} |
Oops, something went wrong.