Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 15 additions & 15 deletions pkg/paigpu/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,21 +234,21 @@ type Env struct {
Value string `json:"value"`
}
type CreateInstanceRequest struct {
Name string `json:"name"`
ProductId string `json:"productId"`
GpuNum int `json:"gpuNum"`
DiskSize int `json:"diskSize"`
BillingMode string `json:"billingMode"`
Duration int `json:"duration"`
ImageUrl string `json:"imageUrl"`
ImageAuth string `json:"imageAuth"`
Ports string `json:"ports"`
Envs []Env `json:"envs"`
Command string `json:"command"`
ClusterId string `json:"clusterId"`
NetworkStorageId string `json:"networkStorageId"`
LocalStorageMountPoint string `json:"localStorageMountPoint"`
NetworkStorageMountPoint string `json:"networkStorageMountPoint"`
Name string `json:"name,omitempty"`
ProductId string `json:"productId,omitempty"`
GpuNum int `json:"gpuNum,omitempty"`
DiskSize int `json:"diskSize,omitempty"`
BillingMode string `json:"billingMode,omitempty"`
Duration int `json:"duration,omitempty"`
ImageUrl string `json:"imageUrl,omitempty"`
ImageAuth string `json:"imageAuth,omitempty"`
Ports string `json:"ports,omitempty"`
Envs []Env `json:"envs,omitempty"`
Command string `json:"command,omitempty"`
ClusterId string `json:"clusterId,omitempty"`
NetworkStorageId string `json:"networkStorageId,omitempty"`
LocalStorageMountPoint string `json:"localStorageMountPoint,omitempty"`
NetworkStorageMountPoint string `json:"networkStorageMountPoint,omitempty"`
}

type CreateInstanceResponse struct {
Expand Down
113 changes: 113 additions & 0 deletions pkg/paigpu/networkstorage.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
package paigpu

import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"net/http"
)

type Storage struct {
StorageID string `json:"storageId"`
StorageName string `json:"storageName"`
StorageSize int `json:"storageSize"`
ClusterID string `json:"clusterId"`
ClusterName string `json:"clusterName"`
Price string `json:"price"`
}

type ListStoragesResponse struct {
Data []Storage `json:"data"`
Total int `json:"total"`
}

func (c *Client) ListStorages(ctx context.Context) (*ListStoragesResponse, error) {
url := fmt.Sprintf("%s/v1/networkstorages/list", c.baseURL)
request, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
if err != nil {
return nil, err
}
request.Header.Set(HeaderAppID, c.appID)
request.Header.Set(HeaderNonce, RandomNonce(16))
timestamp := Timestamp()
request.Header.Set(HeaderTimestamp, fmt.Sprintf("%d", timestamp))
signature := Signature("/openapi/v1/networkstorages/list", c.appID, c.appKey, request.Header.Get(HeaderNonce), timestamp)
request.Header.Set(HeaderSignature, signature)
response, err := c.httpClient.Do(request)
if err != nil {
return nil, err
}
defer response.Body.Close()
body, err := io.ReadAll(response.Body)
if err != nil {
return nil, err
}
result := ListStoragesResponse{}
err = json.Unmarshal(body, &result)
if err != nil {
return nil, err
}
return &result, nil
}

type CreateStorageRequest struct {
ClusterID string `json:"clusterId"`
StorageName string `json:"storageName"`
StorageSize int `json:"storageSize"`
}

type CreateStorageResponse struct {
}

func (c *Client) CreateStorage(ctx context.Context,
clusterID string,
storageName string,
storageSize int,
) (*CreateStorageResponse, error) {
url := fmt.Sprintf("%s/v1/networkstorage/create", c.baseURL)

requestBody := CreateStorageRequest{
ClusterID: clusterID,
StorageName: storageName,
StorageSize: storageSize,
}

requestBodyBytes, err := json.Marshal(requestBody)
if err != nil {
return nil, err
}

request, err := http.NewRequestWithContext(ctx, http.MethodPost, url, bytes.NewReader(requestBodyBytes))
if err != nil {
return nil, err
}

request.Header.Set("Content-Type", "application/json")

request.Header.Set(HeaderAppID, c.appID)
request.Header.Set(HeaderNonce, RandomNonce(16))
timestamp := Timestamp()
request.Header.Set(HeaderTimestamp, fmt.Sprintf("%d", timestamp))
signature := Signature("/openapi/v1/networkstorage/create", c.appID, c.appKey, request.Header.Get(HeaderNonce), timestamp)
request.Header.Set(HeaderSignature, signature)

response, err := c.httpClient.Do(request)

if err != nil {
return nil, err
}
defer response.Body.Close()

responseBody, err := io.ReadAll(response.Body)
if err != nil {
return nil, err
}
result := CreateStorageResponse{}
err = json.Unmarshal(responseBody, &result)
if err != nil {
return nil, err
}
return &result, nil
}
27 changes: 27 additions & 0 deletions pkg/paigpu/networkstorage_integration_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package paigpu

import (
"context"
"os"
"testing"
)

func TestClient_ListStorages(t *testing.T) {
ctx := context.Background()
client := NewClient(os.Getenv("PAIGPU_APP_ID"), os.Getenv("PAIGPU_APP_SECRET"))
storages, err := client.ListStorages(ctx)
if err != nil {
t.Fatal(err)
}
t.Log("storages", storages)
}

func TestClient_CreateStorage(t *testing.T) {
ctx := context.Background()
client := NewClient(os.Getenv("PAIGPU_APP_ID"), os.Getenv("PAIGPU_APP_SECRET"))
response, err := client.CreateStorage(ctx, "22", "paigpu-go-integration-test", 22)
if err != nil {
t.Fatal(err)
}
t.Log("response", response)
}