Skip to content

Commit

Permalink
add client
Browse files Browse the repository at this point in the history
Signed-off-by: Liang Huang <[email protected]>
  • Loading branch information
yellow-shine committed Mar 4, 2024
1 parent cfb16b7 commit f377846
Show file tree
Hide file tree
Showing 8 changed files with 502 additions and 0 deletions.
94 changes: 94 additions & 0 deletions client/cluster.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package client
type ModifyClusterParams struct {
CuSize int `json:"cuSize"`
}

type ModifyClusterResponse struct {
ClusterId string `json:"clusterId"`
}

func (c *Client) ModifyCluster(clusterId string, params *ModifyClusterParams) (*string, error) {
var response zillizResponse[ModifyClusterResponse]
err := c.do("POST", "clusters/"+clusterId+"/modify", params, &response)
if err != nil {
return nil, err
}
return &response.Data.ClusterId, err
}

type DropClusterResponse struct {
ClusterId string `json:"clusterId"`
}

func (c *Client) DropCluster(clusterId string) (*string, error) {
var response zillizResponse[DropClusterResponse]
err := c.do("DELETE", "clusters/"+clusterId+"/drop", nil, &response)
if err != nil {
return nil, err
}
return &response.Data.ClusterId, err
}



type Clusters struct {
zillizPage
Clusters []Cluster `json:"clusters"`
}

type Cluster struct {
ClusterId string `json:"clusterId"`
ClusterName string `json:"clusterName"`
Description string `json:"description"`
RegionId string `json:"regionId"`
ClusterType string `json:"clusterType"`
CuSize int64 `json:"cuSize"`
Status string `json:"status"`
ConnectAddress string `json:"connectAddress"`
PrivateLinkAddress string `json:"privateLinkAddress"`
CreateTime string `json:"createTime"`
}

func (c *Client) ListClusters() (Clusters, error) {
var clusters zillizResponse[Clusters]
err := c.do("GET", "clusters", nil, &clusters)
return clusters.Data, err
}

func (c *Client) DescribeCluster(clusterId string) (Cluster, error) {
var cluster zillizResponse[Cluster]
err := c.do("GET", "clusters/"+clusterId, nil, &cluster)
return cluster.Data, err
}

type CreateClusterParams struct {
Plan string `json:"plan"`
ClusterName string `json:"clusterName"`
CUSize int `json:"cuSize"`
CUType string `json:"cuType"`
ProjectId string `json:"projectId"`
}

type CreateServerlessClusterParams struct {
ClusterName string `json:"clusterName"`
ProjectId string `json:"projectId"`
}

type CreateClusterResponse struct {
ClusterId string `json:"clusterId"`
Username string `json:"username"`
Password string `json:"password"`
Prompt string `json:"prompt"`
}

func (c *Client) CreateCluster(params CreateClusterParams) (*CreateClusterResponse, error) {
var clusterResponse zillizResponse[CreateClusterResponse]
err := c.do("POST", "clusters/create", params, &clusterResponse)
return &clusterResponse.Data, err
}

func (c *Client) CreateServerlessCluster(params CreateServerlessClusterParams) (*CreateClusterResponse, error) {
var clusterResponse zillizResponse[CreateClusterResponse]
err := c.do("POST", "clusters/createServerless", params, &clusterResponse)
return &clusterResponse.Data, err
}
1 change: 1 addition & 0 deletions client/cluster_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package client
14 changes: 14 additions & 0 deletions client/error.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package client

import (
"fmt"
)

type Error struct {
Code int `json:"code"`
Message string `json:"message"`
}

func (err Error) Error() string {
return fmt.Sprintf("code:%d,Message:%s", err.Code, err.Message)
}
32 changes: 32 additions & 0 deletions client/error_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package client

import (
"encoding/json"
"strings"
"testing"
)

var errJson = []byte(`
{
"code":80001,
"message":"Invalid token: Your token is not valid. Please double-check your request parameters and ensure that you have provided a valid token."
}
`)

func TestError_UnmarshalJSON(t *testing.T) {
var e Error
err := json.Unmarshal(errJson, &e)
if err != nil {
t.Errorf("Error.UnmarshalJSON() error = %v", err)
}
wantCode := 80001
if e.Code != wantCode {
t.Errorf("Error.UnmarshalJSON() = %v, want %v", e.Code, wantCode)
}

wantMessage := "Invalid token"
if !strings.Contains(e.Message, wantMessage) {
t.Errorf("Error.UnmarshalJSON() = %v, want %v", e.Message, wantMessage)
}

}
12 changes: 12 additions & 0 deletions client/project.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package client

type Project struct {
ProjectId string `json:"projectId"`
ProjectName string `json:"projectName"`
}

func (c *Client) ListProjects() ([]Project, error) {
var response zillizResponse[[]Project]
err := c.do("GET", "projects", nil, &response)
return response.Data, err
}
91 changes: 91 additions & 0 deletions client/project_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package client

import (
"flag"
"testing"
)

var (
apiKey string
)

func init() {
flag.StringVar(&apiKey, "key", "", "Your TEST secret key for the zilliz cloud API. If present, integration tests will be run using this key.")
}


func TestClient_ListProjects(t *testing.T) {
if apiKey == "" {
t.Skip("No API key provided")
}

type checkFn func(*testing.T, []Project, error)
check := func(fns ...checkFn) []checkFn { return fns }

hasNoErr := func() checkFn {
return func(t *testing.T, _ []Project, err error) {
if err != nil {
t.Fatalf("err = %v; want nil", err)
}
}
}

hasErrCode := func(code int) checkFn {
return func(t *testing.T, _ []Project, err error) {
se, ok := err.(Error)
if !ok {
t.Fatalf("err isn't a Error")
}
if se.Code != code {
t.Errorf("err.Code = %d; want %d", se.Code, code)
}
}
}

hasProject := func(Name string) checkFn {
return func(t *testing.T, p []Project, err error) {
for _, project := range p {
if project.ProjectName == Name {
return
}
}
t.Errorf("project not found: %s", Name)
}
}

type fields struct {
CloudRegionId string
apiKey string
}
tests := []struct {
name string
fields fields
checks []checkFn
}{
{
"postive 1",
fields{CloudRegionId: "gcp-us-west1", apiKey: apiKey},
check(
hasNoErr(),
hasProject("Default Project")),
},
{
"none exist region",
fields{CloudRegionId: "gcp-us-west1", apiKey: "fake"},
check(hasErrCode(80001)),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
c := NewClient(
tt.fields.apiKey,
tt.fields.CloudRegionId,
)

got, err := c.ListProjects()
for _, check := range tt.checks {
check(t, got, err)
}
})
}
}
Loading

0 comments on commit f377846

Please sign in to comment.