From 8ea34767ee083279ce5f1f026e4d7ff676ff633b Mon Sep 17 00:00:00 2001 From: Amulyam24 Date: Fri, 19 Apr 2024 16:46:35 +0530 Subject: [PATCH] Add custom type for resource not found error --- cloud/scope/powervs_cluster.go | 12 +++++----- cloud/scope/types.go | 40 ++++++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 6 deletions(-) create mode 100644 cloud/scope/types.go diff --git a/cloud/scope/powervs_cluster.go b/cloud/scope/powervs_cluster.go index aea86c5ac..62b25da4e 100644 --- a/cloud/scope/powervs_cluster.go +++ b/cloud/scope/powervs_cluster.go @@ -1827,7 +1827,7 @@ func (s *PowerVSClusterScope) DeleteLoadBalancer() (bool, error) { }) if err != nil { - if strings.Contains(err.Error(), "cannot be found") { + if strings.Contains(err.Error(), string(VPCLoadBalancerNotFound)) { s.Info("VPC load balancer successfully deleted") return false, nil } @@ -1862,7 +1862,7 @@ func (s *PowerVSClusterScope) DeleteVPCSubnet() (bool, error) { }) if err != nil { - if strings.Contains(err.Error(), "Subnet not found") { + if strings.Contains(err.Error(), string(VPCSubnetNotFound)) { s.Info("VPC subnet successfully deleted") return false, nil } @@ -1898,7 +1898,7 @@ func (s *PowerVSClusterScope) DeleteVPC() (bool, error) { }) if err != nil { - if strings.Contains(err.Error(), "VPC not found") { + if strings.Contains(err.Error(), string(VPCNotFound)) { s.Info("VPC successfully deleted") return false, nil } @@ -1932,7 +1932,7 @@ func (s *PowerVSClusterScope) DeleteTransitGateway() (bool, error) { }) if err != nil { - if strings.Contains(err.Error(), "gateway was not found") { + if strings.Contains(err.Error(), string(TransitGatewayNotFound)) { s.Info("Transit gateway successfully deleted") return false, nil } @@ -1998,7 +1998,7 @@ func (s *PowerVSClusterScope) DeleteDHCPServer() error { server, err := s.IBMPowerVSClient.GetDHCPServer(*s.IBMPowerVSCluster.Status.DHCPServer.ID) if err != nil { - if strings.Contains(err.Error(), "dhcp server does not exist") { + if strings.Contains(err.Error(), string(DHCPServerNotFound)) { s.Info("DHCP server successfully deleted") return nil } @@ -2067,7 +2067,7 @@ func (s *PowerVSClusterScope) DeleteCOSInstance() error { ID: s.IBMPowerVSCluster.Status.COSInstance.ID, }) if err != nil { - if strings.Contains(err.Error(), "COS instance unavailable") { + if strings.Contains(err.Error(), string(COSInstanceNotFound)) { return nil } return fmt.Errorf("error fetching COS service instance: %w", err) diff --git a/cloud/scope/types.go b/cloud/scope/types.go new file mode 100644 index 000000000..6a122004c --- /dev/null +++ b/cloud/scope/types.go @@ -0,0 +1,40 @@ +/* +Copyright 2024 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 scope + +// ResourceNotFound is the string representing an error when a resource is not found in IBM Cloud. +type ResourceNotFound string + +var ( + // VPCLoadBalancerNotFound is the error returned when a VPC load balancer is not found. + VPCLoadBalancerNotFound = ResourceNotFound("cannot be found") + + // VPCSubnetNotFound is the error returned when a VPC subnet is not found. + VPCSubnetNotFound = ResourceNotFound("Subnet not found") + + // VPCNotFound is the error returned when a VPC is not found. + VPCNotFound = ResourceNotFound("VPC not found") + + // TransitGatewayNotFound is the error returned when a transit gateway is not found. + TransitGatewayNotFound = ResourceNotFound("gateway was not found") + + // DHCPServerNotFound is the error returned when a DHCP server is not found. + DHCPServerNotFound = ResourceNotFound("dhcp server does not exist") + + // COSInstanceNotFound is the error returned when a COS service instance is not found. + COSInstanceNotFound = ResourceNotFound("COS instance unavailable") +)