Skip to content

Commit

Permalink
B #464: VM: add and share raw section
Browse files Browse the repository at this point in the history
  • Loading branch information
treywelsh authored and frousselet committed Jul 19, 2023
1 parent cd053b5 commit c25dcd3
Show file tree
Hide file tree
Showing 5 changed files with 148 additions and 160 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ FEATURES:

* **New Resource**: `opennebula_user_quotas` (#448)
* **New Resource**: `opennebula_group_quotas` (#447)
* resources/opennebula_virtual_machine: add `raw` section and share its code with `opennebula_template` resource. (#464)

DEPRECATION:

Expand Down
115 changes: 2 additions & 113 deletions opennebula/resource_opennebula_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,36 +58,6 @@ func commonTemplateSchemas() map[string]*schema.Schema {
},
},
"disk": diskSchema(),
"raw": {
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
Description: "Low-level hypervisor tuning",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"type": {
Type: schema.TypeString,
Required: true,
ValidateFunc: func(v interface{}, k string) (ws []string, errors []error) {
validtypes := []string{"kvm", "lxd", "vmware"}
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
},
Description: "Name of the hypervisor: kvm, lxd, vmware",
},
"data": {
Type: schema.TypeString,
Required: true,
Description: "Low-level data to pass to the hypervisor",
},
},
},
},
"reg_time": {
Type: schema.TypeInt,
Computed: true,
Expand Down Expand Up @@ -475,32 +445,6 @@ func resourceOpennebulaTemplateReadCustom(ctx context.Context, d *schema.Resourc
diags = append(diags, diag)
}

rawVec, _ := tpl.Template.GetVector("RAW")
if rawVec != nil {

rawMap := make([]map[string]interface{}, 0, 1)

hypType, _ := rawVec.GetStr("TYPE")
data, _ := rawVec.GetStr("DATA")

rawMap = append(rawMap, map[string]interface{}{
"type": hypType,
"data": data,
})

if _, ok := d.GetOk("raw"); ok {
err = d.Set("raw", rawMap)
if err != nil {
diags = append(diags, diag.Diagnostic{
Severity: diag.Error,
Summary: "Failed to set attribute",
Detail: fmt.Sprintf("template (ID: %s): %s", d.Id(), err),
})
return diags
}
}
}

if tpl.LockInfos != nil {
d.Set("lock", LockLevelToString(tpl.LockInfos.Locked))
}
Expand Down Expand Up @@ -714,50 +658,11 @@ func resourceOpennebulaTemplateUpdateCustom(ctx context.Context, d *schema.Resou
}

if d.HasChange("raw") {
newTpl.Del("RAW")

raw := d.Get("raw").([]interface{})
if len(raw) > 0 {
for i := 0; i < len(raw); i++ {
rawConfig := raw[i].(map[string]interface{})
rawVec := newTpl.AddVector("RAW")
rawVec.AddPair("TYPE", rawConfig["type"].(string))
rawVec.AddPair("DATA", rawConfig["data"].(string))
}
}
}

if d.HasChange("sched_requirements") {
schedRequirements := d.Get("sched_requirements").(string)

if len(schedRequirements) > 0 {
newTpl.Placement(vmk.SchedRequirements, schedRequirements)
} else {
newTpl.Del(string(vmk.SchedRequirements))
}
update = true
}

if d.HasChange("sched_ds_requirements") {
schedDSRequirements := d.Get("sched_ds_requirements").(string)

if len(schedDSRequirements) > 0 {
newTpl.Placement(vmk.SchedDSRequirements, schedDSRequirements)
} else {
newTpl.Del(string(vmk.SchedDSRequirements))
}
updateRaw(d, &newTpl.Template)
update = true
}

if d.HasChange("description") {
newTpl.Del(string(vmk.Description))

description := d.Get("description").(string)

if len(description) > 0 {
newTpl.Add(vmk.Description, description)
}

if updateTemplate(d, &newTpl) {
update = true
}

Expand Down Expand Up @@ -813,13 +718,6 @@ func resourceOpennebulaTemplateUpdateCustom(ctx context.Context, d *schema.Resou
update = true
}

if d.HasChange("template_section") {

updateTemplateSection(d, &newTpl.Template)

update = true
}

if d.HasChange("tags") {

oldTagsIf, newTagsIf := d.GetChange("tags")
Expand Down Expand Up @@ -1002,15 +900,6 @@ func generateTemplate(d *schema.ResourceData, meta interface{}) (*vm.Template, e
return nil, err
}

//Generate RAW definition
raw := d.Get("raw").([]interface{})
for i := 0; i < len(raw); i++ {
rawConfig := raw[i].(map[string]interface{})
rawVec := tpl.AddVector("RAW")
rawVec.AddPair("TYPE", rawConfig["type"].(string))
rawVec.AddPair("DATA", rawConfig["data"].(string))
}

// add default tags if they aren't overriden
config := meta.(*Configuration)

Expand Down
45 changes: 6 additions & 39 deletions opennebula/resource_opennebula_virtual_machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -1225,45 +1225,7 @@ func resourceOpennebulaVirtualMachineUpdateCustom(ctx context.Context, d *schema
},
}

if d.HasChange("sched_requirements") {
schedRequirements := d.Get("sched_requirements").(string)

if len(schedRequirements) > 0 {
tpl.Placement(vmk.SchedRequirements, schedRequirements)
} else {
tpl.Del(string(vmk.SchedRequirements))
}
update = true
}

if d.HasChange("sched_ds_requirements") {
schedDSRequirements := d.Get("sched_ds_requirements").(string)

if len(schedDSRequirements) > 0 {
tpl.Placement(vmk.SchedDSRequirements, schedDSRequirements)
} else {
tpl.Del(string(vmk.SchedDSRequirements))
}
update = true
}

if d.HasChange("description") {

tpl.Del(string(vmk.Description))

description := d.Get("description").(string)

if len(description) > 0 {
tpl.Add(vmk.Description, description)
}

update = true
}

if d.HasChange("template_section") {

updateTemplateSection(d, &tpl.Template)

if updateTemplate(d, tpl) {
update = true
}

Expand Down Expand Up @@ -1503,6 +1465,11 @@ func resourceOpennebulaVirtualMachineUpdateCustom(ctx context.Context, d *schema
}
}

if d.HasChange("raw") {
updateRaw(d, &tpl.Template)
updateConf = true
}

if d.HasChange("cpu") || d.HasChange("vcpu") || d.HasChange("memory") {

timeout := time.Duration(d.Get("timeout").(int)) * time.Minute
Expand Down
Loading

0 comments on commit c25dcd3

Please sign in to comment.