Skip to content

Commit 15731c1

Browse files
authored
Merge pull request #23 from serverscom/sbm-servers
Sbm servers
2 parents b195153 + 8c4d96c commit 15731c1

File tree

8 files changed

+389
-1
lines changed

8 files changed

+389
-1
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
[
2+
{
3+
"id": "xkazYeJ0",
4+
"title": "example.aa",
5+
"location_id": 1,
6+
"status": "init",
7+
"configuration": "Dell chassis-9015 / 2 GB RAM / 2 x hdd-model-404",
8+
"private_ipv4_address": null,
9+
"public_ipv4_address": null,
10+
"lease_start_at": null,
11+
"scheduled_release_at": null,
12+
"type": "sbm_server",
13+
"created_at": "2020-04-22T06:22:04Z",
14+
"updated_at": "2020-04-22T06:22:04Z"
15+
},
16+
{
17+
"id": "w9aAOdvM",
18+
"title": "example.bb",
19+
"location_id": 1,
20+
"status": "init",
21+
"configuration": "Dell chassis-9015 / 2 GB RAM / 2 x hdd-model-404",
22+
"private_ipv4_address": null,
23+
"public_ipv4_address": null,
24+
"lease_start_at": null,
25+
"scheduled_release_at": null,
26+
"type": "sbm_server",
27+
"created_at": "2020-04-22T06:22:04Z",
28+
"updated_at": "2020-04-22T06:22:04Z"
29+
}
30+
]
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"id": "xkazYeJ0",
3+
"title": "example.aa",
4+
"location_id": 1,
5+
"status": "active",
6+
"configuration": "REMM R123",
7+
"private_ipv4_address": "10.0.0.1",
8+
"public_ipv4_address": "169.254.0.1",
9+
"lease_start_at": "2020-04-20",
10+
"scheduled_release_at": null,
11+
"type": "sbm_server",
12+
"created_at": "2020-04-22T06:22:02Z",
13+
"updated_at": "2020-04-22T06:22:02Z"
14+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"id": "xkazYeJ0",
3+
"title": "example.aa",
4+
"location_id": 1,
5+
"status": "active",
6+
"configuration": "REMM R123",
7+
"private_ipv4_address": "10.0.0.1",
8+
"public_ipv4_address": "169.254.0.1",
9+
"lease_start_at": "2020-04-20",
10+
"scheduled_release_at": null,
11+
"type": "sbm_server",
12+
"created_at": "2020-04-22T06:22:02Z",
13+
"updated_at": "2020-04-22T06:22:02Z"
14+
}

pkg/hosts.go

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ const (
2727
dedicatedServerReinstallPath = "/hosts/dedicated_servers/%s/reinstall"
2828

2929
kubernetesBaremetalNodePath = "/hosts/kubernetes_baremetal_nodes/%s"
30+
31+
sbmServerCreatePath = "/hosts/sbm_servers"
32+
sbmServerPath = "/hosts/sbm_servers/%s"
3033
)
3134

