Skip to content

Commit

Permalink
B #470: templates datasource shouldn't filter on ID
Browse files Browse the repository at this point in the history
  • Loading branch information
treywelsh committed Jul 18, 2023
1 parent 8a8d47a commit e1622b7
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 10 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ 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)

# 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

0 comments on commit e1622b7

Please sign in to comment.