From 92fd13a00a3fb541f065cb3f9aeb075c19c776c2 Mon Sep 17 00:00:00 2001 From: tshihad Date: Sun, 10 Oct 2021 22:33:06 -0700 Subject: [PATCH 1/6] fix pool ID issure --- pkg/models/networks.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pkg/models/networks.go b/pkg/models/networks.go index cdc4b70..48afa40 100644 --- a/pkg/models/networks.go +++ b/pkg/models/networks.go @@ -23,7 +23,7 @@ type GetSpecificNetworkBody struct { } type GetSpecificNetwork struct { - ID int `json:"id" tf:"id"` + ID int `json:"id" tf:"id,computed"` Name string `json:"name" tf:"name"` Zone IDNameModel `json:"zone"` DisplayName string `json:"displayName" tf:"display_name,computed"` @@ -72,7 +72,7 @@ type CreateNetwork struct { Zone IDModel `json:"zone,omitempty" tf:"zone"` Site IDModel `json:"site,omitempty"` Type IDModel `json:"type,omitempty"` - Pool IDModel `json:"pool,omitempty"` + Pool *IDModel `json:"pool,omitempty"` Cidr string `json:"cidr,omitempty" tf:"cidr"` Gateway string `json:"gateway,omitempty" tf:"gateway"` DNSPrimary string `json:"dnsPrimary,omitempty" tf:"primary_dns"` From 0ee83ce94321b4a96a170c3d44edec9f3237e49f Mon Sep 17 00:00:00 2001 From: tshihad Date: Mon, 11 Oct 2021 23:22:39 -0700 Subject: [PATCH 2/6] add resource permission in network model --- pkg/client/networks.go | 20 ++++++++ pkg/models/networks.go | 101 +++++++++++++++++++++-------------------- 2 files changed, 72 insertions(+), 49 deletions(-) diff --git a/pkg/client/networks.go b/pkg/client/networks.go index c588107..15ec3f5 100644 --- a/pkg/client/networks.go +++ b/pkg/client/networks.go @@ -149,3 +149,23 @@ 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 +} diff --git a/pkg/models/networks.go b/pkg/models/networks.go index 48afa40..23f72aa 100644 --- a/pkg/models/networks.go +++ b/pkg/models/networks.go @@ -23,38 +23,39 @@ type GetSpecificNetworkBody struct { } type GetSpecificNetwork struct { - ID int `json:"id" tf:"id,computed"` - Name string `json:"name" tf:"name"` - Zone IDNameModel `json:"zone"` - DisplayName string `json:"displayName" tf:"display_name,computed"` - Type IDModel `json:"type"` - TypeID int `json:"-" tf:"type_id,computed"` - Owner IDNameModel `json:"owner"` - Code string `json:"code" tf:"code,computed"` - Category string `json:"category"` - ExternalID string `json:"externalId" tf:"external_id,computed"` - InternalID string `json:"internalId" tf:"internal_id,computed"` - UniqueID string `json:"uniqueId" tf:"unique_id,computed"` - ExternalType string `json:"externalType"` - RefType string `json:"refType"` - RefID int `json:"refId"` - DhcpServer bool `json:"dhcpServer" tf:"dhcp_server"` - Status string `json:"status" tf:"status,computed"` - Visibility string `json:"visibility" tf:"visibility"` - EnableAdmin bool `json:"enableAdmin"` - ScanNetwork bool `json:"scanNetwork" tf:"scan_network"` - Active bool `json:"active" tf:"active"` - DefaultNetwork bool `json:"defaultNetwork"` - AssignPublicIP bool `json:"assignPublicIp"` - ApplianceURLProxyBypass bool `json:"applianceUrlProxyBypass" tf:"appliance_url_proxy_bypass"` - ZonePool IDNameModel `json:"zonePool"` - AllowStaticOverride bool `json:"allowStaticOverride"` - Tenants []IDNameModel `json:"tenants"` + ID int `json:"id" tf:"id,computed"` + Name string `json:"name" tf:"name"` + Zone IDNameModel `json:"zone"` + DisplayName string `json:"displayName" tf:"display_name,computed"` + Type IDModel `json:"type"` + TypeID int `json:"-" tf:"type_id,computed"` + Owner IDNameModel `json:"owner"` + Code string `json:"code" tf:"code,computed"` + Category string `json:"category"` + ExternalID string `json:"externalId" tf:"external_id,computed"` + InternalID string `json:"internalId" tf:"internal_id,computed"` + UniqueID string `json:"uniqueId" tf:"unique_id,computed"` + ExternalType string `json:"externalType"` + RefType string `json:"refType"` + RefID int `json:"refId"` + DhcpServer bool `json:"dhcpServer" tf:"dhcp_server"` + Status string `json:"status" tf:"status,computed"` + Visibility string `json:"visibility" tf:"visibility"` + EnableAdmin bool `json:"enableAdmin"` + ScanNetwork bool `json:"scanNetwork" tf:"scan_network"` + Active bool `json:"active" tf:"active"` + DefaultNetwork bool `json:"defaultNetwork"` + AssignPublicIP bool `json:"assignPublicIp"` + ApplianceURLProxyBypass bool `json:"applianceUrlProxyBypass" tf:"appliance_url_proxy_bypass"` + ZonePool IDNameModel `json:"zonePool"` + AllowStaticOverride bool `json:"allowStaticOverride"` + Tenants []IDNameModel `json:"tenants"` + ResourcePermissions CreateNetworkResourcePermission `json:"resourcePermissions" tf:"resource_permissions,sub"` } type CreateNetworkResourcePermission struct { - All bool `json:"all"` - Sites []IDModel `json:"sites"` + All bool `json:"all" tf:"all"` + Sites []IDModel `json:"sites" tf:"sites"` } type CreateNetworkRequest struct { @@ -63,26 +64,28 @@ type CreateNetworkRequest struct { } type CreateNetwork struct { - Name string `json:"name" tf:"name"` - Description string `json:"description" tf:"description"` - CloudID int `json:"-" tf:"cloud_id"` - GroupID int `json:"-" tf:"group_id"` - TypeID int `json:"-" tf:"type_id"` - PoolID int `json:"-" tf:"pool_id"` - Zone IDModel `json:"zone,omitempty" tf:"zone"` - Site IDModel `json:"site,omitempty"` - Type IDModel `json:"type,omitempty"` - Pool *IDModel `json:"pool,omitempty"` - Cidr string `json:"cidr,omitempty" tf:"cidr"` - Gateway string `json:"gateway,omitempty" tf:"gateway"` - DNSPrimary string `json:"dnsPrimary,omitempty" tf:"primary_dns"` - DNSSecondary string `json:"dnsSecondary,omitempty" tf:"secondary_dns"` - Config CreateNetworkConfig `json:"config,omitempty" tf:"config,sub"` - DhcpServer bool `json:"dhcpServer" tf:"dhcp_server"` - ScanNetwork string `json:"scanNetwork,omitempty" tf:"scan_network"` - ApplianceURLProxyBypass string `json:"applianceUrlProxyBypass,omitempty" tf:"appliance_url_proxy_bypass"` - NoProxy string `json:"noProxy,omitempty" tf:"no_proxy"` - ScopeID string `json:"scopeId,omitempty" tf:"scode_id"` + ID int `json:"-" tf:"id,computed"` + Name string `json:"name" tf:"name"` + Description string `json:"description" tf:"description"` + CloudID int `json:"-" tf:"cloud_id"` + GroupID int `json:"-" tf:"group_id"` + TypeID int `json:"-" tf:"type_id"` + PoolID int `json:"-" tf:"pool_id"` + Zone IDModel `json:"zone,omitempty"` + Site IDModel `json:"site,omitempty"` + Type IDModel `json:"type,omitempty"` + Pool *IDModel `json:"pool,omitempty"` + Cidr string `json:"cidr,omitempty" tf:"cidr"` + Gateway string `json:"gateway,omitempty" tf:"gateway"` + DNSPrimary string `json:"dnsPrimary,omitempty" tf:"primary_dns"` + DNSSecondary string `json:"dnsSecondary,omitempty" tf:"secondary_dns"` + Config CreateNetworkConfig `json:"config,omitempty" tf:"config,sub"` + DhcpServer bool `json:"dhcpServer" tf:"dhcp_server"` + ScanNetwork string `json:"scanNetwork,omitempty" tf:"scan_network"` + ApplianceURLProxyBypass string `json:"applianceUrlProxyBypass,omitempty" tf:"appliance_url_proxy_bypass"` + NoProxy string `json:"noProxy,omitempty" tf:"no_proxy"` + ScopeID string `json:"scopeId,omitempty" tf:"scode_id"` + ResourcePermissions CreateNetworkResourcePermission `json:"-" tf:"resource_permissions,sub"` } type CreateNetworkConfig struct { From 5b873484b0ce67bc5075822a8f6f9b8879328920 Mon Sep 17 00:00:00 2001 From: tshihad Date: Tue, 12 Oct 2021 00:11:14 -0700 Subject: [PATCH 3/6] resolve lint and add UT --- pkg/client/networks_test.go | 75 ++++++++++++++++++++++ pkg/models/generic_responses.go | 4 +- pkg/models/networks.go | 106 ++++++++++++++++---------------- 3 files changed, 130 insertions(+), 55 deletions(-) diff --git a/pkg/client/networks_test.go b/pkg/client/networks_test.go index 5b80cbe..a04f9b5 100644 --- a/pkg/client/networks_test.go +++ b/pkg/client/networks_test.go @@ -218,3 +218,78 @@ 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) + } + }) + } +} diff --git a/pkg/models/generic_responses.go b/pkg/models/generic_responses.go index a373ba4..3b714d5 100644 --- a/pkg/models/generic_responses.go +++ b/pkg/models/generic_responses.go @@ -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"` } diff --git a/pkg/models/networks.go b/pkg/models/networks.go index 23f72aa..9ca8b5e 100644 --- a/pkg/models/networks.go +++ b/pkg/models/networks.go @@ -23,69 +23,69 @@ type GetSpecificNetworkBody struct { } type GetSpecificNetwork struct { - ID int `json:"id" tf:"id,computed"` - Name string `json:"name" tf:"name"` - Zone IDNameModel `json:"zone"` - DisplayName string `json:"displayName" tf:"display_name,computed"` - Type IDModel `json:"type"` - TypeID int `json:"-" tf:"type_id,computed"` - Owner IDNameModel `json:"owner"` - Code string `json:"code" tf:"code,computed"` - Category string `json:"category"` - ExternalID string `json:"externalId" tf:"external_id,computed"` - InternalID string `json:"internalId" tf:"internal_id,computed"` - UniqueID string `json:"uniqueId" tf:"unique_id,computed"` - ExternalType string `json:"externalType"` - RefType string `json:"refType"` - RefID int `json:"refId"` - DhcpServer bool `json:"dhcpServer" tf:"dhcp_server"` - Status string `json:"status" tf:"status,computed"` - Visibility string `json:"visibility" tf:"visibility"` - EnableAdmin bool `json:"enableAdmin"` - ScanNetwork bool `json:"scanNetwork" tf:"scan_network"` - Active bool `json:"active" tf:"active"` - DefaultNetwork bool `json:"defaultNetwork"` - AssignPublicIP bool `json:"assignPublicIp"` - ApplianceURLProxyBypass bool `json:"applianceUrlProxyBypass" tf:"appliance_url_proxy_bypass"` - ZonePool IDNameModel `json:"zonePool"` - AllowStaticOverride bool `json:"allowStaticOverride"` - Tenants []IDNameModel `json:"tenants"` - ResourcePermissions CreateNetworkResourcePermission `json:"resourcePermissions" tf:"resource_permissions,sub"` + ID int `json:"id" tf:"id,computed"` + Name string `json:"name" tf:"name"` + Zone IDNameModel `json:"zone"` + DisplayName string `json:"displayName" tf:"display_name,computed"` + Type IDModel `json:"type"` + TypeID int `json:"-" tf:"type_id,computed"` + Owner IDNameModel `json:"owner"` + Code string `json:"code" tf:"code,computed"` + Category string `json:"category"` + ExternalID string `json:"externalId" tf:"external_id,computed"` + InternalID string `json:"internalId" tf:"internal_id,computed"` + UniqueID string `json:"uniqueId" tf:"unique_id,computed"` + ExternalType string `json:"externalType"` + RefType string `json:"refType"` + RefID int `json:"refId"` + DhcpServer bool `json:"dhcpServer" tf:"dhcp_server"` + Status string `json:"status" tf:"status,computed"` + Visibility string `json:"visibility" tf:"visibility"` + EnableAdmin bool `json:"enableAdmin"` + ScanNetwork bool `json:"scanNetwork" tf:"scan_network"` + Active bool `json:"active" tf:"active"` + DefaultNetwork bool `json:"defaultNetwork"` + AssignPublicIP bool `json:"assignPublicIp"` + ApplianceURLProxyBypass bool `json:"applianceUrlProxyBypass" tf:"appliance_url_proxy_bypass"` + ZonePool IDNameModel `json:"zonePool"` + AllowStaticOverride bool `json:"allowStaticOverride"` + Tenants []IDNameModel `json:"tenants"` + ResourcePermissions NetworkResPermission `json:"resourcePermissions" tf:"resource_permissions,sub"` } -type CreateNetworkResourcePermission struct { +type NetworkResPermission struct { All bool `json:"all" tf:"all"` Sites []IDModel `json:"sites" tf:"sites"` } type CreateNetworkRequest struct { - Network CreateNetwork `json:"network"` - ResourcePermissions CreateNetworkResourcePermission `json:"resourcePermissions"` + Network CreateNetwork `json:"network"` + ResourcePermissions NetworkResPermission `json:"resourcePermissions"` } type CreateNetwork struct { - ID int `json:"-" tf:"id,computed"` - Name string `json:"name" tf:"name"` - Description string `json:"description" tf:"description"` - CloudID int `json:"-" tf:"cloud_id"` - GroupID int `json:"-" tf:"group_id"` - TypeID int `json:"-" tf:"type_id"` - PoolID int `json:"-" tf:"pool_id"` - Zone IDModel `json:"zone,omitempty"` - Site IDModel `json:"site,omitempty"` - Type IDModel `json:"type,omitempty"` - Pool *IDModel `json:"pool,omitempty"` - Cidr string `json:"cidr,omitempty" tf:"cidr"` - Gateway string `json:"gateway,omitempty" tf:"gateway"` - DNSPrimary string `json:"dnsPrimary,omitempty" tf:"primary_dns"` - DNSSecondary string `json:"dnsSecondary,omitempty" tf:"secondary_dns"` - Config CreateNetworkConfig `json:"config,omitempty" tf:"config,sub"` - DhcpServer bool `json:"dhcpServer" tf:"dhcp_server"` - ScanNetwork string `json:"scanNetwork,omitempty" tf:"scan_network"` - ApplianceURLProxyBypass string `json:"applianceUrlProxyBypass,omitempty" tf:"appliance_url_proxy_bypass"` - NoProxy string `json:"noProxy,omitempty" tf:"no_proxy"` - ScopeID string `json:"scopeId,omitempty" tf:"scode_id"` - ResourcePermissions CreateNetworkResourcePermission `json:"-" tf:"resource_permissions,sub"` + ID int `json:"-" tf:"id,computed"` + Name string `json:"name" tf:"name"` + Description string `json:"description" tf:"description"` + CloudID int `json:"-" tf:"cloud_id"` + GroupID int `json:"-" tf:"group_id"` + TypeID int `json:"-" tf:"type_id"` + PoolID int `json:"-" tf:"pool_id"` + Zone IDModel `json:"zone,omitempty"` + Site IDModel `json:"site,omitempty"` + Type IDModel `json:"type,omitempty"` + Pool *IDModel `json:"pool,omitempty"` + Cidr string `json:"cidr,omitempty" tf:"cidr"` + Gateway string `json:"gateway,omitempty" tf:"gateway"` + DNSPrimary string `json:"dnsPrimary,omitempty" tf:"primary_dns"` + DNSSecondary string `json:"dnsSecondary,omitempty" tf:"secondary_dns"` + Config CreateNetworkConfig `json:"config,omitempty" tf:"config,sub"` + DhcpServer bool `json:"dhcpServer" tf:"dhcp_server"` + ScanNetwork string `json:"scanNetwork,omitempty" tf:"scan_network"` + AppURLProxyBypass string `json:"applianceUrlProxyBypass,omitempty" tf:"appliance_url_proxy_bypass"` + NoProxy string `json:"noProxy,omitempty" tf:"no_proxy"` + ScopeID string `json:"scopeId,omitempty" tf:"scode_id"` + ResourcePermissions NetworkResPermission `json:"-" tf:"resource_permissions,sub"` } type CreateNetworkConfig struct { From e78b139e12bdfb2221fdf5f25b9bdc218d07e944 Mon Sep 17 00:00:00 2001 From: tshihad Date: Tue, 12 Oct 2021 03:19:59 -0700 Subject: [PATCH 4/6] Get Domain APIs --- pkg/client/domain.go | 57 +++++++++++++++++++++++++++++++++++++++++ pkg/common/constants.go | 1 + pkg/models/domain.go | 24 +++++++++++++++++ pkg/models/networks.go | 6 +++++ 4 files changed, 88 insertions(+) create mode 100644 pkg/client/domain.go create mode 100644 pkg/models/domain.go diff --git a/pkg/client/domain.go b/pkg/client/domain.go new file mode 100644 index 0000000..236a1f4 --- /dev/null +++ b/pkg/client/domain.go @@ -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 +} diff --git a/pkg/common/constants.go b/pkg/common/constants.go index cd255ee..e2f0a57 100644 --- a/pkg/common/constants.go +++ b/pkg/common/constants.go @@ -16,6 +16,7 @@ const ( // NetworkTypePath NetworkTypePath = "network-types" NetworkPoolPath = "pools" + DomainPath = "domains" // LibraryLayoutPath LibraryLayoutPath = "library/layouts" // LibraryInstanceTypesPath diff --git a/pkg/models/domain.go b/pkg/models/domain.go new file mode 100644 index 0000000..9d46218 --- /dev/null +++ b/pkg/models/domain.go @@ -0,0 +1,24 @@ +// (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"` + 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"` +} diff --git a/pkg/models/networks.go b/pkg/models/networks.go index 9ca8b5e..5fbeab9 100644 --- a/pkg/models/networks.go +++ b/pkg/models/networks.go @@ -67,6 +67,7 @@ type CreateNetwork struct { ID int `json:"-" tf:"id,computed"` Name string `json:"name" tf:"name"` Description string `json:"description" tf:"description"` + DisplayName string `json:"displayName" tf:"display_name"` CloudID int `json:"-" tf:"cloud_id"` GroupID int `json:"-" tf:"group_id"` TypeID int `json:"-" tf:"type_id"` @@ -75,13 +76,18 @@ type CreateNetwork struct { Site IDModel `json:"site,omitempty"` Type IDModel `json:"type,omitempty"` Pool *IDModel `json:"pool,omitempty"` + NetworkDomain *IDModel `json:"networkDomain" tf:"domain"` + NetworkProxy *IDModel `json:"networkProxy" tf:"proxy"` + SearchDomains string `json:"searchDomains" tf:"search_domains"` Cidr string `json:"cidr,omitempty" tf:"cidr"` Gateway string `json:"gateway,omitempty" tf:"gateway"` DNSPrimary string `json:"dnsPrimary,omitempty" tf:"primary_dns"` DNSSecondary string `json:"dnsSecondary,omitempty" tf:"secondary_dns"` Config CreateNetworkConfig `json:"config,omitempty" tf:"config,sub"` + Active bool `json:"active" tf:"active"` DhcpServer bool `json:"dhcpServer" tf:"dhcp_server"` ScanNetwork string `json:"scanNetwork,omitempty" tf:"scan_network"` + AllowStaticOverride bool `json:"allowStaticOverride" tf:"allow_static_override"` AppURLProxyBypass string `json:"applianceUrlProxyBypass,omitempty" tf:"appliance_url_proxy_bypass"` NoProxy string `json:"noProxy,omitempty" tf:"no_proxy"` ScopeID string `json:"scopeId,omitempty" tf:"scode_id"` From 04d3c351265cc88ca954ca253fee14ca4e5ebe90 Mon Sep 17 00:00:00 2001 From: tshihad Date: Tue, 12 Oct 2021 22:36:21 -0700 Subject: [PATCH 5/6] remove legacy network models --- pkg/models/domain.go | 1 + pkg/models/networks.go | 42 +++++++++++++++++++++++++----------------- 2 files changed, 26 insertions(+), 17 deletions(-) diff --git a/pkg/models/domain.go b/pkg/models/domain.go index 9d46218..dd72196 100644 --- a/pkg/models/domain.go +++ b/pkg/models/domain.go @@ -21,4 +21,5 @@ type GetDomain struct { type GetSpecificDomain struct { NetworkDomain GetDomain `json:"networkDomain"` + SuccessOrErrorMessage } diff --git a/pkg/models/networks.go b/pkg/models/networks.go index 5fbeab9..a9fcd91 100644 --- a/pkg/models/networks.go +++ b/pkg/models/networks.go @@ -54,40 +54,48 @@ type GetSpecificNetwork struct { } type NetworkResPermission struct { - All bool `json:"all" tf:"all"` - Sites []IDModel `json:"sites" tf:"sites"` + All bool `json:"all,omitempty" tf:"all"` + Sites []NetworkResPermissionSites `json:"sites,omitempty" tf:"sites"` +} + +type NetworkResPermissionSites struct { + ID int `json:"id,omitempty" tf:"id"` + Default string `json:"default,omitempty" tf:"default"` } type CreateNetworkRequest struct { Network CreateNetwork `json:"network"` - ResourcePermissions NetworkResPermission `json:"resourcePermissions"` + ResourcePermissions NetworkResPermission `json:"resourcePermissions,omitempty"` } type CreateNetwork struct { ID int `json:"-" tf:"id,computed"` Name string `json:"name" tf:"name"` - Description string `json:"description" tf:"description"` - DisplayName string `json:"displayName" tf:"display_name"` + Description string `json:"description,omitempty" tf:"description"` + DisplayName string `json:"displayName,omitempty" tf:"display_name"` CloudID int `json:"-" tf:"cloud_id"` - GroupID int `json:"-" tf:"group_id"` + GroupID string `json:"-" tf:"group_id"` TypeID int `json:"-" tf:"type_id"` PoolID int `json:"-" tf:"pool_id"` - Zone IDModel `json:"zone,omitempty"` - Site IDModel `json:"site,omitempty"` + NetworkDomainID int `json:"-" tf:"domain_id"` + Site IDStringModel `json:"site,omitempty"` Type IDModel `json:"type,omitempty"` Pool *IDModel `json:"pool,omitempty"` - NetworkDomain *IDModel `json:"networkDomain" tf:"domain"` - NetworkProxy *IDModel `json:"networkProxy" tf:"proxy"` - SearchDomains string `json:"searchDomains" tf:"search_domains"` + NetworkDomain *IDModel `json:"networkDomain,omitempty"` + NetworkProxy *IDModel `json:"networkProxy,omitempty"` + NetworkServer IDModel `json:"networkServer,omitempty"` + NetworkProxyID int `json:"-" tf:"proxy_id"` + ProxyID int `json:"-" tf:"proxy_id"` + SearchDomains string `json:"searchDomains,omitempty" tf:"search_domains"` Cidr string `json:"cidr,omitempty" tf:"cidr"` Gateway string `json:"gateway,omitempty" tf:"gateway"` DNSPrimary string `json:"dnsPrimary,omitempty" tf:"primary_dns"` DNSSecondary string `json:"dnsSecondary,omitempty" tf:"secondary_dns"` - Config CreateNetworkConfig `json:"config,omitempty" tf:"config,sub"` - Active bool `json:"active" tf:"active"` - DhcpServer bool `json:"dhcpServer" tf:"dhcp_server"` + Config *CreateNetworkConfig `json:"config,omitempty" tf:"config,sub"` + Active bool `json:"active,omitempty" tf:"active"` + DhcpServer bool `json:"dhcpServer,omitempty" tf:"dhcp_server"` ScanNetwork string `json:"scanNetwork,omitempty" tf:"scan_network"` - AllowStaticOverride bool `json:"allowStaticOverride" tf:"allow_static_override"` + AllowStaticOverride bool `json:"allowStaticOverride,omitempty" tf:"allow_static_override"` AppURLProxyBypass string `json:"applianceUrlProxyBypass,omitempty" tf:"appliance_url_proxy_bypass"` NoProxy string `json:"noProxy,omitempty" tf:"no_proxy"` ScopeID string `json:"scopeId,omitempty" tf:"scode_id"` @@ -95,8 +103,8 @@ type CreateNetwork struct { } type CreateNetworkConfig struct { - ConnectedGateway string `json:"connectedGateway" tf:"connected_gateway"` - VlanIDs string `json:"vlanIDs" tf:"vlan_ids"` + ConnectedGateway string `json:"connectedGateway,omitempty" tf:"connected_gateway"` + VlanIDs string `json:"vlanIDs,omitempty" tf:"vlan_ids"` } type GetNetworkTypesResponse struct { From eb678f3d2509a8728d5812639c9808da0d41919e Mon Sep 17 00:00:00 2001 From: tshihad Date: Tue, 12 Oct 2021 23:31:42 -0700 Subject: [PATCH 6/6] Added network proxy API --- pkg/client/networks.go | 19 ++++++++++ pkg/client/networks_test.go | 73 +++++++++++++++++++++++++++++++++++++ pkg/common/constants.go | 7 ++-- pkg/models/network_proxy.go | 19 ++++++++++ 4 files changed, 115 insertions(+), 3 deletions(-) create mode 100644 pkg/models/network_proxy.go diff --git a/pkg/client/networks.go b/pkg/client/networks.go index 15ec3f5..0a448aa 100644 --- a/pkg/client/networks.go +++ b/pkg/client/networks.go @@ -169,3 +169,22 @@ func (n *NetworksAPIService) UpdateNetwork( 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 +} diff --git a/pkg/client/networks_test.go b/pkg/client/networks_test.go index a04f9b5..de20239 100644 --- a/pkg/client/networks_test.go +++ b/pkg/client/networks_test.go @@ -293,3 +293,76 @@ func TestNetworksAPIService_UpdateNetwork(t *testing.T) { }) } } + +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) + } + }) + } +} diff --git a/pkg/common/constants.go b/pkg/common/constants.go index e2f0a57..012d354 100644 --- a/pkg/common/constants.go +++ b/pkg/common/constants.go @@ -14,9 +14,10 @@ const ( // NetworksPath NetworksPath = "networks" // NetworkTypePath - NetworkTypePath = "network-types" - NetworkPoolPath = "pools" - DomainPath = "domains" + NetworkTypePath = "network-types" + NetworkPoolPath = "pools" + NetworkProxyPath = "proxies" + DomainPath = "domains" // LibraryLayoutPath LibraryLayoutPath = "library/layouts" // LibraryInstanceTypesPath diff --git a/pkg/models/network_proxy.go b/pkg/models/network_proxy.go new file mode 100644 index 0000000..2f3f066 --- /dev/null +++ b/pkg/models/network_proxy.go @@ -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"` +}