-
Notifications
You must be signed in to change notification settings - Fork 0
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: Liang Huang <[email protected]>
- Loading branch information
1 parent
cfb16b7
commit f377846
Showing
8 changed files
with
502 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,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 | ||
} |
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 @@ | ||
package client |
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,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) | ||
} |
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,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) | ||
} | ||
|
||
} |
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,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 | ||
} |
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,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) | ||
} | ||
}) | ||
} | ||
} |
Oops, something went wrong.