requests.go 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. package noauth
  2. import (
  3. "fmt"
  4. "strings"
  5. "devel.mephi.ru/iacherepanov/openstack-gophercloud"
  6. )
  7. // EndpointOpts specifies a "noauth" Cinder Endpoint.
  8. type EndpointOpts struct {
  9. // CinderEndpoint [required] is currently only used with "noauth" Cinder.
  10. // A cinder endpoint with "auth_strategy=noauth" is necessary, for example:
  11. // http://example.com:8776/v2.
  12. CinderEndpoint string
  13. }
  14. // NewClient prepares an unauthenticated ProviderClient instance.
  15. func NewClient(options gophercloud.AuthOptions) (*gophercloud.ProviderClient, error) {
  16. if options.Username == "" {
  17. options.Username = "admin"
  18. }
  19. if options.TenantName == "" {
  20. options.TenantName = "admin"
  21. }
  22. client := &gophercloud.ProviderClient{
  23. TokenID: fmt.Sprintf("%s:%s", options.Username, options.TenantName),
  24. }
  25. return client, nil
  26. }
  27. func initClientOpts(client *gophercloud.ProviderClient, eo EndpointOpts) (*gophercloud.ServiceClient, error) {
  28. sc := new(gophercloud.ServiceClient)
  29. if eo.CinderEndpoint == "" {
  30. return nil, fmt.Errorf("CinderEndpoint is required")
  31. }
  32. token := strings.Split(client.TokenID, ":")
  33. if len(token) != 2 {
  34. return nil, fmt.Errorf("Malformed noauth token")
  35. }
  36. endpoint := fmt.Sprintf("%s%s", gophercloud.NormalizeURL(eo.CinderEndpoint), token[1])
  37. sc.Endpoint = gophercloud.NormalizeURL(endpoint)
  38. sc.ProviderClient = client
  39. return sc, nil
  40. }
  41. // NewBlockStorageNoAuth creates a ServiceClient that may be used to access a
  42. // "noauth" block storage service (V2 or V3 Cinder API).
  43. func NewBlockStorageNoAuth(client *gophercloud.ProviderClient, eo EndpointOpts) (*gophercloud.ServiceClient, error) {
  44. return initClientOpts(client, eo)
  45. }