github.com/huaweicloud/golangsdk@v0.0.0-20210831081626-d823fe11ceba/openstack/identity/v2/tokens/results.go (about)

     1  package tokens
     2  
     3  import (
     4  	"time"
     5  
     6  	"github.com/huaweicloud/golangsdk"
     7  	"github.com/huaweicloud/golangsdk/openstack/identity/v2/tenants"
     8  )
     9  
    10  // Token provides only the most basic information related to an authentication
    11  // token.
    12  type Token struct {
    13  	// ID provides the primary means of identifying a user to the OpenStack API.
    14  	// OpenStack defines this field as an opaque value, so do not depend on its
    15  	// content. It is safe, however, to compare for equality.
    16  	ID string
    17  
    18  	// ExpiresAt provides a timestamp in ISO 8601 format, indicating when the
    19  	// authentication token becomes invalid. After this point in time, future
    20  	// API requests made using this  authentication token will respond with
    21  	// errors. Either the caller will need to reauthenticate manually, or more
    22  	// preferably, the caller should exploit automatic re-authentication.
    23  	// See the AuthOptions structure for more details.
    24  	ExpiresAt time.Time
    25  
    26  	// Tenant provides information about the tenant to which this token grants
    27  	// access.
    28  	Tenant tenants.Tenant
    29  }
    30  
    31  // Role is a role for a user.
    32  type Role struct {
    33  	Name string `json:"name"`
    34  }
    35  
    36  // User is an OpenStack user.
    37  type User struct {
    38  	ID       string `json:"id"`
    39  	Name     string `json:"name"`
    40  	UserName string `json:"username"`
    41  	Roles    []Role `json:"roles"`
    42  }
    43  
    44  // Endpoint represents a single API endpoint offered by a service.
    45  // It provides the public and internal URLs, if supported, along with a region
    46  // specifier, again if provided.
    47  //
    48  // The significance of the Region field will depend upon your provider.
    49  //
    50  // In addition, the interface offered by the service will have version
    51  // information associated with it through the VersionId, VersionInfo, and
    52  // VersionList fields, if provided or supported.
    53  //
    54  // In all cases, fields which aren't supported by the provider and service
    55  // combined will assume a zero-value ("").
    56  type Endpoint struct {
    57  	TenantID    string `json:"tenantId"`
    58  	PublicURL   string `json:"publicURL"`
    59  	InternalURL string `json:"internalURL"`
    60  	AdminURL    string `json:"adminURL"`
    61  	Region      string `json:"region"`
    62  	VersionID   string `json:"versionId"`
    63  	VersionInfo string `json:"versionInfo"`
    64  	VersionList string `json:"versionList"`
    65  }
    66  
    67  // CatalogEntry provides a type-safe interface to an Identity API V2 service
    68  // catalog listing.
    69  //
    70  // Each class of service, such as cloud DNS or block storage services, will have
    71  // a single CatalogEntry representing it.
    72  //
    73  // Note: when looking for the desired service, try, whenever possible, to key
    74  // off the type field. Otherwise, you'll tie the representation of the service
    75  // to a specific provider.
    76  type CatalogEntry struct {
    77  	// Name will contain the provider-specified name for the service.
    78  	Name string `json:"name"`
    79  
    80  	// Type will contain a type string if OpenStack defines a type for the
    81  	// service. Otherwise, for provider-specific services, the provider may assign
    82  	// their own type strings.
    83  	Type string `json:"type"`
    84  
    85  	// Endpoints will let the caller iterate over all the different endpoints that
    86  	// may exist for the service.
    87  	Endpoints []Endpoint `json:"endpoints"`
    88  }
    89  
    90  // ServiceCatalog provides a view into the service catalog from a previous,
    91  // successful authentication.
    92  type ServiceCatalog struct {
    93  	Entries []CatalogEntry
    94  }
    95  
    96  // CreateResult is the response from a Create request. Use ExtractToken() to
    97  // interpret it as a Token, or ExtractServiceCatalog() to interpret it as a
    98  // service catalog.
    99  type CreateResult struct {
   100  	golangsdk.Result
   101  }
   102  
   103  // GetResult is the deferred response from a Get call, which is the same with a
   104  // Created token. Use ExtractUser() to interpret it as a User.
   105  type GetResult struct {
   106  	CreateResult
   107  }
   108  
   109  // ExtractToken returns the just-created Token from a CreateResult.
   110  func (r CreateResult) ExtractToken() (*Token, error) {
   111  	var s struct {
   112  		Access struct {
   113  			Token struct {
   114  				Expires string         `json:"expires"`
   115  				ID      string         `json:"id"`
   116  				Tenant  tenants.Tenant `json:"tenant"`
   117  			} `json:"token"`
   118  		} `json:"access"`
   119  	}
   120  
   121  	err := r.ExtractInto(&s)
   122  	if err != nil {
   123  		return nil, err
   124  	}
   125  
   126  	expiresTs, err := time.Parse(golangsdk.RFC3339Milli, s.Access.Token.Expires)
   127  	if err != nil {
   128  		return nil, err
   129  	}
   130  
   131  	return &Token{
   132  		ID:        s.Access.Token.ID,
   133  		ExpiresAt: expiresTs,
   134  		Tenant:    s.Access.Token.Tenant,
   135  	}, nil
   136  }
   137  
   138  // ExtractServiceCatalog returns the ServiceCatalog that was generated along
   139  // with the user's Token.
   140  func (r CreateResult) ExtractServiceCatalog() (*ServiceCatalog, error) {
   141  	var s struct {
   142  		Access struct {
   143  			Entries []CatalogEntry `json:"serviceCatalog"`
   144  		} `json:"access"`
   145  	}
   146  	err := r.ExtractInto(&s)
   147  	return &ServiceCatalog{Entries: s.Access.Entries}, err
   148  }
   149  
   150  // ExtractUser returns the User from a GetResult.
   151  func (r GetResult) ExtractUser() (*User, error) {
   152  	var s struct {
   153  		Access struct {
   154  			User User `json:"user"`
   155  		} `json:"access"`
   156  	}
   157  	err := r.ExtractInto(&s)
   158  	return &s.Access.User, err
   159  }