github.com/verrazzano/verrazzano@v1.7.0/cluster-operator/controllers/quickcreate/controller/oci/common/properties.go (about) 1 // Copyright (c) 2023, Oracle and/or its affiliates. 2 // Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl. 3 4 package common 5 6 import ( 7 "context" 8 "errors" 9 vmcv1alpha1 "github.com/verrazzano/verrazzano/cluster-operator/apis/clusters/v1alpha1" 10 "github.com/verrazzano/verrazzano/cluster-operator/controllers/quickcreate/controller/oci" 11 ocinetwork "github.com/verrazzano/verrazzano/cluster-operator/controllers/quickcreate/controller/oci/network" 12 "k8s.io/apimachinery/pkg/runtime/schema" 13 clipkg "sigs.k8s.io/controller-runtime/pkg/client" 14 ) 15 16 type Values struct { 17 Name string 18 Namespace string 19 *oci.Credentials 20 Network *vmcv1alpha1.Network 21 ExistingSubnets []oci.Subnet 22 OCIClientGetter func(creds *oci.Credentials) (oci.Client, error) 23 } 24 25 func (o *Values) SetCommonValues(ctx context.Context, cli clipkg.Client, q clipkg.Object, gvk schema.GroupVersionKind) error { 26 if !o.HasOCINetwork() { 27 network, err := ocinetwork.GetNetwork(ctx, cli, q, gvk) 28 if err == nil { 29 o.Network = network 30 } 31 32 } 33 if !o.IsQuickCreate() { 34 if err := o.SetExistingSubnets(ctx); err != nil { 35 return err 36 } 37 } 38 return nil 39 } 40 41 func (o *Values) HasOCINetwork() bool { 42 return o.Network != nil && len(o.Network.Subnets) > 0 43 } 44 45 func (o *Values) IsQuickCreate() bool { 46 if o.Network == nil { 47 return true 48 } 49 return o.Network.CreateVCN 50 } 51 52 func (o *Values) SetExistingSubnets(ctx context.Context) error { 53 if o.Credentials == nil { 54 return errors.New("no credentials") 55 } 56 ociClient, err := o.OCIClientGetter(o.Credentials) 57 if err != nil { 58 return err 59 } 60 var subnetList []oci.Subnet 61 for _, sn := range o.Network.Subnets { 62 subnet, err := ociClient.GetSubnetByID(ctx, sn.ID, string(sn.Role)) 63 if err != nil { 64 return err 65 } 66 subnetList = append(subnetList, *subnet) 67 } 68 o.ExistingSubnets = subnetList 69 return nil 70 } 71 72 func (o *Values) GetSubnetNameForRole(role vmcv1alpha1.SubnetRole) string { 73 for _, subnet := range o.ExistingSubnets { 74 if subnet.Role == string(role) { 75 return subnet.Name 76 } 77 } 78 return "" 79 }