Skip to content

Commit

Permalink
New Resource: azurerm_virtual_machine_restore_point (#26526)
Browse files Browse the repository at this point in the history
  • Loading branch information
mbfrahry authored Jul 8, 2024
1 parent c8a0561 commit a4d7588
Show file tree
Hide file tree
Showing 59 changed files with 3,360 additions and 17 deletions.
9 changes: 9 additions & 0 deletions internal/services/compute/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
"github.com/hashicorp/go-azure-sdk/resource-manager/compute/2024-03-01/dedicatedhostgroups"
"github.com/hashicorp/go-azure-sdk/resource-manager/compute/2024-03-01/dedicatedhosts"
"github.com/hashicorp/go-azure-sdk/resource-manager/compute/2024-03-01/restorepointcollections"
"github.com/hashicorp/go-azure-sdk/resource-manager/compute/2024-03-01/restorepoints"
"github.com/hashicorp/go-azure-sdk/resource-manager/compute/2024-03-01/sshpublickeys"
"github.com/hashicorp/go-azure-sdk/resource-manager/compute/2024-03-01/virtualmachineextensions"
"github.com/hashicorp/go-azure-sdk/resource-manager/compute/2024-03-01/virtualmachineimages"
Expand Down Expand Up @@ -63,6 +64,7 @@ type Client struct {
MarketplaceAgreementsClient *agreements.AgreementsClient
ProximityPlacementGroupsClient *proximityplacementgroups.ProximityPlacementGroupsClient
RestorePointCollectionsClient *restorepointcollections.RestorePointCollectionsClient
RestorePointsClient *restorepoints.RestorePointsClient
SkusClient *skus.SkusClient
SSHPublicKeysClient *sshpublickeys.SshPublicKeysClient
SnapshotsClient *snapshots.SnapshotsClient
Expand Down Expand Up @@ -185,6 +187,12 @@ func NewClient(o *common.ClientOptions) (*Client, error) {
}
o.Configure(restorePointCollectionsClient.Client, o.Authorizers.ResourceManager)

restorePointsClient, err := restorepoints.NewRestorePointsClientWithBaseURI(o.Environment.ResourceManager)
if err != nil {
return nil, fmt.Errorf("building RestorePoints client: %+v", err)
}
o.Configure(restorePointsClient.Client, o.Authorizers.ResourceManager)

skusClient, err := skus.NewSkusClientWithBaseURI(o.Environment.ResourceManager)
if err != nil {
return nil, fmt.Errorf("building Skus client: %+v", err)
Expand Down Expand Up @@ -270,6 +278,7 @@ func NewClient(o *common.ClientOptions) (*Client, error) {
MarketplaceAgreementsClient: marketplaceAgreementsClient,
ProximityPlacementGroupsClient: proximityPlacementGroupsClient,
RestorePointCollectionsClient: restorePointCollectionsClient,
RestorePointsClient: restorePointsClient,
SkusClient: skusClient,
SSHPublicKeysClient: sshPublicKeysClient,
SnapshotsClient: snapshotsClient,
Expand Down
2 changes: 2 additions & 0 deletions internal/services/compute/registration.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ func (r Registration) Resources() []sdk.Resource {
GalleryApplicationResource{},
GalleryApplicationVersionResource{},
RestorePointCollectionResource{},
VirtualMachineRestorePointCollectionResource{},
VirtualMachineRestorePointResource{},
VirtualMachineGalleryApplicationAssignmentResource{},
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,15 @@ import (
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
)

// RestorePointCollectionResource remove this in 4.0, the resource is renamed
type RestorePointCollectionResource struct{}

var _ sdk.ResourceWithUpdate = RestorePointCollectionResource{}
var _ sdk.ResourceWithDeprecationReplacedBy = RestorePointCollectionResource{}

func (r RestorePointCollectionResource) DeprecatedInFavourOfResource() string {
return "azurerm_virtual_machine_restore_point_collection"
}

func (r RestorePointCollectionResource) ModelObject() interface{} {
return &RestorePointCollectionResourceModel{}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,218 @@
package compute

import (
"context"
"fmt"
"time"

"github.com/hashicorp/go-azure-helpers/lang/pointer"
"github.com/hashicorp/go-azure-helpers/lang/response"
"github.com/hashicorp/go-azure-helpers/resourcemanager/commonids"
"github.com/hashicorp/go-azure-helpers/resourcemanager/commonschema"
"github.com/hashicorp/go-azure-helpers/resourcemanager/location"
"github.com/hashicorp/go-azure-helpers/resourcemanager/tags"
"github.com/hashicorp/go-azure-sdk/resource-manager/compute/2024-03-01/restorepointcollections"
"github.com/hashicorp/terraform-provider-azurerm/internal/sdk"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
)

// VirtualMachineRestorePointCollectionResource remove this in 4.0, the resource is renamed
type VirtualMachineRestorePointCollectionResource struct{}

var _ sdk.ResourceWithUpdate = VirtualMachineRestorePointCollectionResource{}
var _ sdk.ResourceWithDeprecationReplacedBy = VirtualMachineRestorePointCollectionResource{}

func (r VirtualMachineRestorePointCollectionResource) DeprecatedInFavourOfResource() string {
return "azurerm_virtual_machine_restore_point_collection"
}

func (r VirtualMachineRestorePointCollectionResource) ModelObject() interface{} {
return &VirtualMachineRestorePointCollectionResourceModel{}
}

type VirtualMachineRestorePointCollectionResourceModel struct {
Name string `tfschema:"name"`
ResourceGroup string `tfschema:"resource_group_name"`
Location string `tfschema:"location"`
SourceVirtualMachineId string `tfschema:"source_virtual_machine_id"`
Tags map[string]interface{} `tfschema:"tags"`
}

func (r VirtualMachineRestorePointCollectionResource) IDValidationFunc() pluginsdk.SchemaValidateFunc {
return restorepointcollections.ValidateRestorePointCollectionID
}

func (r VirtualMachineRestorePointCollectionResource) ResourceType() string {
return "azurerm_virtual_machine_restore_point_collection"
}

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

"resource_group_name": commonschema.ResourceGroupName(),

"location": commonschema.Location(),

"source_virtual_machine_id": {
Type: pluginsdk.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: commonids.ValidateVirtualMachineID,
},

"tags": commonschema.Tags(),
}
}

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

func (r VirtualMachineRestorePointCollectionResource) Create() sdk.ResourceFunc {
return sdk.ResourceFunc{
Timeout: 30 * time.Minute,
Func: func(ctx context.Context, metadata sdk.ResourceMetaData) error {
client := metadata.Client.Compute.RestorePointCollectionsClient
subscriptionId := metadata.Client.Account.SubscriptionId

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

id := restorepointcollections.NewRestorePointCollectionID(subscriptionId, config.ResourceGroup, config.Name)

existing, err := client.Get(ctx, id, restorepointcollections.DefaultGetOperationOptions())
if err != nil {
if !response.WasNotFound(existing.HttpResponse) {
return fmt.Errorf("checking for the presence of an existing %s: %+v", id, err)
}
}
if !response.WasNotFound(existing.HttpResponse) {
return metadata.ResourceRequiresImport(r.ResourceType(), id)
}

parameters := restorepointcollections.RestorePointCollection{
Location: location.Normalize(config.Location),
Properties: &restorepointcollections.RestorePointCollectionProperties{
Source: &restorepointcollections.RestorePointCollectionSourceProperties{
Id: pointer.To(config.SourceVirtualMachineId),
},
},
Tags: tags.Expand(config.Tags),
}

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

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

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

schema := VirtualMachineRestorePointCollectionResourceModel{}

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

resp, err := client.Get(ctx, *id, restorepointcollections.DefaultGetOperationOptions())
if err != nil {
if response.WasNotFound(resp.HttpResponse) {
return metadata.MarkAsGone(*id)
}
return fmt.Errorf("retrieving %s: %+v", *id, err)
}

if model := resp.Model; model != nil {
schema.Name = id.RestorePointCollectionName
schema.ResourceGroup = id.ResourceGroupName

if props := model.Properties; props != nil {
if source := props.Source; source != nil {
schema.SourceVirtualMachineId = pointer.From(source.Id)
schema.Location = location.Normalize(pointer.From(source.Location))
}
}

schema.Tags = tags.Flatten(model.Tags)
}

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

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

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

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

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

payload := *existing.Model

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

if metadata.ResourceData.HasChange("tags") {
payload.Tags = tags.Expand(config.Tags)
}

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

return nil
},
}
}

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

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

if err := client.DeleteThenPoll(ctx, *id); err != nil {
return fmt.Errorf("deleting %s: %+v", *id, err)
}

return nil
},
}
}
Loading

0 comments on commit a4d7588

Please sign in to comment.