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

     1  package credentials
     2  
     3  import (
     4  	"github.com/gophercloud/gophercloud"
     5  	"github.com/gophercloud/gophercloud/pagination"
     6  )
     7  
     8  // Credential represents the Credential object
     9  type Credential struct {
    10  	// The ID of the credential.
    11  	ID string `json:"id"`
    12  	// Serialized Blob Credential.
    13  	Blob string `json:"blob"`
    14  	// ID of the user who owns the credential.
    15  	UserID string `json:"user_id"`
    16  	// The type of the credential.
    17  	Type string `json:"type"`
    18  	// The ID of the project the credential was created for.
    19  	ProjectID string `json:"project_id"`
    20  	// Links contains referencing links to the credential.
    21  	Links map[string]interface{} `json:"links"`
    22  }
    23  
    24  type credentialResult struct {
    25  	gophercloud.Result
    26  }
    27  
    28  // GetResult is the response from a Get operation. Call its Extract method
    29  // to interpret it as a Credential.
    30  type GetResult struct {
    31  	credentialResult
    32  }
    33  
    34  // CreateResult is the response from a Create operation. Call its Extract method
    35  // to interpret it as a Credential.
    36  type CreateResult struct {
    37  	credentialResult
    38  }
    39  
    40  // DeleteResult is the response from a Delete operation. Call its ExtractErr to
    41  // determine if the request succeeded or failed.
    42  type DeleteResult struct {
    43  	gophercloud.ErrResult
    44  }
    45  
    46  // UpdateResult is the result of an Update request. Call its Extract method to
    47  // interpret it as a Credential
    48  type UpdateResult struct {
    49  	credentialResult
    50  }
    51  
    52  // a CredentialPage is a single page of a Credential results.
    53  type CredentialPage struct {
    54  	pagination.LinkedPageBase
    55  }
    56  
    57  // IsEmpty determines whether or not a CredentialPage contains any results.
    58  func (r CredentialPage) IsEmpty() (bool, error) {
    59  	if r.StatusCode == 204 {
    60  		return true, nil
    61  	}
    62  
    63  	credentials, err := ExtractCredentials(r)
    64  	return len(credentials) == 0, err
    65  }
    66  
    67  // NextPageURL extracts the "next" link from the links section of the result.
    68  func (r CredentialPage) NextPageURL() (string, error) {
    69  	var s struct {
    70  		Links struct {
    71  			Next     string `json:"next"`
    72  			Previous string `json:"previous"`
    73  		} `json:"links"`
    74  	}
    75  	err := r.ExtractInto(&s)
    76  	if err != nil {
    77  		return "", err
    78  	}
    79  	return s.Links.Next, err
    80  }
    81  
    82  // Extract a Credential returns a slice of Credentials contained in a single page of results.
    83  func ExtractCredentials(r pagination.Page) ([]Credential, error) {
    84  	var s struct {
    85  		Credentials []Credential `json:"credentials"`
    86  	}
    87  	err := (r.(CredentialPage)).ExtractInto(&s)
    88  	return s.Credentials, err
    89  }
    90  
    91  // Extract interprets any credential results as a Credential.
    92  func (r credentialResult) Extract() (*Credential, error) {
    93  	var s struct {
    94  		Credential *Credential `json:"credential"`
    95  	}
    96  	err := r.ExtractInto(&s)
    97  	return s.Credential, err
    98  }