Skip to content
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

fixes #343: new data-source for ovirt_disk_attachments #371

Merged
merged 1 commit into from
May 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.vscode
.idea
*.iml
dist
Expand Down
49 changes: 49 additions & 0 deletions docs/data-sources/disk_attachments.md
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 removed examples/data-sources/.gitkeep
Empty file.
10 changes: 10 additions & 0 deletions examples/data-sources/ovirt_disk_attachments/data-source.tf
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
}
22 changes: 22 additions & 0 deletions examples/data-sources/ovirt_disk_attachments/provider.tf
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
}

38 changes: 38 additions & 0 deletions examples/data-sources/ovirt_disk_attachments/resource.tf
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
]
}
43 changes: 43 additions & 0 deletions examples/data-sources/ovirt_disk_attachments/variables.tf
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
}
86 changes: 86 additions & 0 deletions ovirt/data_source_ovirt_disk_attachments.go
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
}
98 changes: 98 additions & 0 deletions ovirt/data_source_ovirt_disk_attachments_test.go
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
},
),
},
},
})
}
4 changes: 3 additions & 1 deletion ovirt/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,9 @@ func (p *provider) getProvider() *schema.Provider {
"ovirt_nic": p.nicResource(),
"ovirt_tag": p.tagResource(),
},
DataSourcesMap: map[string]*schema.Resource{},
DataSourcesMap: map[string]*schema.Resource{
"ovirt_disk_attachments": p.diskAttachmentsDataSource(),
engelmi marked this conversation as resolved.
Show resolved Hide resolved
},
}
}

Expand Down