github.com/verrazzano/verrazzano@v1.7.0/cluster-operator/controllers/quickcreate/controller/oci/client.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 oci 5 6 import ( 7 "context" 8 "github.com/oracle/oci-go-sdk/v53/common" 9 "github.com/oracle/oci-go-sdk/v53/core" 10 "github.com/oracle/oci-go-sdk/v53/identity" 11 ) 12 13 const ( 14 subnetPrivate = "private" 15 subnetPublic = "public" 16 ) 17 18 // Client interface for OCI Clients 19 type ( 20 Client interface { 21 GetSubnetByID(ctx context.Context, id, role string) (*Subnet, error) 22 GetVCNByID(ctx context.Context, id string) (*core.Vcn, error) 23 GetAvailabilityAndFaultDomains(ctx context.Context) ([]AvailabilityDomain, error) 24 } 25 // ClientImpl OCI Client implementation 26 ClientImpl struct { 27 tenancyID string 28 vnClient core.VirtualNetworkClient 29 identityClient identity.IdentityClient 30 } 31 Subnet struct { 32 ID string 33 Role string 34 Name string 35 DisplayName string 36 CIDR string 37 Type string 38 } 39 AvailabilityDomain struct { 40 Name string 41 FaultDomains []FaultDomain 42 } 43 FaultDomain struct { 44 Name string 45 } 46 ) 47 48 // NewClient creates a new OCI Client 49 func NewClient(creds *Credentials) (Client, error) { 50 provider, err := creds.AsConfigurationProvider() 51 if err != nil { 52 return nil, err 53 } 54 net, err := core.NewVirtualNetworkClientWithConfigurationProvider(provider) 55 if err != nil { 56 return nil, err 57 } 58 i, err := identity.NewIdentityClientWithConfigurationProvider(provider) 59 if err != nil { 60 return nil, err 61 } 62 return &ClientImpl{ 63 tenancyID: creds.Tenancy, 64 vnClient: net, 65 identityClient: i, 66 }, nil 67 } 68 69 // GetSubnetByID retrieves a subnet given that subnet's ID. 70 func (c *ClientImpl) GetSubnetByID(ctx context.Context, subnetID, role string) (*Subnet, error) { 71 response, err := c.vnClient.GetSubnet(ctx, core.GetSubnetRequest{ 72 SubnetId: &subnetID, 73 RequestMetadata: common.RequestMetadata{}, 74 }) 75 if err != nil { 76 return nil, err 77 } 78 79 sn := response.Subnet 80 return &Subnet{ 81 ID: subnetID, 82 CIDR: *sn.CidrBlock, 83 Type: subnetAccess(sn), 84 Name: role, 85 DisplayName: *sn.DisplayName, 86 Role: role, 87 }, nil 88 } 89 90 func (c *ClientImpl) GetVCNByID(ctx context.Context, id string) (*core.Vcn, error) { 91 response, err := c.vnClient.GetVcn(ctx, core.GetVcnRequest{ 92 VcnId: &id, 93 }) 94 if err != nil { 95 return nil, err 96 } 97 return &response.Vcn, nil 98 } 99 100 func (c *ClientImpl) GetAvailabilityAndFaultDomains(ctx context.Context) ([]AvailabilityDomain, error) { 101 ads, err := c.identityClient.ListAvailabilityDomains(ctx, identity.ListAvailabilityDomainsRequest{ 102 CompartmentId: &c.tenancyID, 103 }) 104 if err != nil { 105 return nil, err 106 } 107 var availabilityDomains []AvailabilityDomain 108 for _, ad := range ads.Items { 109 availabilityDomain := AvailabilityDomain{ 110 Name: *ad.Name, 111 } 112 fd, err := c.identityClient.ListFaultDomains(ctx, identity.ListFaultDomainsRequest{ 113 CompartmentId: &c.tenancyID, 114 AvailabilityDomain: ad.Name, 115 }) 116 if err != nil { 117 return nil, err 118 } 119 for _, f := range fd.Items { 120 availabilityDomain.FaultDomains = append(availabilityDomain.FaultDomains, FaultDomain{ 121 Name: *f.Name, 122 }) 123 } 124 availabilityDomains = append(availabilityDomains, availabilityDomain) 125 } 126 return availabilityDomains, nil 127 } 128 129 // subnetAccess returns public or private, depending on a subnet's access type 130 func subnetAccess(subnet core.Subnet) string { 131 if subnet.ProhibitPublicIpOnVnic != nil && subnet.ProhibitInternetIngress != nil && !*subnet.ProhibitPublicIpOnVnic && !*subnet.ProhibitInternetIngress { 132 return subnetPublic 133 } 134 return subnetPrivate 135 }