-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Alejandro JNM <[email protected]>
- Loading branch information
1 parent
cdc4b01
commit cd0f787
Showing
4 changed files
with
231 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
package civogo | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"fmt" | ||
"strings" | ||
) | ||
|
||
// DiskImage represents a DiskImage for launching instances from | ||
type DiskImage struct { | ||
ID string `json:"id"` | ||
Name string `json:"name"` | ||
Version string `json:"version"` | ||
State string `json:"state"` | ||
Distribution string `json:"distribution"` | ||
Description string `json:"description"` | ||
Label string `json:"label"` | ||
} | ||
|
||
// ListDiskImages return all disk image in system | ||
func (c *Client) ListDiskImages() ([]DiskImage, error) { | ||
resp, err := c.SendGetRequest("/v2/disk_images") | ||
if err != nil { | ||
return nil, decodeERROR(err) | ||
} | ||
|
||
diskImages := make([]DiskImage, 0) | ||
if err := json.NewDecoder(bytes.NewReader(resp)).Decode(&diskImages); err != nil { | ||
return nil, err | ||
} | ||
|
||
return diskImages, nil | ||
} | ||
|
||
// GetDiskImage get one disk image using the id | ||
func (c *Client) GetDiskImage(id string) (*DiskImage, error) { | ||
resp, err := c.SendGetRequest(fmt.Sprintf("/v2/disk_images/%s", id)) | ||
if err != nil { | ||
return nil, decodeERROR(err) | ||
} | ||
|
||
diskImage := &DiskImage{} | ||
if err := json.NewDecoder(bytes.NewReader(resp)).Decode(&diskImage); err != nil { | ||
return nil, err | ||
} | ||
|
||
return diskImage, nil | ||
} | ||
|
||
// FindDiskImage finds a disk image by either part of the ID or part of the name | ||
func (c *Client) FindDiskImage(search string) (*DiskImage, error) { | ||
templateList, err := c.ListDiskImages() | ||
if err != nil { | ||
return nil, decodeERROR(err) | ||
} | ||
|
||
exactMatch := false | ||
partialMatchesCount := 0 | ||
result := DiskImage{} | ||
|
||
for _, value := range templateList { | ||
if value.Name == search || value.ID == search { | ||
exactMatch = true | ||
result = value | ||
} else if strings.Contains(value.Name, search) || strings.Contains(value.ID, search) { | ||
if !exactMatch { | ||
result = value | ||
partialMatchesCount++ | ||
} | ||
} | ||
} | ||
|
||
if exactMatch || partialMatchesCount == 1 { | ||
return &result, nil | ||
} else if partialMatchesCount > 1 { | ||
err := fmt.Errorf("unable to find %s because there were multiple matches", search) | ||
return nil, MultipleMatchesError.wrap(err) | ||
} else { | ||
err := fmt.Errorf("unable to find %s, zero matches", search) | ||
return nil, ZeroMatchesError.wrap(err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package civogo | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func TestClienterDiskImage(t *testing.T) { | ||
var c Clienter | ||
|
||
c, _ = NewClient("foo", "NYC1") | ||
c, _ = NewFakeClient() | ||
_, _ = c.ListDiskImages() | ||
} | ||
|
||
func TestListDiskImages(t *testing.T) { | ||
client, _ := NewFakeClient() | ||
|
||
results, err := client.ListDiskImages() | ||
if err != nil { | ||
t.Errorf("Request returned an error: %s", err) | ||
return | ||
} | ||
|
||
if len(results) != 4 { | ||
t.Errorf("Expected %+v, got %+v", 4, len(results)) | ||
return | ||
} | ||
|
||
} | ||
|
||
func TestGetDiskImage(t *testing.T) { | ||
client, _ := NewFakeClient() | ||
|
||
results, err := client.GetDiskImage("b82168fe-66f6-4b38-a3b8-5283542d5475") | ||
if err != nil { | ||
t.Errorf("Request returned an error: %s", err) | ||
return | ||
} | ||
|
||
if results.ID != "b82168fe-66f6-4b38-a3b8-5283542d5475" { | ||
t.Errorf("Expected %+v, got %+v", "b82168fe-66f6-4b38-a3b8-5283542d5475", results.ID) | ||
return | ||
} | ||
|
||
if results.Name != "centos-7" { | ||
t.Errorf("Expected %+v, got %+v", "centos-7", results.Name) | ||
return | ||
} | ||
|
||
} | ||
|
||
func TestFindDiskImage(t *testing.T) { | ||
client, _ := NewFakeClient() | ||
|
||
results, err := client.FindDiskImage("debian-10") | ||
if err != nil { | ||
t.Errorf("Request returned an error: %s", err) | ||
return | ||
} | ||
|
||
if results.ID != "b82168fe-66f6-4b38-a3b8-52835428965" { | ||
t.Errorf("Expected %+v, got %+v", "b82168fe-66f6-4b38-a3b8-52835428965", results.ID) | ||
return | ||
} | ||
|
||
if results.Name != "debian-10" { | ||
t.Errorf("Expected %+v, got %+v", "debian-10", results.Name) | ||
return | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters