Skip to content

Commit 980b6ea

Browse files
authored
feat: implemented block storage scheduler endpoints (#130)
1 parent 6936f4a commit 980b6ea

File tree

6 files changed

+1084
-9
lines changed

6 files changed

+1084
-9
lines changed

README.md

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ go get github.com/MagaluCloud/mgc-sdk-go
3636
- Volumes
3737
- Snapshots
3838
- Volume Types
39+
- Schedulers
3940
- SSH Keys
4041
- Availability Zones
4142
- Audit
@@ -60,13 +61,23 @@ go get github.com/MagaluCloud/mgc-sdk-go
6061
- Info
6162
- Nodepool
6263
- Version
64+
- Load Balancer as a Service (LBaaS)
65+
- Load Balancers
66+
- Listeners
67+
- Backends
68+
- Backend Targets
69+
- Health Checks
70+
- Certificates
71+
- Network ACLs
6372
- Network
6473
- VPCs
65-
- Interfaces
6674
- Subnets
75+
- Ports
6776
- Security Groups
77+
- Rules
6878
- Public IPs
69-
- Subnetpools
79+
- Subnet Pools
80+
- NAT Gateways
7081

7182
## Authentication
7283

@@ -440,7 +451,6 @@ Check the [cmd/examples](cmd/examples) directory for complete working examples o
440451

441452
We welcome contributions from the community! Please read our [Contributing Guide](CONTRIBUTING.md) to learn about our development process, how to propose bugfixes and improvements, and how to build and test your changes.
442453

443-
444454
## License
445455

446456
This project is licensed under the Apache 2.0 License - see the [LICENSE](LICENSE) file for details.

blockstorage/client.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,3 +62,9 @@ func (c *BlockStorageClient) VolumeTypes() VolumeTypeService {
6262
func (c *BlockStorageClient) Snapshots() SnapshotService {
6363
return &snapshotService{client: c}
6464
}
65+
66+
// Schedulers returns a service to manage volume schedulers.
67+
// This method allows access to functionality such as creating, listing, and managing schedulers for automated snapshot creation.
68+
func (c *BlockStorageClient) Schedulers() SchedulerService {
69+
return &schedulerService{client: c}
70+
}

blockstorage/client_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,16 @@ func TestBlockStorageClient_Services(t *testing.T) {
176176
t.Error("expected SnapshotService to be of type *snapshotService")
177177
}
178178
})
179+
180+
t.Run("Schedulers", func(t *testing.T) {
181+
svc := bsClient.Schedulers()
182+
if svc == nil {
183+
t.Error("expected SchedulerService to not be nil")
184+
}
185+
if _, ok := svc.(*schedulerService); !ok {
186+
t.Error("expected SchedulerService to be of type *schedulerService")
187+
}
188+
})
179189
}
180190

181191
func TestBlockStorageClient_DefaultBasePath(t *testing.T) {

blockstorage/schedulers.go

Lines changed: 256 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,256 @@
1+
package blockstorage
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"net/http"
7+
"net/url"
8+
"strconv"
9+
"time"
10+
11+
mgc_http "github.com/MagaluCloud/mgc-sdk-go/internal/http"
12+
)
13+
14+
// SchedulerVolumeExpand is a constant used for expanding volume information in scheduler responses.
15+
const (
16+
SchedulerVolumeExpand = "volume"
17+
)
18+
19+
// SchedulerListResponse represents the response from listing schedulers.
20+
// This structure encapsulates the API response format for schedulers.
21+
type SchedulerListResponse struct {
22+
Meta Metadata `json:"meta"`
23+
Schedulers []SchedulerResponse `json:"schedulers"`
24+
}
25+
26+
// Metadata represents pagination metadata.
27+
type Metadata struct {
28+
Page PageMetadata `json:"page"`
29+
}
30+
31+
// PageMetadata contains pagination information.
32+
type PageMetadata struct {
33+
Offset int `json:"offset"`
34+
Limit int `json:"limit"`
35+
Count int `json:"count"`
36+
Total int `json:"total"`
37+
MaxLimit int `json:"max_limit"`
38+
}
39+
40+
// DailyFrequency represents daily scheduling frequency.
41+
type DailyFrequency struct {
42+
StartTime string `json:"start_time"`
43+
}
44+
45+
// Frequency represents scheduling frequency configuration.
46+
type Frequency struct {
47+
Daily DailyFrequency `json:"daily"`
48+
}
49+
50+
// Policy represents the scheduler policy.
51+
type Policy struct {
52+
RetentionInDays int `json:"retention_in_days"`
53+
Frequency Frequency `json:"frequency"`
54+
}
55+
56+
// SnapshotConfig represents snapshot configuration.
57+
type SnapshotConfig struct {
58+
Type string `json:"type"`
59+
}
60+
61+
// SchedulerState represents the possible states of a scheduler.
62+
type SchedulerState string
63+
64+
const (
65+
SchedulerStateAvailable SchedulerState = "available"
66+
SchedulerStateDeleted SchedulerState = "deleted"
67+
)
68+
69+
// ExpandSchedulers represents the possible expand options for schedulers.
70+
type ExpandSchedulers string
71+
72+
const (
73+
ExpandSchedulersVolume ExpandSchedulers = "volume"
74+
)
75+
76+
// SchedulerResponse represents a scheduler.
77+
// A scheduler automates snapshot creation and retention for volumes.
78+
type SchedulerResponse struct {
79+
ID string `json:"id"`
80+
Name string `json:"name"`
81+
Description *string `json:"description,omitempty"`
82+
Volumes []string `json:"volumes,omitempty"`
83+
Snapshot *SnapshotConfig `json:"snapshot,omitempty"`
84+
State SchedulerState `json:"state"`
85+
Policy Policy `json:"policy"`
86+
CreatedAt time.Time `json:"created_at"`
87+
UpdatedAt time.Time `json:"updated_at"`
88+
}
89+
90+
// SchedulerPayload represents the request to create a new scheduler.
91+
type SchedulerPayload struct {
92+
Name string `json:"name"`
93+
Description *string `json:"description,omitempty"`
94+
Snapshot SnapshotConfig `json:"snapshot"`
95+
Policy Policy `json:"policy"`
96+
}
97+
98+
// SchedulerVolumeIdentifierPayload represents the request to attach/detach a volume to/from a scheduler.
99+
type SchedulerVolumeIdentifierPayload struct {
100+
Volume IDOrName `json:"volume"`
101+
}
102+
103+
// SchedulerListOptions contains options for listing schedulers.
104+
// All fields are optional and allow controlling pagination and expansion.
105+
type SchedulerListOptions struct {
106+
Limit *int
107+
Offset *int
108+
Sort *string
109+
Expand []ExpandSchedulers
110+
}
111+
112+
// SchedulerService provides operations for managing volume schedulers.
113+
// This interface allows creating, listing, retrieving, and managing schedulers.
114+
type SchedulerService interface {
115+
List(ctx context.Context, opts SchedulerListOptions) (*SchedulerListResponse, error)
116+
Create(ctx context.Context, req SchedulerPayload) (string, error)
117+
Get(ctx context.Context, id string, expand []ExpandSchedulers) (*SchedulerResponse, error)
118+
Delete(ctx context.Context, id string) error
119+
AttachVolume(ctx context.Context, id string, req SchedulerVolumeIdentifierPayload) error
120+
DetachVolume(ctx context.Context, id string, req SchedulerVolumeIdentifierPayload) error
121+
}
122+
123+
// schedulerService implements the SchedulerService interface.
124+
// This is an internal implementation that should not be used directly.
125+
type schedulerService struct {
126+
client *BlockStorageClient
127+
}
128+
129+
// List returns all schedulers.
130+
// This method makes an HTTP request to get the list of schedulers
131+
// and applies the filters specified in the options.
132+
func (s *schedulerService) List(ctx context.Context, opts SchedulerListOptions) (*SchedulerListResponse, error) {
133+
query := make(url.Values)
134+
135+
if opts.Limit != nil {
136+
query.Set("_limit", strconv.Itoa(*opts.Limit))
137+
}
138+
if opts.Offset != nil {
139+
query.Set("_offset", strconv.Itoa(*opts.Offset))
140+
}
141+
if opts.Sort != nil {
142+
query.Set("_sort", *opts.Sort)
143+
}
144+
if len(opts.Expand) > 0 {
145+
expandStrs := make([]string, len(opts.Expand))
146+
for i, expand := range opts.Expand {
147+
expandStrs[i] = string(expand)
148+
}
149+
for _, expand := range expandStrs {
150+
query.Add("expand", expand)
151+
}
152+
}
153+
154+
result, err := mgc_http.ExecuteSimpleRequestWithRespBody[SchedulerListResponse](
155+
ctx,
156+
s.client.newRequest,
157+
s.client.GetConfig(),
158+
http.MethodGet,
159+
"/v1/schedulers",
160+
nil,
161+
query,
162+
)
163+
if err != nil {
164+
return nil, err
165+
}
166+
return result, nil
167+
}
168+
169+
// Create provisions a new scheduler.
170+
// This method makes an HTTP request to create a new scheduler
171+
// and returns the ID of the created scheduler.
172+
func (s *schedulerService) Create(ctx context.Context, req SchedulerPayload) (string, error) {
173+
result, err := mgc_http.ExecuteSimpleRequestWithRespBody[struct{ ID string }](
174+
ctx,
175+
s.client.newRequest,
176+
s.client.GetConfig(),
177+
http.MethodPost,
178+
"/v1/schedulers",
179+
req,
180+
nil,
181+
)
182+
if err != nil {
183+
return "", err
184+
}
185+
return result.ID, nil
186+
}
187+
188+
// Get retrieves a specific scheduler.
189+
// This method makes an HTTP request to get detailed information about a scheduler
190+
// and optionally expands related resources.
191+
func (s *schedulerService) Get(ctx context.Context, id string, expand []ExpandSchedulers) (*SchedulerResponse, error) {
192+
path := fmt.Sprintf("/v1/schedulers/%s", id)
193+
query := make(url.Values)
194+
195+
if len(expand) > 0 {
196+
expandStrs := make([]string, len(expand))
197+
for i, expand := range expand {
198+
expandStrs[i] = string(expand)
199+
}
200+
for _, expand := range expandStrs {
201+
query.Add("expand", expand)
202+
}
203+
}
204+
205+
return mgc_http.ExecuteSimpleRequestWithRespBody[SchedulerResponse](
206+
ctx,
207+
s.client.newRequest,
208+
s.client.GetConfig(),
209+
http.MethodGet,
210+
path,
211+
nil,
212+
query,
213+
)
214+
}
215+
216+
// Delete removes a scheduler.
217+
// This method makes an HTTP request to delete a scheduler permanently.
218+
func (s *schedulerService) Delete(ctx context.Context, id string) error {
219+
return mgc_http.ExecuteSimpleRequest(
220+
ctx,
221+
s.client.newRequest,
222+
s.client.GetConfig(),
223+
http.MethodDelete,
224+
fmt.Sprintf("/v1/schedulers/%s", id),
225+
nil,
226+
nil,
227+
)
228+
}
229+
230+
// AttachVolume attaches a volume to a scheduler.
231+
// This method makes an HTTP request to attach a volume to an existing scheduler.
232+
func (s *schedulerService) AttachVolume(ctx context.Context, id string, req SchedulerVolumeIdentifierPayload) error {
233+
return mgc_http.ExecuteSimpleRequest(
234+
ctx,
235+
s.client.newRequest,
236+
s.client.GetConfig(),
237+
http.MethodPost,
238+
fmt.Sprintf("/v1/schedulers/%s/attach", id),
239+
req,
240+
nil,
241+
)
242+
}
243+
244+
// DetachVolume detaches a volume from a scheduler.
245+
// This method makes an HTTP request to detach a volume from an existing scheduler.
246+
func (s *schedulerService) DetachVolume(ctx context.Context, id string, req SchedulerVolumeIdentifierPayload) error {
247+
return mgc_http.ExecuteSimpleRequest(
248+
ctx,
249+
s.client.newRequest,
250+
s.client.GetConfig(),
251+
http.MethodPost,
252+
fmt.Sprintf("/v1/schedulers/%s/detach", id),
253+
req,
254+
nil,
255+
)
256+
}

0 commit comments

Comments
 (0)