github.com/vnpaycloud-console/gophercloud/v2@v2.0.5/openstack/identity/v3/tokens/results.go (about)

     1  package tokens
     2  
     3  import (
     4  	"time"
     5  
     6  	"github.com/vnpaycloud-console/gophercloud/v2"
     7  )
     8  
     9  // Endpoint represents a single API endpoint offered by a service.
    10  // It matches either a public, internal or admin URL.
    11  // If supported, it contains a region specifier, again if provided.
    12  // The significance of the Region field will depend upon your provider.
    13  type Endpoint struct {
    14  	ID        string `json:"id"`
    15  	Region    string `json:"region"`
    16  	RegionID  string `json:"region_id"`
    17  	Interface string `json:"interface"`
    18  	URL       string `json:"url"`
    19  }
    20  
    21  // CatalogEntry provides a type-safe interface to an Identity API V3 service
    22  // catalog listing. Each class of service, such as cloud DNS or block storage
    23  // services, could have multiple CatalogEntry representing it (one by interface
    24  // type, e.g public, admin or internal).
    25  //
    26  // Note: when looking for the desired service, try, whenever possible, to key
    27  // off the type field. Otherwise, you'll tie the representation of the service
    28  // to a specific provider.
    29  type CatalogEntry struct {
    30  	// Service ID
    31  	ID string `json:"id"`
    32  
    33  	// Name will contain the provider-specified name for the service.
    34  	Name string `json:"name"`
    35  
    36  	// Type will contain a type string if OpenStack defines a type for the
    37  	// service. Otherwise, for provider-specific services, the provider may
    38  	// assign their own type strings.
    39  	Type string `json:"type"`
    40  
    41  	// Endpoints will let the caller iterate over all the different endpoints that
    42  	// may exist for the service.
    43  	Endpoints []Endpoint `json:"endpoints"`
    44  }
    45  
    46  // ServiceCatalog provides a view into the service catalog from a previous,
    47  // successful authentication.
    48  type ServiceCatalog struct {
    49  	Entries []CatalogEntry `json:"catalog"`
    50  }
    51  
    52  // Domain provides information about the domain to which this token grants
    53  // access.
    54  type Domain struct {
    55  	ID   string `json:"id"`
    56  	Name string `json:"name"`
    57  }
    58  
    59  // User represents a user resource that exists in the Identity Service.
    60  type User struct {
    61  	Domain Domain `json:"domain"`
    62  	ID     string `json:"id"`
    63  	Name   string `json:"name"`
    64  }
    65  
    66  // Role provides information about roles to which User is authorized.
    67  type Role struct {
    68  	ID   string `json:"id"`
    69  	Name string `json:"name"`
    70  }
    71  
    72  // Project provides information about project to which User is authorized.
    73  type Project struct {
    74  	Domain Domain `json:"domain"`
    75  	ID     string `json:"id"`
    76  	Name   string `json:"name"`
    77  }
    78  
    79  type TrustUser struct {
    80  	ID string `json:"id"`
    81  }
    82  
    83  // Trust provides information about trust with which User is authorized.
    84  type Trust struct {
    85  	ID            string    `json:"id"`
    86  	Impersonation bool      `json:"impersonation"`
    87  	TrusteeUserID TrustUser `json:"trustee_user"`
    88  	TrustorUserID TrustUser `json:"trustor_user"`
    89  }
    90  
    91  // commonResult is the response from a request. A commonResult has various
    92  // methods which can be used to extract different details about the result.
    93  type commonResult struct {
    94  	gophercloud.Result
    95  }
    96  
    97  // Extract is a shortcut for ExtractToken.
    98  // This function is deprecated and still present for backward compatibility.
    99  func (r commonResult) Extract() (*Token, error) {
   100  	return r.ExtractToken()
   101  }
   102  
   103  // ExtractToken interprets a commonResult as a Token.
   104  func (r commonResult) ExtractToken() (*Token, error) {
   105  	var s Token
   106  	err := r.ExtractInto(&s)
   107  	if err != nil {
   108  		return nil, err
   109  	}
   110  
   111  	// Parse the token itself from the stored headers.
   112  	s.ID = r.Header.Get("X-Subject-Token")
   113  
   114  	return &s, err
   115  }
   116  
   117  // ExtractTokenID implements the gophercloud.AuthResult interface. The returned
   118  // string is the same as the ID field of the Token struct returned from
   119  // ExtractToken().
   120  func (r CreateResult) ExtractTokenID() (string, error) {
   121  	return r.Header.Get("X-Subject-Token"), r.Err
   122  }
   123  
   124  // ExtractTokenID implements the gophercloud.AuthResult interface. The returned
   125  // string is the same as the ID field of the Token struct returned from
   126  // ExtractToken().
   127  func (r GetResult) ExtractTokenID() (string, error) {
   128  	return r.Header.Get("X-Subject-Token"), r.Err
   129  }
   130  
   131  // ExtractServiceCatalog returns the ServiceCatalog that was generated along
   132  // with the user's Token.
   133  func (r commonResult) ExtractServiceCatalog() (*ServiceCatalog, error) {
   134  	var s ServiceCatalog
   135  	err := r.ExtractInto(&s)
   136  	return &s, err
   137  }
   138  
   139  // ExtractUser returns the User that is the owner of the Token.
   140  func (r commonResult) ExtractUser() (*User, error) {
   141  	var s struct {
   142  		User *User `json:"user"`
   143  	}
   144  	err := r.ExtractInto(&s)
   145  	return s.User, err
   146  }
   147  
   148  // ExtractRoles returns Roles to which User is authorized.
   149  func (r commonResult) ExtractRoles() ([]Role, error) {
   150  	var s struct {
   151  		Roles []Role `json:"roles"`
   152  	}
   153  	err := r.ExtractInto(&s)
   154  	return s.Roles, err
   155  }
   156  
   157  // ExtractProject returns Project to which User is authorized.
   158  func (r commonResult) ExtractProject() (*Project, error) {
   159  	var s struct {
   160  		Project *Project `json:"project"`
   161  	}
   162  	err := r.ExtractInto(&s)
   163  	return s.Project, err
   164  }
   165  
   166  // ExtractDomain returns Domain to which User is authorized.
   167  func (r commonResult) ExtractDomain() (*Domain, error) {
   168  	var s struct {
   169  		Domain *Domain `json:"domain"`
   170  	}
   171  	err := r.ExtractInto(&s)
   172  	return s.Domain, err
   173  }
   174  
   175  // ExtractTrust returns Trust to which User is authorized.
   176  func (r commonResult) ExtractTrust() (*Trust, error) {
   177  	var s struct {
   178  		Trust *Trust `json:"OS-TRUST:trust"`
   179  	}
   180  	err := r.ExtractInto(&s)
   181  	return s.Trust, err
   182  }
   183  
   184  // CreateResult is the response from a Create request. Use ExtractToken()
   185  // to interpret it as a Token, or ExtractServiceCatalog() to interpret it
   186  // as a service catalog.
   187  type CreateResult struct {
   188  	commonResult
   189  }
   190  
   191  // GetResult is the response from a Get request. Use ExtractToken()
   192  // to interpret it as a Token, or ExtractServiceCatalog() to interpret it
   193  // as a service catalog.
   194  type GetResult struct {
   195  	commonResult
   196  }
   197  
   198  // RevokeResult is response from a Revoke request.
   199  type RevokeResult struct {
   200  	commonResult
   201  }
   202  
   203  // Token is a string that grants a user access to a controlled set of services
   204  // in an OpenStack provider. Each Token is valid for a set length of time.
   205  type Token struct {
   206  	// ID is the issued token.
   207  	ID string `json:"id"`
   208  
   209  	// ExpiresAt is the timestamp at which this token will no longer be accepted.
   210  	ExpiresAt time.Time `json:"expires_at"`
   211  }
   212  
   213  func (r commonResult) ExtractInto(v any) error {
   214  	return r.ExtractIntoStructPtr(v, "token")
   215  }