Skip to content
Draft
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
8 changes: 8 additions & 0 deletions cloudscale.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ type Client struct {
VolumeSnapshots VolumeSnapshotService
Networks NetworkService
Subnets SubnetService
Routers RouterService
FloatingIPs FloatingIPsService
ServerGroups ServerGroupService
ObjectsUsers ObjectsUsersService
Expand Down Expand Up @@ -86,6 +87,13 @@ func NewClient(httpClient *http.Client) *Client {
client: c,
path: subnetBasePath,
}
c.Routers = RouterServiceOperations{
GenericServiceOperations: GenericServiceOperations[Router, RouterCreateRequest, RouterUpdateRequest]{
client: c,
path: routerBasePath,
},
client: c,
}
c.FloatingIPs = GenericServiceOperations[FloatingIP, FloatingIPCreateRequest, FloatingIPUpdateRequest]{
client: c,
path: floatingIPsBasePath,
Expand Down
115 changes: 115 additions & 0 deletions router.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
package cloudscale

import (
"context"
"fmt"
"net/http"
"time"
)

const routerBasePath = "v1/routers"

type Router struct {
ZonalResource
TaggedResource
HREF string `json:"href"`
UUID string `json:"uuid"`
Name string `json:"name"`
CreatedAt time.Time `json:"created_at"`
Status string `json:"status"`
InternetGateway bool `json:"internet_gateway"`
InternetGatewayAddresses []IPAddress `json:"internet_gateway_addresses,omitempty"`
InternalInterfaces []InternalInterface `json:"internal_interfaces,omitempty"`
}

type IPAddress struct {
Address string `json:"address"`
Subnet SubnetStub `json:"subnet"`
Version int `json:"version"`
ReversePTR *string `json:"reverse_ptr"`
}
type InternalInterface struct {
UUID string `json:"uuid"`
Network NetworkStub `json:"network"`
Addresses []IPAddress `json:"addresses"`
Type string `json:"type"`
MACAddress string `json:"mac_address"`
}

type RouterCreateRequest struct {
ZonalResourceRequest
TaggedResourceRequest
Name string `json:"name"`
InternetGateway bool `json:"internet_gateway"`
}

// RouterUpdateRequest is not implemented yet because the API is not implemented yet
type RouterUpdateRequest struct{}

type RouterService interface {
GenericCreateService[Router, RouterCreateRequest]
GenericGetService[Router]
GenericListService[Router]
// GenericUpdateService[Router, RouterUpdateRequest]
GenericDeleteService[Router]
GenericWaitForService[Router]
// CreateInterface creates a new interface attached to this router
CreateInterface(ctx context.Context, routerUUID string, createReq CreateInterfaceRequest) (*InternalInterface, error)
// DeleteInterface removes an interface attached to this router
DeleteInterface(ctx context.Context, routerUUID, interfaceUUID string) error
}

type CreateInterfaceRequest struct {
Network string `json:"network"`
Addresses []CreateAddressRequest `json:"addresses"`
}
type CreateAddressRequest struct {
Subnet string `json:"subnet"`
Address string `json:"address"`
}

type RouterServiceOperations struct {
GenericServiceOperations[Router, RouterCreateRequest, RouterUpdateRequest]
client *Client
}

func (r RouterServiceOperations) CreateInterface(ctx context.Context, routerUUID string, createReq CreateInterfaceRequest) (*InternalInterface, error) {
path := fmt.Sprintf("%s/%s/create_interface", routerBasePath, routerUUID)
ctx = WithOperationPath(ctx, routerBasePath+"/:id/create_interface")
req, err := r.client.NewRequest(ctx, http.MethodPost, path, createReq)
if err != nil {
return nil, err
}
res := &InternalInterface{}
if err := r.client.Do(ctx, req, res); err != nil {
return nil, err
}
return res, nil
}

func (r RouterServiceOperations) DeleteInterface(ctx context.Context, routerUUID, interfaceUUID string) error {
path := fmt.Sprintf("%s/%s/delete_interface", routerBasePath, routerUUID)
ctx = WithOperationPath(ctx, routerBasePath+"/:id/delete_interface")

type DeleteInterfaceRequest struct {
Interface string `json:"interface"`
}
deleteReq := DeleteInterfaceRequest{Interface: interfaceUUID}

req, err := r.client.NewRequest(ctx, http.MethodPost, path, deleteReq)
if err != nil {
return err
}
return r.client.Do(ctx, req, nil)
}

const (
RouterActive = "active"
)

var RouterIsActive = func(router *Router) (bool, error) {
if router.Status == RouterActive {
return true, nil
}
return false, fmt.Errorf("waiting for status: %s, current status: %s", RouterActive, router.Status)
}
Loading
Loading