-
Notifications
You must be signed in to change notification settings - Fork 4.7k
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_restore_point_collection
#26518
Merged
Merged
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
4d9e461
New Resource: azurerm_restore_point_collection
mbfrahry 60dab34
Update restore_point_collection.html.markdown
mbfrahry 84adc74
Address review
mbfrahry ac3ec10
Merge branch 'f-restore-point-collection' of github.com:hashicorp/ter…
mbfrahry 30e0043
lint
mbfrahry File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
211 changes: 211 additions & 0 deletions
211
internal/services/compute/restore_point_collection_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,211 @@ | ||||||
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/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" | ||||||
) | ||||||
|
||||||
type RestorePointCollectionResource struct{} | ||||||
|
||||||
var _ sdk.ResourceWithUpdate = RestorePointCollectionResource{} | ||||||
|
||||||
func (r RestorePointCollectionResource) ModelObject() interface{} { | ||||||
return &RestorePointCollectionResourceModel{} | ||||||
} | ||||||
|
||||||
type RestorePointCollectionResourceModel 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 RestorePointCollectionResource) IDValidationFunc() pluginsdk.SchemaValidateFunc { | ||||||
return restorepointcollections.ValidateRestorePointCollectionID | ||||||
} | ||||||
|
||||||
func (r RestorePointCollectionResource) ResourceType() string { | ||||||
return "azurerm_restore_point_collection" | ||||||
} | ||||||
|
||||||
func (r RestorePointCollectionResource) 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 RestorePointCollectionResource) Attributes() map[string]*pluginsdk.Schema { | ||||||
return map[string]*pluginsdk.Schema{} | ||||||
} | ||||||
|
||||||
func (r RestorePointCollectionResource) 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 RestorePointCollectionResourceModel | ||||||
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: 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 RestorePointCollectionResource) 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 := RestorePointCollectionResourceModel{} | ||||||
|
||||||
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 = pointer.From(source.Location) | ||||||
} | ||||||
} | ||||||
|
||||||
schema.Tags = tags.Flatten(model.Tags) | ||||||
} | ||||||
|
||||||
return metadata.Encode(&schema) | ||||||
}, | ||||||
} | ||||||
} | ||||||
|
||||||
func (r RestorePointCollectionResource) 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("checking for presence of existing %s: %+v", id, err) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
} | ||||||
|
||||||
if existing.Model == nil { | ||||||
return fmt.Errorf("retrieving %s: `model` was nil", id) | ||||||
} | ||||||
|
||||||
payload := *existing.Model | ||||||
|
||||||
var config RestorePointCollectionResourceModel | ||||||
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 RestorePointCollectionResource) 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 | ||||||
}, | ||||||
} | ||||||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should we normalize the location here?