github.com/opentelekomcloud/gophertelekomcloud@v0.9.3/openstack/identity/v3/credentials/requests.go (about)

     1  package credentials
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/opentelekomcloud/gophertelekomcloud"
     7  )
     8  
     9  type Status string
    10  
    11  const parentElement = "credential"
    12  
    13  type ListOptsBuilder interface {
    14  	ToCredentialListQuery() (string, error)
    15  }
    16  
    17  type ListOpts struct {
    18  	UserID string `json:"user_id,omitempty"`
    19  }
    20  
    21  func (opts ListOpts) ToCredentialListQuery() (string, error) {
    22  	q, err := golangsdk.BuildQueryString(opts)
    23  	if err != nil {
    24  		return "", err
    25  	}
    26  	return q.String(), err
    27  }
    28  
    29  func List(client *golangsdk.ServiceClient, opts ListOptsBuilder) (l ListResult) {
    30  	url, err := golangsdk.NewURLBuilder().WithEndpoints(listURL(client)).WithQueryParams(&opts).Build()
    31  	if err != nil {
    32  		l.Err = err
    33  		return
    34  	}
    35  
    36  	_, l.Err = client.Get(url.String(), &l.Body, nil)
    37  	return
    38  }
    39  
    40  func Get(client *golangsdk.ServiceClient, credentialID string) (r GetResult) {
    41  	_, r.Err = client.Get(getURL(client, credentialID), &r.Body, nil)
    42  	return
    43  }
    44  
    45  type CreateOptsBuilder interface {
    46  	ToCredentialCreateMap() (map[string]interface{}, error)
    47  }
    48  
    49  type CreateOpts struct {
    50  	UserID      string `json:"user_id"`
    51  	Description string `json:"description"`
    52  }
    53  
    54  func (opts CreateOpts) ToCredentialCreateMap() (map[string]interface{}, error) {
    55  	return golangsdk.BuildRequestBody(opts, parentElement)
    56  }
    57  
    58  func Create(client *golangsdk.ServiceClient, opts CreateOptsBuilder) (r CreateResult) {
    59  	b, err := opts.ToCredentialCreateMap()
    60  	if err != nil {
    61  		r.Err = err
    62  		return
    63  	}
    64  	_, r.Err = client.Post(createURL(client), &b, &r.Body, nil)
    65  	return
    66  }
    67  
    68  type UpdateOptsBuilder interface {
    69  	ToCredentialUpdateMap() (map[string]interface{}, error)
    70  }
    71  
    72  type UpdateOpts struct {
    73  	Status      string `json:"status,omitempty"`
    74  	Description string `json:"description,omitempty"`
    75  }
    76  
    77  func (opts UpdateOpts) ToCredentialUpdateMap() (map[string]interface{}, error) {
    78  	return golangsdk.BuildRequestBody(opts, parentElement)
    79  }
    80  
    81  func Update(client *golangsdk.ServiceClient, credentialID string, opts UpdateOptsBuilder) (r CreateResult) {
    82  	b, err := opts.ToCredentialUpdateMap()
    83  	if err != nil {
    84  		r.Err = err
    85  		return
    86  	}
    87  	_, r.Err = client.Put(updateURL(client, credentialID), &b, &r.Body, &golangsdk.RequestOpts{
    88  		OkCodes: []int{200},
    89  	})
    90  	return
    91  }
    92  
    93  func Delete(client *golangsdk.ServiceClient, credentialID string) (r DeleteResult) {
    94  	_, r.Err = client.Delete(deleteURL(client, credentialID), nil)
    95  	return
    96  }
    97  
    98  type CreateTemporaryOptsBuilder interface {
    99  	ToTempCredentialCreateMap() (map[string]interface{}, error)
   100  }
   101  
   102  type CreateTemporaryOpts struct {
   103  	// For obtaining a temporary AK/SK with an agency token for use "assume_role"
   104  	// For user with federated token fill "token" in this field
   105  	Methods []string `json:"methods"`
   106  
   107  	// Common token or federated token required for obtaining a temporary AK/SK.
   108  	// You need to choose either the ID in this object or X-Auth-Token in the request header.
   109  	// X-Auth-Token takes priority over the ID in this object.
   110  	Token string `json:"token,omitempty"`
   111  	// Validity period (in seconds) of an AK/SK and security token.
   112  	// The value ranges from 15 minutes to 24 hours.
   113  	// The default value is 15 minutes.
   114  	Duration int `json:"duration_seconds,omitempty"`
   115  
   116  	// Name or ID of the domain to which the delegating party belongs
   117  	DomainName string `json:"domain_name,omitempty"`
   118  	DomainID   string `json:"domain_id,omitempty"`
   119  
   120  	// Name of the agency created by a delegating party
   121  	AgencyName string `json:"agency_name,omitempty"`
   122  }
   123  
   124  // ToTempCredentialCreateMap
   125  func (opts CreateTemporaryOpts) ToTempCredentialCreateMap() (map[string]interface{}, error) {
   126  	// doc: https://docs.otc.t-systems.com/en-us/api/iam/en-us_topic_0097949518.html
   127  
   128  	if len(opts.Methods) < 1 {
   129  		return nil, fmt.Errorf("no auth method provided")
   130  	}
   131  	if len(opts.Methods) > 1 {
   132  		return nil, fmt.Errorf("more than one auth method provided")
   133  	}
   134  
   135  	method := opts.Methods[0]
   136  
   137  	var authMap map[string]interface{}
   138  
   139  	switch method {
   140  	case "token":
   141  		token := map[string]interface{}{
   142  			"id": opts.Token,
   143  		}
   144  		if opts.Duration != 0 {
   145  			token["duration_seconds"] = opts.Duration
   146  		}
   147  
   148  		authMap = map[string]interface{}{
   149  			"auth": map[string]interface{}{
   150  				"identity": map[string]interface{}{
   151  					"methods": opts.Methods,
   152  					"token":   token,
   153  				},
   154  			},
   155  		}
   156  	case "assume_role":
   157  		assumeRole := map[string]interface{}{
   158  			"agency_name": opts.AgencyName,
   159  		}
   160  
   161  		switch {
   162  		case opts.Duration != 0:
   163  			assumeRole["duration_seconds"] = opts.Duration
   164  		case opts.DomainID != "":
   165  			assumeRole["domain_id"] = opts.DomainID
   166  		case opts.DomainName != "":
   167  			assumeRole["domain_name"] = opts.DomainName
   168  		default:
   169  			return nil, fmt.Errorf("you need to provide either delegating domain ID or Name")
   170  		}
   171  
   172  		authMap = map[string]interface{}{
   173  			"auth": map[string]interface{}{
   174  				"identity": map[string]interface{}{
   175  					"methods":     opts.Methods,
   176  					"assume_role": assumeRole,
   177  				},
   178  			},
   179  		}
   180  	default:
   181  		return nil, fmt.Errorf("unknown auth method provided: %s", method)
   182  	}
   183  
   184  	return authMap, nil
   185  }
   186  
   187  func CreateTemporary(client *golangsdk.ServiceClient, opts CreateTemporaryOptsBuilder) (r CreateTemporaryResult) {
   188  	json, err := opts.ToTempCredentialCreateMap()
   189  	if err != nil {
   190  		r.Err = err
   191  		return
   192  	}
   193  	_, r.Err = client.Post(createTempURL(client), &json, &r.Body, &golangsdk.RequestOpts{
   194  		OkCodes: []int{201},
   195  	})
   196  	return
   197  }