Merge branch 'feat/verbose-debug' into master
This commit is contained in:
@@ -11,6 +11,7 @@ type ProviderConfig struct {
|
||||
AccessKeyID string
|
||||
AccessKeySecret string
|
||||
Endpoint string
|
||||
Debug bool
|
||||
}
|
||||
|
||||
type Provider interface {
|
||||
|
||||
@@ -8,6 +8,7 @@ import (
|
||||
"io"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"os"
|
||||
"time"
|
||||
)
|
||||
|
||||
@@ -17,9 +18,10 @@ type client struct {
|
||||
httpClient *http.Client
|
||||
baseURL string
|
||||
apiKey string
|
||||
debug bool
|
||||
}
|
||||
|
||||
func newClient(endpoint, apiKey string) *client {
|
||||
func newClient(endpoint, apiKey string, debug bool) *client {
|
||||
if endpoint == "" {
|
||||
endpoint = defaultEndpoint
|
||||
}
|
||||
@@ -27,6 +29,7 @@ func newClient(endpoint, apiKey string) *client {
|
||||
httpClient: &http.Client{Timeout: 30 * time.Second},
|
||||
baseURL: endpoint,
|
||||
apiKey: apiKey,
|
||||
debug: debug,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -85,14 +88,23 @@ func (c *client) do(ctx context.Context, method, path string, body, result any,
|
||||
}
|
||||
|
||||
var bodyReader io.Reader
|
||||
var bodyBytes []byte
|
||||
if body != nil {
|
||||
b, err := json.Marshal(body)
|
||||
if err != nil {
|
||||
return fmt.Errorf("marshal request: %w", err)
|
||||
}
|
||||
bodyBytes = b
|
||||
bodyReader = bytes.NewReader(b)
|
||||
}
|
||||
|
||||
if c.debug {
|
||||
fmt.Fprintf(os.Stderr, "[debug] > %s %s\n", method, u)
|
||||
if bodyBytes != nil {
|
||||
fmt.Fprintf(os.Stderr, "[debug] %s\n", bodyBytes)
|
||||
}
|
||||
}
|
||||
|
||||
req, err := http.NewRequestWithContext(ctx, method, u, bodyReader)
|
||||
if err != nil {
|
||||
return fmt.Errorf("create request: %w", err)
|
||||
@@ -101,6 +113,7 @@ func (c *client) do(ctx context.Context, method, path string, body, result any,
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
req.Header.Set("Accept", "application/json")
|
||||
|
||||
start := time.Now()
|
||||
resp, err := c.httpClient.Do(req)
|
||||
if err != nil {
|
||||
return fmt.Errorf("request failed: %w", err)
|
||||
@@ -112,6 +125,11 @@ func (c *client) do(ctx context.Context, method, path string, body, result any,
|
||||
return fmt.Errorf("read response: %w", err)
|
||||
}
|
||||
|
||||
if c.debug {
|
||||
fmt.Fprintf(os.Stderr, "[debug] < %d %s %s\n", resp.StatusCode, http.StatusText(resp.StatusCode), time.Since(start).Round(time.Millisecond))
|
||||
fmt.Fprintf(os.Stderr, "[debug] %s\n", respBody)
|
||||
}
|
||||
|
||||
var apiResp apiResponse
|
||||
if err := json.Unmarshal(respBody, &apiResp); err == nil {
|
||||
// Rainyun uses code:200 and code:0 both as success indicators
|
||||
|
||||
@@ -16,7 +16,7 @@ func TestClientAuthHeader(t *testing.T) {
|
||||
}))
|
||||
defer srv.Close()
|
||||
|
||||
c := newClient(srv.URL, "sk-test")
|
||||
c := newClient(srv.URL, "sk-test", false)
|
||||
var result struct {
|
||||
ID int `json:"id"`
|
||||
Name string `json:"name"`
|
||||
@@ -40,7 +40,7 @@ func TestClientAPIError(t *testing.T) {
|
||||
}))
|
||||
defer srv.Close()
|
||||
|
||||
c := newClient(srv.URL, "sk-test")
|
||||
c := newClient(srv.URL, "sk-test", false)
|
||||
var result any
|
||||
err := c.get(context.Background(), "/error", &result)
|
||||
if err == nil {
|
||||
|
||||
@@ -21,7 +21,7 @@ type Rainyun struct {
|
||||
|
||||
// New creates a Rainyun provider from config.
|
||||
func New(cfg provider.ProviderConfig) (provider.Provider, error) {
|
||||
c := newClient(cfg.Endpoint, cfg.APIKey)
|
||||
c := newClient(cfg.Endpoint, cfg.APIKey, cfg.Debug)
|
||||
r := &Rainyun{client: c}
|
||||
r.server = &ServerService{client: c}
|
||||
r.domain = &DomainService{client: c}
|
||||
|
||||
@@ -16,15 +16,15 @@ type ServerService struct {
|
||||
// rainyunServer mirrors the Rainyun RCS API JSON fields (PascalCase).
|
||||
type rainyunServer struct {
|
||||
ID int `json:"ID"`
|
||||
Name string `json:"name"`
|
||||
HostName string `json:"HostName"`
|
||||
Status string `json:"status"`
|
||||
IP string `json:"ip"`
|
||||
VCPU int `json:"vcpu"`
|
||||
MainIPv4 string `json:"MainIPv4"`
|
||||
CPU int `json:"CPU"`
|
||||
Memory int `json:"memory"`
|
||||
Disk int `json:"disk"`
|
||||
OS string `json:"os"`
|
||||
Region string `json:"zone_name"`
|
||||
ExpireDate int64 `json:"ExpDate"` // Unix timestamp
|
||||
OsName string `json:"OsName"`
|
||||
Zone string `json:"Zone"`
|
||||
ExpireDate int64 `json:"ExpDate"`
|
||||
}
|
||||
|
||||
func (s *ServerService) List(ctx context.Context) ([]model.Server, error) {
|
||||
@@ -40,12 +40,14 @@ func (s *ServerService) List(ctx context.Context) ([]model.Server, error) {
|
||||
}
|
||||
|
||||
func (s *ServerService) Get(ctx context.Context, id string) (*model.Server, error) {
|
||||
var raw rainyunServer
|
||||
var resp struct {
|
||||
Data rainyunServer `json:"Data"`
|
||||
}
|
||||
path := fmt.Sprintf("/product/rcs/%s/", id)
|
||||
if err := s.client.get(ctx, path, &raw); err != nil {
|
||||
if err := s.client.get(ctx, path, &resp); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
sv := toServer(raw)
|
||||
sv := toServer(resp.Data)
|
||||
return &sv, nil
|
||||
}
|
||||
|
||||
@@ -90,14 +92,14 @@ func toServer(r rainyunServer) model.Server {
|
||||
expire := time.Unix(r.ExpireDate, 0)
|
||||
return model.Server{
|
||||
ID: fmt.Sprintf("%d", r.ID),
|
||||
Name: r.Name,
|
||||
Name: r.HostName,
|
||||
Status: r.Status,
|
||||
IP: r.IP,
|
||||
CPU: r.VCPU,
|
||||
IP: r.MainIPv4,
|
||||
CPU: r.CPU,
|
||||
Memory: r.Memory,
|
||||
Disk: r.Disk,
|
||||
OS: r.OS,
|
||||
Region: r.Region,
|
||||
OS: r.OsName,
|
||||
Region: r.Zone,
|
||||
ExpireAt: expire,
|
||||
Raw: r,
|
||||
}
|
||||
|
||||
@@ -18,15 +18,16 @@ func TestServerList(t *testing.T) {
|
||||
"data": map[string]any{
|
||||
"Records": []map[string]any{
|
||||
{
|
||||
"ID": 12001,
|
||||
"name": "web-server",
|
||||
"status": "running",
|
||||
"ip": "1.2.3.4",
|
||||
"vcpu": 2,
|
||||
"memory": 4096,
|
||||
"disk": 50,
|
||||
"os": "Ubuntu 22.04",
|
||||
"ExpDate": 1756656000,
|
||||
"ID": 12001,
|
||||
"HostName": "web-server",
|
||||
"status": "running",
|
||||
"MainIPv4": "1.2.3.4",
|
||||
"CPU": 2,
|
||||
"memory": 4096,
|
||||
"disk": 50,
|
||||
"OsName": "Ubuntu 22.04",
|
||||
"Zone": "cn-sy1",
|
||||
"ExpDate": 1756656000,
|
||||
},
|
||||
},
|
||||
"TotalRecords": 1,
|
||||
@@ -36,7 +37,7 @@ func TestServerList(t *testing.T) {
|
||||
}))
|
||||
defer srv.Close()
|
||||
|
||||
rain := &Rainyun{client: newClient(srv.URL, "sk-test")}
|
||||
rain := &Rainyun{client: newClient(srv.URL, "sk-test", false)}
|
||||
svc := rain.Server()
|
||||
servers, err := svc.List(context.Background())
|
||||
if err != nil {
|
||||
@@ -66,7 +67,7 @@ func TestServerStart(t *testing.T) {
|
||||
}))
|
||||
defer srv.Close()
|
||||
|
||||
rain := &Rainyun{client: newClient(srv.URL, "sk-test")}
|
||||
rain := &Rainyun{client: newClient(srv.URL, "sk-test", false)}
|
||||
err := rain.Server().Start(context.Background(), "12001")
|
||||
if err != nil {
|
||||
t.Fatalf("Start() error: %v", err)
|
||||
@@ -83,7 +84,7 @@ func TestServerVNC(t *testing.T) {
|
||||
}))
|
||||
defer srv.Close()
|
||||
|
||||
rain := &Rainyun{client: newClient(srv.URL, "sk-test")}
|
||||
rain := &Rainyun{client: newClient(srv.URL, "sk-test", false)}
|
||||
url, err := rain.Server().GetVNC(context.Background(), "12001")
|
||||
if err != nil {
|
||||
t.Fatalf("GetVNC() error: %v", err)
|
||||
|
||||
Reference in New Issue
Block a user