Skip to content

Commit

Permalink
Implement NetworkCircuit API endpoint
Browse files Browse the repository at this point in the history
* networkcircuit/details
* networkcircuit/list
  • Loading branch information
norrland committed Apr 4, 2024
1 parent 4e41564 commit 033824f
Show file tree
Hide file tree
Showing 5 changed files with 126 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).
## Unreleased
### Added
- NetworkCircuits - Implement new endpoint `/networkcircuit/*`.

## [8.1.0] - 2024-01-15
### Added
Expand Down
2 changes: 2 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ type Client struct {
ServerDisks *ServerDisksService
Networks *NetworkService
NetworkAdapters *NetworkAdapterService
NetworkCircuits *NetworkCircuitService
}

// NewClient creates a new Client for interacting with the GleSYS API. This is
Expand All @@ -63,6 +64,7 @@ func NewClient(project, apiKey, userAgent string) *Client {
c.ServerDisks = &ServerDisksService{client: c}
c.Networks = &NetworkService{client: c}
c.NetworkAdapters = &NetworkAdapterService{client: c}
c.NetworkCircuits = &NetworkCircuitService{client: c}

return c
}
Expand Down
19 changes: 19 additions & 0 deletions doc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,25 @@ func ExampleNetworkService_List() {
}
}

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

ic, _ := client.NetworkCircuits.Details(context.Background(), "ic123456")

fmt.Println(ic.Type)
}

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

ics, _ := client.NetworkCircuits.List(context.Background())

for _, ic := range *ics {
fmt.Println(ic.ID)
fmt.Println(ic.Type)
}
}

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

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

import (
"context"
)

// NetworkCircuitService provides functions to interact with NetworkCircuits
type NetworkCircuitService struct {
client clientInterface
}

// NetworkCircuit represents a networkcircuit
type NetworkCircuit struct {
ID string `json:"id"`
Type string `json:"type"`
Billing NetworkCircuitBilling `json:"billing"`
}

type NetworkCircuitBilling struct {
Currency string `json:"currency"`
Price float64 `json:"price"`
Discount float64 `json:"discount"`
Total float64 `json:"total"`
Details []NetworkCircuitBillingDetails `json:"details,omitempty"`
}

type NetworkCircuitBillingDetails struct {
Text string `json:"text"`
PriceBeforeDiscount float64 `json:"subtotalBeforeDiscount"`
DiscountAmount float64 `json:"discountAmount"`
TotalBeforeTax float64 `json:"totalBeforeTax"`
}

// Details returns detailed information about one NetworkCircuit
func (s *NetworkCircuitService) Details(context context.Context, circuitID string) (*NetworkCircuit, error) {
data := struct {
Response struct {
NetworkCircuit NetworkCircuit
}
}{}
err := s.client.post(context, "networkcircuit/details", &data, circuitID)
return &data.Response.NetworkCircuit, err
}

// List returns a list of NetworkCircuits available under your account
func (s *NetworkCircuitService) List(context context.Context) (*[]NetworkCircuit, error) {
data := struct {
Response struct {
NetworkCircuits []NetworkCircuit
}
}{}

err := s.client.get(context, "networkcircuit/list", &data)
return &data.Response.NetworkCircuits, err
}
48 changes: 48 additions & 0 deletions networkcircuits_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package glesys

import (
"context"
"testing"

"github.com/stretchr/testify/assert"
)

func TestNetworkCircuitsDetails(t *testing.T) {
c := &mockClient{body: `{ "response": { "networkcircuit": {
"id": "ic123456",
"type": "INTERNET_PIPELINE",
"billing": {"currency": "SEK",
"price": "120.0",
"discount": "0.0",
"total": "120.0",
"details": []
}
} } }`}
s := NetworkCircuitService{client: c}

ic, _ := s.Details(context.Background(), "ic123456")

assert.Equal(t, "POST", c.lastMethod, "method used is correct")
assert.Equal(t, "networkcircuit/details", c.lastPath, "path used is correct")
assert.Equal(t, "INTERNET_PIPELINE", ic.Type, "networkcircuit Type is correct")
}

func TestNetworkCircuitsList(t *testing.T) {
c := &mockClient{body: `{ "response": { "networkcircuits": [{
"id": "ic123456",
"type": "INTERNET_PIPELINE",
"billing": {"currency": "SEK",
"price": "120.0",
"discount": "0.0",
"total": "120.0",
"details": []
}
}] } }`}
n := NetworkCircuitService{client: c}

ics, _ := n.List(context.Background())

assert.Equal(t, "GET", c.lastMethod, "method used is correct")
assert.Equal(t, "networkcircuit/list", c.lastPath, "path used is correct")
assert.Equal(t, "INTERNET_PIPELINE", (*ics)[0].Type, "networkcircuit Type is correct")
}

0 comments on commit 033824f

Please sign in to comment.