-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
117 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
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,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() | ||
} |
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