github.com/huaweicloud/golangsdk@v0.0.0-20210831081626-d823fe11ceba/acceptance/clients/clients.go (about) 1 // Package clients contains functions for creating OpenStack service clients 2 // for use in acceptance tests. It also manages the required environment 3 // variables to run the tests. 4 package clients 5 6 import ( 7 "fmt" 8 "os" 9 "strings" 10 11 "github.com/huaweicloud/golangsdk" 12 "github.com/huaweicloud/golangsdk/openstack" 13 ) 14 15 // AcceptanceTestChoices contains image and flavor selections for use by the acceptance tests. 16 type AcceptanceTestChoices struct { 17 // ImageID contains the ID of a valid image. 18 ImageID string 19 20 // FlavorID contains the ID of a valid flavor. 21 FlavorID string 22 23 // FlavorIDResize contains the ID of a different flavor available on the same OpenStack installation, that is distinct 24 // from FlavorID. 25 FlavorIDResize string 26 27 // FloatingIPPool contains the name of the pool from where to obtain floating IPs. 28 FloatingIPPoolName string 29 30 // NetworkName is the name of a network to launch the instance on. 31 NetworkName string 32 33 // ExternalNetworkID is the network ID of the external network. 34 ExternalNetworkID string 35 36 // ShareNetworkID is the Manila Share network ID 37 ShareNetworkID string 38 39 // DBDatastoreType is the datastore type for DB tests. 40 DBDatastoreType string 41 42 // DBDatastoreTypeID is the datastore type version for DB tests. 43 DBDatastoreVersion string 44 } 45 46 // AcceptanceTestChoicesFromEnv populates a ComputeChoices struct from environment variables. 47 // If any required state is missing, an `error` will be returned that enumerates the missing properties. 48 func AcceptanceTestChoicesFromEnv() (*AcceptanceTestChoices, error) { 49 imageID := os.Getenv("OS_IMAGE_ID") 50 flavorID := os.Getenv("OS_FLAVOR_ID") 51 flavorIDResize := os.Getenv("OS_FLAVOR_ID_RESIZE") 52 networkName := os.Getenv("OS_NETWORK_NAME") 53 floatingIPPoolName := os.Getenv("OS_POOL_NAME") 54 externalNetworkID := os.Getenv("OS_EXTGW_ID") 55 shareNetworkID := os.Getenv("OS_SHARE_NETWORK_ID") 56 dbDatastoreType := os.Getenv("OS_DB_DATASTORE_TYPE") 57 dbDatastoreVersion := os.Getenv("OS_DB_DATASTORE_VERSION") 58 59 missing := make([]string, 0, 3) 60 if imageID == "" { 61 missing = append(missing, "OS_IMAGE_ID") 62 } 63 if flavorID == "" { 64 missing = append(missing, "OS_FLAVOR_ID") 65 } 66 if flavorIDResize == "" { 67 missing = append(missing, "OS_FLAVOR_ID_RESIZE") 68 } 69 if floatingIPPoolName == "" { 70 missing = append(missing, "OS_POOL_NAME") 71 } 72 if externalNetworkID == "" { 73 missing = append(missing, "OS_EXTGW_ID") 74 } 75 if networkName == "" { 76 networkName = "private" 77 } 78 if shareNetworkID == "" { 79 missing = append(missing, "OS_SHARE_NETWORK_ID") 80 } 81 notDistinct := "" 82 if flavorID == flavorIDResize { 83 notDistinct = "OS_FLAVOR_ID and OS_FLAVOR_ID_RESIZE must be distinct." 84 } 85 86 if len(missing) > 0 || notDistinct != "" { 87 text := "You're missing some important setup:\n" 88 if len(missing) > 0 { 89 text += " * These environment variables must be provided: " + strings.Join(missing, ", ") + "\n" 90 } 91 if notDistinct != "" { 92 text += " * " + notDistinct + "\n" 93 } 94 95 return nil, fmt.Errorf(text) 96 } 97 98 return &AcceptanceTestChoices{ 99 ImageID: imageID, 100 FlavorID: flavorID, 101 FlavorIDResize: flavorIDResize, 102 FloatingIPPoolName: floatingIPPoolName, 103 NetworkName: networkName, 104 ExternalNetworkID: externalNetworkID, 105 ShareNetworkID: shareNetworkID, 106 DBDatastoreType: dbDatastoreType, 107 DBDatastoreVersion: dbDatastoreVersion, 108 }, nil 109 } 110 111 // NewBlockStorageV1Client returns a *ServiceClient for making calls 112 // to the OpenStack Block Storage v1 API. An error will be returned 113 // if authentication or client creation was not possible. 114 func NewBlockStorageV1Client() (*golangsdk.ServiceClient, error) { 115 ao, err := openstack.AuthOptionsFromEnv() 116 if err != nil { 117 return nil, err 118 } 119 120 client, err := openstack.AuthenticatedClient(ao) 121 if err != nil { 122 return nil, err 123 } 124 125 return openstack.NewBlockStorageV1(client, golangsdk.EndpointOpts{ 126 Region: os.Getenv("OS_REGION_NAME"), 127 }) 128 } 129 130 // NewBlockStorageV2Client returns a *ServiceClient for making calls 131 // to the OpenStack Block Storage v2 API. An error will be returned 132 // if authentication or client creation was not possible. 133 func NewBlockStorageV2Client() (*golangsdk.ServiceClient, error) { 134 ao, err := openstack.AuthOptionsFromEnv() 135 if err != nil { 136 return nil, err 137 } 138 139 client, err := openstack.AuthenticatedClient(ao) 140 if err != nil { 141 return nil, err 142 } 143 144 return openstack.NewBlockStorageV2(client, golangsdk.EndpointOpts{ 145 Region: os.Getenv("OS_REGION_NAME"), 146 }) 147 } 148 149 // NewBlockStorageV3Client returns a *ServiceClient for making calls 150 // to the OpenStack Block Storage v3 API. An error will be returned 151 // if authentication or client creation was not possible. 152 func NewBlockStorageV3Client() (*golangsdk.ServiceClient, error) { 153 ao, err := openstack.AuthOptionsFromEnv() 154 if err != nil { 155 return nil, err 156 } 157 158 client, err := openstack.AuthenticatedClient(ao) 159 if err != nil { 160 return nil, err 161 } 162 163 return openstack.NewBlockStorageV3(client, golangsdk.EndpointOpts{ 164 Region: os.Getenv("OS_REGION_NAME"), 165 }) 166 } 167 168 // NewComputeV2Client returns a *ServiceClient for making calls 169 // to the OpenStack Compute v2 API. An error will be returned 170 // if authentication or client creation was not possible. 171 func NewComputeV2Client() (*golangsdk.ServiceClient, error) { 172 ao, err := openstack.AuthOptionsFromEnv() 173 if err != nil { 174 return nil, err 175 } 176 177 client, err := openstack.AuthenticatedClient(ao) 178 if err != nil { 179 return nil, err 180 } 181 182 return openstack.NewComputeV2(client, golangsdk.EndpointOpts{ 183 Region: os.Getenv("OS_REGION_NAME"), 184 }) 185 } 186 187 // NewDBV1Client returns a *ServiceClient for making calls 188 // to the OpenStack Database v1 API. An error will be returned 189 // if authentication or client creation was not possible. 190 func NewDBV1Client() (*golangsdk.ServiceClient, error) { 191 ao, err := openstack.AuthOptionsFromEnv() 192 if err != nil { 193 return nil, err 194 } 195 196 client, err := openstack.AuthenticatedClient(ao) 197 if err != nil { 198 return nil, err 199 } 200 201 return openstack.NewDBV1(client, golangsdk.EndpointOpts{ 202 Region: os.Getenv("OS_REGION_NAME"), 203 }) 204 } 205 206 // NewDNSV2Client returns a *ServiceClient for making calls 207 // to the OpenStack Compute v2 API. An error will be returned 208 // if authentication or client creation was not possible. 209 func NewDNSV2Client() (*golangsdk.ServiceClient, error) { 210 ao, err := openstack.AuthOptionsFromEnv() 211 if err != nil { 212 return nil, err 213 } 214 215 client, err := openstack.AuthenticatedClient(ao) 216 if err != nil { 217 return nil, err 218 } 219 220 return openstack.NewDNSV2(client, golangsdk.EndpointOpts{ 221 Region: os.Getenv("OS_REGION_NAME"), 222 }) 223 } 224 225 // NewIdentityV2Client returns a *ServiceClient for making calls 226 // to the OpenStack Identity v2 API. An error will be returned 227 // if authentication or client creation was not possible. 228 func NewIdentityV2Client() (*golangsdk.ServiceClient, error) { 229 ao, err := openstack.AuthOptionsFromEnv() 230 if err != nil { 231 return nil, err 232 } 233 234 client, err := openstack.AuthenticatedClient(ao) 235 if err != nil { 236 return nil, err 237 } 238 239 return openstack.NewIdentityV2(client, golangsdk.EndpointOpts{ 240 Region: os.Getenv("OS_REGION_NAME"), 241 }) 242 } 243 244 // NewIdentityV2AdminClient returns a *ServiceClient for making calls 245 // to the Admin Endpoint of the OpenStack Identity v2 API. An error 246 // will be returned if authentication or client creation was not possible. 247 func NewIdentityV2AdminClient() (*golangsdk.ServiceClient, error) { 248 ao, err := openstack.AuthOptionsFromEnv() 249 if err != nil { 250 return nil, err 251 } 252 253 client, err := openstack.AuthenticatedClient(ao) 254 if err != nil { 255 return nil, err 256 } 257 258 return openstack.NewIdentityV2(client, golangsdk.EndpointOpts{ 259 Region: os.Getenv("OS_REGION_NAME"), 260 Availability: golangsdk.AvailabilityAdmin, 261 }) 262 } 263 264 // NewIdentityV2UnauthenticatedClient returns an unauthenticated *ServiceClient 265 // for the OpenStack Identity v2 API. An error will be returned if 266 // authentication or client creation was not possible. 267 func NewIdentityV2UnauthenticatedClient() (*golangsdk.ServiceClient, error) { 268 ao, err := openstack.AuthOptionsFromEnv() 269 if err != nil { 270 return nil, err 271 } 272 273 client, err := openstack.NewClient(ao.IdentityEndpoint) 274 if err != nil { 275 return nil, err 276 } 277 278 return openstack.NewIdentityV2(client, golangsdk.EndpointOpts{}) 279 } 280 281 // NewIdentityV3Client returns a *ServiceClient for making calls 282 // to the OpenStack Identity v3 API. An error will be returned 283 // if authentication or client creation was not possible. 284 func NewIdentityV3Client() (*golangsdk.ServiceClient, error) { 285 ao, err := openstack.AuthOptionsFromEnv() 286 if err != nil { 287 return nil, err 288 } 289 290 client, err := openstack.AuthenticatedClient(ao) 291 if err != nil { 292 return nil, err 293 } 294 295 return openstack.NewIdentityV3(client, golangsdk.EndpointOpts{ 296 Region: os.Getenv("OS_REGION_NAME"), 297 }) 298 } 299 300 // NewIdentityV3UnauthenticatedClient returns an unauthenticated *ServiceClient 301 // for the OpenStack Identity v3 API. An error will be returned if 302 // authentication or client creation was not possible. 303 func NewIdentityV3UnauthenticatedClient() (*golangsdk.ServiceClient, error) { 304 ao, err := openstack.AuthOptionsFromEnv() 305 if err != nil { 306 return nil, err 307 } 308 309 client, err := openstack.NewClient(ao.IdentityEndpoint) 310 if err != nil { 311 return nil, err 312 } 313 314 return openstack.NewIdentityV3(client, golangsdk.EndpointOpts{}) 315 } 316 317 // NewImageServiceV2Client returns a *ServiceClient for making calls to the 318 // OpenStack Image v2 API. An error will be returned if authentication or 319 // client creation was not possible. 320 func NewImageServiceV2Client() (*golangsdk.ServiceClient, error) { 321 ao, err := openstack.AuthOptionsFromEnv() 322 if err != nil { 323 return nil, err 324 } 325 326 client, err := openstack.AuthenticatedClient(ao) 327 if err != nil { 328 return nil, err 329 } 330 331 return openstack.NewImageServiceV2(client, golangsdk.EndpointOpts{ 332 Region: os.Getenv("OS_REGION_NAME"), 333 }) 334 } 335 336 // NewNetworkV1Client returns a *ServiceClient for making calls to the 337 // OpenStack Networking v1 API. An error will be returned if authentication 338 // or client creation was not possible. 339 func NewNetworkV1Client() (*golangsdk.ServiceClient, error) { 340 ao, err := openstack.AuthOptionsFromEnv() 341 if err != nil { 342 return nil, err 343 } 344 345 client, err := openstack.AuthenticatedClient(ao) 346 if err != nil { 347 return nil, err 348 } 349 350 return openstack.NewNetworkV1(client, golangsdk.EndpointOpts{ 351 Region: os.Getenv("OS_REGION_NAME"), 352 }) 353 } 354 355 // NewPeerNetworkV1Client returns a *ServiceClient for making calls to the 356 // OpenStack Networking v1 API for VPC peer. An error will be returned if authentication 357 // or client creation was not possible. 358 func NewPeerNetworkV1Client() (*golangsdk.ServiceClient, error) { 359 ao, err := openstack.AuthOptionsFromEnv() 360 if err != nil { 361 return nil, err 362 } 363 364 err = UpdatePeerTenantDetails(&ao) 365 if err != nil { 366 return nil, err 367 } 368 369 client, err := openstack.AuthenticatedClient(ao) 370 if err != nil { 371 return nil, err 372 } 373 374 return openstack.NewNetworkV1(client, golangsdk.EndpointOpts{ 375 Region: os.Getenv("OS_REGION_NAME"), 376 }) 377 } 378 379 // NewNetworkV2Client returns a *ServiceClient for making calls to the 380 // OpenStack Networking v2 API. An error will be returned if authentication 381 // or client creation was not possible. 382 func NewNetworkV2Client() (*golangsdk.ServiceClient, error) { 383 ao, err := openstack.AuthOptionsFromEnv() 384 if err != nil { 385 return nil, err 386 } 387 388 client, err := openstack.AuthenticatedClient(ao) 389 if err != nil { 390 return nil, err 391 } 392 393 return openstack.NewNetworkV2(client, golangsdk.EndpointOpts{ 394 Region: os.Getenv("OS_REGION_NAME"), 395 }) 396 } 397 398 // NewPeerNetworkV2Client returns a *ServiceClient for making calls to the 399 // OpenStack Networking v2 API for Peer. An error will be returned if authentication 400 // or client creation was not possible. 401 func NewPeerNetworkV2Client() (*golangsdk.ServiceClient, error) { 402 ao, err := openstack.AuthOptionsFromEnv() 403 if err != nil { 404 return nil, err 405 } 406 407 err = UpdatePeerTenantDetails(&ao) 408 if err != nil { 409 return nil, err 410 } 411 412 client, err := openstack.AuthenticatedClient(ao) 413 if err != nil { 414 return nil, err 415 } 416 417 return openstack.NewNetworkV2(client, golangsdk.EndpointOpts{ 418 Region: os.Getenv("OS_REGION_NAME"), 419 }) 420 } 421 422 // NewObjectStorageV1Client returns a *ServiceClient for making calls to the 423 // OpenStack Object Storage v1 API. An error will be returned if authentication 424 // or client creation was not possible. 425 func NewObjectStorageV1Client() (*golangsdk.ServiceClient, error) { 426 ao, err := openstack.AuthOptionsFromEnv() 427 if err != nil { 428 return nil, err 429 } 430 431 client, err := openstack.AuthenticatedClient(ao) 432 if err != nil { 433 return nil, err 434 } 435 436 return openstack.NewObjectStorageV1(client, golangsdk.EndpointOpts{ 437 Region: os.Getenv("OS_REGION_NAME"), 438 }) 439 } 440 441 // NewSharedFileSystemV2Client returns a *ServiceClient for making calls 442 // to the OpenStack Shared File System v2 API. An error will be returned 443 // if authentication or client creation was not possible. 444 func NewSharedFileSystemV2Client() (*golangsdk.ServiceClient, error) { 445 ao, err := openstack.AuthOptionsFromEnv() 446 if err != nil { 447 return nil, err 448 } 449 450 client, err := openstack.AuthenticatedClient(ao) 451 if err != nil { 452 return nil, err 453 } 454 455 return openstack.NewSharedFileSystemV2(client, golangsdk.EndpointOpts{ 456 Region: os.Getenv("OS_REGION_NAME"), 457 }) 458 } 459 460 func UpdatePeerTenantDetails(ao *golangsdk.AuthOptions) error { 461 462 if peerTenantID := os.Getenv("OS_Peer_Tenant_ID"); peerTenantID != "" { 463 ao.TenantID = peerTenantID 464 ao.TenantName = "" 465 return nil 466 467 } else if peerTenantName := os.Getenv("OS_Peer_Tenant_Name"); peerTenantName != "" { 468 ao.TenantID = "" 469 ao.TenantName = peerTenantName 470 return nil 471 472 } else { 473 return fmt.Errorf("You're missing some important setup:\n OS_Peer_Tenant_ID or OS_Peer_Tenant_Name env variables must be provided.") 474 } 475 }