Skip to content

Commit

Permalink
Add /networks API handler.
Browse files Browse the repository at this point in the history
  • Loading branch information
Masahiro Fujiwara committed Dec 14, 2015
1 parent 532b142 commit 4d8c710
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 0 deletions.
2 changes: 2 additions & 0 deletions client/go-wakamevdc/client.go
Expand Up @@ -30,6 +30,7 @@ type Client struct {
SecurityGroup *SecurityGroupService
SshKey *SshKeyService
Image *ImageService
Network *NetworkService
}

func NewClient(baseURL *url.URL, httpClient *http.Client) *Client {
Expand All @@ -44,6 +45,7 @@ func NewClient(baseURL *url.URL, httpClient *http.Client) *Client {
c.SecurityGroup = &SecurityGroupService{client: c}
c.SshKey = &SshKeyService{client: c}
c.Image = &ImageService{client: c}
c.Network = &NetworkService{client: c}
return c
}

Expand Down
72 changes: 72 additions & 0 deletions client/go-wakamevdc/network.go
@@ -0,0 +1,72 @@
package wakamevdc

import (
"fmt"
"net/http"
)

const NetworkPath = "networks"

type Network struct {
ID string `json:"id"`
DisplayName string `json:"display_name"`
Description string `json:"description"`
CreatedAt string `json:"created_at"`
UpdatedAt string `json:"updated_at"`
DeletedAt string `json:"deleted_at"`
}

type NetworkService struct {
client *Client
}

type NetworkCreateParams struct {
ServiceType string `url:"service_type,omitempty"`
Description string `url:"description,omitempty"`
DisplayName string `url:"display_name,omitempty"`
Rules string `url:"rule"`
}

func (s *NetworkService) Create(req *NetworkCreateParams) (*Network, *http.Response, error) {
nw := new(Network)
resp, err := trapAPIError(func(errResp *ErrorResponse) (*http.Response, error) {
return s.client.Sling().Post(NetworkPath).BodyForm(req).Receive(nw, errResp)
})

return nw, resp, err
}

func (s *NetworkService) Delete(id string) (*http.Response, error) {
return trapAPIError(func(errResp *ErrorResponse) (*http.Response, error) {
return s.client.Sling().Delete(fmt.Sprintf(NetworkPath+"/%s", id)).Receive(nil, errResp)
})
}

func (s *NetworkService) GetByID(id string) (*Network, *http.Response, error) {
nw := new(Network)
resp, err := trapAPIError(func(errResp *ErrorResponse) (*http.Response, error) {
return s.client.Sling().Get(fmt.Sprintf(NetworkPath+"/%s", id)).Receive(nw, errResp)
})

return nw, resp, err
}

type NetworksList struct {
Total int `json:"total"`
Start int `json:"start"`
Limit int `json:"limit"`
Results []Network `json:"results"`
}

func (s *NetworkService) List(req *ListRequestParams) (*NetworksList, *http.Response, error) {
nwList := make([]NetworksList, 1)
resp, err := trapAPIError(func(errResp *ErrorResponse) (*http.Response, error) {
return s.client.Sling().Get(NetworkPath).QueryStruct(req).Receive(&nwList, errResp)
})

if err == nil && len(nwList) > 0 {
return &nwList[0], resp, err
}
// Return empty list object.
return &NetworksList{}, resp, err
}
29 changes: 29 additions & 0 deletions client/go-wakamevdc/network_test.go
@@ -0,0 +1,29 @@
package wakamevdc

import (
"testing"
)

func TestNetwork(t *testing.T) {
c := NewClient(nil, nil)

if c.Network == nil {
t.Errorf("ImageService should not be nil")
}
}

func TestNetworkList(t *testing.T) {
c := NewClient(nil, nil)

var nwList *NetworksList

nwList, _, _ = c.Network.List(nil)

nwList, _, _ = c.Network.List(&ListRequestParams{
Start: 1,
Limit: 1,
})
if !(len(nwList.Results) <= nwList.Limit) {
t.Errorf("Results contain more items than specifed in limit")
}
}

0 comments on commit 4d8c710

Please sign in to comment.