Skip to content

Commit

Permalink
F #322: mutualize datasource template(s) code
Browse files Browse the repository at this point in the history
  • Loading branch information
treywelsh committed Feb 1, 2023
1 parent 5bc9478 commit df19bd8
Show file tree
Hide file tree
Showing 2 changed files with 189 additions and 307 deletions.
183 changes: 102 additions & 81 deletions opennebula/data_opennebula_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,99 +11,108 @@ 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{
"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()

Schema: map[string]*schema.Schema{
"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)

if value == 0 {
errs = append(errs, errors.New("cpu should be strictly greater than 0"))
}

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

if value == 0 {
errs = append(errs, errors.New("cpu 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("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{
"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 @@ -161,10 +170,22 @@ func templateFilter(d *schema.ResourceData, meta interface{}) (*templateSc.Templ
match = append(match, &templates.Templates[i])
}

// check filtering results
if len(match) == 0 {
return nil, fmt.Errorf("no template match the constraints")
} else if len(match) > 1 {
return nil, fmt.Errorf("no templates match the constraints")
}

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) > 1 {
return nil, fmt.Errorf("several templates match the constraints")
}

Expand Down
Loading

0 comments on commit df19bd8

Please sign in to comment.