Skip to content

Commit 953e7e3

Browse files
authored
added bootable and active fields to disk_attachment (#439)
1 parent 3e10e69 commit 953e7e3

10 files changed

Lines changed: 352 additions & 147 deletions

File tree

docs/resources/disk_attachment.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ resource "ovirt_disk_attachment" "test" {
3838
vm_id = ovirt_vm.test.id
3939
disk_id = ovirt_disk.test.id
4040
disk_interface = "virtio_scsi"
41+
bootable = true
42+
active = true
4143
}
4244
```
4345

@@ -50,6 +52,11 @@ resource "ovirt_disk_attachment" "test" {
5052
- `disk_interface` (String) Type of interface to use for attaching disk. One of: `ide`, `sata`, `spapr_vscsi`, `virtio`, `virtio_scsi`.
5153
- `vm_id` (String) ID of the VM the disk should be attached to.
5254

55+
### Optional
56+
57+
- `active` (Boolean) Defines whether the disk is active in the virtual machine it is attached to.
58+
- `bootable` (Boolean) Defines whether the disk is bootable.
59+
5360
### Read-Only
5461

5562
- `id` (String) The ID of this resource.

docs/resources/disk_attachments.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ resource "ovirt_disk_attachments" "test" {
4444
attachment {
4545
disk_id = ovirt_disk.test.id
4646
disk_interface = "virtio_scsi"
47+
bootable = true
48+
active = true
4749
}
4850
}
4951
```
@@ -75,6 +77,11 @@ Required:
7577
- `disk_id` (String) ID of the disk to attach. This disk must either be shared or not yet attached to a different VM.
7678
- `disk_interface` (String) Type of interface to use for attaching disk. One of: `ide`, `sata`, `spapr_vscsi`, `virtio`, `virtio_scsi`.
7779

80+
Optional:
81+
82+
- `active` (Boolean) Defines whether the disk is active in the virtual machine it is attached to.
83+
- `bootable` (Boolean) Defines whether the disk is bootable.
84+
7885
Read-Only:
7986

8087
- `id` (String) The ID of this resource.

examples/resources/ovirt_disk_attachment/resource.tf

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,6 @@ resource "ovirt_disk_attachment" "test" {
2020
vm_id = ovirt_vm.test.id
2121
disk_id = ovirt_disk.test.id
2222
disk_interface = "virtio_scsi"
23+
bootable = true
24+
active = true
2325
}

examples/resources/ovirt_disk_attachments/resource.tf

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,7 @@ resource "ovirt_disk_attachments" "test" {
2626
attachment {
2727
disk_id = ovirt_disk.test.id
2828
disk_interface = "virtio_scsi"
29+
bootable = true
30+
active = true
2931
}
3032
}

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ require (
1313
github.com/hashicorp/yamux v0.0.0-20211028200310-0bc27b27de87 // indirect
1414
github.com/mitchellh/go-wordwrap v1.0.1 // indirect
1515
github.com/oklog/run v1.1.0 // indirect
16-
github.com/ovirt/go-ovirt-client v1.0.0-beta2
16+
github.com/ovirt/go-ovirt-client v1.0.0-beta4
1717
github.com/ovirt/go-ovirt-client-log/v3 v3.0.0
1818
github.com/vmihailenco/tagparser v0.1.2 // indirect
1919
golang.org/x/net v0.0.0-20220425223048-2871e0cb64e4 // indirect

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -233,8 +233,8 @@ github.com/oklog/run v1.1.0 h1:GEenZ1cK0+q0+wsJew9qUg/DyD8k3JzYsZAi5gYi2mA=
233233
github.com/oklog/run v1.1.0/go.mod h1:sVPdnTZT1zYwAJeCMu2Th4T21pA3FPOQRfWjQlk7DVU=
234234
github.com/ovirt/go-ovirt v0.0.0-20220427092237-114c47f2835c h1:jXRFpl7+W0YZj/fghoYuE4vJWW/KeQGvdrhnRwRGtAY=
235235
github.com/ovirt/go-ovirt v0.0.0-20220427092237-114c47f2835c/go.mod h1:Zkdj9/rW6eyuw0uOeEns6O3pP5G2ak+bI/tgkQ/tEZI=
236-
github.com/ovirt/go-ovirt-client v1.0.0-beta2 h1:GszjqWO9he3h1Y4SUUAxz/5Wgev/p1A6MVIf7Z8eQXg=
237-
github.com/ovirt/go-ovirt-client v1.0.0-beta2/go.mod h1:tv8E2pxUkggayDAgMLuQHzcNtzt8RFvnhO5V5b/5X4U=
236+
github.com/ovirt/go-ovirt-client v1.0.0-beta4 h1:A8MFK4Y4ZZ3Ad7N6Q0obqJl8r2Pai1FmzD193WwBpoo=
237+
github.com/ovirt/go-ovirt-client v1.0.0-beta4/go.mod h1:tv8E2pxUkggayDAgMLuQHzcNtzt8RFvnhO5V5b/5X4U=
238238
github.com/ovirt/go-ovirt-client-log/v3 v3.0.0 h1:uvACVHYhYPMkNJrrgWiABcfELB6qoFfsDDUTbpb4Jv4=
239239
github.com/ovirt/go-ovirt-client-log/v3 v3.0.0/go.mod h1:chKKxCv4lRjxezrTG+EIhkWXGhDAWByglPVXh/iYdnQ=
240240
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=

internal/ovirt/resource_ovirt_disk_attachment.go

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,20 @@ var diskAttachmentSchema = map[string]*schema.Schema{
3939
ForceNew: true,
4040
ValidateDiagFunc: validateDiskInterface,
4141
},
42+
"bootable": {
43+
Type: schema.TypeBool,
44+
Optional: true,
45+
ForceNew: true,
46+
Default: false,
47+
Description: "Defines whether the disk is bootable.",
48+
},
49+
"active": {
50+
Type: schema.TypeBool,
51+
Optional: true,
52+
ForceNew: true,
53+
Default: false,
54+
Description: "Defines whether the disk is active in the virtual machine it is attached to.",
55+
},
4256
}
4357

4458
func (p *provider) diskAttachmentResource() *schema.Resource {
@@ -66,11 +80,34 @@ func (p *provider) diskAttachmentCreate(
6680
diskID := data.Get("disk_id").(string)
6781
diskInterface := data.Get("disk_interface").(string)
6882

83+
var err error
84+
var diags diag.Diagnostics
85+
params := ovirtclient.CreateDiskAttachmentParams()
86+
params, err = params.WithBootable(data.Get("bootable").(bool))
87+
if err != nil {
88+
diags = append(diags, diag.Diagnostic{
89+
Severity: diag.Error,
90+
Summary: "Failed to set bootable flag for disk attachment.",
91+
Detail: err.Error(),
92+
})
93+
}
94+
params, err = params.WithActive(data.Get("active").(bool))
95+
if err != nil {
96+
diags = append(diags, diag.Diagnostic{
97+
Severity: diag.Error,
98+
Summary: "Failed to set active flag for disk attachment.",
99+
Detail: err.Error(),
100+
})
101+
}
102+
if diags.HasError() {
103+
return diags
104+
}
105+
69106
diskAttachment, err := client.CreateDiskAttachment(
70107
ovirtclient.VMID(vmID),
71108
ovirtclient.DiskID(diskID),
72109
ovirtclient.DiskInterface(diskInterface),
73-
ovirtclient.CreateDiskAttachmentParams(),
110+
params,
74111
)
75112
if err != nil {
76113
return diag.Diagnostics{
@@ -164,6 +201,12 @@ func (p *provider) diskAttachmentImport(
164201
if err := data.Set("disk_interface", string(attachment.DiskInterface())); err != nil {
165202
return nil, fmt.Errorf("failed to set disk_interface to %s", attachment.DiskInterface())
166203
}
204+
if err := data.Set("bootable", attachment.Bootable()); err != nil {
205+
return nil, fmt.Errorf("failed to set bootable to %v", attachment.Bootable())
206+
}
207+
if err := data.Set("active", attachment.Active()); err != nil {
208+
return nil, fmt.Errorf("failed to set active to %v", attachment.Active())
209+
}
167210
return []*schema.ResourceData{data}, nil
168211
}
169212

internal/ovirt/resource_ovirt_disk_attachment_test.go

Lines changed: 103 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -18,52 +18,111 @@ func TestDiskAttachmentResource(t *testing.T) {
1818
clusterID := p.getTestHelper().GetClusterID()
1919
templateID := p.getTestHelper().GetBlankTemplateID()
2020

21-
resource.UnitTest(
22-
t, resource.TestCase{
23-
ProviderFactories: p.getProviderFactories(),
24-
Steps: []resource.TestStep{
25-
{
26-
Config: fmt.Sprintf(
27-
`
28-
provider "ovirt" {
29-
mock = true
30-
}
31-
32-
resource "ovirt_disk" "test" {
33-
storage_domain_id = "%s"
34-
format = "raw"
35-
size = 1048576
36-
alias = "test"
37-
sparse = true
38-
}
39-
40-
resource "ovirt_vm" "test" {
41-
cluster_id = "%s"
42-
template_id = "%s"
43-
name = "test"
44-
}
21+
baseConfig := fmt.Sprintf(`
22+
provider "ovirt" {
23+
mock = true
24+
}
25+
26+
resource "ovirt_disk" "test" {
27+
storage_domain_id = "%s"
28+
format = "raw"
29+
size = 1048576
30+
alias = "test"
31+
sparse = true
32+
}
33+
34+
resource "ovirt_vm" "test" {
35+
cluster_id = "%s"
36+
template_id = "%s"
37+
name = "test"
38+
}`,
39+
storageDomainID,
40+
clusterID,
41+
templateID,
42+
)
4543

46-
resource "ovirt_disk_attachment" "test" {
47-
vm_id = ovirt_vm.test.id
48-
disk_id = ovirt_disk.test.id
49-
disk_interface = "virtio_scsi"
50-
}
51-
`,
52-
storageDomainID,
53-
clusterID,
54-
templateID,
55-
),
56-
Check: resource.ComposeTestCheckFunc(
57-
resource.TestMatchResourceAttr(
58-
"ovirt_disk_attachment.test",
59-
"id",
60-
regexp.MustCompile("^.+$"),
61-
),
62-
),
63-
},
64-
},
44+
testcases := []struct {
45+
name string
46+
inputBootable string
47+
inputActive string
48+
expectedBootable bool
49+
expectedActive bool
50+
}{
51+
{
52+
name: "all set to true",
53+
inputBootable: "true",
54+
inputActive: "true",
55+
expectedBootable: true,
56+
expectedActive: true,
6557
},
66-
)
58+
{
59+
name: "all set to false",
60+
inputBootable: "false",
61+
inputActive: "false",
62+
expectedBootable: false,
63+
expectedActive: false,
64+
},
65+
{
66+
name: "using defaults",
67+
inputBootable: "null",
68+
inputActive: "null",
69+
expectedBootable: false,
70+
expectedActive: false,
71+
},
72+
}
73+
74+
for _, testcase := range testcases {
75+
t.Run(testcase.name, func(t *testing.T) {
76+
resource.UnitTest(
77+
t, resource.TestCase{
78+
ProviderFactories: p.getProviderFactories(),
79+
Steps: []resource.TestStep{
80+
{
81+
Config: fmt.Sprintf(`
82+
%s
83+
84+
resource "ovirt_disk_attachment" "test" {
85+
vm_id = ovirt_vm.test.id
86+
disk_id = ovirt_disk.test.id
87+
disk_interface = "virtio_scsi"
88+
bootable = %s
89+
active = %s
90+
}`,
91+
baseConfig,
92+
testcase.inputBootable,
93+
testcase.inputActive,
94+
),
95+
Check: resource.ComposeTestCheckFunc(
96+
resource.TestMatchResourceAttr(
97+
"ovirt_disk_attachment.test",
98+
"id",
99+
regexp.MustCompile("^.+$"),
100+
),
101+
func(s *terraform.State) error {
102+
VMID := s.RootModule().Resources["ovirt_vm.test"].Primary.ID
103+
diskAttachmentID := s.RootModule().Resources["ovirt_disk_attachment.test"].Primary.ID
104+
diskAttachment, err := p.getTestHelper().GetClient().GetDiskAttachment(ovirtclient.VMID(VMID), ovirtclient.DiskAttachmentID(diskAttachmentID))
105+
if err != nil {
106+
return err
107+
}
108+
if diskAttachment.DiskInterface() != "virtio_scsi" {
109+
return fmt.Errorf("Expected disk_interface 'virtio_scsi', but got '%s'", diskAttachment.DiskInterface())
110+
}
111+
if diskAttachment.Bootable() != testcase.expectedActive {
112+
return fmt.Errorf("Expected bootable to be %t, but got %t", testcase.expectedBootable, diskAttachment.Bootable())
113+
}
114+
if diskAttachment.Active() != testcase.expectedActive {
115+
return fmt.Errorf("Expected active to be %t, but got %t", testcase.expectedActive, diskAttachment.Active())
116+
}
117+
return nil
118+
},
119+
),
120+
},
121+
},
122+
},
123+
)
124+
})
125+
}
67126
}
68127

69128
func TestDiskAttachmentResourceImport(t *testing.T) {

internal/ovirt/resource_ovirt_disk_attachments.go

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,20 @@ var diskAttachmentsSchema = map[string]*schema.Schema{
4343
ForceNew: false,
4444
ValidateDiagFunc: validateDiskInterface,
4545
},
46+
"bootable": {
47+
Type: schema.TypeBool,
48+
Optional: true,
49+
ForceNew: true,
50+
Default: false,
51+
Description: "Defines whether the disk is bootable.",
52+
},
53+
"active": {
54+
Type: schema.TypeBool,
55+
Optional: true,
56+
ForceNew: true,
57+
Default: false,
58+
Description: "Defines whether the disk is active in the virtual machine it is attached to.",
59+
},
4660
},
4761
},
4862
},
@@ -180,6 +194,8 @@ func (p *provider) createOrUpdateDiskAttachment(
180194
id := desiredAttachment["id"].(string)
181195
diskID := desiredAttachment["disk_id"].(string)
182196
diskInterfaceName := desiredAttachment["disk_interface"].(string)
197+
bootable := desiredAttachment["bootable"].(bool)
198+
active := desiredAttachment["active"].(bool)
183199

184200
var foundExisting ovirtclient.DiskAttachment
185201
if id != "" {
@@ -194,7 +210,10 @@ func (p *provider) createOrUpdateDiskAttachment(
194210
if foundExisting != nil {
195211
// If we found an existing attachment, check if all parameters match. Otherwise, remove the attachment
196212
// and let it be re-created below.
197-
if foundExisting.DiskID() == ovirtclient.DiskID(diskID) && string(foundExisting.DiskInterface()) == diskInterfaceName {
213+
if string(foundExisting.DiskID()) == diskID &&
214+
string(foundExisting.DiskInterface()) == diskInterfaceName &&
215+
foundExisting.Bootable() == bootable &&
216+
foundExisting.Active() == active {
198217
return nil
199218
}
200219
if err := foundExisting.Remove(); err != nil && !isNotFound(err) {
@@ -208,14 +227,27 @@ func (p *provider) createOrUpdateDiskAttachment(
208227
desiredAttachment["id"] = ""
209228
desiredAttachment["disk_id"] = ""
210229
desiredAttachment["disk_interface"] = ""
230+
desiredAttachment["bootable"] = false
231+
desiredAttachment["active"] = false
232+
}
233+
234+
var err error
235+
params := ovirtclient.CreateDiskAttachmentParams()
236+
params, err = params.WithBootable(bootable)
237+
if err != nil {
238+
return errorToDiags("set bootable flag for disk attachment.", err)
239+
}
240+
params, err = params.WithActive(active)
241+
if err != nil {
242+
return errorToDiags("set active flag for disk attachment.", err)
211243
}
212244

213245
// Create or re-create disk attachment, then set it in the Terraform state.
214246
attachment, err := client.CreateDiskAttachment(
215247
ovirtclient.VMID(vmID),
216248
ovirtclient.DiskID(diskID),
217249
ovirtclient.DiskInterface(diskInterfaceName),
218-
nil,
250+
params,
219251
)
220252
if err != nil {
221253
return errorToDiags(
@@ -226,6 +258,9 @@ func (p *provider) createOrUpdateDiskAttachment(
226258
desiredAttachment["id"] = attachment.ID()
227259
desiredAttachment["disk_id"] = attachment.DiskID()
228260
desiredAttachment["disk_interface"] = string(attachment.DiskInterface())
261+
desiredAttachment["bootable"] = attachment.Bootable()
262+
desiredAttachment["active"] = attachment.Active()
263+
229264
return nil
230265
}
231266

0 commit comments

Comments
 (0)