123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556 |
- // Package clients contains functions for creating OpenStack service clients
- // for use in acceptance tests. It also manages the required environment
- // variables to run the tests.
- package clients
- import (
- "fmt"
- "net/http"
- "os"
- "strings"
- "devel.mephi.ru/iacherepanov/openstack-gophercloud"
- "devel.mephi.ru/iacherepanov/openstack-gophercloud/openstack"
- "devel.mephi.ru/iacherepanov/openstack-gophercloud/openstack/blockstorage/noauth"
- )
- // AcceptanceTestChoices contains image and flavor selections for use by the acceptance tests.
- type AcceptanceTestChoices struct {
- // ImageID contains the ID of a valid image.
- ImageID string
- // FlavorID contains the ID of a valid flavor.
- FlavorID string
- // FlavorIDResize contains the ID of a different flavor available on the same OpenStack installation, that is distinct
- // from FlavorID.
- FlavorIDResize string
- // FloatingIPPool contains the name of the pool from where to obtain floating IPs.
- FloatingIPPoolName string
- // NetworkName is the name of a network to launch the instance on.
- NetworkName string
- // ExternalNetworkID is the network ID of the external network.
- ExternalNetworkID string
- // ShareNetworkID is the Manila Share network ID
- ShareNetworkID string
- // DBDatastoreType is the datastore type for DB tests.
- DBDatastoreType string
- // DBDatastoreTypeID is the datastore type version for DB tests.
- DBDatastoreVersion string
- }
- // AcceptanceTestChoicesFromEnv populates a ComputeChoices struct from environment variables.
- // If any required state is missing, an `error` will be returned that enumerates the missing properties.
- func AcceptanceTestChoicesFromEnv() (*AcceptanceTestChoices, error) {
- imageID := os.Getenv("OS_IMAGE_ID")
- flavorID := os.Getenv("OS_FLAVOR_ID")
- flavorIDResize := os.Getenv("OS_FLAVOR_ID_RESIZE")
- networkName := os.Getenv("OS_NETWORK_NAME")
- floatingIPPoolName := os.Getenv("OS_POOL_NAME")
- externalNetworkID := os.Getenv("OS_EXTGW_ID")
- shareNetworkID := os.Getenv("OS_SHARE_NETWORK_ID")
- dbDatastoreType := os.Getenv("OS_DB_DATASTORE_TYPE")
- dbDatastoreVersion := os.Getenv("OS_DB_DATASTORE_VERSION")
- missing := make([]string, 0, 3)
- if imageID == "" {
- missing = append(missing, "OS_IMAGE_ID")
- }
- if flavorID == "" {
- missing = append(missing, "OS_FLAVOR_ID")
- }
- if flavorIDResize == "" {
- missing = append(missing, "OS_FLAVOR_ID_RESIZE")
- }
- if floatingIPPoolName == "" {
- missing = append(missing, "OS_POOL_NAME")
- }
- if externalNetworkID == "" {
- missing = append(missing, "OS_EXTGW_ID")
- }
- if networkName == "" {
- networkName = "private"
- }
- if shareNetworkID == "" {
- missing = append(missing, "OS_SHARE_NETWORK_ID")
- }
- notDistinct := ""
- if flavorID == flavorIDResize {
- notDistinct = "OS_FLAVOR_ID and OS_FLAVOR_ID_RESIZE must be distinct."
- }
- if len(missing) > 0 || notDistinct != "" {
- text := "You're missing some important setup:\n"
- if len(missing) > 0 {
- text += " * These environment variables must be provided: " + strings.Join(missing, ", ") + "\n"
- }
- if notDistinct != "" {
- text += " * " + notDistinct + "\n"
- }
- return nil, fmt.Errorf(text)
- }
- return &AcceptanceTestChoices{
- ImageID: imageID,
- FlavorID: flavorID,
- FlavorIDResize: flavorIDResize,
- FloatingIPPoolName: floatingIPPoolName,
- NetworkName: networkName,
- ExternalNetworkID: externalNetworkID,
- ShareNetworkID: shareNetworkID,
- DBDatastoreType: dbDatastoreType,
- DBDatastoreVersion: dbDatastoreVersion,
- }, nil
- }
- // NewBlockStorageV1Client returns a *ServiceClient for making calls
- // to the OpenStack Block Storage v1 API. An error will be returned
- // if authentication or client creation was not possible.
- func NewBlockStorageV1Client() (*gophercloud.ServiceClient, error) {
- ao, err := openstack.AuthOptionsFromEnv()
- if err != nil {
- return nil, err
- }
- client, err := openstack.AuthenticatedClient(ao)
- if err != nil {
- return nil, err
- }
- client = configureDebug(client)
- return openstack.NewBlockStorageV1(client, gophercloud.EndpointOpts{
- Region: os.Getenv("OS_REGION_NAME"),
- })
- }
- // NewBlockStorageV2Client returns a *ServiceClient for making calls
- // to the OpenStack Block Storage v2 API. An error will be returned
- // if authentication or client creation was not possible.
- func NewBlockStorageV2Client() (*gophercloud.ServiceClient, error) {
- ao, err := openstack.AuthOptionsFromEnv()
- if err != nil {
- return nil, err
- }
- client, err := openstack.AuthenticatedClient(ao)
- if err != nil {
- return nil, err
- }
- client = configureDebug(client)
- return openstack.NewBlockStorageV2(client, gophercloud.EndpointOpts{
- Region: os.Getenv("OS_REGION_NAME"),
- })
- }
- // NewBlockStorageV3Client returns a *ServiceClient for making calls
- // to the OpenStack Block Storage v3 API. An error will be returned
- // if authentication or client creation was not possible.
- func NewBlockStorageV3Client() (*gophercloud.ServiceClient, error) {
- ao, err := openstack.AuthOptionsFromEnv()
- if err != nil {
- return nil, err
- }
- client, err := openstack.AuthenticatedClient(ao)
- if err != nil {
- return nil, err
- }
- client = configureDebug(client)
- return openstack.NewBlockStorageV3(client, gophercloud.EndpointOpts{
- Region: os.Getenv("OS_REGION_NAME"),
- })
- }
- // NewBlockStorageV2NoAuthClient returns a noauth *ServiceClient for
- // making calls to the OpenStack Block Storage v2 API. An error will be
- // returned if client creation was not possible.
- func NewBlockStorageV2NoAuthClient() (*gophercloud.ServiceClient, error) {
- client, err := noauth.NewClient(gophercloud.AuthOptions{
- Username: os.Getenv("OS_USERNAME"),
- TenantName: os.Getenv("OS_TENANT_NAME"),
- })
- if err != nil {
- return nil, err
- }
- client = configureDebug(client)
- return noauth.NewBlockStorageNoAuth(client, noauth.EndpointOpts{
- CinderEndpoint: os.Getenv("CINDER_ENDPOINT"),
- })
- }
- // NewBlockStorageV3NoAuthClient returns a noauth *ServiceClient for
- // making calls to the OpenStack Block Storage v2 API. An error will be
- // returned if client creation was not possible.
- func NewBlockStorageV3NoAuthClient() (*gophercloud.ServiceClient, error) {
- client, err := noauth.NewClient(gophercloud.AuthOptions{
- Username: os.Getenv("OS_USERNAME"),
- TenantName: os.Getenv("OS_TENANT_NAME"),
- })
- if err != nil {
- return nil, err
- }
- client = configureDebug(client)
- return noauth.NewBlockStorageNoAuth(client, noauth.EndpointOpts{
- CinderEndpoint: os.Getenv("CINDER_ENDPOINT"),
- })
- }
- // NewComputeV2Client returns a *ServiceClient for making calls
- // to the OpenStack Compute v2 API. An error will be returned
- // if authentication or client creation was not possible.
- func NewComputeV2Client() (*gophercloud.ServiceClient, error) {
- ao, err := openstack.AuthOptionsFromEnv()
- if err != nil {
- return nil, err
- }
- client, err := openstack.AuthenticatedClient(ao)
- if err != nil {
- return nil, err
- }
- client = configureDebug(client)
- return openstack.NewComputeV2(client, gophercloud.EndpointOpts{
- Region: os.Getenv("OS_REGION_NAME"),
- })
- }
- // NewDBV1Client returns a *ServiceClient for making calls
- // to the OpenStack Database v1 API. An error will be returned
- // if authentication or client creation was not possible.
- func NewDBV1Client() (*gophercloud.ServiceClient, error) {
- ao, err := openstack.AuthOptionsFromEnv()
- if err != nil {
- return nil, err
- }
- client, err := openstack.AuthenticatedClient(ao)
- if err != nil {
- return nil, err
- }
- client = configureDebug(client)
- return openstack.NewDBV1(client, gophercloud.EndpointOpts{
- Region: os.Getenv("OS_REGION_NAME"),
- })
- }
- // NewDNSV2Client returns a *ServiceClient for making calls
- // to the OpenStack Compute v2 API. An error will be returned
- // if authentication or client creation was not possible.
- func NewDNSV2Client() (*gophercloud.ServiceClient, error) {
- ao, err := openstack.AuthOptionsFromEnv()
- if err != nil {
- return nil, err
- }
- client, err := openstack.AuthenticatedClient(ao)
- if err != nil {
- return nil, err
- }
- client = configureDebug(client)
- return openstack.NewDNSV2(client, gophercloud.EndpointOpts{
- Region: os.Getenv("OS_REGION_NAME"),
- })
- }
- // NewIdentityV2Client returns a *ServiceClient for making calls
- // to the OpenStack Identity v2 API. An error will be returned
- // if authentication or client creation was not possible.
- func NewIdentityV2Client() (*gophercloud.ServiceClient, error) {
- ao, err := openstack.AuthOptionsFromEnv()
- if err != nil {
- return nil, err
- }
- client, err := openstack.AuthenticatedClient(ao)
- if err != nil {
- return nil, err
- }
- client = configureDebug(client)
- return openstack.NewIdentityV2(client, gophercloud.EndpointOpts{
- Region: os.Getenv("OS_REGION_NAME"),
- })
- }
- // NewIdentityV2AdminClient returns a *ServiceClient for making calls
- // to the Admin Endpoint of the OpenStack Identity v2 API. An error
- // will be returned if authentication or client creation was not possible.
- func NewIdentityV2AdminClient() (*gophercloud.ServiceClient, error) {
- ao, err := openstack.AuthOptionsFromEnv()
- if err != nil {
- return nil, err
- }
- client, err := openstack.AuthenticatedClient(ao)
- if err != nil {
- return nil, err
- }
- client = configureDebug(client)
- return openstack.NewIdentityV2(client, gophercloud.EndpointOpts{
- Region: os.Getenv("OS_REGION_NAME"),
- Availability: gophercloud.AvailabilityAdmin,
- })
- }
- // NewIdentityV2UnauthenticatedClient returns an unauthenticated *ServiceClient
- // for the OpenStack Identity v2 API. An error will be returned if
- // authentication or client creation was not possible.
- func NewIdentityV2UnauthenticatedClient() (*gophercloud.ServiceClient, error) {
- ao, err := openstack.AuthOptionsFromEnv()
- if err != nil {
- return nil, err
- }
- client, err := openstack.NewClient(ao.IdentityEndpoint)
- if err != nil {
- return nil, err
- }
- client = configureDebug(client)
- return openstack.NewIdentityV2(client, gophercloud.EndpointOpts{})
- }
- // NewIdentityV3Client returns a *ServiceClient for making calls
- // to the OpenStack Identity v3 API. An error will be returned
- // if authentication or client creation was not possible.
- func NewIdentityV3Client() (*gophercloud.ServiceClient, error) {
- ao, err := openstack.AuthOptionsFromEnv()
- if err != nil {
- return nil, err
- }
- client, err := openstack.AuthenticatedClient(ao)
- if err != nil {
- return nil, err
- }
- client = configureDebug(client)
- return openstack.NewIdentityV3(client, gophercloud.EndpointOpts{
- Region: os.Getenv("OS_REGION_NAME"),
- })
- }
- // NewIdentityV3UnauthenticatedClient returns an unauthenticated *ServiceClient
- // for the OpenStack Identity v3 API. An error will be returned if
- // authentication or client creation was not possible.
- func NewIdentityV3UnauthenticatedClient() (*gophercloud.ServiceClient, error) {
- ao, err := openstack.AuthOptionsFromEnv()
- if err != nil {
- return nil, err
- }
- client, err := openstack.NewClient(ao.IdentityEndpoint)
- if err != nil {
- return nil, err
- }
- client = configureDebug(client)
- return openstack.NewIdentityV3(client, gophercloud.EndpointOpts{})
- }
- // NewImageServiceV2Client returns a *ServiceClient for making calls to the
- // OpenStack Image v2 API. An error will be returned if authentication or
- // client creation was not possible.
- func NewImageServiceV2Client() (*gophercloud.ServiceClient, error) {
- ao, err := openstack.AuthOptionsFromEnv()
- if err != nil {
- return nil, err
- }
- client, err := openstack.AuthenticatedClient(ao)
- if err != nil {
- return nil, err
- }
- client = configureDebug(client)
- return openstack.NewImageServiceV2(client, gophercloud.EndpointOpts{
- Region: os.Getenv("OS_REGION_NAME"),
- })
- }
- // NewNetworkV2Client returns a *ServiceClient for making calls to the
- // OpenStack Networking v2 API. An error will be returned if authentication
- // or client creation was not possible.
- func NewNetworkV2Client() (*gophercloud.ServiceClient, error) {
- ao, err := openstack.AuthOptionsFromEnv()
- if err != nil {
- return nil, err
- }
- client, err := openstack.AuthenticatedClient(ao)
- if err != nil {
- return nil, err
- }
- client = configureDebug(client)
- return openstack.NewNetworkV2(client, gophercloud.EndpointOpts{
- Region: os.Getenv("OS_REGION_NAME"),
- })
- }
- // NewObjectStorageV1Client returns a *ServiceClient for making calls to the
- // OpenStack Object Storage v1 API. An error will be returned if authentication
- // or client creation was not possible.
- func NewObjectStorageV1Client() (*gophercloud.ServiceClient, error) {
- ao, err := openstack.AuthOptionsFromEnv()
- if err != nil {
- return nil, err
- }
- client, err := openstack.AuthenticatedClient(ao)
- if err != nil {
- return nil, err
- }
- client = configureDebug(client)
- return openstack.NewObjectStorageV1(client, gophercloud.EndpointOpts{
- Region: os.Getenv("OS_REGION_NAME"),
- })
- }
- // NewSharedFileSystemV2Client returns a *ServiceClient for making calls
- // to the OpenStack Shared File System v2 API. An error will be returned
- // if authentication or client creation was not possible.
- func NewSharedFileSystemV2Client() (*gophercloud.ServiceClient, error) {
- ao, err := openstack.AuthOptionsFromEnv()
- if err != nil {
- return nil, err
- }
- client, err := openstack.AuthenticatedClient(ao)
- if err != nil {
- return nil, err
- }
- client = configureDebug(client)
- return openstack.NewSharedFileSystemV2(client, gophercloud.EndpointOpts{
- Region: os.Getenv("OS_REGION_NAME"),
- })
- }
- // NewLoadBalancerV2Client returns a *ServiceClient for making calls to the
- // OpenStack Octavia v2 API. An error will be returned if authentication
- // or client creation was not possible.
- func NewLoadBalancerV2Client() (*gophercloud.ServiceClient, error) {
- ao, err := openstack.AuthOptionsFromEnv()
- if err != nil {
- return nil, err
- }
- client, err := openstack.AuthenticatedClient(ao)
- if err != nil {
- return nil, err
- }
- client = configureDebug(client)
- return openstack.NewLoadBalancerV2(client, gophercloud.EndpointOpts{
- Region: os.Getenv("OS_REGION_NAME"),
- })
- }
- // NewClusteringV1Client returns a *ServiceClient for making calls
- // to the OpenStack Clustering v1 API. An error will be returned
- // if authentication or client creation was not possible.
- func NewClusteringV1Client() (*gophercloud.ServiceClient, error) {
- ao, err := openstack.AuthOptionsFromEnv()
- if err != nil {
- return nil, err
- }
- client, err := openstack.AuthenticatedClient(ao)
- if err != nil {
- return nil, err
- }
- return openstack.NewClusteringV1(client, gophercloud.EndpointOpts{
- Region: os.Getenv("OS_REGION_NAME"),
- })
- }
- // NewMessagingV2Client returns a *ServiceClient for making calls
- // to the OpenStack Messaging (Zaqar) v2 API. An error will be returned
- // if authentication or client creation was not possible.
- func NewMessagingV2Client(clientID string) (*gophercloud.ServiceClient, error) {
- ao, err := openstack.AuthOptionsFromEnv()
- if err != nil {
- return nil, err
- }
- client, err := openstack.AuthenticatedClient(ao)
- if err != nil {
- return nil, err
- }
- client = configureDebug(client)
- return openstack.NewMessagingV2(client, clientID, gophercloud.EndpointOpts{
- Region: os.Getenv("OS_REGION_NAME"),
- })
- }
- // NewContainerV1Client returns a *ServiceClient for making calls
- // to the OpenStack Container V1 API. An error will be returned
- // if authentication or client creation was not possible.
- func NewContainerV1Client() (*gophercloud.ServiceClient, error) {
- ao, err := openstack.AuthOptionsFromEnv()
- if err != nil {
- return nil, err
- }
- client, err := openstack.AuthenticatedClient(ao)
- if err != nil {
- return nil, err
- }
- return openstack.NewContainerV1(client, gophercloud.EndpointOpts{
- Region: os.Getenv("OS_REGION_NAME"),
- })
- }
- // configureDebug will configure the provider client to print the API
- // requests and responses if OS_DEBUG is enabled.
- func configureDebug(client *gophercloud.ProviderClient) *gophercloud.ProviderClient {
- if os.Getenv("OS_DEBUG") != "" {
- client.HTTPClient = http.Client{
- Transport: &LogRoundTripper{
- Rt: &http.Transport{},
- },
- }
- }
- return client
- }
|