|
| 1 | +/* |
| 2 | +Copyright 2024 The Kubernetes Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package cloud |
| 18 | + |
| 19 | +import ( |
| 20 | + "strings" |
| 21 | + |
| 22 | + infrav1 "sigs.k8s.io/cluster-api-provider-cloudstack/api/v1beta3" |
| 23 | + |
| 24 | + "github.com/apache/cloudstack-go/v2/cloudstack" |
| 25 | + "github.com/pkg/errors" |
| 26 | +) |
| 27 | + |
| 28 | +// ResourceTypeVPC is the type identifier for VPC resources. |
| 29 | +const ResourceTypeVPC = "VPC" |
| 30 | + |
| 31 | +// VPCIface defines the interface for VPC operations. |
| 32 | +type VPCIface interface { |
| 33 | + ResolveVPC(*infrav1.VPC) error |
| 34 | + CreateVPC(*infrav1.VPC) error |
| 35 | + GetOrCreateVPC(*infrav1.VPC) error |
| 36 | +} |
| 37 | + |
| 38 | +// ResolveVPC checks if the specified VPC exists by ID or name. |
| 39 | +// If it exists, it updates the VPC struct with the resolved ID or name. |
| 40 | +func (c *client) ResolveVPC(vpc *infrav1.VPC) error { |
| 41 | + if vpc == nil || (vpc.ID == "" && vpc.Name == "") { |
| 42 | + return nil |
| 43 | + } |
| 44 | + |
| 45 | + // If VPC ID is provided, check if it exists |
| 46 | + if vpc.ID != "" { |
| 47 | + resp, count, err := c.cs.VPC.GetVPCByID(vpc.ID, cloudstack.WithProject(c.user.Project.ID)) |
| 48 | + if err != nil { |
| 49 | + c.customMetrics.EvaluateErrorAndIncrementAcsReconciliationErrorCounter(err) |
| 50 | + return errors.Wrapf(err, "failed to get VPC with ID %s", vpc.ID) |
| 51 | + } |
| 52 | + if count == 0 { |
| 53 | + return errors.Errorf("no VPC found with ID %s", vpc.ID) |
| 54 | + } |
| 55 | + vpc.Name = resp.Name |
| 56 | + return nil |
| 57 | + } |
| 58 | + |
| 59 | + // If VPC name is provided, check if it exists |
| 60 | + resp, count, err := c.cs.VPC.GetVPCByName(vpc.Name, cloudstack.WithProject(c.user.Project.ID)) |
| 61 | + if err != nil { |
| 62 | + c.customMetrics.EvaluateErrorAndIncrementAcsReconciliationErrorCounter(err) |
| 63 | + return errors.Wrapf(err, "failed to get VPC with name %s", vpc.Name) |
| 64 | + } |
| 65 | + if count == 0 { |
| 66 | + return errors.Errorf("no VPC found with name %s", vpc.Name) |
| 67 | + } |
| 68 | + vpc.ID = resp.Id |
| 69 | + return nil |
| 70 | +} |
| 71 | + |
| 72 | +// GetOrCreateVPC ensures a VPC exists for the given specification. |
| 73 | +// If the VPC doesn't exist, it creates a new one. |
| 74 | +func (c *client) GetOrCreateVPC(vpc *infrav1.VPC) error { |
| 75 | + if vpc == nil || (vpc.ID == "" && vpc.Name == "") { |
| 76 | + return nil |
| 77 | + } |
| 78 | + |
| 79 | + // Try to resolve the VPC |
| 80 | + err := c.ResolveVPC(vpc) |
| 81 | + if err != nil { |
| 82 | + // If it's a "not found" error and we have a name, create the VPC |
| 83 | + if strings.Contains(err.Error(), "no VPC found") && vpc.Name != "" { |
| 84 | + return c.CreateVPC(vpc) |
| 85 | + } |
| 86 | + return err |
| 87 | + } |
| 88 | + |
| 89 | + return nil |
| 90 | +} |
| 91 | + |
| 92 | +// CreateVPC creates a new VPC in CloudStack. |
| 93 | +func (c *client) CreateVPC(vpc *infrav1.VPC) error { |
| 94 | + if vpc == nil || vpc.Name == "" { |
| 95 | + return errors.New("VPC name must be specified") |
| 96 | + } |
| 97 | + |
| 98 | + // Get VPC offering ID |
| 99 | + p := c.cs.VPC.NewListVPCOfferingsParams() |
| 100 | + resp, err := c.cs.VPC.ListVPCOfferings(p) |
| 101 | + if err != nil { |
| 102 | + c.customMetrics.EvaluateErrorAndIncrementAcsReconciliationErrorCounter(err) |
| 103 | + return errors.Wrap(err, "failed to list VPC offerings") |
| 104 | + } |
| 105 | + if resp.Count == 0 { |
| 106 | + return errors.New("no VPC offerings available") |
| 107 | + } |
| 108 | + |
| 109 | + // Since the SDK's VPC creation API might have compatibility issues with different CloudStack versions, |
| 110 | + // we'll need to handle this in the implementation of the network creation rather than here. |
| 111 | + // For now, we'll just return a "not implemented" error, and handle VPC creation in the isolated network creation. |
| 112 | + return errors.New("creating VPC not directly implemented; handled in isolated network creation") |
| 113 | +} |
0 commit comments