From fe73d519c9060d889d5cc9755c35ad4bf53d8830 Mon Sep 17 00:00:00 2001 From: dailz Date: Wed, 8 Apr 2026 21:32:43 +0800 Subject: [PATCH] feat(slurmdb): add InstancesService Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-openagent) Co-authored-by: Sisyphus --- internal/slurm/slurmdb_instances.go | 90 +++++++++++++++++++++++ internal/slurm/slurmdb_instances_test.go | 93 ++++++++++++++++++++++++ 2 files changed, 183 insertions(+) create mode 100644 internal/slurm/slurmdb_instances.go create mode 100644 internal/slurm/slurmdb_instances_test.go diff --git a/internal/slurm/slurmdb_instances.go b/internal/slurm/slurmdb_instances.go new file mode 100644 index 0000000..d0ebd7a --- /dev/null +++ b/internal/slurm/slurmdb_instances.go @@ -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 +} diff --git a/internal/slurm/slurmdb_instances_test.go b/internal/slurm/slurmdb_instances_test.go new file mode 100644 index 0000000..7bf9fa6 --- /dev/null +++ b/internal/slurm/slurmdb_instances_test.go @@ -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") + } +}