Skip to content

Commit

Permalink
Version 0.0.22
Browse files Browse the repository at this point in the history
- added HideLinks node property
- added another HTTP request error condition
- ran gofumpt over the code base
  • Loading branch information
rschmied committed Dec 28, 2023
1 parent 96c9bd3 commit cfaf897
Show file tree
Hide file tree
Showing 25 changed files with 210 additions and 57 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

Lists the changes in the gocmlclient package.

## Version 0.0.22

- added HideLinks node property
- added another HTTP request error condition
- ran gofumpt over the code base

## Version 0.0.20 and 0.0.21

- added more cases where an error results in ErrSystemNotReady
Expand Down
4 changes: 1 addition & 3 deletions apiclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ func (c *Client) apiRequest(ctx context.Context, method string, path string, dat
}

func (c *Client) doAPI(ctx context.Context, req *http.Request, depth int32) ([]byte, error) {

if c.state.get() == stateInitial {
c.state.set(stateCheckVersion)
c.compatibilityErr = c.versionCheck(ctx, depth)
Expand All @@ -79,7 +78,7 @@ retry:
res, err := c.httpClient.Do(req)
if err != nil {
if urlError, ok := (err).(*url.Error); ok {
if urlError.Timeout() {
if urlError.Timeout() || urlError.Temporary() {
return nil, ErrSystemNotReady
}
}
Expand Down Expand Up @@ -166,7 +165,6 @@ func (c *Client) jsonDelete(ctx context.Context, api string, depth int32) error
}

func (c *Client) jsonReq(ctx context.Context, method, api string, data io.Reader, result any, depth int32) error {

// during initialization, the API client does API calls recursively which
// leads to all sorts of nasty race problems. The below basically prevents
// any additional API calls when recursion happens during initialization or
Expand Down
1 change: 0 additions & 1 deletion apiclient_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ func TestClient_compatibility(t *testing.T) {
}

func TestClient_putpatch(t *testing.T) {

putResponse := mr.MockRespList{
mr.MockResp{Code: 204},
}
Expand Down
4 changes: 0 additions & 4 deletions auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
)

func TestClient_authenticate(t *testing.T) {

tc := newTestAPIclient()
tc.client.state.set(stateAuthRequired)
tc.client.userpass = userPass{"qwe", "qwe"}
Expand Down Expand Up @@ -76,7 +75,6 @@ func TestClient_authenticate(t *testing.T) {
}

func TestClient_token_auth(t *testing.T) {

tc := newAuthedTestAPIclient()
tc.client.apiToken = "sometoken"
tc.client.userpass = userPass{}
Expand Down Expand Up @@ -183,7 +181,6 @@ func TestClient_SetCACert(t *testing.T) {
}

func TestClient_complete(t *testing.T) {

tc := newTestAPIclient()

tc.mr.SetData(mr.MockRespList{
Expand Down Expand Up @@ -232,7 +229,6 @@ func TestClient_complete(t *testing.T) {
}

func TestClient_Race(t *testing.T) {

// GOFLAGS="-count=1000" time go test -race -parallel 2 . -run 'Race$'
t.Parallel()

Expand Down
1 change: 0 additions & 1 deletion cmd/ctest/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
)

func main() {

// address and lab id
host, found := os.LookupEnv("CML_HOST")
if !found {
Expand Down
80 changes: 80 additions & 0 deletions extconn.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package cmlclient

import (
"context"
"fmt"
)

/*
[
{
"id": "58568fbb-e1f8-4b83-a1f8-148c656eed39",
"device_name": "virbr0",
"label": "NAT",
"protected": false,
"snooped": true,
"tags": [
"NAT"
],
"operational": {
"forwarding": "NAT",
"label": "NAT",
"mtu": 1500,
"exists": true,
"enabled": true,
"protected": false,
"snooped": true,
"stp": false,
"interface": null,
"ip_networks": []
}
}
]
*/

// Operational data struct
type opdata struct {
Forwarding string `json:"forwarding"`
Label string `json:"label"`
MTU int `json:"mtu"`
Exists bool `json:"exits"`
Enabled bool `json:"enabled"`
Protected bool `json:"protected"`
Snooped bool `json:"snooped"`
STP bool `json:"stp"`
// Interface string `json:"interface"`
IPNetworks []string `json:"ip_networks"`
}

// ExtConn defines the data structure for a CML external connector
type ExtConn struct {
ID string `json:"id"`
DeviceName string `json:"device_name"`
Label string `json:"label"`
Protected bool `json:"protected"`
Snooped bool `json:"snooped"`
Tags []string `json:"tags"`
Operational opdata `json:"operational"`
}

// ExtConnGet returns the external connector specified by the ID given
func (c *Client) ExtConnGet(ctx context.Context, extConnID string) (*ExtConn, error) {
api := fmt.Sprintf("system/external_connectors/%s", extConnID)
extconn := &ExtConn{}
err := c.jsonGet(ctx, api, extconn, 0)
if err != nil {
return nil, err
}
return extconn, err
}

// ExtConnectors returns all external connectors on the system
func (c *Client) ExtConnectors(ctx context.Context) ([]*ExtConn, error) {
api := fmt.Sprintf("system/external_connectors")
extconnlist := make([]*ExtConn, 0)
err := c.jsonGet(ctx, api, &extconnlist, 0)
if err != nil {
return nil, err
}
return extconnlist, err
}
71 changes: 71 additions & 0 deletions extconn_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package cmlclient

import (
"testing"

mr "github.com/rschmied/mockresponder"
"github.com/stretchr/testify/assert"
)

func TestClient_ExtConns(t *testing.T) {
tc := newAuthedTestAPIclient()

extConn := []byte(`
{
"id": "58568fbb-e1f8-4b83-a1f8-148c656eed39",
"device_name": "virbr0",
"label": "NAT",
"protected": false,
"snooped": true,
"tags": [
"NAT"
],
"operational": {
"forwarding": "NAT",
"label": "NAT",
"mtu": 1500,
"exists": true,
"enabled": true,
"protected": false,
"snooped": true,
"stp": false,
"interface": null,
"ip_networks": []
}
}
`)
extConns := append([]byte(`[`), extConn...)
extConns = append(extConns, []byte(`]`)...)

tests := []struct {
name string
data mr.MockRespList
wantErr bool
}{
{"good", mr.MockRespList{mr.MockResp{Data: extConns}}, false},
{"badjson", mr.MockRespList{mr.MockResp{Data: []byte(`///`)}}, true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tc.mr.SetData(tt.data)
got, err := tc.client.ExtConnectors(tc.ctx)
if err != nil {
if !tt.wantErr {
t.Error("unexpected error!", err)
}
return
}
assert.NoError(t, err)
assert.Len(t, got, 1)
assert.True(t, got[0].Label == "NAT")
})
}

t.Run("single", func(t *testing.T) {
tc.mr.SetData(mr.MockRespList{mr.MockResp{Data: extConn}})
got, err := tc.client.ExtConnGet(tc.ctx, "58568fbb-e1f8-4b83-a1f8-148c656eed39")
assert.NoError(t, err)
assert.Equal(t, got.ID, "58568fbb-e1f8-4b83-a1f8-148c656eed39")
assert.True(t, got.Label == "NAT")
})
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ require (
github.com/Masterminds/semver/v3 v3.2.1
github.com/rschmied/mockresponder v1.0.4
github.com/stretchr/testify v1.8.2
golang.org/x/sync v0.1.0
golang.org/x/sync v0.5.0
)

require (
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8=
github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
golang.org/x/sync v0.1.0 h1:wsuoTGHzEhffawBOhz5CYhcrV4IdKZbEyZjBMuTp12o=
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.5.0 h1:60k92dhOjHxJkrqnwsfl8KuaHbn/5dl0lUPUklKo3qE=
golang.org/x/sync v0.5.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
Expand Down
5 changes: 0 additions & 5 deletions groups_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ var (
)

func TestClient_GetGroups(t *testing.T) {

tc := newAuthedTestAPIclient()

tests := []struct {
Expand Down Expand Up @@ -109,7 +108,6 @@ func TestClient_GetGroups(t *testing.T) {
}

func TestClient_GetGroupByName(t *testing.T) {

tc := newAuthedTestAPIclient()

tests := []struct {
Expand Down Expand Up @@ -167,7 +165,6 @@ func TestClient_GetGroupByName(t *testing.T) {
}

func TestClient_GetGroup(t *testing.T) {

tc := newAuthedTestAPIclient()

// ID of group1 from above
Expand Down Expand Up @@ -243,7 +240,6 @@ func TestClient_GroupDestroy(t *testing.T) {
}

func TestClient_GroupCreate(t *testing.T) {

tc := newAuthedTestAPIclient()
newGroup := Group{Name: "doesntmatter"}

Expand Down Expand Up @@ -299,7 +295,6 @@ func TestClient_GroupCreate(t *testing.T) {
}

func TestClient_GroupUpdate(t *testing.T) {

tc := newAuthedTestAPIclient()
newGroup := Group{Name: "doesntmatter"}

Expand Down
2 changes: 0 additions & 2 deletions iface.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,6 @@ func (c *Client) getInterfacesForNode(ctx context.Context, node *Node) error {

// InterfaceGet returns the interface identified by its `ID` (iface.ID).
func (c *Client) InterfaceGet(ctx context.Context, iface *Interface) (*Interface, error) {

if iface, ok := c.getCachedIface(iface); ok {
return iface, nil
}
Expand All @@ -238,7 +237,6 @@ func (c *Client) InterfaceGet(ctx context.Context, iface *Interface) (*Interface
// is >= 0, the request creates all unallocated slots up to and including
// that slot. Conversely, if the slot is < 0 (e.g. -1), the next free slot is used.
func (c *Client) InterfaceCreate(ctx context.Context, labID, nodeID string, slot int) (*Interface, error) {

var slotPtr *int

if slot >= 0 {
Expand Down
1 change: 0 additions & 1 deletion iface_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,6 @@ func TestClient_IfaceDelete(t *testing.T) {
}

func Test_Race(t *testing.T) {

tc := newAuthedTestAPIclient()
tc.client.useCache = true

Expand Down
1 change: 0 additions & 1 deletion imagedef_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,6 @@ import (
// ]

func TestClient_GetImageDefs(t *testing.T) {

tc := newAuthedTestAPIclient()

aimgdef := `{
Expand Down
19 changes: 8 additions & 11 deletions lab.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,14 @@ type LabGroup struct {
Permission string `json:"permission"`
}

type IDlist []string
type NodeMap map[string]*Node
type InterfaceList []*Interface
type nodeList []*Node
type linkList []*Link
type LabGroupList []*LabGroup
type (
IDlist []string
NodeMap map[string]*Node
InterfaceList []*Interface
nodeList []*Node
linkList []*Link
LabGroupList []*LabGroup
)

type labAlias struct {
Lab
Expand Down Expand Up @@ -185,7 +187,6 @@ func (c *Client) deleteCachedLab(id string, err error) error {

// LabCreate creates a new lab on the controller.
func (c *Client) LabCreate(ctx context.Context, lab Lab) (*Lab, error) {

// TODO: inconsistent attributes lab_title vs title, ...
postAlias := labPatchPostAlias{
Title: lab.Title,
Expand All @@ -212,7 +213,6 @@ func (c *Client) LabCreate(ctx context.Context, lab Lab) (*Lab, error) {

// LabUpdate updates specific fields of a lab (title, description and notes).
func (c *Client) LabUpdate(ctx context.Context, lab Lab) (*Lab, error) {

// TODO: inconsistent attributes lab_title vs title, ...
patchAlias := labPatchPostAlias{
Title: lab.Title,
Expand Down Expand Up @@ -290,7 +290,6 @@ func (c *Client) LabDestroy(ctx context.Context, id string) error {
// LabGetByTitle returns the lab identified by its `title`. For the use of
// `deep` see LabGet().
func (c *Client) LabGetByTitle(ctx context.Context, title string, deep bool) (*Lab, error) {

var data map[string]map[string]*labAlias

err := c.jsonGet(ctx, "populate_lab_tiles", &data, 0)
Expand All @@ -315,7 +314,6 @@ func (c *Client) LabGetByTitle(ctx context.Context, title string, deep bool) (*L
// then the nodes, their interfaces and links are also fetched from the controller.
// Also, with `deep`, the L3 IP address info is fetched for the given lab.
func (c *Client) LabGet(ctx context.Context, id string, deep bool) (*Lab, error) {

if lab, ok := c.getCachedLab(id, deep); ok {
return lab, nil
}
Expand All @@ -333,7 +331,6 @@ func (c *Client) LabGet(ctx context.Context, id string, deep bool) (*Lab, error)
}

func (c *Client) labFill(ctx context.Context, la *labAlias) (*Lab, error) {

var err error
g, ctx := errgroup.WithContext(ctx)

Expand Down
Loading

0 comments on commit cfaf897

Please sign in to comment.