github.com/vmware/go-vcloud-director/v2@v2.24.0/govcd/nsxt_alb_clouds.go (about) 1 /* 2 * Copyright 2021 VMware, Inc. All rights reserved. Licensed under the Apache v2 License. 3 */ 4 5 package govcd 6 7 import ( 8 "errors" 9 "fmt" 10 "net/url" 11 12 "github.com/vmware/go-vcloud-director/v2/types/v56" 13 ) 14 15 // NsxtAlbCloud helps to use the virtual infrastructure provided by NSX Advanced Load Balancer, register NSX-T Cloud 16 // instances with VMware Cloud Director by consuming NsxtAlbImportableCloud. 17 type NsxtAlbCloud struct { 18 NsxtAlbCloud *types.NsxtAlbCloud 19 vcdClient *VCDClient 20 } 21 22 // GetAllAlbClouds returns all configured NSX-T ALB Clouds 23 func (vcdClient *VCDClient) GetAllAlbClouds(queryParameters url.Values) ([]*NsxtAlbCloud, error) { 24 client := vcdClient.Client 25 if !client.IsSysAdmin { 26 return nil, errors.New("handling NSX-T ALB Clouds require System user") 27 } 28 29 endpoint := types.OpenApiPathVersion1_0_0 + types.OpenApiEndpointAlbCloud 30 apiVersion, err := client.checkOpenApiEndpointCompatibility(endpoint) 31 if err != nil { 32 return nil, err 33 } 34 35 urlRef, err := client.OpenApiBuildEndpoint(endpoint) 36 if err != nil { 37 return nil, err 38 } 39 40 typeResponses := []*types.NsxtAlbCloud{{}} 41 err = client.OpenApiGetAllItems(apiVersion, urlRef, queryParameters, &typeResponses, nil) 42 if err != nil { 43 return nil, err 44 } 45 46 // Wrap all typeResponses into NsxtAlbCloud types with client 47 wrappedResponses := make([]*NsxtAlbCloud, len(typeResponses)) 48 for sliceIndex := range typeResponses { 49 wrappedResponses[sliceIndex] = &NsxtAlbCloud{ 50 NsxtAlbCloud: typeResponses[sliceIndex], 51 vcdClient: vcdClient, 52 } 53 } 54 55 return wrappedResponses, nil 56 } 57 58 // GetAlbCloudByName returns NSX-T ALB Cloud by name 59 func (vcdClient *VCDClient) GetAlbCloudByName(name string) (*NsxtAlbCloud, error) { 60 queryParameters := copyOrNewUrlValues(nil) 61 queryParameters.Add("filter", "name=="+name) 62 63 albClouds, err := vcdClient.GetAllAlbClouds(queryParameters) 64 if err != nil { 65 return nil, fmt.Errorf("error reading NSX-T ALB Cloud with Name '%s': %s", name, err) 66 } 67 68 if len(albClouds) == 0 { 69 return nil, fmt.Errorf("%s could not find NSX-T ALB Cloud with Name '%s'", ErrorEntityNotFound, name) 70 } 71 72 if len(albClouds) > 1 { 73 return nil, fmt.Errorf("found more than 1 NSX-T ALB Cloud with Name '%s'", name) 74 } 75 76 return albClouds[0], nil 77 } 78 79 // GetAlbCloudById returns NSX-T ALB Cloud by ID 80 // 81 // Note. This function uses server side filtering instead of directly querying endpoint with specified ID because such 82 // endpoint does not exist 83 func (vcdClient *VCDClient) GetAlbCloudById(id string) (*NsxtAlbCloud, error) { 84 85 queryParameters := copyOrNewUrlValues(nil) 86 queryParameters.Add("filter", "id=="+id) 87 88 albCloud, err := vcdClient.GetAllAlbClouds(queryParameters) 89 if err != nil { 90 return nil, fmt.Errorf("error reading NSX-T ALB Cloud with ID '%s': %s", id, err) 91 } 92 93 if len(albCloud) == 0 { 94 return nil, fmt.Errorf("%s could not find NSX-T ALB Cloud by ID '%s'", ErrorEntityNotFound, id) 95 } 96 97 return albCloud[0], nil 98 } 99 100 // CreateAlbCloud creates NSX-T ALB Cloud 101 func (vcdClient *VCDClient) CreateAlbCloud(albCloudConfig *types.NsxtAlbCloud) (*NsxtAlbCloud, error) { 102 client := vcdClient.Client 103 if !client.IsSysAdmin { 104 return nil, errors.New("handling NSX-T ALB Clouds require System user") 105 } 106 107 endpoint := types.OpenApiPathVersion1_0_0 + types.OpenApiEndpointAlbCloud 108 minimumApiVersion, err := client.checkOpenApiEndpointCompatibility(endpoint) 109 if err != nil { 110 return nil, err 111 } 112 113 urlRef, err := client.OpenApiBuildEndpoint(endpoint) 114 if err != nil { 115 return nil, err 116 } 117 118 returnObject := &NsxtAlbCloud{ 119 NsxtAlbCloud: &types.NsxtAlbCloud{}, 120 vcdClient: vcdClient, 121 } 122 123 err = client.OpenApiPostItem(minimumApiVersion, urlRef, nil, albCloudConfig, returnObject.NsxtAlbCloud, nil) 124 if err != nil { 125 return nil, fmt.Errorf("error creating NSX-T ALB Cloud: %s", err) 126 } 127 128 return returnObject, nil 129 } 130 131 // Update is not supported in VCD 10.3 and older therefore this function remains commented 132 // 133 // Update updates existing NSX-T ALB Cloud with new supplied albCloudConfig configuration 134 //func (nsxtAlbCloud *NsxtAlbCloud) Update(albCloudConfig *types.NsxtAlbCloud) (*NsxtAlbCloud, error) { 135 // client := nsxtAlbCloud.vcdClient.Client 136 // endpoint := types.OpenApiPathVersion1_0_0 + types.OpenApiEndpointAlbCloud 137 // minimumApiVersion, err := client.checkOpenApiEndpointCompatibility(endpoint) 138 // if err != nil { 139 // return nil, err 140 // } 141 // 142 // if albCloudConfig.ID == "" { 143 // return nil, fmt.Errorf("cannot update NSX-T ALB Cloud without ID") 144 // } 145 // 146 // urlRef, err := client.OpenApiBuildEndpoint(endpoint, albCloudConfig.ID) 147 // if err != nil { 148 // return nil, err 149 // } 150 // 151 // responseAlbCloud := &NsxtAlbCloud{ 152 // NsxtAlbCloud: &types.NsxtAlbCloud{}, 153 // vcdClient: nsxtAlbCloud.vcdClient, 154 // } 155 // 156 // err = client.OpenApiPutItem(minimumApiVersion, urlRef, nil, albCloudConfig, responseAlbCloud.NsxtAlbCloud, nil) 157 // if err != nil { 158 // return nil, fmt.Errorf("error updating NSX-T ALB Cloud: %s", err) 159 // } 160 // 161 // return responseAlbCloud, nil 162 //} 163 164 // Delete removes NSX-T ALB Cloud configuration 165 func (nsxtAlbCloud *NsxtAlbCloud) Delete() error { 166 client := nsxtAlbCloud.vcdClient.Client 167 endpoint := types.OpenApiPathVersion1_0_0 + types.OpenApiEndpointAlbCloud 168 minimumApiVersion, err := client.checkOpenApiEndpointCompatibility(endpoint) 169 if err != nil { 170 return err 171 } 172 173 if nsxtAlbCloud.NsxtAlbCloud.ID == "" { 174 return fmt.Errorf("cannot delete NSX-T ALB Cloud without ID") 175 } 176 177 urlRef, err := client.OpenApiBuildEndpoint(endpoint, nsxtAlbCloud.NsxtAlbCloud.ID) 178 if err != nil { 179 return err 180 } 181 182 err = client.OpenApiDeleteItem(minimumApiVersion, urlRef, nil, nil) 183 if err != nil { 184 return fmt.Errorf("error deleting NSX-T ALB Cloud: %s", err) 185 } 186 187 return nil 188 }