func dataSourceCloudstackTemplateRead()

in cloudstack/data_source_cloudstack_template.go [91:128]


func dataSourceCloudstackTemplateRead(d *schema.ResourceData, meta interface{}) error {
	cs := meta.(*cloudstack.CloudStackClient)

	p := cloudstack.ListTemplatesParams{}
	p.SetListall(true)
	p.SetTemplatefilter(d.Get("template_filter").(string))

	csTemplates, err := cs.Template.ListTemplates(&p)
	if err != nil {
		return fmt.Errorf("Failed to list templates: %s", err)
	}

	filters := d.Get("filter")
	var templates []*cloudstack.Template

	for _, t := range csTemplates.Templates {
		match, err := applyFilters(t, filters.(*schema.Set))
		if err != nil {
			return err
		}

		if match {
			templates = append(templates, t)
		}
	}

	if len(templates) == 0 {
		return fmt.Errorf("No template is matching with the specified regex")
	}

	template, err := latestTemplate(templates)
	if err != nil {
		return err
	}
	log.Printf("[DEBUG] Selected template: %s\n", template.Displaytext)

	return templateDescriptionAttributes(d, template)
}