3235
// HostsService is an interface for interfacing with Host, Dedicated Server endpoints
@@ -40,7 +43,10 @@ type HostsService interface {
4043
// Generic operations
4144
GetDedicatedServer(ctx context.Context, id string) (*DedicatedServer, error)
4245
GetKubernetesBaremetalNode(ctx context.Context, id string) (*KubernetesBaremetalNode, error)
46+
GetSBMServer(ctx context.Context, id string) (*SBMServer, error)
4347
CreateDedicatedServers(ctx context.Context, input DedicatedServerCreateInput) ([]DedicatedServer, error)
48+
CreateSBMServers(ctx context.Context, input SBMServerCreateInput) ([]SBMServer, error)
49+
ReleaseSBMServer(ctx context.Context, id string) (*SBMServer, error)
4450

4551
// Additional operations
4652
ScheduleReleaseForDedicatedServer(ctx context.Context, id string) (*DedicatedServer, error)
@@ -338,3 +344,70 @@ func (h *HostsHandler) DedicatedServerPTRRecords(id string) Collection[PTRRecord
338344

339345
return NewCollection[PTRRecord](h.client, path)
340346
}
347+
348+
// GetSBMServer returns an sbm server
349+
// Endpoint: https://developers.servers.com/api-documentation/v1/#tag/Scalable-Baremetal-Server/operation/GetAnSbmServer
350+
func (h *HostsHandler) GetSBMServer(ctx context.Context, id string) (*SBMServer, error) {
351+
url := h.client.buildURL(sbmServerPath, []interface{}{id}...)
352+
353+
body, err := h.client.buildAndExecRequest(ctx, "GET", url, nil)
354+
355+
if err != nil {
356+
return nil, err
357+
}
358+
359+
sbmServer := new(SBMServer)
360+
361+
if err := json.Unmarshal(body, &sbmServer); err != nil {
362+
return nil, err
363+
}
364+
365+
return sbmServer, nil
366+
}
367+
368+
// CreateSBMServers creates an SBM servers
369+
// Endpoint: https://developers.servers.com/api-documentation/v1/#tag/Scalable-Baremetal-Server/operation/CreateAnSbmServer
370+
func (h *HostsHandler) CreateSBMServers(ctx context.Context, input SBMServerCreateInput) ([]SBMServer, error) {
371+
payload, err := json.Marshal(input)
372+
373+
if err != nil {
374+
return nil, err
375+
}
376+
377+
url := h.client.buildURL(sbmServerCreatePath)
378+
379+
body, err := h.client.buildAndExecRequest(ctx, "POST", url, payload)
380+
381+
if err != nil {
382+
return nil, err
383+
}
384+
385+
var sbmServers []SBMServer
386+
387+
if err := json.Unmarshal(body, &sbmServers); err != nil {
388+
return nil, err
389+
}
390+
391+
return sbmServers, nil
392+
}
393+
394+
// ReleaseSBMServer removes an SBM server from account.
395+
// This action is irreversible and the removal process will be initiated immediately!!!
396+
// Endpoint: https://developers.servers.com/api-documentation/v1/#tag/Scalable-Baremetal-Server/operation/ReleaseAnSbmServer
397+
func (h *HostsHandler) ReleaseSBMServer(ctx context.Context, id string) (*SBMServer, error) {
398+
url := h.client.buildURL(sbmServerPath, []interface{}{id}...)
399+
400+
body, err := h.client.buildAndExecRequest(ctx, "DELETE", url, nil)
401+
402+
if err != nil {
403+
return nil, err
404+
}
405+
406+
sbmServer := new(SBMServer)
407+
408+
if err := json.Unmarshal(body, &sbmServer); err != nil {
409+
return nil, err
410+
}
411+
412+
return sbmServer, nil
413+
}

pkg/hosts_test.go

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -548,3 +548,123 @@ func TestDedicatedServerPTRRecordsCollection(t *testing.T) {
548548
g.Expect(collection.HasFirstPage()).To(Equal(false))
549549
g.Expect(collection.HasLastPage()).To(Equal(false))
550550
}
551+
552+
func TestHostsCreateSBMServers(t *testing.T) {
553+
g := NewGomegaWithT(t)
554+
555+
ts, client := newFakeServer().
556+
WithRequestPath("/hosts/sbm_servers").
557+
WithRequestMethod("POST").
558+
WithResponseBodyStubFile("fixtures/hosts/sbm_servers/create_response.json").
559+
WithResponseCode(201).
560+
Build()
561+
562+
defer ts.Close()
563+
564+
input := SBMServerCreateInput{
565+
LocationID: int64(1),
566+
FlavorModelID: int64(1),
567+
Hosts: []SBMServerHostInput{
568+
{Hostname: "example.aa"},
569+
{Hostname: "example.bb"},
570+
},
571+
}
572+
573+
ctx := context.TODO()
574+
575+
sbmServers, err := client.Hosts.CreateSBMServers(ctx, input)
576+
577+
g.Expect(err).To(BeNil())
578+
g.Expect(len(sbmServers)).To(Equal(2))
579+
580+
sbmServer := sbmServers[0]
581+
582+
g.Expect(sbmServer.ID).To(Equal("xkazYeJ0"))
583+
g.Expect(sbmServer.Title).To(Equal("example.aa"))
584+
g.Expect(sbmServer.Type).To(Equal("sbm_server"))
585+
g.Expect(sbmServer.LocationID).To(Equal(int64(1)))
586+
g.Expect(sbmServer.Status).To(Equal("init"))
587+
g.Expect(sbmServer.Configuration).To(Equal("Dell chassis-9015 / 2 GB RAM / 2 x hdd-model-404"))
588+
g.Expect(sbmServer.PrivateIPv4Address).To(BeNil())
589+
g.Expect(sbmServer.PublicIPv4Address).To(BeNil())
590+
g.Expect(sbmServer.ScheduledRelease).To(BeNil())
591+
g.Expect(sbmServer.Created.String()).To(Equal("2020-04-22 06:22:04 +0000 UTC"))
592+
g.Expect(sbmServer.Updated.String()).To(Equal("2020-04-22 06:22:04 +0000 UTC"))
593+
594+
sbmServer = sbmServers[1]
595+
596+
g.Expect(sbmServer.ID).To(Equal("w9aAOdvM"))
597+
g.Expect(sbmServer.Title).To(Equal("example.bb"))
598+
g.Expect(sbmServer.Type).To(Equal("sbm_server"))
599+
g.Expect(sbmServer.LocationID).To(Equal(int64(1)))
600+
g.Expect(sbmServer.Status).To(Equal("init"))
601+
g.Expect(sbmServer.Configuration).To(Equal("Dell chassis-9015 / 2 GB RAM / 2 x hdd-model-404"))
602+
g.Expect(sbmServer.PrivateIPv4Address).To(BeNil())
603+
g.Expect(sbmServer.PublicIPv4Address).To(BeNil())
604+
g.Expect(sbmServer.ScheduledRelease).To(BeNil())
605+
g.Expect(sbmServer.Created.String()).To(Equal("2020-04-22 06:22:04 +0000 UTC"))
606+
g.Expect(sbmServer.Updated.String()).To(Equal("2020-04-22 06:22:04 +0000 UTC"))
607+
}
608+
609+
func TestHostsGetSBMServer(t *testing.T) {
610+
g := NewGomegaWithT(t)
611+
612+
ts, client := newFakeServer().
613+
WithRequestPath("/hosts/sbm_servers/xkazYeJ0").
614+
WithRequestMethod("GET").
615+
WithResponseBodyStubFile("fixtures/hosts/sbm_servers/get_response.json").
616+
WithResponseCode(200).
617+
Build()
618+
619+
defer ts.Close()
620+
621+
ctx := context.TODO()
622+
623+
sbmServer, err := client.Hosts.GetSBMServer(ctx, "xkazYeJ0")
624+
625+
g.Expect(err).To(BeNil())
626+
g.Expect(sbmServer).ToNot(BeNil())
627+
628+
g.Expect(sbmServer.ID).To(Equal("xkazYeJ0"))
629+
g.Expect(sbmServer.Title).To(Equal("example.aa"))
630+
g.Expect(sbmServer.Type).To(Equal("sbm_server"))
631+
g.Expect(sbmServer.LocationID).To(Equal(int64(1)))
632+
g.Expect(sbmServer.Status).To(Equal("active"))
633+
g.Expect(sbmServer.Configuration).To(Equal("REMM R123"))
634+
g.Expect(*sbmServer.PrivateIPv4Address).To(Equal("10.0.0.1"))
635+
g.Expect(*sbmServer.PublicIPv4Address).To(Equal("169.254.0.1"))
636+
g.Expect(sbmServer.ScheduledRelease).To(BeNil())
637+
g.Expect(sbmServer.Created.String()).To(Equal("2020-04-22 06:22:02 +0000 UTC"))
638+
g.Expect(sbmServer.Updated.String()).To(Equal("2020-04-22 06:22:02 +0000 UTC"))
639+
}
640+
641+
func TestHostsReleaseSBMServer(t *testing.T) {
642+
g := NewGomegaWithT(t)
643+
644+
ts, client := newFakeServer().
645+
WithRequestPath("/hosts/sbm_servers/xkazYeJ0").
646+
WithRequestMethod("DELETE").
647+
WithResponseBodyStubFile("fixtures/hosts/sbm_servers/release_response.json").
648+
WithResponseCode(200).
649+
Build()
650+
651+
defer ts.Close()
652+
653+
ctx := context.TODO()
654+
655+
sbmServer, err := client.Hosts.ReleaseSBMServer(ctx, "xkazYeJ0")
656+
657+
g.Expect(err).To(BeNil())
658+
g.Expect(sbmServer).ToNot(BeNil())
659+
660+
g.Expect(sbmServer.ID).To(Equal("xkazYeJ0"))
661+
g.Expect(sbmServer.Title).To(Equal("example.aa"))
662+
g.Expect(sbmServer.Type).To(Equal("sbm_server"))
663+
g.Expect(sbmServer.LocationID).To(Equal(int64(1)))
664+
g.Expect(sbmServer.Status).To(Equal("active"))
665+
g.Expect(sbmServer.Configuration).To(Equal("REMM R123"))
666+
g.Expect(*sbmServer.PrivateIPv4Address).To(Equal("10.0.0.1"))
667+
g.Expect(*sbmServer.PublicIPv4Address).To(Equal("169.254.0.1"))
668+
g.Expect(sbmServer.Created.String()).To(Equal("2020-04-22 06:22:02 +0000 UTC"))
669+
g.Expect(sbmServer.Updated.String()).To(Equal("2020-04-22 06:22:02 +0000 UTC"))
670+
}

pkg/locations.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ const (
1414
uplinkOptionListPath = "/locations/%d/order_options/server_models/%d/uplink_models"
1515

1616
bandwidthOptionListPath = "/locations/%d/order_options/server_models/%d/uplink_models/%d/bandwidth"
17+
18+
sbmFlavorOptionListPath = "/locations/%d/order_options/sbm_flavor_models"
19+
20+
sbmOperatingSystemOptionListPath = "/locations/%d/order_options/sbm_flavor_models/%d/operating_systems"
1721
)
1822

1923
// LocationsService is an interface to interfacing with the Location and Order options endpoints
@@ -36,6 +40,8 @@ type LocationsService interface {
3640
DriveModelOptions(LocationID, ServerModelID int64) Collection[DriveModel]
3741
UplinkOptions(LocationID, ServerModelID int64) Collection[UplinkOption]
3842
BandwidthOptions(LocationID, ServerModelID, uplinkID int64) Collection[BandwidthOption]
43+
SBMFlavorOptions(LocationID int64) Collection[SBMFlavor]
44+
SBMOperatingSystemOptions(LocationID, sbmFlavorModelID int64) Collection[OperatingSystemOption]
3945
}
4046

4147
// LocationsHandler handles operations around cloud instances
@@ -89,3 +95,17 @@ func (h *LocationsHandler) BandwidthOptions(LocationID, ServerModelID, uplinkID
8995

9096
return NewCollection[BandwidthOption](h.client, path)
9197
}
98+
99+
// SBMFlavorOptions builds a new Collection[SBMFlavor] interface
100+
func (h *LocationsHandler) SBMFlavorOptions(LocationID int64) Collection[SBMFlavor] {
101+
path := h.client.buildPath(sbmFlavorOptionListPath, []interface{}{LocationID}...)
102+
103+
return NewCollection[SBMFlavor](h.client, path)
104+
}
105+
106+
// SBMOperatingSystemOptions builds a new Collection[OperatingSystemOption] interface
107+
func (h *LocationsHandler) SBMOperatingSystemOptions(LocationID, SBMFlavorModelID int64) Collection[OperatingSystemOption] {
108+
path := h.client.buildPath(sbmOperatingSystemOptionListPath, []interface{}{LocationID, SBMFlavorModelID}...)
109+
110+
return NewCollection[OperatingSystemOption](h.client, path)
111+
}

pkg/locations_test.go

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,3 +188,55 @@ func TestBandwidthOptionsCollection(t *testing.T) {
188188
g.Expect(collection.HasFirstPage()).To(Equal(false))
189189
g.Expect(collection.HasLastPage()).To(Equal(false))
190190
}
191+
192+
func TestSBMFlavorOptionsCollection(t *testing.T) {
193+
g := NewGomegaWithT(t)
194+
195+
ts, client := newFakeServer().
196+
WithRequestPath("/locations/1/order_options/sbm_flavor_models").
197+
WithRequestMethod("GET").
198+
WithResponseBodyStubInline(`[]`).
199+
WithResponseCode(200).
200+
Build()
201+
202+
defer ts.Close()
203+
204+
collection := client.Locations.SBMFlavorOptions(int64(1))
205+
206+
ctx := context.TODO()
207+
208+
list, err := collection.List(ctx)
209+
210+
g.Expect(err).To(BeNil())
211+
g.Expect(list).To(BeEmpty())
212+
g.Expect(collection.HasNextPage()).To(Equal(false))
213+
g.Expect(collection.HasPreviousPage()).To(Equal(false))
214+
g.Expect(collection.HasFirstPage()).To(Equal(false))
215+
g.Expect(collection.HasLastPage()).To(Equal(false))
216+
}
217+
218+
func TestSBMOperatingSystemOptionsCollection(t *testing.T) {
219+
g := NewGomegaWithT(t)
220+
221+
ts, client := newFakeServer().
222+
WithRequestPath("/locations/1/order_options/sbm_flavor_models/1/operating_systems").
223+
WithRequestMethod("GET").
224+
WithResponseBodyStubInline(`[]`).
225+
WithResponseCode(200).
226+
Build()
227+
228+
defer ts.Close()
229+
230+
collection := client.Locations.SBMOperatingSystemOptions(int64(1), int64(1))
231+
232+
ctx := context.TODO()
233+
234+
list, err := collection.List(ctx)
235+
236+
g.Expect(err).To(BeNil())
237+
g.Expect(list).To(BeEmpty())
238+
g.Expect(collection.HasNextPage()).To(Equal(false))
239+
g.Expect(collection.HasPreviousPage()).To(Equal(false))
240+
g.Expect(collection.HasFirstPage()).To(Equal(false))
241+
g.Expect(collection.HasLastPage()).To(Equal(false))
242+
}

0 commit comments

Comments
 (0)