Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

B-470: fix templates filtering #471

Merged
merged 3 commits into from
Jul 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ DEPRECATION:
* `quotas` section of `opennebula_user` resource (#448)
* `quotas` section of `opennebula_group` resource (#447)

# 1.2.2 (Unreleased)
BUG FIXES:

* data/opennebula_template: fix filtering and documentation (#470)
* resources/opennebula_virtual_machine: fix a check on context

# 1.2.2 (May 31st, 2023)

Expand Down
25 changes: 16 additions & 9 deletions opennebula/data_opennebula_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,6 @@ func commonTemplatesFilter(d *schema.ResourceData, meta interface{}) ([]*templat
}

// filter templates with user defined criterias
id := d.Get("id")
name, nameOk := d.GetOk("name")
hasCPU := d.Get("has_cpu").(bool)
hasVCPU := d.Get("has_vcpu").(bool)
Expand All @@ -143,10 +142,6 @@ func commonTemplatesFilter(d *schema.ResourceData, meta interface{}) ([]*templat
match := make([]*templateSc.Template, 0, 1)
for i, template := range templates.Templates {

if id != -1 && template.ID != id {
continue
}

if nameOk && template.Name != name {
continue
}
Expand Down Expand Up @@ -186,19 +181,31 @@ func commonTemplatesFilter(d *schema.ResourceData, meta interface{}) ([]*templat

func templateFilter(d *schema.ResourceData, meta interface{}) (*templateSc.Template, error) {

match, err := commonTemplatesFilter(d, meta)
matched, err := commonTemplatesFilter(d, meta)
if err != nil {
return nil, err
}

newMatched := make([]*templateSc.Template, 0)

id := d.Get("id").(int)
if id != -1 {
for _, tpl := range matched {
if tpl.ID != id {
continue
}
newMatched = append(newMatched, tpl)
}
}

// the template datasource should match at most one element
if len(match) == 0 {
if len(newMatched) == 0 {
return nil, fmt.Errorf("no templates match the constraints")
} else if len(match) > 1 {
} else if len(newMatched) > 1 {
return nil, fmt.Errorf("several templates match the constraints")
}

return match[0], nil
return newMatched[0], nil
}

func datasourceOpennebulaTemplateRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
Expand Down
2 changes: 1 addition & 1 deletion opennebula/data_opennebula_templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func dataOpennebulaTemplates() *schema.Resource {
"order": {
Type: schema.TypeString,
Optional: true,
Description: "Ordering of the sort: asc or desc",
Description: "Ordering of the sort: ASC or DESC",
},
"templates": {
Type: schema.TypeList,
Expand Down
6 changes: 5 additions & 1 deletion opennebula/resource_opennebula_virtual_machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -2190,8 +2190,12 @@ func generateVm(d *schema.ResourceData, meta interface{}, templateContent *vm.Te
log.Printf("Number of CONTEXT vars: %d", len(context))
log.Printf("CONTEXT Map: %s", context)

var tplContext *dyn.Vector
if templateContent != nil {
tplContext, _ := templateContent.GetVector(vmk.ContextVec)
tplContext, _ = templateContent.GetVector(vmk.ContextVec)
}

if tplContext != nil {

// Update existing context:
// - add new pairs
Expand Down
4 changes: 2 additions & 2 deletions website/docs/d/templates.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ data "opennebula_templates" "example" {
name_regex = "test.*"
has_cpu = true
sort_on = "register_date"
sort_on = "ASC"
order = "ASC"
}
```

Expand All @@ -32,7 +32,7 @@ data "opennebula_templates" "example" {
* `has_memory` - (Optional) Indicate if a memory value has been defined.
* `memory` - (Optional) Amount of RAM assigned to the VM in MB.
* `tags` - (Optional) Template tags (Key = Value).
* `order` - (Optional) Ordering of the sort: asc or desc.
* `order` - (Optional) Ordering of the sort: ASC or DESC.

## Attribute Reference

Expand Down
Loading