Skip to content

Commit

Permalink
F OpenNebula#115: VM: add type and format disk attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
treywelsh committed Nov 16, 2021
1 parent c02ba36 commit 852fc90
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 18 deletions.
5 changes: 4 additions & 1 deletion opennebula/resource_opennebula_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -514,7 +514,10 @@ func generateTemplate(d *schema.ResourceData) (string, error) {
tpl.AddCtx(vmk.Context(keyUp), fmt.Sprint(value))
}

generateVMTemplate(d, tpl)
err := generateVMTemplate(d, tpl)
if err != nil {
return "", err
}

//Generate RAW definition
raw := d.Get("raw").([]interface{})
Expand Down
47 changes: 37 additions & 10 deletions opennebula/resource_opennebula_virtual_machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -593,11 +593,14 @@ func matchDisk(diskConfig map[string]interface{}, disk shared.Disk) bool {
size, _ := disk.GetI(shared.Size)
driver, _ := disk.Get(shared.Driver)
target, _ := disk.Get(shared.TargetDisk)
diskType, _ := disk.Get("TYPE")
format, _ := disk.Get("FORMAT")

return emptyOrEqual(diskConfig["target"], target) &&
emptyOrEqual(diskConfig["size"], size) &&
emptyOrEqual(diskConfig["driver"], driver)

emptyOrEqual(diskConfig["driver"], driver) &&
emptyOrEqual(diskConfig["volatile_type"], diskType) &&
emptyOrEqual(diskConfig["format"], format)
}

func matchDiskComputed(diskConfig map[string]interface{}, disk shared.Disk) bool {
Expand Down Expand Up @@ -653,6 +656,16 @@ diskLoop:
imageID, _ := disk.GetI(shared.ImageID)
diskMap["image_id"] = imageID

// for volatile disk, TYPE has the same value
// than DISK_TYPE
if imageID == -1 {
diskType, _ := disk.Get("TYPE")
diskMap["volatile_type"] = diskType
}

format, _ := disk.Get("FORMAT")
diskMap["format"] = format

diskList = append(diskList, diskMap)

break
Expand Down Expand Up @@ -1028,7 +1041,9 @@ func resourceOpennebulaVirtualMachineUpdate(d *schema.ResourceData, meta interfa
},
"image_id",
"target",
"driver")
"driver",
"volatile_type",
"format")

