From 37b0ea38cf285254fa65c41b7c6919925609587b Mon Sep 17 00:00:00 2001 From: Ken Schmidt Date: Tue, 2 Jan 2018 10:16:05 -0800 Subject: [PATCH] adding disk data source --- ovirt/data_source_disk.go | 59 +++++++++++++++++++++++++++++++++++++++ ovirt/provider.go | 3 ++ 2 files changed, 62 insertions(+) create mode 100644 ovirt/data_source_disk.go diff --git a/ovirt/data_source_disk.go b/ovirt/data_source_disk.go new file mode 100644 index 00000000..f97c18a5 --- /dev/null +++ b/ovirt/data_source_disk.go @@ -0,0 +1,59 @@ +package ovirt + +import ( + "strconv" + + "github.com/EMSL-MSC/ovirtapi" + "github.com/hashicorp/terraform/helper/schema" +) + +func dataSourceDisk() *schema.Resource { + return &schema.Resource{ + Read: dataSourceDiskRead, + Schema: map[string]*schema.Schema{ + "name": { + Type: schema.TypeString, + Required: true, + }, + "format": { + Type: schema.TypeString, + Optional: true, + }, + "storage_domain_id": { + Type: schema.TypeString, + Optional: true, + }, + "size": { + Type: schema.TypeInt, + Optional: true, + }, + "shareable": { + Type: schema.TypeBool, + Optional: true, + }, + "sparse": { + Type: schema.TypeBool, + Optional: true, + }, + }, + } +} + +func dataSourceDiskRead(d *schema.ResourceData, meta interface{}) error { + con := meta.(*ovirtapi.Connection) + disk, err := con.GetDisk(d.Id()) + if err != nil { + d.SetId("") + return nil + } + + d.Set("name", disk.Name) + d.Set("size", disk.ProvisionedSize) + d.Set("format", disk.Format) + d.Set("storage_domain_id", disk.StorageDomains.StorageDomain[0].ID) + shareable, _ := strconv.ParseBool(disk.Shareable) + d.Set("shareable", shareable) + sparse, _ := strconv.ParseBool(disk.Sparse) + d.Set("sparse", sparse) + return nil +} diff --git a/ovirt/provider.go b/ovirt/provider.go index 93407d82..41849210 100644 --- a/ovirt/provider.go +++ b/ovirt/provider.go @@ -32,6 +32,9 @@ func Provider() terraform.ResourceProvider { "ovirt_vm": resourceVM(), "ovirt_disk": resourceDisk(), }, + DataSourcesMap: map[string]*schema.Resource{ + "ovirt_disk": dataSourceDisk(), + }, } }