Skip to content

Sbm servers #23

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions pkg/fixtures/hosts/sbm_servers/create_response.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
[
{
"id": "xkazYeJ0",
"title": "example.aa",
"location_id": 1,
"status": "init",
"configuration": "Dell chassis-9015 / 2 GB RAM / 2 x hdd-model-404",
"private_ipv4_address": null,
"public_ipv4_address": null,
"lease_start_at": null,
"scheduled_release_at": null,
"type": "sbm_server",
"created_at": "2020-04-22T06:22:04Z",
"updated_at": "2020-04-22T06:22:04Z"
},
{
"id": "w9aAOdvM",
"title": "example.bb",
"location_id": 1,
"status": "init",
"configuration": "Dell chassis-9015 / 2 GB RAM / 2 x hdd-model-404",
"private_ipv4_address": null,
"public_ipv4_address": null,
"lease_start_at": null,
"scheduled_release_at": null,
"type": "sbm_server",
"created_at": "2020-04-22T06:22:04Z",
"updated_at": "2020-04-22T06:22:04Z"
}
]
14 changes: 14 additions & 0 deletions pkg/fixtures/hosts/sbm_servers/get_response.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"id": "xkazYeJ0",
"title": "example.aa",
"location_id": 1,
"status": "active",
"configuration": "REMM R123",
"private_ipv4_address": "10.0.0.1",
"public_ipv4_address": "169.254.0.1",
"lease_start_at": "2020-04-20",
"scheduled_release_at": null,
"type": "sbm_server",
"created_at": "2020-04-22T06:22:02Z",
"updated_at": "2020-04-22T06:22:02Z"
}
14 changes: 14 additions & 0 deletions pkg/fixtures/hosts/sbm_servers/release_response.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"id": "xkazYeJ0",
"title": "example.aa",
"location_id": 1,
"status": "active",
"configuration": "REMM R123",
"private_ipv4_address": "10.0.0.1",
"public_ipv4_address": "169.254.0.1",
"lease_start_at": "2020-04-20",
"scheduled_release_at": null,
"type": "sbm_server",
"created_at": "2020-04-22T06:22:02Z",
"updated_at": "2020-04-22T06:22:02Z"
}
73 changes: 73 additions & 0 deletions pkg/hosts.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ const (
dedicatedServerReinstallPath = "/hosts/dedicated_servers/%s/reinstall"

kubernetesBaremetalNodePath = "/hosts/kubernetes_baremetal_nodes/%s"

sbmServerCreatePath = "/hosts/sbm_servers"
sbmServerPath = "/hosts/sbm_servers/%s"
)

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

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

return NewCollection[PTRRecord](h.client, path)
}

// GetSBMServer returns an sbm server
// Endpoint: https://developers.servers.com/api-documentation/v1/#tag/Scalable-Baremetal-Server/operation/GetAnSbmServer
func (h *HostsHandler) GetSBMServer(ctx context.Context, id string) (*SBMServer, error) {
url := h.client.buildURL(sbmServerPath, []interface{}{id}...)

body, err := h.client.buildAndExecRequest(ctx, "GET", url, nil)

if err != nil {
return nil, err
}

sbmServer := new(SBMServer)

if err := json.Unmarshal(body, &sbmServer); err != nil {
return nil, err
}

return sbmServer, nil
}

// CreateSBMServers creates an SBM servers
// Endpoint: https://developers.servers.com/api-documentation/v1/#tag/Scalable-Baremetal-Server/operation/CreateAnSbmServer
func (h *HostsHandler) CreateSBMServers(ctx context.Context, input SBMServerCreateInput) ([]SBMServer, error) {
payload, err := json.Marshal(input)

if err != nil {
return nil, err
}

url := h.client.buildURL(sbmServerCreatePath)

body, err := h.client.buildAndExecRequest(ctx, "POST", url, payload)

if err != nil {
return nil, err
}

var sbmServers []SBMServer

if err := json.Unmarshal(body, &sbmServers); err != nil {
return nil, err
}

return sbmServers, nil
}

// ReleaseSBMServer removes an SBM server from account.
// This action is irreversible and the removal process will be initiated immediately!!!
// Endpoint: https://developers.servers.com/api-documentation/v1/#tag/Scalable-Baremetal-Server/operation/ReleaseAnSbmServer
func (h *HostsHandler) ReleaseSBMServer(ctx context.Context, id string) (*SBMServer, error) {
url := h.client.buildURL(sbmServerPath, []interface{}{id}...)

body, err := h.client.buildAndExecRequest(ctx, "DELETE", url, nil)

if err != nil {
return nil, err
}

sbmServer := new(SBMServer)

if err := json.Unmarshal(body, &sbmServer); err != nil {
return nil, err
}

return sbmServer, nil
}
120 changes: 120 additions & 0 deletions pkg/hosts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -548,3 +548,123 @@ func TestDedicatedServerPTRRecordsCollection(t *testing.T) {
g.Expect(collection.HasFirstPage()).To(Equal(false))
g.Expect(collection.HasLastPage()).To(Equal(false))
}

