Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Network model fixes and API enhancment #17

Merged
merged 7 commits into from
Oct 13, 2021
Merged
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
57 changes: 57 additions & 0 deletions pkg/client/domain.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// (C) Copyright 2021 Hewlett Packard Enterprise Development LP

package client

import (
"context"
"encoding/json"
"fmt"

consts "github.com/HewlettPackard/hpegl-vmaas-cmp-go-sdk/pkg/common"
"github.com/HewlettPackard/hpegl-vmaas-cmp-go-sdk/pkg/models"
)

type DomainAPIService struct {
Client APIClientHandler
Cfg Configuration
}

func (d *DomainAPIService) GetAllDomains(
ctx context.Context,
param map[string]string,
) (models.GetAllDomains, error) {
var domainResp models.GetAllDomains
networkAPI := &api{
method: "GET",
path: fmt.Sprintf("%s/%s/%s/%s",
d.Cfg.Host, consts.VmaasCmpAPIBasePath, consts.NetworksPath, consts.DomainPath),
client: d.Client,

jsonParser: func(body []byte) error {
return json.Unmarshal(body, &domainResp)
},
}
err := networkAPI.do(ctx, nil, param)

return domainResp, err
}

func (d *DomainAPIService) GetSpecificDomain(
ctx context.Context,
domainID int,
) (models.GetSpecificDomain, error) {
var domainResp models.GetSpecificDomain
networkAPI := &api{
method: "GET",
path: fmt.Sprintf("%s/%s/%s/%s/%d",
d.Cfg.Host, consts.VmaasCmpAPIBasePath, consts.NetworksPath, consts.DomainPath, domainID),
client: d.Client,

jsonParser: func(body []byte) error {
return json.Unmarshal(body, &domainResp)
},
}
err := networkAPI.do(ctx, nil, nil)

return domainResp, err
}
39 changes: 39 additions & 0 deletions pkg/client/networks.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,3 +149,42 @@ func (n *NetworksAPIService) GetSpecificNetworkPool(

return resp, err
}

func (n *NetworksAPIService) UpdateNetwork(
ctx context.Context,
networkID int,
request models.CreateNetworkRequest,
) (models.SuccessOrErrorMessage, error) {
var output models.SuccessOrErrorMessage
networkAPI := &api{
method: "PUT",
path: fmt.Sprintf("%s/%s/%s/%d", n.Cfg.Host, consts.VmaasCmpAPIBasePath, consts.NetworksPath, networkID),
client: n.Client,

jsonParser: func(body []byte) error {
return json.Unmarshal(body, &output)
},
}
err := networkAPI.do(ctx, request, nil)

return output, err
}

func (n *NetworksAPIService) GetNetworkProxy(
ctx context.Context,
params map[string]string,
) (models.GetAllNetworkProxies, error) {
var proxyResp models.GetAllNetworkProxies
networkAPI := &api{
method: "GET",
path: fmt.Sprintf("%s/%s/%s/%s", n.Cfg.Host, consts.VmaasCmpAPIBasePath, consts.NetworksPath, consts.NetworkProxyPath),
client: n.Client,

jsonParser: func(body []byte) error {
return json.Unmarshal(body, &proxyResp)
},
}
err := networkAPI.do(ctx, nil, params)

return proxyResp, err
}
148 changes: 148 additions & 0 deletions pkg/client/networks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,3 +218,151 @@ func TestNetworksAPIService_CreateNetwork(t *testing.T) {
})
}
}

