Skip to content

Commit

Permalink
Added FindTemplate fucntion to the template module
Browse files Browse the repository at this point in the history
Signed-off-by: Alejandro JNM <alejandrojnm@gmail.com>
  • Loading branch information
alejandrojnm committed Aug 11, 2020
1 parent 4cc1b48 commit 59a8681
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions template.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/json"
"errors"
"fmt"
"strings"
)

// Template represents a Template for launching instances from
Expand Down Expand Up @@ -85,6 +86,31 @@ func (c *Client) GetTemplateByCode(code string) (*Template, error) {
return nil, errors.New("template not found")
}

// FindTemplate finds a template by either part of the ID or part of the code
func (c *Client) FindTemplate(search string) (*Template, error) {
resp, err := c.ListTemplates()
if err != nil {
return nil, decodeERROR(err)
}

found := -1

for i, template := range resp {
if strings.Contains(template.ID, search) || strings.Contains(template.Code, search) {
if found != -1 {
return nil, fmt.Errorf("unable to find %s because there were multiple matches", search)
}
found = i
}
}

if found == -1 {
return nil, fmt.Errorf("unable to find %s, zero matches", search)
}

return &resp[found], nil
}

// DeleteTemplate deletes requested template
func (c *Client) DeleteTemplate(id string) (*SimpleResponse, error) {
resp, err := c.SendDeleteRequest(fmt.Sprintf("/v2/templates/%s", id))
Expand Down

0 comments on commit 59a8681

Please sign in to comment.