func TestHostsCreateSBMServers(t *testing.T) {
g := NewGomegaWithT(t)

ts, client := newFakeServer().
WithRequestPath("/hosts/sbm_servers").
WithRequestMethod("POST").
WithResponseBodyStubFile("fixtures/hosts/sbm_servers/create_response.json").
WithResponseCode(201).
Build()

defer ts.Close()

input := SBMServerCreateInput{
LocationID: int64(1),
FlavorModelID: int64(1),
Hosts: []SBMServerHostInput{
{Hostname: "example.aa"},
{Hostname: "example.bb"},
},
}

ctx := context.TODO()

sbmServers, err := client.Hosts.CreateSBMServers(ctx, input)

g.Expect(err).To(BeNil())
g.Expect(len(sbmServers)).To(Equal(2))

sbmServer := sbmServers[0]

g.Expect(sbmServer.ID).To(Equal("xkazYeJ0"))
g.Expect(sbmServer.Title).To(Equal("example.aa"))
g.Expect(sbmServer.Type).To(Equal("sbm_server"))
g.Expect(sbmServer.LocationID).To(Equal(int64(1)))
g.Expect(sbmServer.Status).To(Equal("init"))
g.Expect(sbmServer.Configuration).To(Equal("Dell chassis-9015 / 2 GB RAM / 2 x hdd-model-404"))
g.Expect(sbmServer.PrivateIPv4Address).To(BeNil())
g.Expect(sbmServer.PublicIPv4Address).To(BeNil())
g.Expect(sbmServer.ScheduledRelease).To(BeNil())
g.Expect(sbmServer.Created.String()).To(Equal("2020-04-22 06:22:04 +0000 UTC"))
g.Expect(sbmServer.Updated.String()).To(Equal("2020-04-22 06:22:04 +0000 UTC"))

sbmServer = sbmServers[1]

g.Expect(sbmServer.ID).To(Equal("w9aAOdvM"))
g.Expect(sbmServer.Title).To(Equal("example.bb"))
g.Expect(sbmServer.Type).To(Equal("sbm_server"))
g.Expect(sbmServer.LocationID).To(Equal(int64(1)))
g.Expect(sbmServer.Status).To(Equal("init"))
g.Expect(sbmServer.Configuration).To(Equal("Dell chassis-9015 / 2 GB RAM / 2 x hdd-model-404"))
g.Expect(sbmServer.PrivateIPv4Address).To(BeNil())
g.Expect(sbmServer.PublicIPv4Address).To(BeNil())
g.Expect(sbmServer.ScheduledRelease).To(BeNil())
g.Expect(sbmServer.Created.String()).To(Equal("2020-04-22 06:22:04 +0000 UTC"))
g.Expect(sbmServer.Updated.String()).To(Equal("2020-04-22 06:22:04 +0000 UTC"))
}

func TestHostsGetSBMServer(t *testing.T) {
g := NewGomegaWithT(t)

ts, client := newFakeServer().
WithRequestPath("/hosts/sbm_servers/xkazYeJ0").
WithRequestMethod("GET").
WithResponseBodyStubFile("fixtures/hosts/sbm_servers/get_response.json").
WithResponseCode(200).
Build()

defer ts.Close()

ctx := context.TODO()

sbmServer, err := client.Hosts.GetSBMServer(ctx, "xkazYeJ0")

g.Expect(err).To(BeNil())
g.Expect(sbmServer).ToNot(BeNil())

g.Expect(sbmServer.ID).To(Equal("xkazYeJ0"))
g.Expect(sbmServer.Title).To(Equal("example.aa"))
g.Expect(sbmServer.Type).To(Equal("sbm_server"))
g.Expect(sbmServer.LocationID).To(Equal(int64(1)))
g.Expect(sbmServer.Status).To(Equal("active"))
g.Expect(sbmServer.Configuration).To(Equal("REMM R123"))
g.Expect(*sbmServer.PrivateIPv4Address).To(Equal("10.0.0.1"))
g.Expect(*sbmServer.PublicIPv4Address).To(Equal("169.254.0.1"))
g.Expect(sbmServer.ScheduledRelease).To(BeNil())
g.Expect(sbmServer.Created.String()).To(Equal("2020-04-22 06:22:02 +0000 UTC"))
g.Expect(sbmServer.Updated.String()).To(Equal("2020-04-22 06:22:02 +0000 UTC"))
}