func TestNetworksAPIService_UpdateNetwork(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()

type args struct {
networkID int
request models.CreateNetworkRequest
}
tests := []struct {
name string
args args
given func(m *MockAPIClientHandler)
want models.SuccessOrErrorMessage
wantErr bool
}{
{
name: "Normal test case 1: update network",
args: args{
networkID: 1,
request: models.CreateNetworkRequest{
Network: models.CreateNetwork{
Name: "test_net",
},
},
},
given: func(m *MockAPIClientHandler) {
path := mockHost + "/v1/networks/1"
method := "PUT"
headers := getDefaultHeaders()
req, _ := http.NewRequest(method, path, nil)

m.EXPECT().prepareRequest(gomock.Any(), path, method,
models.CreateNetworkRequest{
Network: models.CreateNetwork{
Name: "test_net",
},
},
headers, url.Values{}, url.Values{}, "", nil).Return(req, nil)

m.EXPECT().callAPI(req).Return(&http.Response{
StatusCode: 200,
Body: ioutil.NopCloser(bytes.NewReader([]byte(`
{
"Success": true
}
`))),
}, nil)
},
want: models.SuccessOrErrorMessage{
Success: true,
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
mockAPIClient := NewMockAPIClientHandler(ctrl)
tt.given(mockAPIClient)
n := NetworksAPIService{
Client: mockAPIClient,
Cfg: Configuration{
Host: mockHost,
},
}
got, err := n.UpdateNetwork(context.Background(), tt.args.networkID, tt.args.request)
if (err != nil) != tt.wantErr {
t.Errorf("NetworksAPIService.UpdateNetwork() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("NetworksAPIService.UpdateNetwork() = %v, want %v", got, tt.want)
}
})
}
}

func TestNetworksAPIService_GetNetworkProxy(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()

tests := []struct {
name string
args map[string]string
given func(m *MockAPIClientHandler)
want models.GetAllNetworkProxies
wantErr bool
}{
{
name: "Normal test case 1: Get all proxies",
args: map[string]string{
"name": "NSXT",
},
given: func(m *MockAPIClientHandler) {
path := mockHost + "/v1/networks/proxies"
method := "GET"
headers := getDefaultHeaders()
req, _ := http.NewRequest(method, path, nil)
respBody := ioutil.NopCloser(bytes.NewReader([]byte(`
{
"networkProxies": [
{
"id": 1,
"name": "test_proxy"
}
]
}
`)))
m.EXPECT().prepareRequest(gomock.Any(), path, method, nil, headers,
getURLValues(map[string]string{
"name": "NSXT",
}), url.Values{}, "", nil).Return(req, nil)

m.EXPECT().callAPI(req).Return(&http.Response{
StatusCode: 200,
Body: respBody,
}, nil)
},
want: models.GetAllNetworkProxies{
GetNetworkProxies: []models.GetNetworkProxy{
{
ID: 1,
Name: "test_proxy",
},
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
mockAPIClient := NewMockAPIClientHandler(ctrl)
tt.given(mockAPIClient)
n := NetworksAPIService{
Client: mockAPIClient,
Cfg: Configuration{
Host: mockHost,
},
}
got, err := n.GetNetworkProxy(context.Background(), tt.args)
if (err != nil) != tt.wantErr {
t.Errorf("NetworksAPIService.GetNetworkProxy() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("NetworksAPIService.GetNetworkProxy() = %v, want %v", got, tt.want)
}
})
}
}
6 changes: 4 additions & 2 deletions pkg/common/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@ const (
// NetworksPath
NetworksPath = "networks"
// NetworkTypePath
NetworkTypePath = "network-types"
NetworkPoolPath = "pools"
NetworkTypePath = "network-types"
NetworkPoolPath = "pools"
NetworkProxyPath = "proxies"
DomainPath = "domains"
// LibraryLayoutPath
LibraryLayoutPath = "library/layouts"
// LibraryInstanceTypesPath
Expand Down
25 changes: 25 additions & 0 deletions pkg/models/domain.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// (C) Copyright 2021 Hewlett Packard Enterprise Development LP

package models

type GetAllDomains struct {
NetworkDomains []GetDomain `json:"networkDomains"`

NetworkDomainCount int `json:"networkDomainCount"`
Meta Meta `json:"meta"`
tshihad marked this conversation as resolved.
Show resolved Hide resolved
AccountID int `json:"accountId"`
}

type GetDomain struct {
ID int `json:"id" tf:"id,computed"`
Name string `json:"name" tf:"name"`
Active bool `json:"active" tf:"active,computed"`
Visibility string `json:"visibility"`
DomainController bool `json:"domainController"`
PublicZone bool `json:"publicZone"`
}

type GetSpecificDomain struct {
NetworkDomain GetDomain `json:"networkDomain"`
SuccessOrErrorMessage
}
4 changes: 2 additions & 2 deletions pkg/models/generic_responses.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ type ErrUnauthorized struct {

// Success Or Failure Message
type SuccessOrErrorMessage struct {
Success bool `json:"success"`
Success bool `json:"success,omitempty"`
Msg string `json:"msg,omitempty"`
Message string `json:"message,omitempty"`
Error interface{} `json:"error"`
Error interface{} `json:"error,omitempty"`
}
19 changes: 19 additions & 0 deletions pkg/models/network_proxy.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// (C) Copyright 2021 Hewlett Packard Enterprise Development LP

package models

type GetAllNetworkProxies struct {
GetNetworkProxies []GetNetworkProxy `json:"networkProxies"`
NetworkProxyCount int `json:"networkProxyCount"`
Meta Meta `json:"meta"`
}

type GetNetworkProxy struct {
ID int `json:"id" tf:"id,computed"`
Name string `json:"name" tf:"name"`
ProxyHost string `json:"proxyHost"`
ProxyPort int `json:"proxyPort"`
Visibility string `json:"visibility"`
Account IDModel `json:"account"`
Owner IDModel `json:"owner"`
}
Loading