forked from oVirt/terraform-provider-ovirt
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fixes oVirt#343: new data-source for ovirt_disk_attachments
- Loading branch information
Showing
10 changed files
with
350 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
.vscode | ||
.idea | ||
*.iml | ||
dist | ||
|
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,49 @@ | ||
--- | ||
# generated by https://github.com/hashicorp/terraform-plugin-docs | ||
page_title: "ovirt_disk_attachments Data Source - terraform-provider-ovirt" | ||
subcategory: "" | ||
description: |- | ||
A set of all attachments of a VM. | ||
--- | ||
|
||
# ovirt_disk_attachments (Data Source) | ||
|
||
A set of all attachments of a VM. | ||
|
||
## Example Usage | ||
|
||
```terraform | ||
data "ovirt_disk_attachments" "set" { | ||
vm_id = ovirt_vm.test.id | ||
depends_on = [ | ||
ovirt_disk_attachments.test | ||
] | ||
} | ||
output "attachment_set" { | ||
value = data.ovirt_disk_attachments.set | ||
} | ||
``` | ||
|
||
<!-- schema generated by tfplugindocs --> | ||
## Schema | ||
|
||
### Required | ||
|
||
- `vm_id` (String) oVirt ID of the VM to be optimized. | ||
|
||
### Read-Only | ||
|
||
- `attachments` (Set of Object) (see [below for nested schema](#nestedatt--attachments)) | ||
- `id` (String) The ID of this resource. | ||
|
||
<a id="nestedatt--attachments"></a> | ||
### Nested Schema for `attachments` | ||
|
||
Read-Only: | ||
|
||
- `disk_id` (String) | ||
- `disk_interface` (String) | ||
- `id` (String) | ||
|
||
|
Empty file.
10 changes: 10 additions & 0 deletions
10
examples/data-sources/ovirt_disk_attachments/data-source.tf
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,10 @@ | ||
data "ovirt_disk_attachments" "set" { | ||
vm_id = ovirt_vm.test.id | ||
depends_on = [ | ||
ovirt_disk_attachments.test | ||
] | ||
} | ||
|
||
output "attachment_set" { | ||
value = data.ovirt_disk_attachments.set | ||
} |
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,22 @@ | ||
terraform { | ||
required_providers { | ||
ovirt = { | ||
source = "ovirt/ovirt" | ||
} | ||
} | ||
|
||
required_version = ">= 0.15" | ||
} | ||
|
||
provider "ovirt" { | ||
url = var.url | ||
username = var.username | ||
password = var.password | ||
tls_ca_bundle = var.tls_ca_bundle | ||
tls_system = var.tls_system | ||
tls_ca_dirs = var.tls_ca_dirs | ||
tls_ca_files = var.tls_ca_files | ||
tls_insecure = var.tls_insecure | ||
mock = var.mock | ||
} | ||
|
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,38 @@ | ||
resource "ovirt_disk" "test1" { | ||
storagedomain_id = var.storagedomain_id | ||
format = "raw" | ||
size = 1048576 | ||
alias = "test" | ||
sparse = true | ||
} | ||
|
||
resource "ovirt_disk" "test2" { | ||
storagedomain_id = var.storagedomain_id | ||
format = "raw" | ||
size = 1048576 | ||
alias = "test" | ||
sparse = true | ||
} | ||
|
||
resource "ovirt_vm" "test" { | ||
cluster_id = var.cluster_id | ||
template_id = "00000000-0000-0000-0000-000000000000" | ||
name = "test" | ||
} | ||
|
||
resource "ovirt_disk_attachments" "test" { | ||
vm_id = ovirt_vm.test.id | ||
|
||
attachment { | ||
disk_id = ovirt_disk.test1.id | ||
disk_interface = "virtio_scsi" | ||
} | ||
attachment { | ||
disk_id = ovirt_disk.test2.id | ||
disk_interface = "virtio_scsi" | ||
} | ||
|
||
depends_on = [ | ||
ovirt_vm.test, ovirt_disk.test1, ovirt_disk.test2 | ||
] | ||
} |
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,43 @@ | ||
variable "storagedomain_id" { | ||
type = string | ||
description = "ID of the storage domain to create the disk on." | ||
} | ||
|
||
variable "cluster_id" { | ||
type = string | ||
} | ||
|
||
variable "username" { | ||
type = string | ||
} | ||
variable "password" { | ||
type = string | ||
} | ||
variable "url" { | ||
type = string | ||
} | ||
variable "tls_ca_files" { | ||
type = list(string) | ||
default = [] | ||
} | ||
variable "tls_ca_dirs" { | ||
type = list(string) | ||
default = [] | ||
} | ||
variable "tls_insecure" { | ||
type = bool | ||
default = false | ||
} | ||
variable "tls_ca_bundle" { | ||
type = string | ||
default = "" | ||
} | ||
variable "tls_system" { | ||
type = bool | ||
default = true | ||
description = "Take TLS CA certificates from system root. Does not work on Windows." | ||
} | ||
variable "mock" { | ||
type = bool | ||
default = true | ||
} |
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,86 @@ | ||
package ovirt | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/v2/diag" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
ovirtclient "github.com/ovirt/go-ovirt-client" | ||
) | ||
|
||
func (p *provider) diskAttachmentsDataSource() *schema.Resource { | ||
return &schema.Resource{ | ||
ReadContext: p.diskAttachmentsDataSourceRead, | ||
Schema: map[string]*schema.Schema{ | ||
"vm_id": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
Description: "oVirt ID of the VM to be optimized.", | ||
ValidateDiagFunc: validateUUID, | ||
}, | ||
"attachments": { | ||
Type: schema.TypeSet, | ||
Computed: true, | ||
Elem: &schema.Resource{ | ||
Schema: map[string]*schema.Schema{ | ||
"id": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
Description: "ID of the attachement.", | ||
}, | ||
"disk_id": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
Description: "ID of the disk in this attachment.", | ||
}, | ||
"disk_interface": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
Description: fmt.Sprintf( | ||
"Type of interface of the attached disk. One of: `%s`.", | ||
strings.Join(ovirtclient.DiskInterfaceValues().Strings(), "`, `"), | ||
), | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
Description: `A set of all attachments of a VM.`, | ||
} | ||
} | ||
|
||
func (p *provider) diskAttachmentsDataSourceRead( | ||
ctx context.Context, | ||
data *schema.ResourceData, | ||
_ interface{}, | ||
) diag.Diagnostics { | ||
client := p.client.WithContext(ctx) | ||
|
||
vmID := data.Get("vm_id").(string) | ||
diskAttachments, err := client.ListDiskAttachments(ovirtclient.VMID(vmID)) | ||
if err != nil { | ||
return errorToDiags(fmt.Sprintf("list disk attachments of VM %s", vmID), err) | ||
} | ||
|
||
attachments := make([]map[string]interface{}, 0) | ||
|
||
for _, diskAttachment := range diskAttachments { | ||
attachment := make(map[string]interface{}, 0) | ||
|
||
attachment["id"] = diskAttachment.ID() | ||
attachment["disk_id"] = diskAttachment.DiskID() | ||
attachment["disk_interface"] = diskAttachment.DiskInterface() | ||
|
||
attachments = append(attachments, attachment) | ||
} | ||
|
||
if err := data.Set("attachments", attachments); err != nil { | ||
return errorToDiags("set attachments", err) | ||
} | ||
|
||
data.SetId(vmID) | ||
|
||
return nil | ||
} |
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,98 @@ | ||
package ovirt | ||
|
||
import ( | ||
"fmt" | ||
"regexp" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform" | ||
) | ||
|
||
func TestDiskAttachmentDataSource(t *testing.T) { | ||
p := newProvider(newTestLogger(t)) | ||
|
||
config := | ||
fmt.Sprintf(` | ||
provider "ovirt" { | ||
mock = true | ||
} | ||
resource "ovirt_disk" "test1" { | ||
storagedomain_id = "%s" | ||
format = "raw" | ||
size = 1048576 | ||
alias = "test" | ||
sparse = true | ||
} | ||
resource "ovirt_disk" "test2" { | ||
storagedomain_id = "%s" | ||
format = "raw" | ||
size = 1048576 | ||
alias = "test" | ||
sparse = true | ||
} | ||
resource "ovirt_vm" "test" { | ||
cluster_id = "%s" | ||
template_id = "%s" | ||
name = "test" | ||
} | ||
resource "ovirt_disk_attachments" "test" { | ||
vm_id = ovirt_vm.test.id | ||
attachment { | ||
disk_id = ovirt_disk.test1.id | ||
disk_interface = "virtio_scsi" | ||
} | ||
attachment { | ||
disk_id = ovirt_disk.test2.id | ||
disk_interface = "virtio_scsi" | ||
} | ||
} | ||
data "ovirt_disk_attachments" "list" { | ||
vm_id = ovirt_vm.test.id | ||
depends_on = [ | ||
ovirt_disk_attachments.test | ||
] | ||
} | ||
output "attachment_list" { | ||
value = data.ovirt_disk_attachments.list | ||
}`, | ||
p.getTestHelper().GetStorageDomainID(), | ||
p.getTestHelper().GetStorageDomainID(), | ||
p.getTestHelper().GetClusterID(), | ||
p.getTestHelper().GetBlankTemplateID()) | ||
|
||
resource.UnitTest(t, resource.TestCase{ | ||
ProviderFactories: p.getProviderFactories(), | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: config, | ||
Check: resource.ComposeTestCheckFunc( | ||
resource.TestMatchResourceAttr( | ||
"ovirt_disk_attachments.test", | ||
"attachment.#", | ||
regexp.MustCompile("^2$"), | ||
), | ||
func(s *terraform.State) error { | ||
v := s.RootModule().Outputs["attachment_list"].Value.(map[string]interface{}) | ||
attachments, ok := v["attachments"] | ||
if !ok { | ||
return fmt.Errorf("missing key 'attachments' in output") | ||
} | ||
attachmentSize := len(attachments.([]interface{})) | ||
if attachmentSize != 2 { | ||
return fmt.Errorf("expected 2 attachments, but got only %d", attachmentSize) | ||
} | ||
return nil | ||
}, | ||
), | ||
}, | ||
}, | ||
}) | ||
} |
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