func TestHostsReleaseSBMServer(t *testing.T) {
g := NewGomegaWithT(t)

ts, client := newFakeServer().
WithRequestPath("/hosts/sbm_servers/xkazYeJ0").
WithRequestMethod("DELETE").
WithResponseBodyStubFile("fixtures/hosts/sbm_servers/release_response.json").
WithResponseCode(200).
Build()

defer ts.Close()

ctx := context.TODO()

sbmServer, err := client.Hosts.ReleaseSBMServer(ctx, "xkazYeJ0")

g.Expect(err).To(BeNil())
g.Expect(sbmServer).ToNot(BeNil())

g.Expect(sbmServer.ID).To(Equal("xkazYeJ0"))
g.Expect(sbmServer.Title).To(Equal("example.aa"))
g.Expect(sbmServer.Type).To(Equal("sbm_server"))
g.Expect(sbmServer.LocationID).To(Equal(int64(1)))
g.Expect(sbmServer.Status).To(Equal("active"))
g.Expect(sbmServer.Configuration).To(Equal("REMM R123"))
g.Expect(*sbmServer.PrivateIPv4Address).To(Equal("10.0.0.1"))
g.Expect(*sbmServer.PublicIPv4Address).To(Equal("169.254.0.1"))
g.Expect(sbmServer.Created.String()).To(Equal("2020-04-22 06:22:02 +0000 UTC"))
g.Expect(sbmServer.Updated.String()).To(Equal("2020-04-22 06:22:02 +0000 UTC"))
}
20 changes: 20 additions & 0 deletions pkg/locations.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ const (
uplinkOptionListPath = "/locations/%d/order_options/server_models/%d/uplink_models"

bandwidthOptionListPath = "/locations/%d/order_options/server_models/%d/uplink_models/%d/bandwidth"

sbmFlavorOptionListPath = "/locations/%d/order_options/sbm_flavor_models"

sbmOperatingSystemOptionListPath = "/locations/%d/order_options/sbm_flavor_models/%d/operating_systems"
)

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

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

return NewCollection[BandwidthOption](h.client, path)
}

// SBMFlavorOptions builds a new Collection[SBMFlavor] interface
func (h *LocationsHandler) SBMFlavorOptions(LocationID int64) Collection[SBMFlavor] {
path := h.client.buildPath(sbmFlavorOptionListPath, []interface{}{LocationID}...)

return NewCollection[SBMFlavor](h.client, path)
}

// SBMOperatingSystemOptions builds a new Collection[OperatingSystemOption] interface
func (h *LocationsHandler) SBMOperatingSystemOptions(LocationID, SBMFlavorModelID int64) Collection[OperatingSystemOption] {
path := h.client.buildPath(sbmOperatingSystemOptionListPath, []interface{}{LocationID, SBMFlavorModelID}...)

return NewCollection[OperatingSystemOption](h.client, path)
}
52 changes: 52 additions & 0 deletions pkg/locations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,3 +188,55 @@ func TestBandwidthOptionsCollection(t *testing.T) {
g.Expect(collection.HasFirstPage()).To(Equal(false))
g.Expect(collection.HasLastPage()).To(Equal(false))
}

func TestSBMFlavorOptionsCollection(t *testing.T) {
g := NewGomegaWithT(t)

ts, client := newFakeServer().
WithRequestPath("/locations/1/order_options/sbm_flavor_models").
WithRequestMethod("GET").
WithResponseBodyStubInline(`[]`).
WithResponseCode(200).
Build()

defer ts.Close()

collection := client.Locations.SBMFlavorOptions(int64(1))

ctx := context.TODO()

list, err := collection.List(ctx)

g.Expect(err).To(BeNil())
g.Expect(list).To(BeEmpty())
g.Expect(collection.HasNextPage()).To(Equal(false))
g.Expect(collection.HasPreviousPage()).To(Equal(false))
g.Expect(collection.HasFirstPage()).To(Equal(false))
g.Expect(collection.HasLastPage()).To(Equal(false))
}

func TestSBMOperatingSystemOptionsCollection(t *testing.T) {
g := NewGomegaWithT(t)

ts, client := newFakeServer().
WithRequestPath("/locations/1/order_options/sbm_flavor_models/1/operating_systems").
WithRequestMethod("GET").
WithResponseBodyStubInline(`[]`).
WithResponseCode(200).
Build()

defer ts.Close()

collection := client.Locations.SBMOperatingSystemOptions(int64(1), int64(1))

ctx := context.TODO()

list, err := collection.List(ctx)

g.Expect(err).To(BeNil())
g.Expect(list).To(BeEmpty())
g.Expect(collection.HasNextPage()).To(Equal(false))
g.Expect(collection.HasPreviousPage()).To(Equal(false))
g.Expect(collection.HasFirstPage()).To(Equal(false))
g.Expect(collection.HasLastPage()).To(Equal(false))
}
Loading
Loading