github.com/huaweicloud/golangsdk@v0.0.0-20210831081626-d823fe11ceba/openstack/blockstorage/noauth/requests.go (about)

     1  package noauth
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  
     7  	"github.com/huaweicloud/golangsdk"
     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 golangsdk.AuthOptions) (*golangsdk.ProviderClient, error) {
    20  	if options.Username == "" {
    21  		options.Username = "admin"
    22  	}
    23  	if options.TenantName == "" {
    24  		options.TenantName = "admin"
    25  	}
    26  
    27  	client := &golangsdk.ProviderClient{
    28  		TokenID: fmt.Sprintf("%s:%s", options.Username, options.TenantName),
    29  	}
    30  
    31  	return client, nil
    32  }
    33  
    34  func initClientOpts(client *golangsdk.ProviderClient, eo EndpointOpts) (*golangsdk.ServiceClient, error) {
    35  	sc := new(golangsdk.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", golangsdk.NormalizeURL(eo.CinderEndpoint), token[1])
    46  	sc.Endpoint = golangsdk.NormalizeURL(endpoint)
    47  	sc.ProviderClient = client
    48  	return sc, nil
    49  }
    50  
    51  // NewBlockStorageNoAuth creates a ServiceClient that may be used to access a
    52  // "noauth" block storage service (V2 or V3 Cinder API).
    53  func NewBlockStorageNoAuth(client *golangsdk.ProviderClient, eo EndpointOpts) (*golangsdk.ServiceClient, error) {
    54  	return initClientOpts(client, eo)
    55  }