Skip to content

Commit

Permalink
add template endpoints
Browse files Browse the repository at this point in the history
* add new feature to Template module

- You can now list all templates, create a new one, update and delete any templates that belonged to the user

BREAKING CHANGE: No

Signed-off-by: Alejandro JNM <[email protected]>
  • Loading branch information
alejandrojnm authored Mar 27, 2020
1 parent d20f49e commit c73d51f
Show file tree
Hide file tree
Showing 2 changed files with 201 additions and 7 deletions.
71 changes: 64 additions & 7 deletions template.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,36 +4,93 @@ import (
"bytes"
"encoding/json"
"errors"
"fmt"
)

// Template represents a Template for launching instances from
type Template struct {
ID string `json:"id"`
Code string `json:"code"`
Name string `json:"name"`
AccountID string `json:"account_id"`
ImageID string `json:"image_id"`
AccountID string `json:"account_id,omitempty"`
ImageID string `json:"image_id,omitempty"`
VolumeID string `json:"volume_id"`
ShortDescription string `json:"short_description"`
Description string `json:"description"`
DefaultUsername string `json:"default_username"`
CloudConfig string `json:"cloud_config"`
}

// GetTemplateByCode finds the Template for an account with the specified code
func (c *Client) GetTemplateByCode(code string) (*Template, error) {
// ListTemplates return all template in system
func (c *Client) ListTemplates() ([]Template, error) {
resp, err := c.SendGetRequest("/v2/templates")
if err != nil {
return nil, err
}

templates := make([]Template, 0)
err = json.NewDecoder(bytes.NewReader(resp)).Decode(&templates)
for _, template := range templates {
if err := json.NewDecoder(bytes.NewReader(resp)).Decode(&templates); err != nil {
return nil, err
}

return templates, nil
}

// NewTemplate will create a new template for the current user
func (c *Client) NewTemplate(conf *Template) (*SimpleResponse, error) {
if conf.ImageID == "" && conf.VolumeID == "" {
return nil, errors.New("if image id is not present, volume id must be")
}

resp, err := c.SendPostRequest("/v2/templates", conf)
if err != nil {
return nil, err
}

return c.DecodeSimpleResponse(resp)
}

// UpdateTemplate will update a template for the current user
func (c *Client) UpdateTemplate(id string, conf *Template) (*Template, error) {
if conf.ImageID == "" && conf.VolumeID == "" {
return nil, errors.New("if image id is not present, volume id must be")
}

resp, err := c.SendPutRequest(fmt.Sprintf("/v2/templates/%s", id), conf)
if err != nil {
return nil, err
}

template := &Template{}
if err := json.NewDecoder(bytes.NewReader(resp)).Decode(template); err != nil {
return nil, err
}

return template, nil
}

// GetTemplateByCode finds the Template for an account with the specified code
func (c *Client) GetTemplateByCode(code string) (*Template, error) {
resp, err := c.ListTemplates()
if err != nil {
return nil, err
}

for _, template := range resp {
if template.Code == code {
return &template, nil
}
}

return nil, errors.New("Template not found")
return nil, errors.New("template not found")
}

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

return c.DecodeSimpleResponse(resp)
}
137 changes: 137 additions & 0 deletions template_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package civogo

import (
"reflect"
"testing"
)

Expand All @@ -19,3 +20,139 @@ func TestGetTemplateByCode(t *testing.T) {
t.Errorf("Expected %s, got %s", "12345", got.ID)
}
}

