Skip to content

Commit

Permalink
Add custom type for resource not found error (#1739)
Browse files Browse the repository at this point in the history
  • Loading branch information
Amulyam24 committed Apr 24, 2024
1 parent 1aebc49 commit 526264b
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 6 deletions.
12 changes: 6 additions & 6 deletions cloud/scope/powervs_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -1841,7 +1841,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
}
Expand Down Expand Up @@ -1876,7 +1876,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
}
Expand Down Expand Up @@ -1912,7 +1912,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
}
Expand Down Expand Up @@ -1946,7 +1946,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
}
Expand Down Expand Up @@ -2012,7 +2012,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
}
Expand Down Expand Up @@ -2081,7 +2081,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)
Expand Down
40 changes: 40 additions & 0 deletions cloud/scope/types.go
Original file line number Diff line number Diff line change
@@ -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")
)

0 comments on commit 526264b

Please sign in to comment.