Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
260 additions
and
14 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package main | ||
|
||
import ( | ||
"bufio" | ||
"github.com/nvthongswansea/gsclient-go" | ||
log "github.com/sirupsen/logrus" | ||
"os" | ||
) | ||
|
||
func main() { | ||
uuid := os.Getenv("GRIDSCALE_UUID") | ||
token := os.Getenv("GRIDSCALE_TOKEN") | ||
config := gsclient.NewConfiguration( | ||
"https://api.gridscale.io", | ||
uuid, | ||
token, | ||
true, | ||
) | ||
client := gsclient.NewClient(config) | ||
log.Info("gridscale client configured") | ||
|
||
log.Info("Create label: Press 'Enter' to continue...") | ||
bufio.NewReader(os.Stdin).ReadBytes('\n') | ||
|
||
_, err := client.CreateLabel(gsclient.LabelCreateRequest{ | ||
Label: "go-client-label", | ||
}) | ||
if err != nil { | ||
log.Error("Create label has failed with error", err) | ||
return | ||
} | ||
log.Info("Label successfully created") | ||
defer func() { | ||
err := client.DeleteLabel("go-client-label") | ||
if err != nil { | ||
log.Error("Delete label has failed with error", err) | ||
return | ||
} | ||
}() | ||
|
||
log.Info("Retrieve labels: Press 'Enter' to continue...") | ||
bufio.NewReader(os.Stdin).ReadBytes('\n') | ||
|
||
labels, err := client.GetLabelList() | ||
if err != nil { | ||
log.Error("Retrieve labels has failed with error", err) | ||
return | ||
} | ||
log.WithFields(log.Fields{ | ||
"labels": labels, | ||
}).Info("Labels successfully retrieved") | ||
|
||
log.Info("Delete label: Press 'Enter' to continue...") | ||
bufio.NewReader(os.Stdin).ReadBytes('\n') | ||
} |
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,70 @@ | ||
package gsclient | ||
|
||
import ( | ||
"net/http" | ||
"path" | ||
) | ||
|
||
//LabelList JSON struct of a list of labels | ||
type LabelList struct { | ||
List map[string]LabelProperties `json:"labels"` | ||
} | ||
|
||
//Label JSON struct of a single label | ||
type Label struct { | ||
Properties LabelProperties `json:"label"` | ||
} | ||
|
||
//LabelProperties JSON struct of properties of a label | ||
type LabelProperties struct { | ||
Label string `json:"label"` | ||
CreateTime string `json:"create_time"` | ||
ChangeTime string `json:"change_time"` | ||
Relations []interface{} `json:"relations"` | ||
Status string `json:"status"` | ||
} | ||
|
||
//LabelCreateRequest JSON struct of a request for creating a label | ||
type LabelCreateRequest struct { | ||
Label string `json:"label"` | ||
} | ||
|
||
//GetLabelList gets a list of available labels | ||
func (c *Client) GetLabelList() ([]Label, error) { | ||
r := Request{ | ||
uri: apiLabelBase, | ||
method: http.MethodGet, | ||
} | ||
var response LabelList | ||
var labels []Label | ||
err := r.execute(*c, &response) | ||
for _, properties := range response.List { | ||
labels = append(labels, Label{Properties: properties}) | ||
} | ||
return labels, err | ||
} | ||
|
||
//CreateLabel creates a new label | ||
func (c *Client) CreateLabel(body LabelCreateRequest) (CreateResponse, error) { | ||
r := Request{ | ||
uri: apiLabelBase, | ||
method: http.MethodPost, | ||
body: body, | ||
} | ||
var response CreateResponse | ||
err := r.execute(*c, &response) | ||
if err != nil { | ||
return CreateResponse{}, err | ||
} | ||
err = c.WaitForRequestCompletion(response.RequestUUID) | ||
return response, err | ||
} | ||
|
||
//DeleteLabel deletes a label | ||
func (c *Client) DeleteLabel(label string) error { | ||
r := Request{ | ||
uri: path.Join(apiLabelBase, label), | ||
method: http.MethodDelete, | ||
} | ||
return r.execute(*c, nil) | ||
} |
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,89 @@ | ||
package gsclient | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"github.com/stretchr/testify/assert" | ||
"net/http" | ||
"path" | ||
"testing" | ||
) | ||
|
||
func TestClient_GetLabelList(t *testing.T) { | ||
server, client, mux := setupTestClient() | ||
defer server.Close() | ||
uri := apiLabelBase | ||
mux.HandleFunc(uri, func(writer http.ResponseWriter, request *http.Request) { | ||
assert.Equal(t, http.MethodGet, request.Method) | ||
fmt.Fprintf(writer, prepareLabelListHTTPGet()) | ||
}) | ||
res, err := client.GetLabelList() | ||
if err != nil { | ||
t.Errorf("GetLabelList returned an error %v", err) | ||
} | ||
assert.Equal(t, 1, len(res)) | ||
assert.Equal(t, fmt.Sprintf("[%v]", getMockLabel()), fmt.Sprintf("%v", res)) | ||
} | ||
|
||
func TestClient_CreateLabel(t *testing.T) { | ||
server, client, mux := setupTestClient() | ||
defer server.Close() | ||
uri := apiLabelBase | ||
mux.HandleFunc(uri, func(writer http.ResponseWriter, request *http.Request) { | ||
assert.Equal(t, http.MethodPost, request.Method) | ||
fmt.Fprint(writer, prepareLabelCreateResponse()) | ||
}) | ||
httpResponse := fmt.Sprintf(`{"%s": {"status":"done"}}`, dummyRequestUUID) | ||
mux.HandleFunc("/requests/", func(w http.ResponseWriter, r *http.Request) { | ||
fmt.Fprint(w, httpResponse) | ||
}) | ||
res, err := client.CreateLabel(LabelCreateRequest{Label: "test"}) | ||
if err != nil { | ||
t.Errorf("CreateLabel returned an error %v", err) | ||
} | ||
assert.Equal(t, fmt.Sprintf("%v", getMockLabelCreateResponse()), fmt.Sprintf("%v", res)) | ||
} | ||
|
||
func TestClient_DeleteLabel(t *testing.T) { | ||
server, client, mux := setupTestClient() | ||
defer server.Close() | ||
uri := path.Join(apiLabelBase, "test") | ||
mux.HandleFunc(uri, func(writer http.ResponseWriter, request *http.Request) { | ||
assert.Equal(t, http.MethodDelete, request.Method) | ||
fmt.Fprint(writer, "") | ||
}) | ||
err := client.DeleteLabel("test") | ||
if err != nil { | ||
t.Errorf("DeleteLabel returned an error %v", err) | ||
} | ||
} | ||
|
||
func getMockLabel() Label { | ||
mock := Label{Properties: LabelProperties{ | ||
Label: "test", | ||
CreateTime: dummyTime, | ||
ChangeTime: dummyTime, | ||
Relations: nil, | ||
Status: "active", | ||
}} | ||
return mock | ||
} | ||
|
||
func getMockLabelCreateResponse() CreateResponse { | ||
mock := CreateResponse{ | ||
RequestUUID: dummyRequestUUID, | ||
} | ||
return mock | ||
} | ||
|
||
func prepareLabelListHTTPGet() string { | ||
label := getMockLabel() | ||
res, _ := json.Marshal(label.Properties) | ||
return fmt.Sprintf(`{"labels": {"%s": %s}}`, dummyUUID, string(res)) | ||
} | ||
|
||
func prepareLabelCreateResponse() string { | ||
response := getMockLabelCreateResponse() | ||
res, _ := json.Marshal(response) | ||
return string(res) | ||
} |
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
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