From 0244d6a25a42c8850de17ef22a2a6867f2cffcfa Mon Sep 17 00:00:00 2001 From: manjunath-batakurki Date: Tue, 9 Nov 2021 21:46:14 -0800 Subject: [PATCH] Go SDK for Router Firewall Groups --- pkg/client/router.go | 104 ++++++++++++++---- pkg/client/router_test.go | 220 ++++++++++++++++++++++++++++++++++++++ pkg/common/constants.go | 11 +- pkg/models/router.go | 39 +++++++ 4 files changed, 347 insertions(+), 27 deletions(-) diff --git a/pkg/client/router.go b/pkg/client/router.go index 767ab08..70eb62e 100644 --- a/pkg/client/router.go +++ b/pkg/client/router.go @@ -21,7 +21,7 @@ func (r *RouterAPIService) GetAllRouter( queryParams map[string]string, ) (models.GetAllNetworkRouter, error) { routerResp := models.GetAllNetworkRouter{} - serverAPI := &api{ + routerAPI := &api{ method: "GET", path: fmt.Sprintf("%s/%s/%s/%s", r.Cfg.Host, consts.VmaasCmpAPIBasePath, consts.NetworksPath, consts.NetworkRouterPath), @@ -30,7 +30,7 @@ func (r *RouterAPIService) GetAllRouter( return json.Unmarshal(body, &routerResp) }, } - err := serverAPI.do(ctx, nil, queryParams) + err := routerAPI.do(ctx, nil, queryParams) return routerResp, err } @@ -40,7 +40,7 @@ func (r *RouterAPIService) GetSpecificRouter( routerID int, ) (models.GetNetworkRouter, error) { routerResp := models.GetNetworkRouter{} - serverAPI := &api{ + routerAPI := &api{ method: "GET", path: fmt.Sprintf("%s/%s/%s/%s/%d", r.Cfg.Host, consts.VmaasCmpAPIBasePath, consts.NetworksPath, consts.NetworkRouterPath, routerID), @@ -49,7 +49,7 @@ func (r *RouterAPIService) GetSpecificRouter( return json.Unmarshal(body, &routerResp) }, } - err := serverAPI.do(ctx, nil, nil) + err := routerAPI.do(ctx, nil, nil) return routerResp, err } @@ -59,7 +59,7 @@ func (r *RouterAPIService) CreateRouter( request models.CreateRouterRequest, ) (models.CreateRouterResp, error) { routerResp := models.CreateRouterResp{} - serverAPI := &api{ + routerAPI := &api{ method: "POST", path: fmt.Sprintf("%s/%s/%s/%s", r.Cfg.Host, consts.VmaasCmpAPIBasePath, consts.NetworksPath, consts.NetworkRouterPath), @@ -68,7 +68,7 @@ func (r *RouterAPIService) CreateRouter( return json.Unmarshal(body, &routerResp) }, } - err := serverAPI.do(ctx, request, nil) + err := routerAPI.do(ctx, request, nil) return routerResp, err } @@ -79,7 +79,7 @@ func (r *RouterAPIService) UpdateRouter( request models.CreateRouterRequest, ) (models.SuccessOrErrorMessage, error) { routerResp := models.SuccessOrErrorMessage{} - serverAPI := &api{ + routerAPI := &api{ method: "PUT", path: fmt.Sprintf("%s/%s/%s/%s/%d", r.Cfg.Host, consts.VmaasCmpAPIBasePath, consts.NetworksPath, consts.NetworkRouterPath, routerID), @@ -88,7 +88,7 @@ func (r *RouterAPIService) UpdateRouter( return json.Unmarshal(body, &routerResp) }, } - err := serverAPI.do(ctx, request, nil) + err := routerAPI.do(ctx, request, nil) return routerResp, err } @@ -98,7 +98,7 @@ func (r *RouterAPIService) DeleteRouter( routerID int, ) (models.SuccessOrErrorMessage, error) { routerResp := models.SuccessOrErrorMessage{} - serverAPI := &api{ + routerAPI := &api{ method: "DELETE", path: fmt.Sprintf("%s/%s/%s/%s/%d", r.Cfg.Host, consts.VmaasCmpAPIBasePath, consts.NetworksPath, consts.NetworkRouterPath, routerID), @@ -107,7 +107,7 @@ func (r *RouterAPIService) DeleteRouter( return json.Unmarshal(body, &routerResp) }, } - err := serverAPI.do(ctx, nil, nil) + err := routerAPI.do(ctx, nil, nil) return routerResp, err } @@ -117,7 +117,7 @@ func (r *RouterAPIService) GetRouterTypes( queryParams map[string]string, ) (models.GetNetworlRouterTypes, error) { routerResp := models.GetNetworlRouterTypes{} - serverAPI := &api{ + routerAPI := &api{ method: "GET", path: fmt.Sprintf("%s/%s/%s", r.Cfg.Host, consts.VmaasCmpAPIBasePath, consts.NetworkRouterTypePath), @@ -126,7 +126,7 @@ func (r *RouterAPIService) GetRouterTypes( return json.Unmarshal(body, &routerResp) }, } - err := serverAPI.do(ctx, nil, queryParams) + err := routerAPI.do(ctx, nil, queryParams) return routerResp, err } @@ -136,7 +136,7 @@ func (r *RouterAPIService) GetNetworkServices( queryParams map[string]string, ) (models.GetNetworkServicesResp, error) { routerResp := models.GetNetworkServicesResp{} - serverAPI := &api{ + routerAPI := &api{ method: "GET", path: fmt.Sprintf("%s/%s/%s/%s", r.Cfg.Host, consts.VmaasCmpAPIBasePath, consts.NetworksPath, consts.NetworkServicePath), @@ -145,7 +145,7 @@ func (r *RouterAPIService) GetNetworkServices( return json.Unmarshal(body, &routerResp) }, } - err := serverAPI.do(ctx, nil, queryParams) + err := routerAPI.do(ctx, nil, queryParams) return routerResp, err } @@ -156,7 +156,7 @@ func (r *RouterAPIService) CreateRouterNat( request models.CreateRouterNatRequest, ) (models.CreateRouterNatResponse, error) { natResp := models.CreateRouterNatResponse{} - serverAPI := &api{ + routerAPI := &api{ method: "POST", path: fmt.Sprintf("%s/%s/%s/%s/%d/%s", r.Cfg.Host, consts.VmaasCmpAPIBasePath, consts.NetworksPath, consts.NetworkRouterPath, routerID, consts.RoutersNatPath), @@ -165,7 +165,7 @@ func (r *RouterAPIService) CreateRouterNat( return json.Unmarshal(body, &natResp) }, } - err := serverAPI.do(ctx, request, nil) + err := routerAPI.do(ctx, request, nil) return natResp, err } @@ -175,7 +175,7 @@ func (r *RouterAPIService) GetSpecificRouterNat( routerID, natID int, ) (models.GetSpecificRouterNatResponse, error) { natResp := models.GetSpecificRouterNatResponse{} - serverAPI := &api{ + routerAPI := &api{ method: "GET", path: fmt.Sprintf("%s/%s/%s/%s/%d/%s/%d", r.Cfg.Host, consts.VmaasCmpAPIBasePath, consts.NetworksPath, @@ -185,7 +185,7 @@ func (r *RouterAPIService) GetSpecificRouterNat( return json.Unmarshal(body, &natResp) }, } - err := serverAPI.do(ctx, nil, nil) + err := routerAPI.do(ctx, nil, nil) return natResp, err } @@ -196,7 +196,7 @@ func (r *RouterAPIService) UpdateRouterNat( req models.CreateRouterNatRequest, ) (models.CreateRouterNatResponse, error) { natResp := models.CreateRouterNatResponse{} - serverAPI := &api{ + routerAPI := &api{ method: "PUT", path: fmt.Sprintf("%s/%s/%s/%s/%d/%s/%d", r.Cfg.Host, consts.VmaasCmpAPIBasePath, consts.NetworksPath, @@ -206,7 +206,7 @@ func (r *RouterAPIService) UpdateRouterNat( return json.Unmarshal(body, &natResp) }, } - err := serverAPI.do(ctx, req, nil) + err := routerAPI.do(ctx, req, nil) return natResp, err } @@ -216,7 +216,7 @@ func (r *RouterAPIService) DeleteRouterNat( routerID, natID int, ) (models.SuccessOrErrorMessage, error) { natResp := models.SuccessOrErrorMessage{} - serverAPI := &api{ + routerAPI := &api{ method: "DELETE", path: fmt.Sprintf("%s/%s/%s/%s/%d/%s/%d", r.Cfg.Host, consts.VmaasCmpAPIBasePath, consts.NetworksPath, @@ -226,7 +226,67 @@ func (r *RouterAPIService) DeleteRouterNat( return json.Unmarshal(body, &natResp) }, } - err := serverAPI.do(ctx, nil, nil) + err := routerAPI.do(ctx, nil, nil) return natResp, err } + +func (r *RouterAPIService) CreateRouterFirewallRuleGroup( + ctx context.Context, + routerID int, + request models.CreateRouterFirewallRuleGroupRequest, +) (models.CreateRouterFirewallRuleGroupResponse, error) { + firewallGroupResp := models.CreateRouterFirewallRuleGroupResponse{} + routerAPI := &api{ + method: "POST", + path: fmt.Sprintf("%s/%s/%s/%s/%d/%s", r.Cfg.Host, consts.VmaasCmpAPIBasePath, consts.NetworksPath, + consts.NetworkRouterPath, routerID, consts.RoutersFirewallRuleGroupPath), + client: r.Client, + jsonParser: func(body []byte) error { + return json.Unmarshal(body, &firewallGroupResp) + }, + } + err := routerAPI.do(ctx, request, nil) + + return firewallGroupResp, err +} + +func (r *RouterAPIService) GetSpecificRouterFirewallRuleGroup( + ctx context.Context, + routerID, firewallGroupID int, +) (models.GetSpecificRouterFirewallRuleGroupResponse, error) { + firewallGroupResp := models.GetSpecificRouterFirewallRuleGroupResponse{} + routerAPI := &api{ + method: "GET", + path: fmt.Sprintf("%s/%s/%s/%s/%d/%s/%d", + r.Cfg.Host, consts.VmaasCmpAPIBasePath, consts.NetworksPath, + consts.NetworkRouterPath, routerID, consts.RoutersFirewallRuleGroupPath, firewallGroupID), + client: r.Client, + jsonParser: func(body []byte) error { + return json.Unmarshal(body, &firewallGroupResp) + }, + } + err := routerAPI.do(ctx, nil, nil) + + return firewallGroupResp, err +} + +func (r *RouterAPIService) DeleteRouterFirewallRuleGroup( + ctx context.Context, + routerID, firewallGroupID int, +) (models.SuccessOrErrorMessage, error) { + firewallGroupResp := models.SuccessOrErrorMessage{} + routerAPI := &api{ + method: "DELETE", + path: fmt.Sprintf("%s/%s/%s/%s/%d/%s/%d", + r.Cfg.Host, consts.VmaasCmpAPIBasePath, consts.NetworksPath, + consts.NetworkRouterPath, routerID, consts.RoutersFirewallRuleGroupPath, firewallGroupID), + client: r.Client, + jsonParser: func(body []byte) error { + return json.Unmarshal(body, &firewallGroupResp) + }, + } + err := routerAPI.do(ctx, nil, nil) + + return firewallGroupResp, err +} diff --git a/pkg/client/router_test.go b/pkg/client/router_test.go index 53d69f2..e18548f 100644 --- a/pkg/client/router_test.go +++ b/pkg/client/router_test.go @@ -1111,3 +1111,223 @@ func TestRouterAPIService_UpdateRouterNat(t *testing.T) { }) } } + +func TestRouterAPIService_DeleteRouterFirewallRuleGroup(t *testing.T) { + ctrl := gomock.NewController(t) + defer ctrl.Finish() + + type args struct { + routerID int + firewallGroupID int + } + tests := []struct { + name string + args args + given func(m *MockAPIClientHandler) + want models.SuccessOrErrorMessage + wantErr bool + }{ + { + name: "Normal test case 1", + args: args{ + routerID: 1, + firewallGroupID: 2, + }, + given: func(m *MockAPIClientHandler) { + path := mockHost + "/v1beta1/networks/routers/1/firewall-rule-groups/2" + method := "DELETE" + headers := getDefaultHeaders() + req, _ := http.NewRequest(method, path, nil) + respBody := ioutil.NopCloser(bytes.NewReader([]byte(` + { + "success": true + } + `))) + m.EXPECT().prepareRequest(gomock.Any(), path, method, nil, headers, + url.Values{}, url.Values{}, "", nil).Return(req, nil) + + m.EXPECT().callAPI(req).Return(&http.Response{ + StatusCode: 200, + Body: respBody, + }, nil) + }, + want: models.SuccessOrErrorMessage{ + Success: true, + }, + wantErr: false, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + mockAPIClient := NewMockAPIClientHandler(ctrl) + r := RouterAPIService{ + Client: mockAPIClient, + Cfg: Configuration{ + Host: mockHost, + }, + } + tt.given(mockAPIClient) + got, err := r.DeleteRouterFirewallRuleGroup(context.Background(), tt.args.routerID, tt.args.firewallGroupID) + if (err != nil) != tt.wantErr { + t.Errorf("RouterAPIService.DeleteRouterFirewallRuleGroup() error = %v, wantErr %v", err, tt.wantErr) + return + } + if !reflect.DeepEqual(got, tt.want) { + t.Errorf("RouterAPIService.DeleteRouterFirewallRuleGroup() = %v, want %v", got, tt.want) + } + }) + } +} + +func TestRouterAPIService_GetSpecificRouterFirewallRuleGroup(t *testing.T) { + ctrl := gomock.NewController(t) + defer ctrl.Finish() + + type args struct { + routerID int + firewallGroupID int + } + tests := []struct { + name string + args args + given func(m *MockAPIClientHandler) + want models.GetSpecificRouterFirewallRuleGroupResponse + wantErr bool + }{ + { + name: "Normal test case 1", + args: args{ + routerID: 1, + firewallGroupID: 2, + }, + given: func(m *MockAPIClientHandler) { + path := mockHost + "/v1beta1/networks/routers/1/firewall-rule-groups/2" + method := "GET" + headers := getDefaultHeaders() + req, _ := http.NewRequest(method, path, nil) + respBody := ioutil.NopCloser(bytes.NewReader([]byte(` + { + "ruleGroup": { + "id": 1 + } + } + `))) + m.EXPECT().prepareRequest(gomock.Any(), path, method, nil, headers, + url.Values{}, url.Values{}, "", nil).Return(req, nil) + + m.EXPECT().callAPI(req).Return(&http.Response{ + StatusCode: 200, + Body: respBody, + }, nil) + }, + want: models.GetSpecificRouterFirewallRuleGroupResponse{ + GetSpecificRouterFirewallRuleGroup: models.GetSpecificRouterFirewallRuleGroup{ + ID: 1, + }, + }, + wantErr: false, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + mockAPIClient := NewMockAPIClientHandler(ctrl) + r := RouterAPIService{ + Client: mockAPIClient, + Cfg: Configuration{ + Host: mockHost, + }, + } + tt.given(mockAPIClient) + got, err := r.GetSpecificRouterFirewallRuleGroup(context.Background(), tt.args.routerID, tt.args.firewallGroupID) + if (err != nil) != tt.wantErr { + t.Errorf("RouterAPIService.GetSpecificRouterFirewallRuleGroup() error = %v, wantErr %v", err, tt.wantErr) + return + } + if !reflect.DeepEqual(got, tt.want) { + t.Errorf("RouterAPIService.GetSpecificRouterFirewallRuleGroup() = %v, want %v", got, tt.want) + } + }) + } +} + +func TestRouterAPIService_CreateRouterFirewallRuleGroup(t *testing.T) { + ctrl := gomock.NewController(t) + defer ctrl.Finish() + + type args struct { + ctx context.Context + routerID int + request models.CreateRouterFirewallRuleGroupRequest + } + tests := []struct { + name string + args args + given func(m *MockAPIClientHandler) + want models.CreateRouterFirewallRuleGroupResponse + wantErr bool + }{ + { + name: "Normal test case 1", + args: args{ + routerID: 1, + ctx: context.Background(), + request: models.CreateRouterFirewallRuleGroupRequest{ + CreateRouterFirewallRuleGroup: models.CreateRouterFirewallRuleGroup{ + Name: "test_firewall_rule_group", + }, + }, + }, + given: func(m *MockAPIClientHandler) { + path := mockHost + "/v1beta1/networks/routers/1/firewall-rule-groups" + method := "POST" + headers := getDefaultHeaders() + reqModel := models.CreateRouterFirewallRuleGroupRequest{ + CreateRouterFirewallRuleGroup: models.CreateRouterFirewallRuleGroup{ + Name: "test_firewall_rule_group", + }, + } + jsonByte, _ := json.Marshal(reqModel) + postBody := ioutil.NopCloser(bytes.NewReader(jsonByte)) + req, _ := http.NewRequest(method, path, postBody) + respBody := ioutil.NopCloser(bytes.NewReader([]byte(` + { + "success": true, + "id": 2 + } + `))) + m.EXPECT().prepareRequest(gomock.Any(), path, method, reqModel, headers, + url.Values{}, url.Values{}, "", nil).Return(req, nil) + + m.EXPECT().callAPI(req).Return(&http.Response{ + StatusCode: 200, + Body: respBody, + }, nil) + }, + want: models.CreateRouterFirewallRuleGroupResponse{ + IDModel: models.IDModel{ID: 2}, + SuccessOrErrorMessage: models.SuccessOrErrorMessage{Success: true}, + }, + wantErr: false, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + mockAPIClient := NewMockAPIClientHandler(ctrl) + r := RouterAPIService{ + Client: mockAPIClient, + Cfg: Configuration{ + Host: mockHost, + }, + } + tt.given(mockAPIClient) + got, err := r.CreateRouterFirewallRuleGroup(tt.args.ctx, tt.args.routerID, tt.args.request) + if (err != nil) != tt.wantErr { + t.Errorf("RouterAPIService.CreateRouterFirewallRuleGroup() error = %v, wantErr %v", err, tt.wantErr) + return + } + if !reflect.DeepEqual(got, tt.want) { + t.Errorf("RouterAPIService.CreateRouterFirewallRuleGroup() = %v, want %v", got, tt.want) + } + }) + } +} diff --git a/pkg/common/constants.go b/pkg/common/constants.go index d40d223..8fe7863 100644 --- a/pkg/common/constants.go +++ b/pkg/common/constants.go @@ -14,11 +14,12 @@ const ( // NetworksPath NetworksPath = "networks" // NetworkTypePath - NetworkTypePath = "network-types" - NetworkPoolPath = "pools" - NetworkProxyPath = "proxies" - DomainPath = "domains" - RoutersNatPath = "nats" + NetworkTypePath = "network-types" + NetworkPoolPath = "pools" + NetworkProxyPath = "proxies" + DomainPath = "domains" + RoutersNatPath = "nats" + RoutersFirewallRuleGroupPath = "firewall-rule-groups" // LibraryLayoutPath LibraryLayoutPath = "library/layouts" // LibraryInstanceTypesPath diff --git a/pkg/models/router.go b/pkg/models/router.go index 4806cd2..a02d0c1 100644 --- a/pkg/models/router.go +++ b/pkg/models/router.go @@ -227,3 +227,42 @@ type GetSpecificRouterNat struct { LastUpdated string `json:"lastUpdated"` IsDeprecated bool `json:"-" tf:"is_deprecated,computed"` } + +type CreateRouterFirewallRuleGroupRequest struct { + CreateRouterFirewallRuleGroup CreateRouterFirewallRuleGroup `json:"ruleGroup"` +} + +type CreateRouterFirewallRuleGroup struct { + ID int `json:"-" tf:"id,computed"` + Name string `json:"name,omitempty" tf:"name"` + Description string `json:"description,omitempty" tf:"description"` + Priority int `json:"priority" tf:"priority"` + ExternalType string `json:"externalType" tf:"external_type"` + GroupLayer string `json:"groupLayer" tf:"group_layer"` + RouterID int `json:"-" tf:"router_id"` + IsDeprecated bool `json:"-" tf:"is_deprecated"` +} + +type TfCreateRouterFirewallRuleGroup struct { + FirewallRuleGroup []CreateRouterFirewallRuleGroup `tf:"firewall_rule_group"` +} + +type CreateRouterFirewallRuleGroupResponse struct { + IDModel + SuccessOrErrorMessage +} + +type GetSpecificRouterFirewallRuleGroupResponse struct { + GetSpecificRouterFirewallRuleGroup GetSpecificRouterFirewallRuleGroup `json:"ruleGroup"` +} + +type GetSpecificRouterFirewallRuleGroup struct { + ID int `json:"id" tf:"id"` + Name string `json:"name"` + Description string `json:"description"` + ExternalID string `json:"externalId"` + Status string `json:"status"` + Priority int `json:"priority"` + GroupLayer string `json:"groupLayer"` + IsDeprecated bool `json:"-" tf:"is_deprecated,computed"` +}