From e10d1bfb899998220109201510294332645db70a Mon Sep 17 00:00:00 2001 From: Michael Captain Date: Mon, 12 Jul 2021 17:04:30 +0300 Subject: [PATCH] Add mock client and 2 tests for networking package Refactors the Service in the networking package to use a client with CRUD functions for manipulating OpenStack resources. This interface is combined with mockgen code generation to make the package unit testable. Two tests are added to illustrate how writing tests for the package will work. --- go.mod | 1 + go.sum | 3 + pkg/cloud/services/networking/client.go | 282 ++++++++ pkg/cloud/services/networking/floatingip.go | 31 +- .../services/networking/floatingip_test.go | 87 +++ .../services/networking/mock_client/client.go | 605 ++++++++++++++++++ pkg/cloud/services/networking/network.go | 86 +-- pkg/cloud/services/networking/router.go | 67 +- .../services/networking/securitygroups.go | 16 +- pkg/cloud/services/networking/service.go | 12 +- 10 files changed, 1039 insertions(+), 151 deletions(-) create mode 100644 pkg/cloud/services/networking/client.go create mode 100644 pkg/cloud/services/networking/floatingip_test.go create mode 100644 pkg/cloud/services/networking/mock_client/client.go diff --git a/go.mod b/go.mod index d09637baf3..8822ca8e8d 100644 --- a/go.mod +++ b/go.mod @@ -4,6 +4,7 @@ go 1.16 require ( github.com/go-logr/logr v0.4.0 + github.com/golang/mock v1.6.0 github.com/gophercloud/gophercloud v0.16.0 github.com/gophercloud/utils v0.0.0-20210323225332-7b186010c04f github.com/onsi/ginkgo v1.16.4 diff --git a/go.sum b/go.sum index 08452959e1..8703a7df76 100644 --- a/go.sum +++ b/go.sum @@ -410,6 +410,8 @@ github.com/golang/mock v1.4.1/go.mod h1:UOMv5ysSaYNkG+OFQykRIcU/QvvxJf3p21QfJ2Bt github.com/golang/mock v1.4.3/go.mod h1:UOMv5ysSaYNkG+OFQykRIcU/QvvxJf3p21QfJ2Bt3cw= github.com/golang/mock v1.4.4/go.mod h1:l3mdAwkq5BuhzHwde/uurv3sEJeZMXNpwsxVWU71h+4= github.com/golang/mock v1.5.0/go.mod h1:CWnOUgYIOo4TcNZ0wHX3YZCqsaM1I1Jvs6v3mP3KVu8= +github.com/golang/mock v1.6.0 h1:ErTB+efbowRARo13NNdxyJji2egdxLGQhRaY+DUumQc= +github.com/golang/mock v1.6.0/go.mod h1:p6yTPP+5HYm5mzsMV8JkE6ZKdX+/wYM6Hr+LicevLPs= github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= @@ -1178,6 +1180,7 @@ golang.org/x/tools v0.0.0-20201224043029-2b0845dc783e/go.mod h1:emZCQorbCU4vsT4f golang.org/x/tools v0.0.0-20210105154028-b0ab187a4818/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA= golang.org/x/tools v0.0.0-20210106214847-113979e3529a/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA= golang.org/x/tools v0.1.0/go.mod h1:xkSsbof2nBLbhDlRMhhhyNLN/zl3eTqcnHD5viDpcZ0= +golang.org/x/tools v0.1.1/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk= golang.org/x/tools v0.1.2 h1:kRBLX7v7Af8W7Gdbbc908OJcdgtK8bOz9Uaj8/F1ACA= golang.org/x/tools v0.1.2/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk= golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= diff --git a/pkg/cloud/services/networking/client.go b/pkg/cloud/services/networking/client.go new file mode 100644 index 0000000000..dff7b93c33 --- /dev/null +++ b/pkg/cloud/services/networking/client.go @@ -0,0 +1,282 @@ +/* +Copyright 2021 The Kubernetes Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package networking + +//go:generate mockgen -destination=mock_client/client.go -package=mock_client sigs.k8s.io/cluster-api-provider-openstack/pkg/cloud/services/networking NetworkClient +//go:generate /usr/bin/env bash -c "cat ../../../../hack/boilerplate/boilerplate.generatego.txt mock_client/client.go > mock_client/_client.go && mv mock_client/_client.go mock_client/client.go" + +import ( + "github.com/gophercloud/gophercloud" + "github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/attributestags" + "github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/layer3/floatingips" + "github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/layer3/routers" + "github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/security/groups" + "github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/security/rules" + "github.com/gophercloud/gophercloud/openstack/networking/v2/networks" + "github.com/gophercloud/gophercloud/openstack/networking/v2/ports" + "github.com/gophercloud/gophercloud/openstack/networking/v2/subnets" + + "sigs.k8s.io/cluster-api-provider-openstack/pkg/metrics" +) + +type NetworkClient interface { + ListFloatingIP(opts floatingips.ListOptsBuilder) ([]floatingips.FloatingIP, error) + CreateFloatingIP(opts floatingips.CreateOptsBuilder) (*floatingips.FloatingIP, error) + DeleteFloatingIP(id string) error + GetFloatingIP(id string) (*floatingips.FloatingIP, error) + UpdateFloatingIP(id string, opts floatingips.UpdateOptsBuilder) (*floatingips.FloatingIP, error) + + ListPort(opts ports.ListOptsBuilder) ([]ports.Port, error) + CreatePort(opts ports.CreateOptsBuilder) (*ports.Port, error) + DeletePort(id string) error + GetPort(id string) (*ports.Port, error) + UpdatePort(id string, opts ports.UpdateOptsBuilder) (*ports.Port, error) + + ListRouter(opts routers.ListOpts) ([]routers.Router, error) + CreateRouter(opts routers.CreateOptsBuilder) (*routers.Router, error) + DeleteRouter(id string) error + GetRouter(id string) (*routers.Router, error) + UpdateRouter(id string, opts routers.UpdateOptsBuilder) (*routers.Router, error) + AddRouterInterface(id string, opts routers.AddInterfaceOptsBuilder) (*routers.InterfaceInfo, error) + RemoveRouterInterface(id string, opts routers.RemoveInterfaceOptsBuilder) (*routers.InterfaceInfo, error) + + ListSecGroup(opts groups.ListOpts) ([]groups.SecGroup, error) + CreateSecGroup(opts groups.CreateOptsBuilder) (*groups.SecGroup, error) + DeleteSecGroup(id string) error + GetSecGroup(id string) (*groups.SecGroup, error) + UpdateSecGroup(id string, opts groups.UpdateOptsBuilder) (*groups.SecGroup, error) + + ListSecGroupRule(opts rules.ListOpts) ([]rules.SecGroupRule, error) + CreateSecGroupRule(opts rules.CreateOptsBuilder) (*rules.SecGroupRule, error) + DeleteSecGroupRule(id string) error + GetSecGroupRule(id string) (*rules.SecGroupRule, error) + + ListNetwork(opts networks.ListOptsBuilder) ([]networks.Network, error) + CreateNetwork(opts networks.CreateOptsBuilder) (*networks.Network, error) + DeleteNetwork(id string) error + GetNetwork(id string) (*networks.Network, error) + UpdateNetwork(id string, opts networks.UpdateOptsBuilder) (*networks.Network, error) + + ListSubnet(opts subnets.ListOptsBuilder) ([]subnets.Subnet, error) + CreateSubnet(opts subnets.CreateOptsBuilder) (*subnets.Subnet, error) + DeleteSubnet(id string) error + GetSubnet(id string) (*subnets.Subnet, error) + UpdateSubnet(id string, opts subnets.UpdateOptsBuilder) (*subnets.Subnet, error) + + ReplaceAllAttributesTags(resourceType string, resourceID string, opts attributestags.ReplaceAllOptsBuilder) ([]string, error) +} + +type networkClient struct { + serviceClient *gophercloud.ServiceClient +} + +func (c networkClient) AddRouterInterface(id string, opts routers.AddInterfaceOptsBuilder) (*routers.InterfaceInfo, error) { + mc := metrics.NewMetricPrometheusContext("server_os_interface", "create") + interfaceInfo, err := routers.AddInterface(c.serviceClient, id, opts).Extract() + if mc.ObserveRequest(err) != nil { + return nil, err + } + return interfaceInfo, nil +} + +func (c networkClient) RemoveRouterInterface(id string, opts routers.RemoveInterfaceOptsBuilder) (*routers.InterfaceInfo, error) { + mc := metrics.NewMetricPrometheusContext("server_os_interface", "delete") + interfaceInfo, err := routers.RemoveInterface(c.serviceClient, id, opts).Extract() + if mc.ObserveRequestIgnoreNotFound(err) != nil { + return nil, err + } + return interfaceInfo, nil +} + +func (c networkClient) ReplaceAllAttributesTags(resourceType string, resourceID string, opts attributestags.ReplaceAllOptsBuilder) ([]string, error) { + return attributestags.ReplaceAll(c.serviceClient, resourceType, resourceID, opts).Extract() +} + +func (c networkClient) ListRouter(opts routers.ListOpts) ([]routers.Router, error) { + mc := metrics.NewMetricPrometheusContext("router", "list") + allPages, err := routers.List(c.serviceClient, opts).AllPages() + if mc.ObserveRequest(err) != nil { + return nil, err + } + return routers.ExtractRouters(allPages) +} + +func (c networkClient) ListFloatingIP(opts floatingips.ListOptsBuilder) ([]floatingips.FloatingIP, error) { + mc := metrics.NewMetricPrometheusContext("floating_ip", "list") + allPages, err := floatingips.List(c.serviceClient, opts).AllPages() + if mc.ObserveRequest(err) != nil { + return nil, err + } + return floatingips.ExtractFloatingIPs(allPages) +} + +func (c networkClient) CreateFloatingIP(opts floatingips.CreateOptsBuilder) (*floatingips.FloatingIP, error) { + return floatingips.Create(c.serviceClient, opts).Extract() +} + +func (c networkClient) DeleteFloatingIP(id string) error { + return floatingips.Delete(c.serviceClient, id).ExtractErr() +} + +func (c networkClient) GetFloatingIP(id string) (*floatingips.FloatingIP, error) { + mc := metrics.NewMetricPrometheusContext("floating_ip", "list") + fip, err := floatingips.Get(c.serviceClient, id).Extract() + if mc.ObserveRequest(err) != nil { + return nil, err + } + return fip, nil +} + +func (c networkClient) UpdateFloatingIP(id string, opts floatingips.UpdateOptsBuilder) (*floatingips.FloatingIP, error) { + return floatingips.Update(c.serviceClient, id, opts).Extract() +} + +func (c networkClient) ListPort(opts ports.ListOptsBuilder) ([]ports.Port, error) { + mc := metrics.NewMetricPrometheusContext("port", "list") + allPages, err := ports.List(c.serviceClient, opts).AllPages() + if mc.ObserveRequest(err) != nil { + return nil, err + } + return ports.ExtractPorts(allPages) +} + +func (c networkClient) CreatePort(opts ports.CreateOptsBuilder) (*ports.Port, error) { + return ports.Create(c.serviceClient, opts).Extract() +} + +func (c networkClient) DeletePort(id string) error { + return ports.Delete(c.serviceClient, id).ExtractErr() +} + +func (c networkClient) GetPort(id string) (*ports.Port, error) { + return ports.Get(c.serviceClient, id).Extract() +} + +func (c networkClient) UpdatePort(id string, opts ports.UpdateOptsBuilder) (*ports.Port, error) { + return ports.Update(c.serviceClient, id, opts).Extract() +} + +func (c networkClient) CreateRouter(opts routers.CreateOptsBuilder) (*routers.Router, error) { + return routers.Create(c.serviceClient, opts).Extract() +} + +func (c networkClient) DeleteRouter(id string) error { + return routers.Delete(c.serviceClient, id).ExtractErr() +} + +func (c networkClient) GetRouter(id string) (*routers.Router, error) { + return routers.Get(c.serviceClient, id).Extract() +} + +func (c networkClient) UpdateRouter(id string, opts routers.UpdateOptsBuilder) (*routers.Router, error) { + return routers.Update(c.serviceClient, id, opts).Extract() +} + +func (c networkClient) ListSecGroup(opts groups.ListOpts) ([]groups.SecGroup, error) { + mc := metrics.NewMetricPrometheusContext("group", "list") + allPages, err := groups.List(c.serviceClient, opts).AllPages() + if mc.ObserveRequest(err) != nil { + return nil, err + } + return groups.ExtractGroups(allPages) +} + +func (c networkClient) CreateSecGroup(opts groups.CreateOptsBuilder) (*groups.SecGroup, error) { + return groups.Create(c.serviceClient, opts).Extract() +} + +func (c networkClient) DeleteSecGroup(id string) error { + return groups.Delete(c.serviceClient, id).ExtractErr() +} + +func (c networkClient) GetSecGroup(id string) (*groups.SecGroup, error) { + return groups.Get(c.serviceClient, id).Extract() +} + +func (c networkClient) UpdateSecGroup(id string, opts groups.UpdateOptsBuilder) (*groups.SecGroup, error) { + return groups.Update(c.serviceClient, id, opts).Extract() +} + +func (c networkClient) ListSecGroupRule(opts rules.ListOpts) ([]rules.SecGroupRule, error) { + allPages, err := rules.List(c.serviceClient, opts).AllPages() + if err != nil { + return nil, err + } + return rules.ExtractRules(allPages) +} + +func (c networkClient) CreateSecGroupRule(opts rules.CreateOptsBuilder) (*rules.SecGroupRule, error) { + return rules.Create(c.serviceClient, opts).Extract() +} + +func (c networkClient) DeleteSecGroupRule(id string) error { + return rules.Delete(c.serviceClient, id).ExtractErr() +} + +func (c networkClient) GetSecGroupRule(id string) (*rules.SecGroupRule, error) { + return rules.Get(c.serviceClient, id).Extract() +} + +func (c networkClient) ListNetwork(opts networks.ListOptsBuilder) ([]networks.Network, error) { + mc := metrics.NewMetricPrometheusContext("network", "list") + allPages, err := networks.List(c.serviceClient, opts).AllPages() + if mc.ObserveRequest(err) != nil { + return nil, err + } + return networks.ExtractNetworks(allPages) +} + +func (c networkClient) CreateNetwork(opts networks.CreateOptsBuilder) (*networks.Network, error) { + return networks.Create(c.serviceClient, opts).Extract() +} + +func (c networkClient) DeleteNetwork(id string) error { + return networks.Delete(c.serviceClient, id).ExtractErr() +} + +func (c networkClient) GetNetwork(id string) (*networks.Network, error) { + return networks.Get(c.serviceClient, id).Extract() +} + +func (c networkClient) UpdateNetwork(id string, opts networks.UpdateOptsBuilder) (*networks.Network, error) { + return networks.Update(c.serviceClient, id, opts).Extract() +} + +func (c networkClient) ListSubnet(opts subnets.ListOptsBuilder) ([]subnets.Subnet, error) { + mc := metrics.NewMetricPrometheusContext("subnet", "list") + allPages, err := subnets.List(c.serviceClient, opts).AllPages() + if mc.ObserveRequest(err) != nil { + return nil, err + } + return subnets.ExtractSubnets(allPages) +} + +func (c networkClient) CreateSubnet(opts subnets.CreateOptsBuilder) (*subnets.Subnet, error) { + return subnets.Create(c.serviceClient, opts).Extract() +} + +func (c networkClient) DeleteSubnet(id string) error { + return subnets.Delete(c.serviceClient, id).ExtractErr() +} + +func (c networkClient) GetSubnet(id string) (*subnets.Subnet, error) { + return subnets.Get(c.serviceClient, id).Extract() +} + +func (c networkClient) UpdateSubnet(id string, opts subnets.UpdateOptsBuilder) (*subnets.Subnet, error) { + return subnets.Update(c.serviceClient, id, opts).Extract() +} diff --git a/pkg/cloud/services/networking/floatingip.go b/pkg/cloud/services/networking/floatingip.go index 6a1147d3b4..a0feae853f 100644 --- a/pkg/cloud/services/networking/floatingip.go +++ b/pkg/cloud/services/networking/floatingip.go @@ -50,7 +50,7 @@ func (s *Service) GetOrCreateFloatingIP(openStackCluster *infrav1.OpenStackClust fpCreateOpts.Description = names.GetDescription(clusterName) mc := metrics.NewMetricPrometheusContext("floating_ip", "create") - fp, err = floatingips.Create(s.client, fpCreateOpts).Extract() + fp, err = s.client.CreateFloatingIP(fpCreateOpts) if mc.ObserveRequest(err) != nil { record.Warnf(openStackCluster, "FailedCreateFloatingIP", "Failed to create floating IP %s: %v", ip, err) return nil, err @@ -58,9 +58,9 @@ func (s *Service) GetOrCreateFloatingIP(openStackCluster *infrav1.OpenStackClust if len(openStackCluster.Spec.Tags) > 0 { mc := metrics.NewMetricPrometheusContext("floating_ip", "update") - _, err = attributestags.ReplaceAll(s.client, "floatingips", fp.ID, attributestags.ReplaceAllOpts{ + _, err = s.client.ReplaceAllAttributesTags("floatingips", fp.ID, attributestags.ReplaceAllOpts{ Tags: openStackCluster.Spec.Tags, - }).Extract() + }) if mc.ObserveRequest(err) != nil { return nil, err } @@ -71,12 +71,7 @@ func (s *Service) GetOrCreateFloatingIP(openStackCluster *infrav1.OpenStackClust } func (s *Service) checkIfFloatingIPExists(ip string) (*floatingips.FloatingIP, error) { - mc := metrics.NewMetricPrometheusContext("floating_ip", "list") - allPages, err := floatingips.List(s.client, floatingips.ListOpts{FloatingIP: ip}).AllPages() - if mc.ObserveRequest(err) != nil { - return nil, err - } - fpList, err := floatingips.ExtractFloatingIPs(allPages) + fpList, err := s.client.ListFloatingIP(floatingips.ListOpts{FloatingIP: ip}) if err != nil { return nil, err } @@ -87,12 +82,7 @@ func (s *Service) checkIfFloatingIPExists(ip string) (*floatingips.FloatingIP, e } func (s *Service) GetFloatingIPByPortID(portID string) (*floatingips.FloatingIP, error) { - mc := metrics.NewMetricPrometheusContext("floating_ip", "list") - allPages, err := floatingips.List(s.client, floatingips.ListOpts{PortID: portID}).AllPages() - if mc.ObserveRequest(err) != nil { - return nil, err - } - fpList, err := floatingips.ExtractFloatingIPs(allPages) + fpList, err := s.client.ListFloatingIP(floatingips.ListOpts{PortID: portID}) if err != nil { return nil, err } @@ -113,7 +103,7 @@ func (s *Service) DeleteFloatingIP(openStackCluster *infrav1.OpenStackCluster, i } mc := metrics.NewMetricPrometheusContext("floating_ip", "delete") - err = floatingips.Delete(s.client, fip.ID).ExtractErr() + err = s.client.DeleteFloatingIP(fip.ID) if mc.ObserveRequest(err) != nil { record.Warnf(openStackCluster, "FailedDeleteFloatingIP", "Failed to delete floating IP %s: %v", ip, err) return err @@ -138,7 +128,7 @@ func (s *Service) AssociateFloatingIP(openStackCluster *infrav1.OpenStackCluster } mc := metrics.NewMetricPrometheusContext("floating_ip", "update") - _, err := floatingips.Update(s.client, fp.ID, fpUpdateOpts).Extract() + _, err := s.client.UpdateFloatingIP(fp.ID, fpUpdateOpts) if mc.ObserveRequest(err) != nil { record.Warnf(openStackCluster, "FailedAssociateFloatingIP", "Failed to associate floating IP %s with port %s: %v", fp.FloatingIP, portID, err) return err @@ -170,7 +160,7 @@ func (s *Service) DisassociateFloatingIP(openStackCluster *infrav1.OpenStackClus } mc := metrics.NewMetricPrometheusContext("floating_ip", "update") - _, err = floatingips.Update(s.client, fip.ID, fpUpdateOpts).Extract() + _, err = s.client.UpdateFloatingIP(fip.ID, fpUpdateOpts) if mc.ObserveRequest(err) != nil { record.Warnf(openStackCluster, "FailedDisassociateFloatingIP", "Failed to disassociate floating IP %s: %v", fip.FloatingIP, err) return err @@ -188,9 +178,8 @@ func (s *Service) DisassociateFloatingIP(openStackCluster *infrav1.OpenStackClus func (s *Service) waitForFloatingIP(id, target string) error { s.logger.Info("Waiting for floating IP", "id", id, "targetStatus", target) return wait.ExponentialBackoff(backoff, func() (bool, error) { - mc := metrics.NewMetricPrometheusContext("floating_ip", "get") - fip, err := floatingips.Get(s.client, id).Extract() - if mc.ObserveRequest(err) != nil { + fip, err := s.client.GetFloatingIP(id) + if err != nil { return false, err } return fip.Status == target, nil diff --git a/pkg/cloud/services/networking/floatingip_test.go b/pkg/cloud/services/networking/floatingip_test.go new file mode 100644 index 0000000000..68037cdd56 --- /dev/null +++ b/pkg/cloud/services/networking/floatingip_test.go @@ -0,0 +1,87 @@ +/* +Copyright 2021 The Kubernetes Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package networking + +import ( + "testing" + + "github.com/go-logr/logr" + "github.com/golang/mock/gomock" + "github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/layer3/floatingips" + . "github.com/onsi/gomega" + + infrav1 "sigs.k8s.io/cluster-api-provider-openstack/api/v1alpha4" + "sigs.k8s.io/cluster-api-provider-openstack/pkg/cloud/services/networking/mock_client" +) + +func Test_GetOrCreateFloatingIP(t *testing.T) { + mockCtrl := gomock.NewController(t) + defer mockCtrl.Finish() + + tests := []struct { + name string + ip string + expect func(m *mock_client.MockNetworkClientMockRecorder) + want *floatingips.FloatingIP + }{ + { + name: "creates floating IP when one doesn't already exist", + ip: "192.168.111.0", + expect: func(m *mock_client.MockNetworkClientMockRecorder) { + m. + ListFloatingIP(floatingips.ListOpts{FloatingIP: "192.168.111.0"}). + Return([]floatingips.FloatingIP{}, nil) + m. + CreateFloatingIP(floatingips.CreateOpts{ + FloatingIP: "192.168.111.0", + Description: "Created by cluster-api-provider-openstack cluster test-cluster", + }). + Return(&floatingips.FloatingIP{FloatingIP: "192.168.111.0"}, nil) + }, + want: &floatingips.FloatingIP{FloatingIP: "192.168.111.0"}, + }, + { + name: "finds existing floating IP where one exists", + ip: "192.168.111.0", + expect: func(m *mock_client.MockNetworkClientMockRecorder) { + m. + ListFloatingIP(floatingips.ListOpts{FloatingIP: "192.168.111.0"}). + Return([]floatingips.FloatingIP{{FloatingIP: "192.168.111.0"}}, nil) + }, + want: &floatingips.FloatingIP{FloatingIP: "192.168.111.0"}, + }, + } + openStackCluster := &infrav1.OpenStackCluster{Status: infrav1.OpenStackClusterStatus{ + ExternalNetwork: &infrav1.Network{ + ID: "", + }, + }} + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + g := NewWithT(t) + mockClient := mock_client.NewMockNetworkClient(mockCtrl) + tt.expect(mockClient.EXPECT()) + s := Service{ + client: mockClient, + logger: logr.DiscardLogger{}, + } + got, err := s.GetOrCreateFloatingIP(openStackCluster, "test-cluster", tt.ip) + g.Expect(err).ShouldNot(HaveOccurred()) + g.Expect(got).To(Equal(tt.want)) + }) + } +} diff --git a/pkg/cloud/services/networking/mock_client/client.go b/pkg/cloud/services/networking/mock_client/client.go new file mode 100644 index 0000000000..b6739a5da4 --- /dev/null +++ b/pkg/cloud/services/networking/mock_client/client.go @@ -0,0 +1,605 @@ +/* +Copyright 2021 The Kubernetes Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +// Code generated by MockGen. DO NOT EDIT. +// Source: sigs.k8s.io/cluster-api-provider-openstack/pkg/cloud/services/networking (interfaces: NetworkClient) + +// Package mock_client is a generated GoMock package. +package mock_client + +import ( + gomock "github.com/golang/mock/gomock" + attributestags "github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/attributestags" + floatingips "github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/layer3/floatingips" + routers "github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/layer3/routers" + groups "github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/security/groups" + rules "github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/security/rules" + networks "github.com/gophercloud/gophercloud/openstack/networking/v2/networks" + ports "github.com/gophercloud/gophercloud/openstack/networking/v2/ports" + subnets "github.com/gophercloud/gophercloud/openstack/networking/v2/subnets" + reflect "reflect" +) + +// MockNetworkClient is a mock of NetworkClient interface +type MockNetworkClient struct { + ctrl *gomock.Controller + recorder *MockNetworkClientMockRecorder +} + +// MockNetworkClientMockRecorder is the mock recorder for MockNetworkClient +type MockNetworkClientMockRecorder struct { + mock *MockNetworkClient +} + +// NewMockNetworkClient creates a new mock instance +func NewMockNetworkClient(ctrl *gomock.Controller) *MockNetworkClient { + mock := &MockNetworkClient{ctrl: ctrl} + mock.recorder = &MockNetworkClientMockRecorder{mock} + return mock +} + +// EXPECT returns an object that allows the caller to indicate expected use +func (m *MockNetworkClient) EXPECT() *MockNetworkClientMockRecorder { + return m.recorder +} + +// AddRouterInterface mocks base method +func (m *MockNetworkClient) AddRouterInterface(arg0 string, arg1 routers.AddInterfaceOptsBuilder) (*routers.InterfaceInfo, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "AddRouterInterface", arg0, arg1) + ret0, _ := ret[0].(*routers.InterfaceInfo) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// AddRouterInterface indicates an expected call of AddRouterInterface +func (mr *MockNetworkClientMockRecorder) AddRouterInterface(arg0, arg1 interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "AddRouterInterface", reflect.TypeOf((*MockNetworkClient)(nil).AddRouterInterface), arg0, arg1) +} + +// CreateFloatingIP mocks base method +func (m *MockNetworkClient) CreateFloatingIP(arg0 floatingips.CreateOptsBuilder) (*floatingips.FloatingIP, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "CreateFloatingIP", arg0) + ret0, _ := ret[0].(*floatingips.FloatingIP) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// CreateFloatingIP indicates an expected call of CreateFloatingIP +func (mr *MockNetworkClientMockRecorder) CreateFloatingIP(arg0 interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "CreateFloatingIP", reflect.TypeOf((*MockNetworkClient)(nil).CreateFloatingIP), arg0) +} + +// CreateNetwork mocks base method +func (m *MockNetworkClient) CreateNetwork(arg0 networks.CreateOptsBuilder) (*networks.Network, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "CreateNetwork", arg0) + ret0, _ := ret[0].(*networks.Network) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// CreateNetwork indicates an expected call of CreateNetwork +func (mr *MockNetworkClientMockRecorder) CreateNetwork(arg0 interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "CreateNetwork", reflect.TypeOf((*MockNetworkClient)(nil).CreateNetwork), arg0) +} + +// CreatePort mocks base method +func (m *MockNetworkClient) CreatePort(arg0 ports.CreateOptsBuilder) (*ports.Port, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "CreatePort", arg0) + ret0, _ := ret[0].(*ports.Port) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// CreatePort indicates an expected call of CreatePort +func (mr *MockNetworkClientMockRecorder) CreatePort(arg0 interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "CreatePort", reflect.TypeOf((*MockNetworkClient)(nil).CreatePort), arg0) +} + +// CreateRouter mocks base method +func (m *MockNetworkClient) CreateRouter(arg0 routers.CreateOptsBuilder) (*routers.Router, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "CreateRouter", arg0) + ret0, _ := ret[0].(*routers.Router) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// CreateRouter indicates an expected call of CreateRouter +func (mr *MockNetworkClientMockRecorder) CreateRouter(arg0 interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "CreateRouter", reflect.TypeOf((*MockNetworkClient)(nil).CreateRouter), arg0) +} + +// CreateSecGroup mocks base method +func (m *MockNetworkClient) CreateSecGroup(arg0 groups.CreateOptsBuilder) (*groups.SecGroup, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "CreateSecGroup", arg0) + ret0, _ := ret[0].(*groups.SecGroup) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// CreateSecGroup indicates an expected call of CreateSecGroup +func (mr *MockNetworkClientMockRecorder) CreateSecGroup(arg0 interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "CreateSecGroup", reflect.TypeOf((*MockNetworkClient)(nil).CreateSecGroup), arg0) +} + +// CreateSecGroupRule mocks base method +func (m *MockNetworkClient) CreateSecGroupRule(arg0 rules.CreateOptsBuilder) (*rules.SecGroupRule, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "CreateSecGroupRule", arg0) + ret0, _ := ret[0].(*rules.SecGroupRule) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// CreateSecGroupRule indicates an expected call of CreateSecGroupRule +func (mr *MockNetworkClientMockRecorder) CreateSecGroupRule(arg0 interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "CreateSecGroupRule", reflect.TypeOf((*MockNetworkClient)(nil).CreateSecGroupRule), arg0) +} + +// CreateSubnet mocks base method +func (m *MockNetworkClient) CreateSubnet(arg0 subnets.CreateOptsBuilder) (*subnets.Subnet, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "CreateSubnet", arg0) + ret0, _ := ret[0].(*subnets.Subnet) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// CreateSubnet indicates an expected call of CreateSubnet +func (mr *MockNetworkClientMockRecorder) CreateSubnet(arg0 interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "CreateSubnet", reflect.TypeOf((*MockNetworkClient)(nil).CreateSubnet), arg0) +} + +// DeleteFloatingIP mocks base method +func (m *MockNetworkClient) DeleteFloatingIP(arg0 string) error { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "DeleteFloatingIP", arg0) + ret0, _ := ret[0].(error) + return ret0 +} + +// DeleteFloatingIP indicates an expected call of DeleteFloatingIP +func (mr *MockNetworkClientMockRecorder) DeleteFloatingIP(arg0 interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "DeleteFloatingIP", reflect.TypeOf((*MockNetworkClient)(nil).DeleteFloatingIP), arg0) +} + +// DeleteNetwork mocks base method +func (m *MockNetworkClient) DeleteNetwork(arg0 string) error { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "DeleteNetwork", arg0) + ret0, _ := ret[0].(error) + return ret0 +} + +// DeleteNetwork indicates an expected call of DeleteNetwork +func (mr *MockNetworkClientMockRecorder) DeleteNetwork(arg0 interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "DeleteNetwork", reflect.TypeOf((*MockNetworkClient)(nil).DeleteNetwork), arg0) +} + +// DeletePort mocks base method +func (m *MockNetworkClient) DeletePort(arg0 string) error { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "DeletePort", arg0) + ret0, _ := ret[0].(error) + return ret0 +} + +// DeletePort indicates an expected call of DeletePort +func (mr *MockNetworkClientMockRecorder) DeletePort(arg0 interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "DeletePort", reflect.TypeOf((*MockNetworkClient)(nil).DeletePort), arg0) +} + +// DeleteRouter mocks base method +func (m *MockNetworkClient) DeleteRouter(arg0 string) error { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "DeleteRouter", arg0) + ret0, _ := ret[0].(error) + return ret0 +} + +// DeleteRouter indicates an expected call of DeleteRouter +func (mr *MockNetworkClientMockRecorder) DeleteRouter(arg0 interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "DeleteRouter", reflect.TypeOf((*MockNetworkClient)(nil).DeleteRouter), arg0) +} + +// DeleteSecGroup mocks base method +func (m *MockNetworkClient) DeleteSecGroup(arg0 string) error { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "DeleteSecGroup", arg0) + ret0, _ := ret[0].(error) + return ret0 +} + +// DeleteSecGroup indicates an expected call of DeleteSecGroup +func (mr *MockNetworkClientMockRecorder) DeleteSecGroup(arg0 interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "DeleteSecGroup", reflect.TypeOf((*MockNetworkClient)(nil).DeleteSecGroup), arg0) +} + +// DeleteSecGroupRule mocks base method +func (m *MockNetworkClient) DeleteSecGroupRule(arg0 string) error { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "DeleteSecGroupRule", arg0) + ret0, _ := ret[0].(error) + return ret0 +} + +// DeleteSecGroupRule indicates an expected call of DeleteSecGroupRule +func (mr *MockNetworkClientMockRecorder) DeleteSecGroupRule(arg0 interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "DeleteSecGroupRule", reflect.TypeOf((*MockNetworkClient)(nil).DeleteSecGroupRule), arg0) +} + +// DeleteSubnet mocks base method +func (m *MockNetworkClient) DeleteSubnet(arg0 string) error { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "DeleteSubnet", arg0) + ret0, _ := ret[0].(error) + return ret0 +} + +// DeleteSubnet indicates an expected call of DeleteSubnet +func (mr *MockNetworkClientMockRecorder) DeleteSubnet(arg0 interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "DeleteSubnet", reflect.TypeOf((*MockNetworkClient)(nil).DeleteSubnet), arg0) +} + +// GetFloatingIP mocks base method +func (m *MockNetworkClient) GetFloatingIP(arg0 string) (*floatingips.FloatingIP, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "GetFloatingIP", arg0) + ret0, _ := ret[0].(*floatingips.FloatingIP) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// GetFloatingIP indicates an expected call of GetFloatingIP +func (mr *MockNetworkClientMockRecorder) GetFloatingIP(arg0 interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetFloatingIP", reflect.TypeOf((*MockNetworkClient)(nil).GetFloatingIP), arg0) +} + +// GetNetwork mocks base method +func (m *MockNetworkClient) GetNetwork(arg0 string) (*networks.Network, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "GetNetwork", arg0) + ret0, _ := ret[0].(*networks.Network) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// GetNetwork indicates an expected call of GetNetwork +func (mr *MockNetworkClientMockRecorder) GetNetwork(arg0 interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetNetwork", reflect.TypeOf((*MockNetworkClient)(nil).GetNetwork), arg0) +} + +// GetPort mocks base method +func (m *MockNetworkClient) GetPort(arg0 string) (*ports.Port, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "GetPort", arg0) + ret0, _ := ret[0].(*ports.Port) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// GetPort indicates an expected call of GetPort +func (mr *MockNetworkClientMockRecorder) GetPort(arg0 interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetPort", reflect.TypeOf((*MockNetworkClient)(nil).GetPort), arg0) +} + +// GetRouter mocks base method +func (m *MockNetworkClient) GetRouter(arg0 string) (*routers.Router, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "GetRouter", arg0) + ret0, _ := ret[0].(*routers.Router) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// GetRouter indicates an expected call of GetRouter +func (mr *MockNetworkClientMockRecorder) GetRouter(arg0 interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetRouter", reflect.TypeOf((*MockNetworkClient)(nil).GetRouter), arg0) +} + +// GetSecGroup mocks base method +func (m *MockNetworkClient) GetSecGroup(arg0 string) (*groups.SecGroup, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "GetSecGroup", arg0) + ret0, _ := ret[0].(*groups.SecGroup) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// GetSecGroup indicates an expected call of GetSecGroup +func (mr *MockNetworkClientMockRecorder) GetSecGroup(arg0 interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetSecGroup", reflect.TypeOf((*MockNetworkClient)(nil).GetSecGroup), arg0) +} + +// GetSecGroupRule mocks base method +func (m *MockNetworkClient) GetSecGroupRule(arg0 string) (*rules.SecGroupRule, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "GetSecGroupRule", arg0) + ret0, _ := ret[0].(*rules.SecGroupRule) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// GetSecGroupRule indicates an expected call of GetSecGroupRule +func (mr *MockNetworkClientMockRecorder) GetSecGroupRule(arg0 interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetSecGroupRule", reflect.TypeOf((*MockNetworkClient)(nil).GetSecGroupRule), arg0) +} + +// GetSubnet mocks base method +func (m *MockNetworkClient) GetSubnet(arg0 string) (*subnets.Subnet, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "GetSubnet", arg0) + ret0, _ := ret[0].(*subnets.Subnet) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// GetSubnet indicates an expected call of GetSubnet +func (mr *MockNetworkClientMockRecorder) GetSubnet(arg0 interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetSubnet", reflect.TypeOf((*MockNetworkClient)(nil).GetSubnet), arg0) +} + +// ListFloatingIP mocks base method +func (m *MockNetworkClient) ListFloatingIP(arg0 floatingips.ListOptsBuilder) ([]floatingips.FloatingIP, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "ListFloatingIP", arg0) + ret0, _ := ret[0].([]floatingips.FloatingIP) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// ListFloatingIP indicates an expected call of ListFloatingIP +func (mr *MockNetworkClientMockRecorder) ListFloatingIP(arg0 interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ListFloatingIP", reflect.TypeOf((*MockNetworkClient)(nil).ListFloatingIP), arg0) +} + +// ListNetwork mocks base method +func (m *MockNetworkClient) ListNetwork(arg0 networks.ListOptsBuilder) ([]networks.Network, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "ListNetwork", arg0) + ret0, _ := ret[0].([]networks.Network) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// ListNetwork indicates an expected call of ListNetwork +func (mr *MockNetworkClientMockRecorder) ListNetwork(arg0 interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ListNetwork", reflect.TypeOf((*MockNetworkClient)(nil).ListNetwork), arg0) +} + +// ListPort mocks base method +func (m *MockNetworkClient) ListPort(arg0 ports.ListOptsBuilder) ([]ports.Port, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "ListPort", arg0) + ret0, _ := ret[0].([]ports.Port) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// ListPort indicates an expected call of ListPort +func (mr *MockNetworkClientMockRecorder) ListPort(arg0 interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ListPort", reflect.TypeOf((*MockNetworkClient)(nil).ListPort), arg0) +} + +// ListRouter mocks base method +func (m *MockNetworkClient) ListRouter(arg0 routers.ListOpts) ([]routers.Router, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "ListRouter", arg0) + ret0, _ := ret[0].([]routers.Router) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// ListRouter indicates an expected call of ListRouter +func (mr *MockNetworkClientMockRecorder) ListRouter(arg0 interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ListRouter", reflect.TypeOf((*MockNetworkClient)(nil).ListRouter), arg0) +} + +// ListSecGroup mocks base method +func (m *MockNetworkClient) ListSecGroup(arg0 groups.ListOpts) ([]groups.SecGroup, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "ListSecGroup", arg0) + ret0, _ := ret[0].([]groups.SecGroup) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// ListSecGroup indicates an expected call of ListSecGroup +func (mr *MockNetworkClientMockRecorder) ListSecGroup(arg0 interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ListSecGroup", reflect.TypeOf((*MockNetworkClient)(nil).ListSecGroup), arg0) +} + +// ListSecGroupRule mocks base method +func (m *MockNetworkClient) ListSecGroupRule(arg0 rules.ListOpts) ([]rules.SecGroupRule, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "ListSecGroupRule", arg0) + ret0, _ := ret[0].([]rules.SecGroupRule) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// ListSecGroupRule indicates an expected call of ListSecGroupRule +func (mr *MockNetworkClientMockRecorder) ListSecGroupRule(arg0 interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ListSecGroupRule", reflect.TypeOf((*MockNetworkClient)(nil).ListSecGroupRule), arg0) +} + +// ListSubnet mocks base method +func (m *MockNetworkClient) ListSubnet(arg0 subnets.ListOptsBuilder) ([]subnets.Subnet, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "ListSubnet", arg0) + ret0, _ := ret[0].([]subnets.Subnet) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// ListSubnet indicates an expected call of ListSubnet +func (mr *MockNetworkClientMockRecorder) ListSubnet(arg0 interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ListSubnet", reflect.TypeOf((*MockNetworkClient)(nil).ListSubnet), arg0) +} + +// RemoveRouterInterface mocks base method +func (m *MockNetworkClient) RemoveRouterInterface(arg0 string, arg1 routers.RemoveInterfaceOptsBuilder) (*routers.InterfaceInfo, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "RemoveRouterInterface", arg0, arg1) + ret0, _ := ret[0].(*routers.InterfaceInfo) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// RemoveRouterInterface indicates an expected call of RemoveRouterInterface +func (mr *MockNetworkClientMockRecorder) RemoveRouterInterface(arg0, arg1 interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "RemoveRouterInterface", reflect.TypeOf((*MockNetworkClient)(nil).RemoveRouterInterface), arg0, arg1) +} + +// ReplaceAllAttributesTags mocks base method +func (m *MockNetworkClient) ReplaceAllAttributesTags(arg0, arg1 string, arg2 attributestags.ReplaceAllOptsBuilder) ([]string, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "ReplaceAllAttributesTags", arg0, arg1, arg2) + ret0, _ := ret[0].([]string) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// ReplaceAllAttributesTags indicates an expected call of ReplaceAllAttributesTags +func (mr *MockNetworkClientMockRecorder) ReplaceAllAttributesTags(arg0, arg1, arg2 interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ReplaceAllAttributesTags", reflect.TypeOf((*MockNetworkClient)(nil).ReplaceAllAttributesTags), arg0, arg1, arg2) +} + +// UpdateFloatingIP mocks base method +func (m *MockNetworkClient) UpdateFloatingIP(arg0 string, arg1 floatingips.UpdateOptsBuilder) (*floatingips.FloatingIP, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "UpdateFloatingIP", arg0, arg1) + ret0, _ := ret[0].(*floatingips.FloatingIP) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// UpdateFloatingIP indicates an expected call of UpdateFloatingIP +func (mr *MockNetworkClientMockRecorder) UpdateFloatingIP(arg0, arg1 interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "UpdateFloatingIP", reflect.TypeOf((*MockNetworkClient)(nil).UpdateFloatingIP), arg0, arg1) +} + +// UpdateNetwork mocks base method +func (m *MockNetworkClient) UpdateNetwork(arg0 string, arg1 networks.UpdateOptsBuilder) (*networks.Network, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "UpdateNetwork", arg0, arg1) + ret0, _ := ret[0].(*networks.Network) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// UpdateNetwork indicates an expected call of UpdateNetwork +func (mr *MockNetworkClientMockRecorder) UpdateNetwork(arg0, arg1 interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "UpdateNetwork", reflect.TypeOf((*MockNetworkClient)(nil).UpdateNetwork), arg0, arg1) +} + +// UpdatePort mocks base method +func (m *MockNetworkClient) UpdatePort(arg0 string, arg1 ports.UpdateOptsBuilder) (*ports.Port, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "UpdatePort", arg0, arg1) + ret0, _ := ret[0].(*ports.Port) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// UpdatePort indicates an expected call of UpdatePort +func (mr *MockNetworkClientMockRecorder) UpdatePort(arg0, arg1 interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "UpdatePort", reflect.TypeOf((*MockNetworkClient)(nil).UpdatePort), arg0, arg1) +} + +// UpdateRouter mocks base method +func (m *MockNetworkClient) UpdateRouter(arg0 string, arg1 routers.UpdateOptsBuilder) (*routers.Router, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "UpdateRouter", arg0, arg1) + ret0, _ := ret[0].(*routers.Router) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// UpdateRouter indicates an expected call of UpdateRouter +func (mr *MockNetworkClientMockRecorder) UpdateRouter(arg0, arg1 interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "UpdateRouter", reflect.TypeOf((*MockNetworkClient)(nil).UpdateRouter), arg0, arg1) +} + +// UpdateSecGroup mocks base method +func (m *MockNetworkClient) UpdateSecGroup(arg0 string, arg1 groups.UpdateOptsBuilder) (*groups.SecGroup, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "UpdateSecGroup", arg0, arg1) + ret0, _ := ret[0].(*groups.SecGroup) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// UpdateSecGroup indicates an expected call of UpdateSecGroup +func (mr *MockNetworkClientMockRecorder) UpdateSecGroup(arg0, arg1 interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "UpdateSecGroup", reflect.TypeOf((*MockNetworkClient)(nil).UpdateSecGroup), arg0, arg1) +} + +// UpdateSubnet mocks base method +func (m *MockNetworkClient) UpdateSubnet(arg0 string, arg1 subnets.UpdateOptsBuilder) (*subnets.Subnet, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "UpdateSubnet", arg0, arg1) + ret0, _ := ret[0].(*subnets.Subnet) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// UpdateSubnet indicates an expected call of UpdateSubnet +func (mr *MockNetworkClientMockRecorder) UpdateSubnet(arg0, arg1 interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "UpdateSubnet", reflect.TypeOf((*MockNetworkClient)(nil).UpdateSubnet), arg0, arg1) +} diff --git a/pkg/cloud/services/networking/network.go b/pkg/cloud/services/networking/network.go index 97507e8ae8..12f196ef9e 100644 --- a/pkg/cloud/services/networking/network.go +++ b/pkg/cloud/services/networking/network.go @@ -24,7 +24,6 @@ import ( "github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/external" "github.com/gophercloud/gophercloud/openstack/networking/v2/networks" "github.com/gophercloud/gophercloud/openstack/networking/v2/subnets" - "github.com/gophercloud/gophercloud/pagination" infrav1 "sigs.k8s.io/cluster-api-provider-openstack/api/v1alpha4" "sigs.k8s.io/cluster-api-provider-openstack/pkg/metrics" @@ -66,12 +65,7 @@ func (s *Service) ReconcileExternalNetwork(openStackCluster *infrav1.OpenStackCl External: &iTrue, } - mc := metrics.NewMetricPrometheusContext("network", "list") - allPages, err := networks.List(s.client, listOpts).AllPages() - if mc.ObserveRequest(err) != nil { - return err - } - networkList, err := networks.ExtractNetworks(allPages) + networkList, err := s.client.ListNetwork(listOpts) if err != nil { return err } @@ -127,7 +121,7 @@ func (s *Service) ReconcileNetwork(openStackCluster *infrav1.OpenStackCluster, c } mc := metrics.NewMetricPrometheusContext("network", "create") - network, err := networks.Create(s.client, opts).Extract() + network, err := s.client.CreateNetwork(opts) if mc.ObserveRequest(err) != nil { record.Warnf(openStackCluster, "FailedCreateNetwork", "Failed to create network %s: %v", networkName, err) return err @@ -135,9 +129,9 @@ func (s *Service) ReconcileNetwork(openStackCluster *infrav1.OpenStackCluster, c record.Eventf(openStackCluster, "SuccessfulCreateNetwork", "Created network %s with id %s", networkName, network.ID) if len(openStackCluster.Spec.Tags) > 0 { - _, err = attributestags.ReplaceAll(s.client, "networks", network.ID, attributestags.ReplaceAllOpts{ + _, err = s.client.ReplaceAllAttributesTags("networks", network.ID, attributestags.ReplaceAllOpts{ Tags: openStackCluster.Spec.Tags, - }).Extract() + }) if err != nil { return err } @@ -162,7 +156,7 @@ func (s *Service) DeleteNetwork(openStackCluster *infrav1.OpenStackCluster, clus } mc := metrics.NewMetricPrometheusContext("network", "delete") - err = networks.Delete(s.client, network.ID).ExtractErr() + err = s.client.DeleteNetwork(network.ID) if mc.ObserveRequest(err) != nil { record.Warnf(openStackCluster, "FailedDeleteNetwork", "Failed to delete network %s with id %s: %v", network.Name, network.ID, err) return err @@ -181,16 +175,10 @@ func (s *Service) ReconcileSubnet(openStackCluster *infrav1.OpenStackCluster, cl subnetName := getSubnetName(clusterName) s.logger.Info("Reconciling subnet", "name", subnetName) - mc := metrics.NewMetricPrometheusContext("subnet", "list") - allPages, err := subnets.List(s.client, subnets.ListOpts{ + subnetList, err := s.client.ListSubnet(subnets.ListOpts{ NetworkID: openStackCluster.Status.Network.ID, CIDR: openStackCluster.Spec.NodeCIDR, - }).AllPages() - if mc.ObserveRequest(err) != nil { - return err - } - - subnetList, err := subnets.ExtractSubnets(allPages) + }) if err != nil { return err } @@ -231,7 +219,7 @@ func (s *Service) createSubnet(openStackCluster *infrav1.OpenStackCluster, clust } mc := metrics.NewMetricPrometheusContext("subnet", "create") - subnet, err := subnets.Create(s.client, opts).Extract() + subnet, err := s.client.CreateSubnet(opts) if mc.ObserveRequest(err) != nil { record.Warnf(openStackCluster, "FailedCreateSubnet", "Failed to create subnet %s: %v", name, err) @@ -241,9 +229,9 @@ func (s *Service) createSubnet(openStackCluster *infrav1.OpenStackCluster, clust if len(openStackCluster.Spec.Tags) > 0 { mc := metrics.NewMetricPrometheusContext("subnet", "update") - _, err = attributestags.ReplaceAll(s.client, "subnets", subnet.ID, attributestags.ReplaceAllOpts{ + _, err = s.client.ReplaceAllAttributesTags("subnets", subnet.ID, attributestags.ReplaceAllOpts{ Tags: openStackCluster.Spec.Tags, - }).Extract() + }) if mc.ObserveRequest(err) != nil { return nil, err } @@ -257,13 +245,7 @@ func (s *Service) getNetworkByID(networkID string) (networks.Network, error) { ID: networkID, } - mc := metrics.NewMetricPrometheusContext("network", "list") - allPages, err := networks.List(s.client, opts).AllPages() - if mc.ObserveRequest(err) != nil { - return networks.Network{}, err - } - - networkList, err := networks.ExtractNetworks(allPages) + networkList, err := s.client.ListNetwork(opts) if err != nil { return networks.Network{}, err } @@ -282,13 +264,7 @@ func (s *Service) getNetworkByName(networkName string) (networks.Network, error) Name: networkName, } - mc := metrics.NewMetricPrometheusContext("network", "list") - allPages, err := networks.List(s.client, opts).AllPages() - if mc.ObserveRequest(err) != nil { - return networks.Network{}, err - } - - networkList, err := networks.ExtractNetworks(allPages) + networkList, err := s.client.ListNetwork(opts) if err != nil { return networks.Network{}, err } @@ -307,23 +283,14 @@ func (s *Service) GetNetworksByFilter(opts networks.ListOptsBuilder) ([]networks if opts == nil { return nil, fmt.Errorf("no Filters were passed") } - mc := metrics.NewMetricPrometheusContext("network", "list") - pager := networks.List(s.client, opts) - var nets []networks.Network - err := pager.EachPage(func(page pagination.Page) (bool, error) { - networkList, err := networks.ExtractNetworks(page) - if mc.ObserveRequest(err) != nil { - return false, err - } else if len(networkList) == 0 { - return false, fmt.Errorf("no networks could be found with the filters provided") - } - nets = networkList - return true, nil - }) + networkList, err := s.client.ListNetwork(opts) if err != nil { return nil, err } - return nets, nil + if len(networkList) == 0 { + return nil, fmt.Errorf("no networks could be found with the filters provided") + } + return networkList, nil } // GetNetworkIDsByFilter retrieves network ids by querying openstack with filters. @@ -344,23 +311,14 @@ func (s *Service) GetSubnetsByFilter(opts subnets.ListOptsBuilder) ([]subnets.Su if opts == nil { return []subnets.Subnet{}, fmt.Errorf("no Filters were passed") } - mc := metrics.NewMetricPrometheusContext("subnet", "list") - pager := subnets.List(s.client, opts) - var snets []subnets.Subnet - err := pager.EachPage(func(page pagination.Page) (bool, error) { - subnetList, err := subnets.ExtractSubnets(page) - if mc.ObserveRequest(err) != nil { - return false, err - } else if len(subnetList) == 0 { - return false, fmt.Errorf("no subnets could be found with the filters provided") - } - snets = append(snets, subnetList...) - return true, nil - }) + subnetList, err := s.client.ListSubnet(opts) if err != nil { return []subnets.Subnet{}, err } - return snets, nil + if len(subnetList) == 0 { + return nil, fmt.Errorf("no subnets could be found with the filters provided") + } + return subnetList, nil } func getSubnetName(clusterName string) string { diff --git a/pkg/cloud/services/networking/router.go b/pkg/cloud/services/networking/router.go index ae0b094593..11c4dfe5b1 100644 --- a/pkg/cloud/services/networking/router.go +++ b/pkg/cloud/services/networking/router.go @@ -48,15 +48,9 @@ func (s *Service) ReconcileRouter(openStackCluster *infrav1.OpenStackCluster, cl routerName := getRouterName(clusterName) s.logger.Info("Reconciling router", "name", routerName) - mc := metrics.NewMetricPrometheusContext("router", "list") - allPages, err := routers.List(s.client, routers.ListOpts{ + routerList, err := s.client.ListRouter(routers.ListOpts{ Name: routerName, - }).AllPages() - if mc.ObserveRequest(err) != nil { - return err - } - - routerList, err := routers.ExtractRouters(allPages) + }) if err != nil { return err } @@ -110,11 +104,10 @@ INTERFACE_LOOP: // ... and create a router interface for our subnet. if createInterface { s.logger.V(4).Info("Creating RouterInterface", "routerID", router.ID, "subnetID", openStackCluster.Status.Network.Subnet.ID) - mc := metrics.NewMetricPrometheusContext("server_os_interface", "create") - routerInterface, err := routers.AddInterface(s.client, router.ID, routers.AddInterfaceOpts{ + routerInterface, err := s.client.AddRouterInterface(router.ID, routers.AddInterfaceOpts{ SubnetID: openStackCluster.Status.Network.Subnet.ID, - }).Extract() - if mc.ObserveRequest(err) != nil { + }) + if err != nil { return fmt.Errorf("unable to create router interface: %v", err) } s.logger.V(4).Info("Created RouterInterface", "id", routerInterface.ID) @@ -139,7 +132,7 @@ func (s *Service) createRouter(openStackCluster *infrav1.OpenStackCluster, clust mc := metrics.NewMetricPrometheusContext("router", "create") - router, err := routers.Create(s.client, opts).Extract() + router, err := s.client.CreateRouter(opts) if mc.ObserveRequest(err) != nil { record.Warnf(openStackCluster, "FailedCreateRouter", "Failed to create router %s: %v", name, err) @@ -148,9 +141,9 @@ func (s *Service) createRouter(openStackCluster *infrav1.OpenStackCluster, clust record.Eventf(openStackCluster, "SuccessfulCreateRouter", "Created router %s with id %s", name, router.ID) if len(openStackCluster.Spec.Tags) > 0 { - _, err = attributestags.ReplaceAll(s.client, "routers", router.ID, attributestags.ReplaceAllOpts{ + _, err = s.client.ReplaceAllAttributesTags("routers", router.ID, attributestags.ReplaceAllOpts{ Tags: openStackCluster.Spec.Tags, - }).Extract() + }) if err != nil { return nil, err } @@ -186,7 +179,7 @@ func (s *Service) setRouterExternalIPs(openStackCluster *infrav1.OpenStackCluste } mc := metrics.NewMetricPrometheusContext("router", "update") - _, err := routers.Update(s.client, router.ID, updateOpts).Extract() + _, err := s.client.UpdateRouter(router.ID, updateOpts) if mc.ObserveRequest(err) != nil { record.Warnf(openStackCluster, "FailedUpdateRouter", "Failed to update router %s with id %s: %v", router.Name, router.ID, err) return err @@ -207,11 +200,10 @@ func (s *Service) DeleteRouter(openStackCluster *infrav1.OpenStackCluster, clust } if subnet.ID != "" { - mc := metrics.NewMetricPrometheusContext("server_os_interface", "delete") - _, err = routers.RemoveInterface(s.client, router.ID, routers.RemoveInterfaceOpts{ + _, err = s.client.RemoveRouterInterface(router.ID, routers.RemoveInterfaceOpts{ SubnetID: subnet.ID, - }).Extract() - if mc.ObserveRequestIgnoreNotFound(err) != nil { + }) + if err != nil { if !capoerrors.IsNotFound(err) { return fmt.Errorf("unable to remove router interface: %v", err) } @@ -222,7 +214,7 @@ func (s *Service) DeleteRouter(openStackCluster *infrav1.OpenStackCluster, clust } mc := metrics.NewMetricPrometheusContext("router", "delete") - err = routers.Delete(s.client, router.ID).ExtractErr() + err = s.client.DeleteRouter(router.ID) if mc.ObserveRequest(err) != nil { record.Warnf(openStackCluster, "FailedDeleteRouter", "Failed to delete router %s with id %s: %v", router.Name, router.ID, err) return err @@ -233,20 +225,9 @@ func (s *Service) DeleteRouter(openStackCluster *infrav1.OpenStackCluster, clust } func (s *Service) getRouterInterfaces(routerID string) ([]ports.Port, error) { - mc := metrics.NewMetricPrometheusContext("port", "list") - allPages, err := ports.List(s.client, ports.ListOpts{ + return s.client.ListPort(ports.ListOpts{ DeviceID: routerID, - }).AllPages() - if mc.ObserveRequest(err) != nil { - return []ports.Port{}, err - } - - portList, err := ports.ExtractPorts(allPages) - if err != nil { - return []ports.Port{}, err - } - - return portList, nil + }) } func (s *Service) getRouter(clusterName string) (routers.Router, subnets.Subnet, error) { @@ -266,15 +247,9 @@ func (s *Service) getRouter(clusterName string) (routers.Router, subnets.Subnet, } func (s *Service) getRouterByName(routerName string) (routers.Router, error) { - mc := metrics.NewMetricPrometheusContext("router", "list") - allPages, err := routers.List(s.client, routers.ListOpts{ + routerList, err := s.client.ListRouter(routers.ListOpts{ Name: routerName, - }).AllPages() - if mc.ObserveRequest(err) != nil { - return routers.Router{}, err - } - - routerList, err := routers.ExtractRouters(allPages) + }) if err != nil { return routers.Router{}, err } @@ -293,13 +268,7 @@ func (s *Service) getSubnetByName(subnetName string) (subnets.Subnet, error) { Name: subnetName, } - mc := metrics.NewMetricPrometheusContext("subnet", "list") - allPages, err := subnets.List(s.client, opts).AllPages() - if mc.ObserveRequest(err) != nil { - return subnets.Subnet{}, err - } - - subnetList, err := subnets.ExtractSubnets(allPages) + subnetList, err := s.client.ListSubnet(opts) if err != nil { return subnets.Subnet{}, err } diff --git a/pkg/cloud/services/networking/securitygroups.go b/pkg/cloud/services/networking/securitygroups.go index a53639457d..39671b0915 100644 --- a/pkg/cloud/services/networking/securitygroups.go +++ b/pkg/cloud/services/networking/securitygroups.go @@ -362,7 +362,7 @@ func (s *Service) deleteSecurityGroup(openStackCluster *infrav1.OpenStackCluster return nil } mc := metrics.NewMetricPrometheusContext("security_group", "delete") - err = groups.Delete(s.client, group.ID).ExtractErr() + err = s.client.DeleteSecGroup(group.ID) if mc.ObserveRequest(err) != nil { record.Warnf(openStackCluster, "FailedDeleteSecurityGroup", "Failed to delete security group %s with id %s: %v", group.Name, group.ID, err) return err @@ -421,7 +421,7 @@ func (s *Service) reconcileGroupRules(desired, observed infrav1.SecurityGroup) ( for _, rule := range rulesToDelete { s.logger.V(6).Info("Deleting rule", "ruleID", rule.ID, "groupName", observed.Name) mc := metrics.NewMetricPrometheusContext("security_group_rule", "delete") - err := rules.Delete(s.client, rule.ID).ExtractErr() + err := s.client.DeleteSecGroupRule(rule.ID) if mc.ObserveRequest(err) != nil { return infrav1.SecurityGroup{}, err } @@ -460,7 +460,7 @@ func (s *Service) createSecurityGroupIfNotExists(openStackCluster *infrav1.OpenS s.logger.V(6).Info("Creating group", "name", groupName) mc := metrics.NewMetricPrometheusContext("security_group", "create") - group, err := groups.Create(s.client, createOpts).Extract() + group, err := s.client.CreateSecGroup(createOpts) if mc.ObserveRequest(err) != nil { record.Warnf(openStackCluster, "FailedCreateSecurityGroup", "Failed to create security group %s: %v", groupName, err) return err @@ -481,13 +481,7 @@ func (s *Service) getSecurityGroupByName(name string) (*infrav1.SecurityGroup, e } s.logger.V(6).Info("Attempting to fetch security group with", "name", name) - mc := metrics.NewMetricPrometheusContext("group", "list") - allPages, err := groups.List(s.client, opts).AllPages() - if mc.ObserveRequest(err) != nil { - return &infrav1.SecurityGroup{}, err - } - - allGroups, err := groups.ExtractGroups(allPages) + allGroups, err := s.client.ListSecGroup(opts) if err != nil { return &infrav1.SecurityGroup{}, err } @@ -520,7 +514,7 @@ func (s *Service) createRule(r infrav1.SecurityGroupRule) (infrav1.SecurityGroup } s.logger.V(6).Info("Creating rule", "Description", r.Description, "Direction", dir, "PortRangeMin", r.PortRangeMin, "PortRangeMax", r.PortRangeMax, "Proto", proto, "etherType", etherType, "RemoteGroupID", r.RemoteGroupID, "RemoteIPPrefix", r.RemoteIPPrefix, "SecurityGroupID", r.SecurityGroupID) mc := metrics.NewMetricPrometheusContext("security_group_rule", "create") - rule, err := rules.Create(s.client, createOpts).Extract() + rule, err := s.client.CreateSecGroupRule(createOpts) if mc.ObserveRequest(err) != nil { return infrav1.SecurityGroupRule{}, err } diff --git a/pkg/cloud/services/networking/service.go b/pkg/cloud/services/networking/service.go index 3043655359..ffcb0a7957 100644 --- a/pkg/cloud/services/networking/service.go +++ b/pkg/cloud/services/networking/service.go @@ -35,17 +35,17 @@ const ( // It will create a network related infrastructure for the cluster, like network, subnet, router, security groups. type Service struct { projectID string - client *gophercloud.ServiceClient + client NetworkClient logger logr.Logger } // NewService returns an instance of the networking service. -func NewService(client *gophercloud.ProviderClient, clientOpts *clientconfig.ClientOpts, logger logr.Logger) (*Service, error) { - serviceClient, err := openstack.NewNetworkV2(client, gophercloud.EndpointOpts{ +func NewService(providerClient *gophercloud.ProviderClient, clientOpts *clientconfig.ClientOpts, logger logr.Logger) (*Service, error) { + serviceClient, err := openstack.NewNetworkV2(providerClient, gophercloud.EndpointOpts{ Region: clientOpts.RegionName, }) if err != nil { - return nil, fmt.Errorf("failed to create networking service client: %v", err) + return nil, fmt.Errorf("failed to create networking service providerClient: %v", err) } if clientOpts.AuthInfo == nil { @@ -54,7 +54,7 @@ func NewService(client *gophercloud.ProviderClient, clientOpts *clientconfig.Cli projectID := clientOpts.AuthInfo.ProjectID if projectID == "" && clientOpts.AuthInfo.ProjectName != "" { - projectID, err = provider.GetProjectID(client, clientOpts.AuthInfo.ProjectName) + projectID, err = provider.GetProjectID(providerClient, clientOpts.AuthInfo.ProjectName) if err != nil { return nil, fmt.Errorf("error retrieveing project id: %v", err) } @@ -65,7 +65,7 @@ func NewService(client *gophercloud.ProviderClient, clientOpts *clientconfig.Cli return &Service{ projectID: projectID, - client: serviceClient, + client: networkClient{serviceClient}, logger: logger, }, nil }