github.com/gophercloud/gophercloud@v1.11.0/openstack/blockstorage/noauth/requests.go (about)

     1  package noauth
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  
     7  	"github.com/gophercloud/gophercloud"
     8  )
     9  
    10  // EndpointOpts specifies a "noauth" Cinder Endpoint.
    11  type EndpointOpts struct {
    12  	// CinderEndpoint [required] is currently only used with "noauth" Cinder.
    13  	// A cinder endpoint with "auth_strategy=noauth" is necessary, for example:
    14  	// http://example.com:8776/v2.
    15  	CinderEndpoint string
    16  }
    17  
    18  // NewClient prepares an unauthenticated ProviderClient instance.
    19  func NewClient(options gophercloud.AuthOptions) (*gophercloud.ProviderClient, error) {
    20  	if options.Username == "" {
    21  		options.Username = "admin"
    22  	}
    23  	if options.TenantName == "" {
    24  		options.TenantName = "admin"
    25  	}
    26  
    27  	client := &gophercloud.ProviderClient{
    28  		TokenID: fmt.Sprintf("%s:%s", options.Username, options.TenantName),
    29  	}
    30  
    31  	return client, nil
    32  }
    33  
    34  func initClientOpts(client *gophercloud.ProviderClient, eo EndpointOpts, clientType string) (*gophercloud.ServiceClient, error) {
    35  	sc := new(gophercloud.ServiceClient)
    36  	if eo.CinderEndpoint == "" {
    37  		return nil, fmt.Errorf("CinderEndpoint is required")
    38  	}
    39  
    40  	token := strings.Split(client.TokenID, ":")
    41  	if len(token) != 2 {
    42  		return nil, fmt.Errorf("Malformed noauth token")
    43  	}
    44  
    45  	endpoint := fmt.Sprintf("%s%s", gophercloud.NormalizeURL(eo.CinderEndpoint), token[1])
    46  	sc.Endpoint = gophercloud.NormalizeURL(endpoint)
    47  	sc.ProviderClient = client
    48  	sc.Type = clientType
    49  	return sc, nil
    50  }
    51  
    52  // NewBlockStorageNoAuthV2 creates a ServiceClient that may be used to access "noauth" v2 block storage service.
    53  func NewBlockStorageNoAuthV2(client *gophercloud.ProviderClient, eo EndpointOpts) (*gophercloud.ServiceClient, error) {
    54  	return initClientOpts(client, eo, "volumev2")
    55  }
    56  
    57  // NewBlockStorageNoAuthV3 creates a ServiceClient that may be used to access "noauth" v3 block storage service.
    58  func NewBlockStorageNoAuthV3(client *gophercloud.ProviderClient, eo EndpointOpts) (*gophercloud.ServiceClient, error) {
    59  	return initClientOpts(client, eo, "volumev3")
    60  }