Skip to content
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

cloudscale-go-sdk v5.0.0 #43

Merged
merged 8 commits into from
Mar 12, 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
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
v4.0.0
v5.0.0
2 changes: 1 addition & 1 deletion cloudscale.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
)

const (
libraryVersion = "v4.0.0"
libraryVersion = "v5.0.0"
defaultBaseURL = "https://api.cloudscale.ch/"
userAgent = "cloudscale/" + libraryVersion
mediaType = "application/json"
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module github.com/cloudscale-ch/cloudscale-go-sdk/v4
module github.com/cloudscale-ch/cloudscale-go-sdk/v5

go 1.18

Expand Down
82 changes: 71 additions & 11 deletions subnets.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,16 @@ package cloudscale

import (
"context"
"encoding/json"
"fmt"
"net/http"
"reflect"
)

const subnetBasePath = "v1/subnets"

var UseCloudscaleDefaults = []string{"CLOUDSCALE_DEFAULTS"}

type Subnet struct {
TaggedResource
// Just use omitempty everywhere. This makes it easy to use restful. Errors
Expand All @@ -28,16 +32,74 @@ type SubnetStub struct {

type SubnetCreateRequest struct {
TaggedResourceRequest
CIDR string `json:"cidr,omitempty"`
Network string `json:"network,omitempty"`
GatewayAddress string `json:"gateway_address,omitempty"`
DNSServers []string `json:"dns_servers,omitempty"`
CIDR string `json:"cidr,omitempty"`
Network string `json:"network,omitempty"`
GatewayAddress string `json:"gateway_address,omitempty"`
DNSServers *[]string `json:"dns_servers,omitempty"`
}

type SubnetUpdateRequest struct {
TaggedResourceRequest
GatewayAddress string `json:"gateway_address,omitempty"`
DNSServers []string `json:"dns_servers,omitempty"`
GatewayAddress string `json:"gateway_address,omitempty"`
DNSServers *[]string `json:"dns_servers"`
}

func (request SubnetUpdateRequest) MarshalJSON() ([]byte, error) {
type Alias SubnetUpdateRequest // Create an alias to avoid recursion

if request.DNSServers == nil {
return json.Marshal(&struct {
Alias
DNSServers []string `json:"dns_servers,omitempty"`
}{
Alias: (Alias)(request),
})
}

if reflect.DeepEqual(*request.DNSServers, UseCloudscaleDefaults) {
return json.Marshal(&struct {
Alias
DNSServers []string `json:"dns_servers"` // important: no omitempty
}{
Alias: (Alias)(request),
DNSServers: nil,
})
}

return json.Marshal(&struct {
Alias
}{
Alias: (Alias)(request),
})
}

func (request SubnetCreateRequest) MarshalJSON() ([]byte, error) {
type Alias SubnetCreateRequest // Create an alias to avoid recursion

if request.DNSServers == nil {
return json.Marshal(&struct {
Alias
DNSServers []string `json:"dns_servers,omitempty"`
}{
Alias: (Alias)(request),
})
}

if reflect.DeepEqual(*request.DNSServers, UseCloudscaleDefaults) {
return json.Marshal(&struct {
Alias
DNSServers []string `json:"dns_servers"` // important: no omitempty
}{
Alias: (Alias)(request),
DNSServers: nil,
})
}

return json.Marshal(&struct {
Alias
}{
Alias: (Alias)(request),
})
}

type SubnetService interface {
Expand Down Expand Up @@ -70,22 +132,21 @@ func (s SubnetServiceOperations) Create(ctx context.Context, createRequest *Subn
return subnet, nil
}

func (f SubnetServiceOperations) Update(ctx context.Context, subnetID string, updateRequest *SubnetUpdateRequest) error {
func (s SubnetServiceOperations) Update(ctx context.Context, subnetID string, updateRequest *SubnetUpdateRequest) error {
path := fmt.Sprintf("%s/%s", subnetBasePath, subnetID)

req, err := f.client.NewRequest(ctx, http.MethodPatch, path, updateRequest)
req, err := s.client.NewRequest(ctx, http.MethodPatch, path, updateRequest)
if err != nil {
return err
}

err = f.client.Do(ctx, req, nil)
err = s.client.Do(ctx, req, nil)
if err != nil {
return err
}
return nil
}


func (s SubnetServiceOperations) Get(ctx context.Context, subnetID string) (*Subnet, error) {
path := fmt.Sprintf("%s/%s", subnetBasePath, subnetID)

Expand Down Expand Up @@ -113,7 +174,6 @@ func (s SubnetServiceOperations) Delete(ctx context.Context, subnetID string) er
return s.client.Do(ctx, req, nil)
}


func (s SubnetServiceOperations) List(ctx context.Context, modifiers ...ListRequestModifier) ([]Subnet, error) {
path := subnetBasePath

Expand Down
113 changes: 113 additions & 0 deletions subnets_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cloudscale

import (
"encoding/json"
"fmt"
"net/http"
"reflect"
Expand Down Expand Up @@ -47,3 +48,115 @@ func TestSubnets_List(t *testing.T) {
}

}

func TestMarshalingOfDNSServersInSubnetUpdateRequest(t *testing.T) {
testCases := []struct {
name string
request SubnetUpdateRequest
expected string // This tests the value sent to the API, this is not the expected value returned on the subnet.
}{
{
name: "one dns server",
request: SubnetUpdateRequest{
DNSServers: &[]string{"8.8.8.8"},
},
expected: "{\"dns_servers\":[\"8.8.8.8\"]}",
},
{
name: "two dns servers",
request: SubnetUpdateRequest{
DNSServers: &[]string{"8.8.8.8", "8.8.4.4"},
},
expected: "{\"dns_servers\":[\"8.8.8.8\",\"8.8.4.4\"]}",
},
{
name: "no dns servers",
request: SubnetUpdateRequest{
DNSServers: &[]string{},
},
expected: "{\"dns_servers\":[]}",
},
{
name: "defaults",
request: SubnetUpdateRequest{
DNSServers: &UseCloudscaleDefaults,
},
expected: "{\"dns_servers\":null}",
},
{
name: "gateway",
request: SubnetUpdateRequest{
GatewayAddress: "192.168.1.1",
},
expected: "{\"gateway_address\":\"192.168.1.1\"}",
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
b, err := json.Marshal(tc.request)
if err != nil {
t.Errorf("Error marshaling JSON: %v", err)
}
if actualOutput := string(b); actualOutput != tc.expected {
t.Errorf("Unexpected JSON output:\nExpected: %s\nActual: %s", tc.expected, actualOutput)
}
})
}
}

func TestMarshalingOfDNSServersInSubnetSubnetCreateRequest(t *testing.T) {
testCases := []struct {
name string
request SubnetCreateRequest
expected string // This tests the value sent to the API, this is not the expected value returned on the subnet.
}{
{
name: "one dns server",
request: SubnetCreateRequest{
DNSServers: &[]string{"8.8.8.8"},
},
expected: "{\"dns_servers\":[\"8.8.8.8\"]}",
},
{
name: "two dns servers",
request: SubnetCreateRequest{
DNSServers: &[]string{"8.8.8.8", "8.8.4.4"},
},
expected: "{\"dns_servers\":[\"8.8.8.8\",\"8.8.4.4\"]}",
},
{
name: "no dns servers",
request: SubnetCreateRequest{
DNSServers: &[]string{},
},
expected: "{\"dns_servers\":[]}",
},
{
name: "defaults",
request: SubnetCreateRequest{
DNSServers: &UseCloudscaleDefaults,
},
expected: "{\"dns_servers\":null}",
},
{
name: "gateway",
request: SubnetCreateRequest{
GatewayAddress: "192.168.1.1",
},
expected: "{\"gateway_address\":\"192.168.1.1\"}",
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
b, err := json.Marshal(tc.request)
if err != nil {
t.Errorf("Error marshaling JSON: %v", err)
}
if actualOutput := string(b); actualOutput != tc.expected {
t.Errorf("Unexpected JSON output:\nExpected: %s\nActual: %s", tc.expected, actualOutput)
}
})
}
}
2 changes: 1 addition & 1 deletion test/integration/cloudscale_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
"testing"
"time"

"github.com/cloudscale-ch/cloudscale-go-sdk/v4"
"github.com/cloudscale-ch/cloudscale-go-sdk/v5"
"golang.org/x/oauth2"
)

Expand Down
2 changes: 1 addition & 1 deletion test/integration/custom_images_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"errors"
"fmt"
"github.com/cenkalti/backoff"
"github.com/cloudscale-ch/cloudscale-go-sdk/v4"
"github.com/cloudscale-ch/cloudscale-go-sdk/v5"
"io"
"net/http"
"testing"
Expand Down
2 changes: 1 addition & 1 deletion test/integration/floating_ips_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"testing"
"time"

"github.com/cloudscale-ch/cloudscale-go-sdk/v4"
"github.com/cloudscale-ch/cloudscale-go-sdk/v5"
)

const pubKey string = "ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBFEepRNW5hDct4AdJ8oYsb4lNP5E9XY5fnz3ZvgNCEv7m48+bhUjJXUPuamWix3zigp2lgJHC6SChI/okJ41GUY="
Expand Down
2 changes: 1 addition & 1 deletion test/integration/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ package integration

import (
"context"
"github.com/cloudscale-ch/cloudscale-go-sdk/v4"
"github.com/cloudscale-ch/cloudscale-go-sdk/v5"
"math/rand"
"reflect"
"testing"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ package integration

import (
"context"
"github.com/cloudscale-ch/cloudscale-go-sdk/v4"
"github.com/cloudscale-ch/cloudscale-go-sdk/v5"
"reflect"
"testing"
"time"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ package integration

import (
"context"
"github.com/cloudscale-ch/cloudscale-go-sdk/v4"
"github.com/cloudscale-ch/cloudscale-go-sdk/v5"
"reflect"
"testing"
"time"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ package integration

import (
"context"
"github.com/cloudscale-ch/cloudscale-go-sdk/v4"
"github.com/cloudscale-ch/cloudscale-go-sdk/v5"
"reflect"
"testing"
"time"
Expand Down
2 changes: 1 addition & 1 deletion test/integration/load_balancer_pools_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ package integration

import (
"context"
"github.com/cloudscale-ch/cloudscale-go-sdk/v4"
"github.com/cloudscale-ch/cloudscale-go-sdk/v5"
"reflect"
"testing"
"time"
Expand Down
2 changes: 1 addition & 1 deletion test/integration/load_balancers_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"context"
"errors"
"github.com/cenkalti/backoff"
"github.com/cloudscale-ch/cloudscale-go-sdk/v4"
"github.com/cloudscale-ch/cloudscale-go-sdk/v5"
"reflect"
"testing"
"time"
Expand Down
2 changes: 1 addition & 1 deletion test/integration/metrics_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ package integration

import (
"context"
"github.com/cloudscale-ch/cloudscale-go-sdk/v4"
"github.com/cloudscale-ch/cloudscale-go-sdk/v5"
"testing"
"time"
)
Expand Down
2 changes: 1 addition & 1 deletion test/integration/networks_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ package integration
import (
"context"
"fmt"
"github.com/cloudscale-ch/cloudscale-go-sdk/v4"
"github.com/cloudscale-ch/cloudscale-go-sdk/v5"
"regexp"
"sync"
"testing"
Expand Down
2 changes: 1 addition & 1 deletion test/integration/objects_users_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"fmt"
"testing"

"github.com/cloudscale-ch/cloudscale-go-sdk/v4"
"github.com/cloudscale-ch/cloudscale-go-sdk/v5"
)

func TestIntegrationObjectsUser_CRUD(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion test/integration/server_groups_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"sync"
"testing"

"github.com/cloudscale-ch/cloudscale-go-sdk/v4"
"github.com/cloudscale-ch/cloudscale-go-sdk/v5"
)

func TestIntegrationServerGroup_CRUD(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion test/integration/servers_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
"time"

"github.com/cenkalti/backoff"
"github.com/cloudscale-ch/cloudscale-go-sdk/v4"
"github.com/cloudscale-ch/cloudscale-go-sdk/v5"
)

const DefaultImageSlug = "debian-11"
Expand Down
Loading
Loading