func TestListTemplates(t *testing.T) {
client, server, _ := NewClientForTesting(map[string]string{
"/v2/templates": `[{
"id": "773aea72-d068-4cb7-8e08-28841acef0cb",
"code": "ubuntu-18.04",
"name": "Ubuntu 18.04",
"account_id": null,
"image_id": null,
"volume_id": "61c2cfe2-f7f3-46e7-b5c9-7b03ae25ea86",
"short_description": "Ubuntu 18.04",
"description": "The freely available Ubuntu 18.04 OS, minimally installed with just OpenSSH server",
"default_username": "ubuntu",
"cloud_config": "#cloud-config contents"
}]`,
})
defer server.Close()
got, err := client.ListTemplates()

if err != nil {
t.Errorf("Request returned an error: %s", err)
return
}
expected := []Template{
{
ID: "773aea72-d068-4cb7-8e08-28841acef0cb",
Code: "ubuntu-18.04",
Name: "Ubuntu 18.04",
VolumeID: "61c2cfe2-f7f3-46e7-b5c9-7b03ae25ea86",
ShortDescription: "Ubuntu 18.04",
Description: "The freely available Ubuntu 18.04 OS, minimally installed with just OpenSSH server",
DefaultUsername: "ubuntu",
CloudConfig: "#cloud-config contents",
},
}
if !reflect.DeepEqual(got, expected) {
t.Errorf("Expected %+v, got %+v", expected, got)
}

}

func TestNewTemplate(t *testing.T) {
client, server, _ := NewClientForTesting(map[string]string{
"/v2/templates": `{
"result": "success",
"id": "283d5ee6-fa9f-4e40-8e1a-bdc28812d593"
}`,
})
defer server.Close()

conf := &Template{
Code: "test",
Name: "Test",
ImageID: "811a8dfb-8202-49ad-b1ef-1e6320b20497",
ShortDescription: "my custom",
Description: "my custom image from golang",
DefaultUsername: "root",
}

got, err := client.NewTemplate(conf)
if err != nil {
t.Errorf("Request returned an error: %s", err)
return
}

expected := &SimpleResponse{Result: "success", ID: "283d5ee6-fa9f-4e40-8e1a-bdc28812d593"}
if !reflect.DeepEqual(got, expected) {
t.Errorf("Expected %+v, got %+v", expected, got)
}

}

func TestUpdateTemplate(t *testing.T) {
client, server, _ := NewClientForTesting(map[string]string{
"/v2/templates/283d5ee6-fa9f-4e40-8e1a-bdc28812d593": `{
"id": "283d5ee6-fa9f-4e40-8e1a-bdc28812d593",
"code": "my-linux-1.0",
"name": "My Linux 1.0",
"account_id": null,
"image_id": "811a8dfb-8202-49ad-b1ef-1e6320b20497",
"volume_id": null,
"short_description": "...",
"description": "...",
"default_username": "Ubuntu",
"cloud_config": "..."
}`,
})
defer server.Close()

conf := &Template{
Code: "my-linux-1.0",
Name: "My Linux 1.0",
ImageID: "811a8dfb-8202-49ad-b1ef-1e6320b20497",
ShortDescription: "...",
Description: "...",
DefaultUsername: "Ubuntu",
}

got, err := client.UpdateTemplate("283d5ee6-fa9f-4e40-8e1a-bdc28812d593", conf)
if err != nil {
t.Errorf("Request returned an error: %s", err)
return
}

expected := &Template{
ID: "283d5ee6-fa9f-4e40-8e1a-bdc28812d593",
Code: "my-linux-1.0",
Name: "My Linux 1.0",
ImageID: "811a8dfb-8202-49ad-b1ef-1e6320b20497",
ShortDescription: "...",
Description: "...",
DefaultUsername: "Ubuntu",
CloudConfig: "...",
}
if !reflect.DeepEqual(got, expected) {
t.Errorf("Expected %+v, got %+v", expected, got)
}

}

func TestDeleteTemplate(t *testing.T) {
client, server, _ := NewClientForTesting(map[string]string{
"/v2/templates/12346": `{"result": "success"}`,
})
defer server.Close()
got, err := client.DeleteTemplate("12346")
if err != nil {
t.Errorf("Request returned an error: %s", err)
return
}

expected := &SimpleResponse{Result: "success"}
if !reflect.DeepEqual(got, expected) {
t.Errorf("Expected %+v, got %+v", expected, got)
}
}

0 comments on commit c73d51f

Please sign in to comment.