-
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_virtual_machine_implicit_data_disk_from_source
#25537
Merged
Merged
Changes from 6 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
b12d940
add code
ms-zhenhua bf34f34
resolve comments
ms-zhenhua d6debd5
fix website lint
ms-zhenhua 42b931f
fix acc test
ms-zhenhua a751e45
Merge branch 'main' of https://github.com/hashicorp/terraform-provide…
ms-zhenhua 1f5778f
add code
ms-zhenhua a8d1298
resolve comments
ms-zhenhua 19fa7f2
add test case
ms-zhenhua 49bd69a
add virtual_machine.detach_implicit_data_disk_on_deletion in features…
ms-zhenhua e219132
fix website lint
ms-zhenhua 4438fbf
add TestAccVirtualMachineImplicitDataDiskFromSource_detachImplicitDat…
ms-zhenhua e05ce4b
Update website/docs/guides/features-block.html.markdown
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ | |
package compute | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"log" | ||
"time" | ||
|
@@ -29,9 +30,51 @@ func resourceVirtualMachineDataDiskAttachment() *pluginsdk.Resource { | |
Read: resourceVirtualMachineDataDiskAttachmentRead, | ||
Update: resourceVirtualMachineDataDiskAttachmentCreateUpdate, | ||
Delete: resourceVirtualMachineDataDiskAttachmentDelete, | ||
Importer: pluginsdk.ImporterValidatingResourceId(func(id string) error { | ||
Importer: pluginsdk.ImporterValidatingResourceIdThen(func(id string) error { | ||
_, err := parse.DataDiskID(id) | ||
return err | ||
}, func(ctx context.Context, d *pluginsdk.ResourceData, meta interface{}) ([]*pluginsdk.ResourceData, error) { | ||
client := meta.(*clients.Client).Compute.VirtualMachinesClient | ||
id, err := parse.DataDiskID(d.Id()) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
virtualMachineId := virtualmachines.NewVirtualMachineID(id.SubscriptionId, id.ResourceGroup, id.VirtualMachineName) | ||
virtualMachine, err := client.Get(ctx, virtualMachineId, virtualmachines.DefaultGetOperationOptions()) | ||
if err != nil { | ||
if response.WasNotFound(virtualMachine.HttpResponse) { | ||
return nil, fmt.Errorf("%s was not found therefore Data Disk Attachment cannot be imported", virtualMachineId) | ||
} | ||
|
||
return nil, fmt.Errorf("retrieving %s: %+v", id, err) | ||
} | ||
|
||
var disk *virtualmachines.DataDisk | ||
if model := virtualMachine.Model; model != nil { | ||
if props := model.Properties; props != nil { | ||
if profile := props.StorageProfile; profile != nil { | ||
if dataDisks := profile.DataDisks; dataDisks != nil { | ||
for _, dataDisk := range *dataDisks { | ||
if *dataDisk.Name == id.Name { | ||
disk = &dataDisk | ||
break | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
if disk == nil { | ||
return nil, fmt.Errorf("Data Disk %s was not found", *id) | ||
} | ||
|
||
if disk.CreateOption != virtualmachines.DiskCreateOptionTypesAttach && disk.CreateOption != virtualmachines.DiskCreateOptionTypesEmpty { | ||
return nil, fmt.Errorf("the value of `create_option` for the imported `azurerm_virtual_machine_data_disk_attachment` instance must be `Attach` or `Empty`, whereas now is %s", disk.CreateOption) | ||
} | ||
|
||
return []*pluginsdk.ResourceData{d}, nil | ||
}), | ||
|
||
Timeouts: &pluginsdk.ResourceTimeout{ | ||
|
@@ -154,6 +197,15 @@ func resourceVirtualMachineDataDiskAttachmentCreateUpdate(d *pluginsdk.ResourceD | |
WriteAcceleratorEnabled: pointer.To(writeAcceleratorEnabled), | ||
} | ||
|
||
// there are ways to provision a VM without a StorageProfile and/or DataDisks | ||
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. This should also include a test to confirm that the existing VM won't have any diffs when a disk is attached this way 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.
|
||
if virtualMachine.Model.Properties.StorageProfile == nil { | ||
virtualMachine.Model.Properties.StorageProfile = &virtualmachines.StorageProfile{} | ||
} | ||
|
||
if virtualMachine.Model.Properties.StorageProfile.DataDisks == nil { | ||
virtualMachine.Model.Properties.StorageProfile.DataDisks = pointer.To(make([]virtualmachines.DataDisk, 0)) | ||
} | ||
|
||
disks := *virtualMachine.Model.Properties.StorageProfile.DataDisks | ||
|
||
existingIndex := -1 | ||
|
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.
Is it possible to make a test for this?
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.
Yes, added a new testcase:
=== RUN TestAccVirtualMachineDataDiskAttachment_importImplicitDataDisk
=== PAUSE TestAccVirtualMachineDataDiskAttachment_importImplicitDataDisk
=== CONT TestAccVirtualMachineDataDiskAttachment_importImplicitDataDisk
--- PASS: TestAccVirtualMachineDataDiskAttachment_importImplicitDataDisk (594.13s)
PASS