Skip to content

Commit

Permalink
B #464: mutualize raw schema and code parts
Browse files Browse the repository at this point in the history
  • Loading branch information
treywelsh committed Jul 11, 2023
1 parent d77ca7b commit 20f0559
Show file tree
Hide file tree
Showing 3 changed files with 134 additions and 160 deletions.
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
40 changes: 1 addition & 39 deletions opennebula/resource_opennebula_virtual_machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -1229,45 +1229,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
139 changes: 131 additions & 8 deletions opennebula/shared_schemas.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"

"github.com/OpenNebula/one/src/oca/go/src/goca/dynamic"
dyn "github.com/OpenNebula/one/src/oca/go/src/goca/dynamic"
"github.com/OpenNebula/one/src/oca/go/src/goca/schemas/shared"
"github.com/OpenNebula/one/src/oca/go/src/goca/schemas/vm"
vmk "github.com/OpenNebula/one/src/oca/go/src/goca/schemas/vm/keys"
Expand Down Expand Up @@ -85,14 +86,44 @@ func commonVMSchemas() map[string]*schema.Schema {

func commonInstanceSchema() map[string]*schema.Schema {
return map[string]*schema.Schema{
"cpu": cpuSchema(),
"vcpu": vcpuSchema(),
"memory": memorySchema(),
"context": contextSchema(),
"cpumodel": cpumodelSchema(),
"graphics": graphicsSchema(),
"os": osSchema(),
"vmgroup": vmGroupSchema(),
"cpu": cpuSchema(),
"vcpu": vcpuSchema(),
"memory": memorySchema(),
"context": contextSchema(),
"cpumodel": cpumodelSchema(),
"graphics": graphicsSchema(),
"os": osSchema(),
"vmgroup": vmGroupSchema(),
"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 !contains(value, validtypes) {
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",
},
},
},
},
"tags": tagsSchema(),
"default_tags": defaultTagsSchemaComputed(),
"tags_all": tagsSchemaComputed(),
Expand Down Expand Up @@ -654,6 +685,15 @@ func generateVMTemplate(d *schema.ResourceData, tpl *vm.Template) error {

}

//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))
}

descr, ok := d.GetOk("description")
if ok {
tpl.Add(vmk.Description, descr.(string))
Expand All @@ -662,6 +702,67 @@ func generateVMTemplate(d *schema.ResourceData, tpl *vm.Template) error {
return nil
}

func updateTemplate(d *schema.ResourceData, tpl *vm.Template) bool {

update := false

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)
update = true
}

return update
}

func updateRaw(d *schema.ResourceData, tpl *dyn.Template) {
tpl.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 := tpl.AddVector("RAW")
rawVec.AddPair("TYPE", rawConfig["type"].(string))
rawVec.AddPair("DATA", rawConfig["data"].(string))
}
}
}

func flattenNIC(nic shared.NIC) map[string]interface{} {

sg := make([]int, 0)
Expand Down Expand Up @@ -768,6 +869,8 @@ func flattenTemplate(d *schema.ResourceData, inheritedVectors map[string]interfa
port, _ := vmTemplate.GetIOGraphic(vmk.Port)
t, _ := vmTemplate.GetIOGraphic(vmk.GraphicType)
keymap, _ := vmTemplate.GetIOGraphic(vmk.Keymap)
// Raw
rawVec, _ := vmTemplate.GetVector("RAW")

// VM size
cpu, _ := vmTemplate.GetCPU()
Expand Down Expand Up @@ -841,6 +944,26 @@ func flattenTemplate(d *schema.ResourceData, inheritedVectors map[string]interfa
}
}

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 {
return err
}
}
}

return nil
}

Expand Down

0 comments on commit 20f0559

Please sign in to comment.