Skip to content

Commit

Permalink
F #322: add templates datasource
Browse files Browse the repository at this point in the history
  • Loading branch information
treywelsh committed Mar 7, 2023
1 parent 6110a31 commit 0372cc2
Show file tree
Hide file tree
Showing 5 changed files with 408 additions and 86 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:

* resources/opennebula_virtual_network_address_range: add `ipam` field (#192)
* resources/opennebula_virtual_network_address_range: add `custom` section to allow to pass user defined custom attributes (#376)
* **New Data Source**: `opennebula_templates` (#322)

BUG FIXES:

Expand Down
191 changes: 105 additions & 86 deletions opennebula/data_opennebula_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,105 +11,114 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

func dataOpennebulaTemplate() *schema.Resource {
return &schema.Resource{
ReadContext: datasourceOpennebulaTemplateRead,
func commonDatasourceTemplateSchema() map[string]*schema.Schema {
return map[string]*schema.Schema{
"has_cpu": {
Type: schema.TypeBool,
Optional: true,
Description: "Indicate if template has CPU defined",
},
"has_vcpu": {
Type: schema.TypeBool,
Optional: true,
Description: "Indicate if template has VCPU defined",
},
"has_memory": {
Type: schema.TypeBool,
Optional: true,
Description: "Indicate if template has memory defined",
},
"cpu": func() *schema.Schema {
s := cpuSchema()

Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeInt,
Optional: true,
Default: -1,
Description: "Id of the template",
},
"name": {
Type: schema.TypeString,
Optional: true,
Description: "Name of the Template",
},
"has_cpu": {
Type: schema.TypeBool,
Optional: true,
Description: "Indicate if template has CPU defined",
},
"has_vcpu": {
Type: schema.TypeBool,
Optional: true,
Description: "Indicate if template has VCPU defined",
},
"has_memory": {
Type: schema.TypeBool,
Optional: true,
Description: "Indicate if template has memory defined",
},
"cpu": func() *schema.Schema {
s := cpuSchema()
s.ValidateFunc = func(v interface{}, k string) (ws []string, errs []error) {
value := v.(float64)

s.ValidateFunc = func(v interface{}, k string) (ws []string, errs []error) {
value := v.(float64)
if value == 0 {
errs = append(errs, errors.New("cpu should be strictly greater than 0"))
}

if value == 0 {
errs = append(errs, errors.New("cpu should be strictly greater than 0"))
}
return
}
return s
}(),
"vcpu": func() *schema.Schema {
s := vcpuSchema()

return
s.ValidateFunc = func(v interface{}, k string) (ws []string, errs []error) {
value := v.(int)

if value == 0 {
errs = append(errs, errors.New("vcpu should be strictly greater than 0"))
}
return s
}(),
"vcpu": func() *schema.Schema {
s := vcpuSchema()

s.ValidateFunc = func(v interface{}, k string) (ws []string, errs []error) {
value := v.(int)
return
}
return s
}(),
"memory": func() *schema.Schema {
s := memorySchema()

if value == 0 {
errs = append(errs, errors.New("vcpu should be strictly greater than 0"))
}
s.ValidateFunc = func(v interface{}, k string) (ws []string, errs []error) {
value := v.(int)

return
if value == 0 {
errs = append(errs, errors.New("memory should be strictly greater than 0"))
}
return s
}(),
"memory": func() *schema.Schema {
s := memorySchema()

s.ValidateFunc = func(v interface{}, k string) (ws []string, errs []error) {
value := v.(int)
return
}
return s
}(),
"tags": tagsSchema(),
}
}

if value == 0 {
errs = append(errs, errors.New("memory should be strictly greater than 0"))
}
func dataOpennebulaTemplate() *schema.Resource {
return &schema.Resource{
ReadContext: datasourceOpennebulaTemplateRead,

return
}
return s
}(),
"disk": func() *schema.Schema {
s := diskSchema()
s.Computed = true
s.Optional = false
return s
}(),
"nic": func() *schema.Schema {
s := nicSchema()
s.Computed = true
s.Optional = false
return s
}(),
"vmgroup": func() *schema.Schema {
s := vmGroupSchema()
s.Computed = true
s.Optional = false
s.MaxItems = 0
s.Description = "Virtual Machine Group to associate with during VM creation only."
return s
}(),
"tags": tagsSchema(),
},
Schema: mergeSchemas(
commonDatasourceTemplateSchema(),
map[string]*schema.Schema{
"id": {
Type: schema.TypeInt,
Optional: true,
Default: -1,
Description: "Id of the template",
},
"name": {
Type: schema.TypeString,
Optional: true,
Description: "Name of the Template",
},
"disk": func() *schema.Schema {
s := diskSchema()
s.Computed = true
s.Optional = false
return s
}(),
"nic": func() *schema.Schema {
s := nicSchema()
s.Computed = true
s.Optional = false
return s
}(),
"vmgroup": func() *schema.Schema {
s := vmGroupSchema()
s.Computed = true
s.Optional = false
s.MaxItems = 0
s.Description = "Virtual Machine Group to associate with during VM creation only."
return s
}(),
},
),
}
}

func templateFilter(d *schema.ResourceData, meta interface{}) (*templateSc.Template, error) {
// shared with opennebula_templates datasource
func commonTemplatesFilter(d *schema.ResourceData, meta interface{}) ([]*templateSc.Template, error) {

config := meta.(*Configuration)
controller := config.Controller
Expand Down Expand Up @@ -172,9 +181,19 @@ func templateFilter(d *schema.ResourceData, meta interface{}) (*templateSc.Templ
match = append(match, &templates.Templates[i])
}

// check filtering results
return match, nil
}

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

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

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

0 comments on commit 0372cc2

Please sign in to comment.