github.com/opentelekomcloud/gophertelekomcloud@v0.9.3/openstack/objectstorage/v1/swauth/requests.go (about)

     1  package swauth
     2  
     3  import "github.com/opentelekomcloud/gophertelekomcloud"
     4  
     5  // AuthOptsBuilder describes struct types that can be accepted by the Auth call.
     6  type AuthOptsBuilder interface {
     7  	ToAuthOptsMap() (map[string]string, error)
     8  }
     9  
    10  // AuthOpts specifies an authentication request.
    11  type AuthOpts struct {
    12  	// User is an Swauth-based username in username:tenant format.
    13  	User string `h:"X-Auth-User" required:"true"`
    14  
    15  	// Key is a secret/password to authenticate the User with.
    16  	Key string `h:"X-Auth-Key" required:"true"`
    17  }
    18  
    19  // ToAuthOptsMap formats an AuthOpts structure into a request body.
    20  func (opts AuthOpts) ToAuthOptsMap() (map[string]string, error) {
    21  	return golangsdk.BuildHeaders(opts)
    22  }
    23  
    24  // Auth performs an authentication request for a Swauth-based user.
    25  func Auth(c *golangsdk.ProviderClient, opts AuthOptsBuilder) (r GetAuthResult) {
    26  	h := make(map[string]string)
    27  
    28  	if opts != nil {
    29  		headers, err := opts.ToAuthOptsMap()
    30  		if err != nil {
    31  			r.Err = err
    32  			return
    33  		}
    34  
    35  		for k, v := range headers {
    36  			h[k] = v
    37  		}
    38  	}
    39  
    40  	resp, err := c.Request("GET", getURL(c), &golangsdk.RequestOpts{
    41  		MoreHeaders: h,
    42  		OkCodes:     []int{200},
    43  	})
    44  
    45  	if resp != nil {
    46  		r.Header = resp.Header
    47  	}
    48  
    49  	r.Err = err
    50  
    51  	return r
    52  }