github.com/gophercloud/gophercloud@v1.11.0/openstack/identity/v3/applicationcredentials/results.go (about)

     1  package applicationcredentials
     2  
     3  import (
     4  	"encoding/json"
     5  	"time"
     6  
     7  	"github.com/gophercloud/gophercloud"
     8  	"github.com/gophercloud/gophercloud/pagination"
     9  )
    10  
    11  type Role struct {
    12  	// DomainID is the domain ID the role belongs to.
    13  	DomainID string `json:"domain_id,omitempty"`
    14  	// ID is the unique ID of the role.
    15  	ID string `json:"id,omitempty"`
    16  	// Name is the role name
    17  	Name string `json:"name,omitempty"`
    18  }
    19  
    20  // ApplicationCredential represents the access rule object
    21  type AccessRule struct {
    22  	// The ID of the access rule
    23  	ID string `json:"id,omitempty"`
    24  	// The API path that the application credential is permitted to access
    25  	Path string `json:"path,omitempty"`
    26  	// The request method that the application credential is permitted to use for a
    27  	// given API endpoint
    28  	Method string `json:"method,omitempty"`
    29  	// The service type identifier for the service that the application credential
    30  	// is permitted to access
    31  	Service string `json:"service,omitempty"`
    32  }
    33  
    34  // ApplicationCredential represents the application credential object
    35  type ApplicationCredential struct {
    36  	// The ID of the application credential.
    37  	ID string `json:"id"`
    38  	// The name of the application credential.
    39  	Name string `json:"name"`
    40  	// A description of the application credential’s purpose.
    41  	Description string `json:"description"`
    42  	// A flag indicating whether the application credential may be used for creation or destruction of other application credentials or trusts.
    43  	// Defaults to false
    44  	Unrestricted bool `json:"unrestricted"`
    45  	// The secret for the application credential, either generated by the server or provided by the user.
    46  	// This is only ever shown once in the response to a create request. It is not stored nor ever shown again.
    47  	// If the secret is lost, a new application credential must be created.
    48  	Secret string `json:"secret"`
    49  	// The ID of the project the application credential was created for and that authentication requests using this application credential will be scoped to.
    50  	ProjectID string `json:"project_id"`
    51  	// A list of one or more roles that this application credential has associated with its project.
    52  	// A token using this application credential will have these same roles.
    53  	Roles []Role `json:"roles"`
    54  	// The expiration time of the application credential, if one was specified.
    55  	ExpiresAt time.Time `json:"-"`
    56  	// A list of access rules objects.
    57  	AccessRules []AccessRule `json:"access_rules,omitempty"`
    58  	// Links contains referencing links to the application credential.
    59  	Links map[string]interface{} `json:"links"`
    60  }
    61  
    62  func (r *ApplicationCredential) UnmarshalJSON(b []byte) error {
    63  	type tmp ApplicationCredential
    64  	var s struct {
    65  		tmp
    66  		ExpiresAt gophercloud.JSONRFC3339MilliNoZ `json:"expires_at"`
    67  	}
    68  	err := json.Unmarshal(b, &s)
    69  	if err != nil {
    70  		return err
    71  	}
    72  	*r = ApplicationCredential(s.tmp)
    73  
    74  	r.ExpiresAt = time.Time(s.ExpiresAt)
    75  
    76  	return nil
    77  }
    78  
    79  type applicationCredentialResult struct {
    80  	gophercloud.Result
    81  }
    82  
    83  // GetResult is the response from a Get operation. Call its Extract method
    84  // to interpret it as an ApplicationCredential.
    85  type GetResult struct {
    86  	applicationCredentialResult
    87  }
    88  
    89  // CreateResult is the response from a Create operation. Call its Extract method
    90  // to interpret it as an ApplicationCredential.
    91  type CreateResult struct {
    92  	applicationCredentialResult
    93  }
    94  
    95  // DeleteResult is the response from a Delete operation. Call its ExtractErr to
    96  // determine if the request succeeded or failed.
    97  type DeleteResult struct {
    98  	gophercloud.ErrResult
    99  }
   100  
   101  // an ApplicationCredentialPage is a single page of an ApplicationCredential results.
   102  type ApplicationCredentialPage struct {
   103  	pagination.LinkedPageBase
   104  }
   105  
   106  // IsEmpty determines whether or not a an ApplicationCredentialPage contains any results.
   107  func (r ApplicationCredentialPage) IsEmpty() (bool, error) {
   108  	if r.StatusCode == 204 {
   109  		return true, nil
   110  	}
   111  
   112  	applicationCredentials, err := ExtractApplicationCredentials(r)
   113  	return len(applicationCredentials) == 0, err
   114  }
   115  
   116  // NextPageURL extracts the "next" link from the links section of the result.
   117  func (r ApplicationCredentialPage) NextPageURL() (string, error) {
   118  	var s struct {
   119  		Links struct {
   120  			Next     string `json:"next"`
   121  			Previous string `json:"previous"`
   122  		} `json:"links"`
   123  	}
   124  	err := r.ExtractInto(&s)
   125  	if err != nil {
   126  		return "", err
   127  	}
   128  	return s.Links.Next, err
   129  }
   130  
   131  // Extractan ApplicationCredentials returns a slice of ApplicationCredentials contained in a single page of results.
   132  func ExtractApplicationCredentials(r pagination.Page) ([]ApplicationCredential, error) {
   133  	var s struct {
   134  		ApplicationCredentials []ApplicationCredential `json:"application_credentials"`
   135  	}
   136  	err := (r.(ApplicationCredentialPage)).ExtractInto(&s)
   137  	return s.ApplicationCredentials, err
   138  }
   139  
   140  // Extract interprets any application_credential results as an ApplicationCredential.
   141  func (r applicationCredentialResult) Extract() (*ApplicationCredential, error) {
   142  	var s struct {
   143  		ApplicationCredential *ApplicationCredential `json:"application_credential"`
   144  	}
   145  	err := r.ExtractInto(&s)
   146  	return s.ApplicationCredential, err
   147  }
   148  
   149  // GetAccessRuleResult is the response from a Get operation. Call its Extract method
   150  // to interpret it as an AccessRule.
   151  type GetAccessRuleResult struct {
   152  	gophercloud.Result
   153  }
   154  
   155  // an AccessRulePage is a single page of an AccessRule results.
   156  type AccessRulePage struct {
   157  	pagination.LinkedPageBase
   158  }
   159  
   160  // IsEmpty determines whether or not a an AccessRulePage contains any results.
   161  func (r AccessRulePage) IsEmpty() (bool, error) {
   162  	if r.StatusCode == 204 {
   163  		return true, nil
   164  	}
   165  
   166  	accessRules, err := ExtractAccessRules(r)
   167  	return len(accessRules) == 0, err
   168  }
   169  
   170  // NextPageURL extracts the "next" link from the links section of the result.
   171  func (r AccessRulePage) NextPageURL() (string, error) {
   172  	var s struct {
   173  		Links struct {
   174  			Next     string `json:"next"`
   175  			Previous string `json:"previous"`
   176  		} `json:"links"`
   177  	}
   178  	err := r.ExtractInto(&s)
   179  	if err != nil {
   180  		return "", err
   181  	}
   182  	return s.Links.Next, err
   183  }
   184  
   185  // ExtractAccessRules returns a slice of AccessRules contained in a single page of results.
   186  func ExtractAccessRules(r pagination.Page) ([]AccessRule, error) {
   187  	var s struct {
   188  		AccessRules []AccessRule `json:"access_rules"`
   189  	}
   190  	err := (r.(AccessRulePage)).ExtractInto(&s)
   191  	return s.AccessRules, err
   192  }
   193  
   194  // Extract interprets any access_rule results as an AccessRule.
   195  func (r GetAccessRuleResult) Extract() (*AccessRule, error) {
   196  	var s struct {
   197  		AccessRule *AccessRule `json:"access_rule"`
   198  	}
   199  	err := r.ExtractInto(&s)
   200  	return s.AccessRule, err
   201  }