Skip to content

Commit

Permalink
adding disk resource
Browse files Browse the repository at this point in the history
  • Loading branch information
Maigard committed Dec 30, 2017
1 parent 94500fc commit e5ec777
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 1 deletion.
8 changes: 8 additions & 0 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,11 @@ resource "ovirt_vm" "my_vm" {
]
}
}

resource "ovirt_disk" "my_disk" {
name = "my_first_disk"
size = 1024
format = "cow"
storage_domain_id = "dfe8e7be-e495-49a7-be2d-71aba891ceb4"
sparse = true
}
107 changes: 107 additions & 0 deletions ovirt/disk.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
package ovirt

import (
"strconv"

"github.com/EMSL-MSC/ovirtapi"
"github.com/hashicorp/terraform/helper/schema"
)

func resourceDisk() *schema.Resource {
return &schema.Resource{
Create: resourceDiskCreate,
Read: resourceDiskRead,
Delete: resourceDiskDelete,
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"format": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"storage_domain_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"size": {
Type: schema.TypeInt,
Required: true,
ForceNew: true,
},
"shareable": {
Type: schema.TypeBool,
Optional: true,
ForceNew: true,
},
"sparse": {
Type: schema.TypeBool,
Optional: true,
ForceNew: true,
},
},
}
}

func resourceDiskCreate(d *schema.ResourceData, meta interface{}) error {
con := meta.(*ovirtapi.Connection)

newDisk := con.NewDisk()
err := resourceDiskModify(d, newDisk)
if err != nil {
newDisk.Delete()
return err
}
d.SetId(newDisk.ID)
return nil
}

func resourceDiskModify(d *schema.ResourceData, disk *ovirtapi.Disk) error {
disk.ProvisionedSize = d.Get("size").(int)
disk.Format = d.Get("format").(string)
disk.Name = d.Get("name").(string)
storageDomains := ovirtapi.StorageDomains{}
storageDomains.StorageDomain = append(storageDomains.StorageDomain, ovirtapi.Link{
ID: d.Get("storage_domain_id").(string),
})
disk.StorageDomains = &storageDomains
if d.Get("shareable").(bool) {
disk.Shareable = "true"
}
if d.Get("sparse").(bool) {
disk.Sparse = "true"
}
return disk.Save()
}

func resourceDiskRead(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
}

func resourceDiskDelete(d *schema.ResourceData, meta interface{}) error {
con := meta.(*ovirtapi.Connection)
disk, err := con.GetDisk(d.Id())
if err != nil {
return nil
}
return disk.Delete()
}
3 changes: 2 additions & 1 deletion ovirt/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ func Provider() terraform.ResourceProvider {
},
ConfigureFunc: ConfigureProvider,
ResourcesMap: map[string]*schema.Resource{
"ovirt_vm": resourceVM(),
"ovirt_vm": resourceVM(),
"ovirt_disk": resourceDisk(),
},
}
}
Expand Down

0 comments on commit e5ec777

Please sign in to comment.