Skip to content

Commit

Permalink
Implement PrivateNetworks API endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
norrland committed Aug 29, 2024
1 parent 7162e79 commit 35ec433
Show file tree
Hide file tree
Showing 4 changed files with 474 additions and 0 deletions.
2 changes: 2 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ type Client struct {
IPs *IPService
LoadBalancers *LoadBalancerService
ObjectStorages *ObjectStorageService
PrivateNetworks *PrivateNetworkService
Servers *ServerService
ServerDisks *ServerDisksService
Networks *NetworkService
Expand All @@ -60,6 +61,7 @@ func NewClient(project, apiKey, userAgent string) *Client {
c.IPs = &IPService{client: c}
c.LoadBalancers = &LoadBalancerService{client: c}
c.ObjectStorages = &ObjectStorageService{client: c}
c.PrivateNetworks = &PrivateNetworkService{client: c}
c.Servers = &ServerService{client: c}
c.ServerDisks = &ServerDisksService{client: c}
c.Networks = &NetworkService{client: c}
Expand Down
109 changes: 109 additions & 0 deletions doc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,115 @@ func ExampleNetworkCircuitService_List() {
}
}

func ExamplePrivateNetworkService_Create() {
client := glesys.NewClient("CL12345", "your-api-key", "my-application/0.0.1")

privnet, _ := client.PrivateNetworks.Create(context.Background(), "my-network")

fmt.Printf("ID: %s - Name: %s - IPv6: %s\n", privnet.ID, privnet.Name, privnet.IPv6Aggregate)
}

func ExamplePrivateNetworkService_Destroy() {
client := glesys.NewClient("CL12345", "your-api-key", "my-application/0.0.1")

err := client.PrivateNetworks.Destroy(context.Background(), "pn-123ab")

if err != nil {
fmt.Printf("Cannot remove private network. Error: %s", err)
}
}

func ExamplePrivateNetworkService_Details() {
client := glesys.NewClient("CL12345", "your-api-key", "my-application/0.0.1")

privnet, _ := client.PrivateNetworks.Details(context.Background(), "pn-123ab")

fmt.Printf("ID: %s - Name: %s - IPv6: %s\n", privnet.ID, privnet.Name, privnet.IPv6Aggregate)
}

func ExamplePrivateNetworkService_EstimatedCost() {
client := glesys.NewClient("CL12345", "your-api-key", "my-application/0.0.1")

netCost, _ := client.PrivateNetworks.EstimatedCost(context.Background(), "pn-123ab")

fmt.Printf("Price: %.2f%s - Discount: %.2f - Total: %.2f\n", netCost.Price, netCost.Currency, netCost.Discount, netCost.Total)
}

func ExamplePrivateNetworkService_Edit() {
client := glesys.NewClient("CL12345", "your-api-key", "my-application/0.0.1")

params := glesys.EditPrivateNetworkParams{
ID: "pn-123ab",
Name: "new-network-name",
}

privnet, _ := client.PrivateNetworks.Edit(context.Background(), params)

fmt.Printf("ID: %s - Name: %s - IPv6: %s\n", privnet.ID, privnet.Name, privnet.IPv6Aggregate)
}

func ExamplePrivateNetworkService_List() {
client := glesys.NewClient("CL12345", "your-api-key", "my-application/0.0.1")

privnet, _ := client.PrivateNetworks.List(context.Background())

for _, net := range *privnet {
fmt.Printf("ID: %s - Name: %s - IPv6: %s\n", net.ID, net.Name, net.IPv6Aggregate)
}
}

func ExamplePrivateNetworkService_CreateSegment() {
client := glesys.NewClient("CL12345", "your-api-key", "my-application/0.0.1")

params := glesys.CreatePrivateNetworkSegmentParams{
PrivateNetworkID: "pn-123ab",
Datacenter: "dc-fbg1",
Name: "kvm-segment",
Platform: "kvm",
IPv4Subnet: "192.168.0.0/24",
}

segment, _ := client.PrivateNetworks.CreateSegment(context.Background(), params)

fmt.Printf("ID: %s - Name: %s\nIPv6: %s\nIPv4: %s\nPlatform: %s\nDatacenter: %s\n",
segment.ID, segment.Name, segment.IPv6Subnet, segment.IPv4Subnet, segment.Platform, segment.Datacenter)
}

func ExamplePrivateNetworkService_EditSegment() {
client := glesys.NewClient("CL12345", "your-api-key", "my-application/0.0.1")

params := glesys.EditPrivateNetworkSegmentParams{
ID: "6f5cb761-163e-4a5f-b9da-98acbe68e28d",
Name: "updated-name",
}

segment, _ := client.PrivateNetworks.EditSegment(context.Background(), params)

fmt.Printf("ID: %s - Name: %s\nIPv6: %s\nIPv4: %s\nPlatform: %s\nDatacenter: %s\n",
segment.ID, segment.Name, segment.IPv6Subnet, segment.IPv4Subnet, segment.Platform, segment.Datacenter)
}

func ExamplePrivateNetworkService_ListSegments() {
client := glesys.NewClient("CL12345", "your-api-key", "my-application/0.0.1")

segments, _ := client.PrivateNetworks.ListSegments(context.Background(), "pn-123ab")

for _, s := range *segments {
fmt.Printf("ID: %s - Name: %s\nIPv6: %s\nIPv4: %s\nPlatform: %s\nDatacenter: %s\n",
s.ID, s.Name, s.IPv6Subnet, s.IPv4Subnet, s.Platform, s.Datacenter)
}
}

func ExamplePrivateNetworkService_DestroySegment() {
client := glesys.NewClient("CL12345", "your-api-key", "my-application/0.0.1")

err := client.PrivateNetworks.DestroySegment(context.Background(), "6f5cb761-163e-4a5f-b9da-98acbe68e28d")

if err != nil {
fmt.Printf("Cannot remove private network segment. Error: %s", err)
}
}

func ExampleServerService_Create() {
client := glesys.NewClient("CL12345", "your-api-key", "my-application/0.0.1")

Expand Down
164 changes: 164 additions & 0 deletions privatenetworks.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
package glesys

import (
"context"
)

// PrivateNetworkService provides functions to interact with PrivateNetworks
type PrivateNetworkService struct {
client clientInterface
}

// PrivateNetwork represents a privatenetwork
type PrivateNetwork struct {
ID string `json:"id"`
IPv6Aggregate string `json:"ipv6aggregate"`
Name string `json:"name"`
}

// PrivateNetworkBilling
type PrivateNetworkBilling struct {
Currency string `json:"currency"`
Price float64 `json:"price"`
Discount float64 `json:"discount"` // discount in $Currency
Total float64 `json:"total"`
}

// EditPrivateNetworkParams is used when editing an existing private network
type EditPrivateNetworkParams struct {
ID string `json:"privatenetworkid"`
Name string `json:"name,omitempty"`
}

// PrivateNetworkSegment represents a segment as part of a PrivateNetwork
type PrivateNetworkSegment struct {
ID string `json:"id"`
Name string `json:"name"`
IPv4Subnet string `json:"ipv4subnet"`
IPv6Subnet string `json:"ipv6subnet"`
Platform string `json:"platform"`
Datacenter string `json:"datacenter"`
}

// CreatePrivateNetworkSegmentParams is used when creating Segments in a PrivateNetwork
type CreatePrivateNetworkSegmentParams struct {
PrivateNetworkID string `json:"privatenetworkid"`
Datacenter string `json:"datacenter"`
IPv4Subnet string `json:"ipv4subnet"` // x.y.z.a/netmask
Name string `json:"name"`
Platform string `json:"platform"`
}

// EditPrivateNetworkSegmentParams is used when editing an existing segment
type EditPrivateNetworkSegmentParams struct {
ID string `json:"id"`
Name string `json:"name,omitempty"`
}

// Create creates a new PrivateNetwork
func (s *PrivateNetworkService) Create(context context.Context, name string) (*PrivateNetwork, error) {
data := struct {
Response struct {
PrivateNetwork PrivateNetwork
}
}{}
err := s.client.post(context, "privatenetwork/create", &data, name)
return &data.Response.PrivateNetwork, err
}

// Details returns detailed information about a PrivateNetwork
func (s *PrivateNetworkService) Details(context context.Context, privateNetworkID string) (*PrivateNetwork, error) {
data := struct {
Response struct {
PrivateNetwork PrivateNetwork
}
}{}
err := s.client.post(context, "privatenetwork/details", &data, privateNetworkID)
return &data.Response.PrivateNetwork, err
}

// List returns detailed information about a PrivateNetwork
func (s *PrivateNetworkService) List(context context.Context) (*[]PrivateNetwork, error) {
data := struct {
Response struct {
PrivateNetworks []PrivateNetwork
}
}{}
err := s.client.post(context, "privatenetwork/list", &data, nil)
return &data.Response.PrivateNetworks, err
}

// Destroy deletes a PrivateNetwork
func (s *PrivateNetworkService) Destroy(context context.Context, privateNetworkID string) error {
return s.client.post(context, "privatenetwork/delete", nil, struct {
PrivateNetworkID string `json:"privatenetworkid"`
}{privateNetworkID})
}

// Edit modifies a PrivateNetwork
func (s *PrivateNetworkService) Edit(context context.Context, params EditPrivateNetworkParams) (*PrivateNetwork, error) {
data := struct {
Response struct {
PrivateNetwork PrivateNetwork
}
}{}
err := s.client.post(context, "privatenetwork/edit", &data, struct {
EditPrivateNetworkParams
}{params})
return &data.Response.PrivateNetwork, err
}

// EstimatedCost returns billing information about a PrivateNetwork
func (s *PrivateNetworkService) EstimatedCost(context context.Context, privateNetworkID string) (*PrivateNetworkBilling, error) {
data := struct {
Response struct {
Billing PrivateNetworkBilling
}
}{}
err := s.client.post(context, "privatenetwork/estimatedcost", &data, struct {
ID string `json:"privatenetworkid,omitempty"`
}{privateNetworkID})
return &data.Response.Billing, err
}

// Create creates a new PrivateNetworkSegment
func (s *PrivateNetworkService) CreateSegment(context context.Context, params CreatePrivateNetworkSegmentParams) (*PrivateNetworkSegment, error) {
data := struct {
Response struct {
PrivateNetworkSegment PrivateNetworkSegment
}
}{}
err := s.client.post(context, "privatenetwork/createsegment", &data, params)
return &data.Response.PrivateNetworkSegment, err
}

// Edit modifies a new PrivateNetworkSegment
func (s *PrivateNetworkService) EditSegment(context context.Context, params EditPrivateNetworkSegmentParams) (*PrivateNetworkSegment, error) {
data := struct {
Response struct {
PrivateNetworkSegment PrivateNetworkSegment
}
}{}
err := s.client.post(context, "privatenetwork/editsegment", &data, params)
return &data.Response.PrivateNetworkSegment, err
}

// ListSegments returns detailed information about a PrivateNetwork
func (s *PrivateNetworkService) ListSegments(context context.Context, privatenetworkid string) (*[]PrivateNetworkSegment, error) {
data := struct {
Response struct {
PrivateNetworkSegments []PrivateNetworkSegment
}
}{}
err := s.client.post(context, "privatenetwork/listsegments", &data, struct {
PrivateNetworkID string `json:"privatenetworkid"`
}{privatenetworkid})
return &data.Response.PrivateNetworkSegments, err
}

// DestroySegment deletes a PrivateNetworkSegment
func (s *PrivateNetworkService) DestroySegment(context context.Context, id string) error {
return s.client.post(context, "privatenetwork/deletesegment", nil, struct {
ID string `json:"id"`
}{id})
}
Loading

0 comments on commit 35ec433

Please sign in to comment.