// reorder toAttach disk list according to new disks list order
newDisktoAttach := make([]interface{}, len(toAttach))
Expand Down Expand Up @@ -1064,8 +1079,11 @@ func resourceOpennebulaVirtualMachineUpdate(d *schema.ResourceData, meta interfa
for _, diskIf := range toDetach {
diskConfig := diskIf.(map[string]interface{})

imageID := diskConfig["image_id"].(int)
if imageID == -1 {
// ignore disk without image_id and type
if diskConfig["image_id"].(int) == -1 &&
len(diskConfig["volatile_type"].(string)) == 0 {

log.Printf("[INFO] ignore disk without image_id and type")
continue
}

Expand All @@ -1082,8 +1100,11 @@ func resourceOpennebulaVirtualMachineUpdate(d *schema.ResourceData, meta interfa
for _, diskIf := range toAttach {
diskConfig := diskIf.(map[string]interface{})

imageID := diskConfig["image_id"].(int)
if imageID == -1 {
// ignore disk without image_id and type
if diskConfig["image_id"].(int) == -1 &&
len(diskConfig["volatile_type"].(string)) == 0 {

log.Printf("[INFO] ignore disk without image_id and type")
continue
}

Expand All @@ -1099,8 +1120,11 @@ func resourceOpennebulaVirtualMachineUpdate(d *schema.ResourceData, meta interfa
for _, diskIf := range toResize {
diskConfig := diskIf.(map[string]interface{})

imageID := diskConfig["image_id"].(int)
if imageID == -1 {
// ignore disk without image_id and type
if diskConfig["image_id"].(int) == -1 &&
len(diskConfig["volatile_type"].(string)) == 0 {

log.Printf("[INFO] ignore disk without image_id and type")
continue
}

Expand Down Expand Up @@ -1495,7 +1519,10 @@ func generateVm(d *schema.ResourceData, tplContext *dyn.Vector) (string, error)
}
}

generateVMTemplate(d, tpl)
err := generateVMTemplate(d, tpl)
if err != nil {
return "", err
}

tplStr := tpl.String()
log.Printf("[INFO] VM definition: %s", tplStr)
Expand Down
65 changes: 58 additions & 7 deletions opennebula/shared_schemas.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,36 @@ func diskFields(customFields ...map[string]*schema.Schema) map[string]*schema.Sc
Type: schema.TypeString,
Optional: true,
},
"volatile_type": {
Type: schema.TypeString,
Optional: true,
Description: "Type of the disk: swap or fs.",
ValidateFunc: func(v interface{}, k string) (ws []string, errors []error) {
validtypes := []string{"swap", "fs"}
value := v.(string)

if inArray(value, validtypes) < 0 {
errors = append(errors, fmt.Errorf("Type %q must be one of: %s", k, strings.Join(validtypes, ",")))
}

return
},
},
"format": {
Type: schema.TypeString,
Optional: true,
Description: "Format of the Image: raw or qcow2.",
ValidateFunc: func(v interface{}, k string) (ws []string, errors []error) {
validtypes := []string{"raw", "qcow2"}
value := v.(string)

if inArray(value, validtypes) < 0 {
errors = append(errors, fmt.Errorf("Format %q must be one of: %s", k, strings.Join(validtypes, ",")))
}

return
},
},
}
}

Expand Down Expand Up @@ -264,7 +294,10 @@ func makeDiskVector(diskConfig map[string]interface{}) *shared.Disk {
for k, v := range diskConfig {

if k == "image_id" {
disk.Add(shared.ImageID, strconv.Itoa(v.(int)))
imageID := v.(int)
if imageID >= 0 {
disk.Add(shared.ImageID, strconv.Itoa(imageID))
}
continue
}

Expand All @@ -279,6 +312,10 @@ func makeDiskVector(diskConfig map[string]interface{}) *shared.Disk {
disk.Add(shared.Driver, v.(string))
case "size":
disk.Add(shared.Size, strconv.Itoa(v.(int)))
case "volatile_type":
disk.Add("TYPE", v.(string))
case "format":
disk.Add("FORMAT", v.(string))
}
}

Expand Down Expand Up @@ -356,7 +393,7 @@ func addGraphic(tpl *vm.Template, graphics []interface{}) {
}
}

func generateVMTemplate(d *schema.ResourceData, tpl *vm.Template) {
func generateVMTemplate(d *schema.ResourceData, tpl *vm.Template) error {

//Generate NIC definition
nics := d.Get("nic").([]interface{})
Expand All @@ -377,8 +414,17 @@ func generateVMTemplate(d *schema.ResourceData, tpl *vm.Template) {
for i := 0; i < len(disks); i++ {
diskconfig := disks[i].(map[string]interface{})

// ConflictsWith can't be used among attributes of a nested part: disk, nic etc.
// So we need to add a check here
if diskconfig["image_id"].(int) != -1 &&
(len(diskconfig["volatile_type"].(string)) > 0 ||
len(diskconfig["format"].(string)) > 0) {
return fmt.Errorf("disk attritutes image_id can't be defined at the same time as volatile_type or format")
}

// Ignore disk creation if Image ID is -1
if diskconfig["image_id"].(int) < 0 {
if diskconfig["image_id"].(int) == -1 &&
len(diskconfig["volatile_type"].(string)) == 0 {
continue
}

Expand Down Expand Up @@ -426,6 +472,7 @@ func generateVMTemplate(d *schema.ResourceData, tpl *vm.Template) {
tpl.AddPair(strings.ToUpper(k), v)
}

return nil
}

func flattenNIC(nic shared.NIC) map[string]interface{} {
Expand Down Expand Up @@ -470,12 +517,16 @@ func flattenDisk(disk shared.Disk) map[string]interface{} {
driver, _ := disk.Get(shared.Driver)
target, _ := disk.Get(shared.TargetDisk)
imageID, _ := disk.GetI(shared.ImageID)
volatileType, _ := disk.Get("TYPE")
format, _ := disk.Get("FORMAT")

return map[string]interface{}{
"image_id": imageID,
"size": size,
"target": target,
"driver": driver,
"image_id": imageID,
"size": size,
"target": target,
"driver": driver,
"volatile_type": volatileType,
"format": format,
}
}

Expand Down

0 comments on commit 852fc90

Please sign in to comment.