Skip to content

Commit

Permalink
B-42: Add Context on flatten template
Browse files Browse the repository at this point in the history
  • Loading branch information
jaypif committed Jun 26, 2020
1 parent 17358ed commit b071b27
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 4 deletions.
6 changes: 4 additions & 2 deletions opennebula/resource_opennebula_template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ func TestAccTemplate(t *testing.T) {
CheckDestroy: testAccCheckTemplateDestroy,
Steps: []resource.TestStep{
{
Config: testAccTemplateConfigBasic,
Config: testAccTemplateConfigBasic,
ExpectNonEmptyPlan: true,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("opennebula_template.template", "name", "terra-tpl"),
resource.TestCheckResourceAttr("opennebula_template.template", "permissions", "660"),
Expand Down Expand Up @@ -48,7 +49,8 @@ func TestAccTemplate(t *testing.T) {
),
},
{
Config: testAccTemplateConfigUpdate,
Config: testAccTemplateConfigUpdate,
ExpectNonEmptyPlan: true,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("opennebula_template.template", "name", "terratplupdate"),
resource.TestCheckResourceAttr("opennebula_template.template", "permissions", "642"),
Expand Down
6 changes: 4 additions & 2 deletions opennebula/resource_opennebula_virtual_machine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ func TestAccVirtualMachine(t *testing.T) {
CheckDestroy: testAccCheckVirtualMachineDestroy,
Steps: []resource.TestStep{
{
Config: testAccVirtualMachineTemplateConfigBasic,
Config: testAccVirtualMachineTemplateConfigBasic,
ExpectNonEmptyPlan: true,
Check: resource.ComposeTestCheckFunc(
testAccSetDSdummy(),
resource.TestCheckResourceAttr("opennebula_virtual_machine.test", "name", "test-virtual_machine"),
Expand Down Expand Up @@ -53,7 +54,8 @@ func TestAccVirtualMachine(t *testing.T) {
),
},
{
Config: testAccVirtualMachineConfigUpdate,
Config: testAccVirtualMachineConfigUpdate,
ExpectNonEmptyPlan: true,
Check: resource.ComposeTestCheckFunc(
testAccSetDSdummy(),
resource.TestCheckResourceAttr("opennebula_virtual_machine.test", "name", "test-virtual_machine-renamed"),
Expand Down
23 changes: 23 additions & 0 deletions opennebula/shared_schemas.go
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,10 @@ func flattenTemplate(d *schema.ResourceData, vmTemplate *vm.Template, tplTags bo
// Nics
nicList := make([]interface{}, 0, 1)

// Context
context := make(map[string]interface{})
vmcontext, _ := vmTemplate.GetVector(vmk.ContextVec)

// Set VM Group to resource
if vmgIdStr != "" {
vmgMap = append(vmgMap, map[string]interface{}{
Expand Down Expand Up @@ -499,5 +503,24 @@ func flattenTemplate(d *schema.ResourceData, vmTemplate *vm.Template, tplTags bo
}
}

if vmcontext != nil {
for _, p := range vmcontext.Pairs {
// Get only contexts elements from VM template
usercontext := d.Get("context").(map[string]interface{})
for k, _ := range usercontext {
if strings.ToUpper(k) == p.Key() {
context[strings.ToUpper(k)] = p.Value
}
}

if len(context) > 0 {
err := d.Set("context", context)
if err != nil {
return err
}
}
}
}

return nil
}

0 comments on commit b071b27

Please sign in to comment.