feat(slurmdb): add InstancesService

Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-openagent)

Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>
This commit is contained in:
dailz
2026-04-08 21:32:43 +08:00
parent 7af62eac41
commit fe73d519c9
2 changed files with 183 additions and 0 deletions

View File

@@ -0,0 +1,90 @@
package slurm
import (
"context"
"net/url"
)
// GetInstancesOptions specifies optional parameters for GetInstances and GetInstance.
type GetInstancesOptions struct {
Cluster *string `url:"cluster,omitempty"`
Extra *string `url:"extra,omitempty"`
InstanceID *string `url:"instance_id,omitempty"`
InstanceType *string `url:"instance_type,omitempty"`
}
// GetInstances lists all instances.
func (s *SlurmdbInstancesService) GetInstances(ctx context.Context, opts *GetInstancesOptions) (*OpenapiInstancesResp, *Response, error) {
path := "slurmdb/v0.0.40/instances"
req, err := s.client.NewRequest("GET", path, nil)
if err != nil {
return nil, nil, err
}
if opts != nil {
u, parseErr := url.Parse(req.URL.String())
if parseErr != nil {
return nil, nil, parseErr
}
q := u.Query()
if opts.Cluster != nil {
q.Set("cluster", *opts.Cluster)
}
if opts.Extra != nil {
q.Set("extra", *opts.Extra)
}
if opts.InstanceID != nil {
q.Set("instance_id", *opts.InstanceID)
}
if opts.InstanceType != nil {
q.Set("instance_type", *opts.InstanceType)
}
u.RawQuery = q.Encode()
req.URL = u
}
var result OpenapiInstancesResp
resp, err := s.client.Do(ctx, req, &result)
if err != nil {
return nil, resp, err
}
return &result, resp, nil
}
// GetInstance returns a single instance matching the query parameters.
func (s *SlurmdbInstancesService) GetInstance(ctx context.Context, opts *GetInstancesOptions) (*OpenapiInstancesResp, *Response, error) {
path := "slurmdb/v0.0.40/instance"
req, err := s.client.NewRequest("GET", path, nil)
if err != nil {
return nil, nil, err
}
if opts != nil {
u, parseErr := url.Parse(req.URL.String())
if parseErr != nil {
return nil, nil, parseErr
}
q := u.Query()
if opts.Cluster != nil {
q.Set("cluster", *opts.Cluster)
}
if opts.Extra != nil {
q.Set("extra", *opts.Extra)
}
if opts.InstanceID != nil {
q.Set("instance_id", *opts.InstanceID)
}
if opts.InstanceType != nil {
q.Set("instance_type", *opts.InstanceType)
}
u.RawQuery = q.Encode()
req.URL = u
}
var result OpenapiInstancesResp
resp, err := s.client.Do(ctx, req, &result)
if err != nil {
return nil, resp, err
}
return &result, resp, nil
}

View File

@@ -0,0 +1,93 @@
package slurm
import (
"context"
"fmt"
"net/http"
"net/http/httptest"
"testing"
)
func TestSlurmdbInstancesService_GetInstances(t *testing.T) {
mux := http.NewServeMux()
mux.HandleFunc("/slurmdb/v0.0.40/instances", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "GET")
fmt.Fprint(w, `{"instances": []}`)
})
server := httptest.NewServer(mux)
defer server.Close()
client, _ := NewClient(server.URL, nil)
resp, _, err := client.SlurmdbInstances.GetInstances(context.Background(), nil)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if resp == nil {
t.Fatal("expected non-nil response")
}
}
func TestSlurmdbInstancesService_GetInstancesWithOptions(t *testing.T) {
mux := http.NewServeMux()
mux.HandleFunc("/slurmdb/v0.0.40/instances", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "GET")
q := r.URL.Query()
if q.Get("cluster") != "test-cluster" {
t.Errorf("expected cluster=test-cluster, got %s", q.Get("cluster"))
}
if q.Get("extra") != "some-extra" {
t.Errorf("expected extra=some-extra, got %s", q.Get("extra"))
}
if q.Get("instance_id") != "i-12345" {
t.Errorf("expected instance_id=i-12345, got %s", q.Get("instance_id"))
}
if q.Get("instance_type") != "t2.micro" {
t.Errorf("expected instance_type=t2.micro, got %s", q.Get("instance_type"))
}
fmt.Fprint(w, `{"instances": []}`)
})
server := httptest.NewServer(mux)
defer server.Close()
client, _ := NewClient(server.URL, nil)
opts := &GetInstancesOptions{
Cluster: Ptr("test-cluster"),
Extra: Ptr("some-extra"),
InstanceID: Ptr("i-12345"),
InstanceType: Ptr("t2.micro"),
}
resp, _, err := client.SlurmdbInstances.GetInstances(context.Background(), opts)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if resp == nil {
t.Fatal("expected non-nil response")
}
}
func TestSlurmdbInstancesService_GetInstance(t *testing.T) {
mux := http.NewServeMux()
mux.HandleFunc("/slurmdb/v0.0.40/instance", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "GET")
fmt.Fprint(w, `{"instances": []}`)
})
server := httptest.NewServer(mux)
defer server.Close()
client, _ := NewClient(server.URL, nil)
resp, _, err := client.SlurmdbInstances.GetInstance(context.Background(), nil)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if resp == nil {
t.Fatal("expected non-nil response")
}
}
func TestSlurmdbInstancesService_GetInstances_InvalidURL(t *testing.T) {
client, _ := NewClient("http://invalid-host-does-not-exist.local", nil)
_, _, err := client.SlurmdbInstances.GetInstances(context.Background(), nil)
if err == nil {
t.Fatal("expected error for invalid URL")
}
}