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

     1  package policies
     2  
     3  import (
     4  	"encoding/json"
     5  
     6  	"github.com/gophercloud/gophercloud"
     7  	"github.com/gophercloud/gophercloud/pagination"
     8  )
     9  
    10  // Policy is an arbitrarily serialized policy engine rule
    11  // set to be consumed by a remote service.
    12  type Policy struct {
    13  	// ID is the unique ID of the policy.
    14  	ID string `json:"id"`
    15  
    16  	// Blob is the policy rule as a serialized blob.
    17  	Blob string `json:"blob"`
    18  
    19  	// Type is the MIME media type of the serialized policy blob.
    20  	Type string `json:"type"`
    21  
    22  	// Links contains referencing links to the policy.
    23  	Links map[string]interface{} `json:"links"`
    24  
    25  	// Extra is a collection of miscellaneous key/values.
    26  	Extra map[string]interface{} `json:"-"`
    27  }
    28  
    29  func (r *Policy) UnmarshalJSON(b []byte) error {
    30  	type tmp Policy
    31  	var s struct {
    32  		tmp
    33  		Extra map[string]interface{} `json:"extra"`
    34  	}
    35  	err := json.Unmarshal(b, &s)
    36  	if err != nil {
    37  		return err
    38  	}
    39  	*r = Policy(s.tmp)
    40  
    41  	// Collect other fields and bundle them into Extra
    42  	// but only if a field titled "extra" wasn't sent.
    43  	if s.Extra != nil {
    44  		r.Extra = s.Extra
    45  	} else {
    46  		var result interface{}
    47  		err := json.Unmarshal(b, &result)
    48  		if err != nil {
    49  			return err
    50  		}
    51  		if resultMap, ok := result.(map[string]interface{}); ok {
    52  			r.Extra = gophercloud.RemainingKeys(Policy{}, resultMap)
    53  		}
    54  	}
    55  
    56  	return err
    57  }
    58  
    59  type policyResult struct {
    60  	gophercloud.Result
    61  }
    62  
    63  // CreateResult is the response from a Create operation. Call its Extract method
    64  // to interpret it as a Policy
    65  type CreateResult struct {
    66  	policyResult
    67  }
    68  
    69  // GetResult is the response from a Get operation. Call its Extract method
    70  // to interpret it as a Policy.
    71  type GetResult struct {
    72  	policyResult
    73  }
    74  
    75  // UpdateResult is the response from an Update operation. Call its Extract
    76  // method to interpret it as a Policy.
    77  type UpdateResult struct {
    78  	policyResult
    79  }
    80  
    81  // DeleteResult is the response from a Delete operation. Call its ExtractErr to
    82  // determine if the request succeeded or failed.
    83  type DeleteResult struct {
    84  	gophercloud.ErrResult
    85  }
    86  
    87  // PolicyPage is a single page of Policy results.
    88  type PolicyPage struct {
    89  	pagination.LinkedPageBase
    90  }
    91  
    92  // IsEmpty determines whether or not a page of Policies contains any results.
    93  func (r PolicyPage) IsEmpty() (bool, error) {
    94  	if r.StatusCode == 204 {
    95  		return true, nil
    96  	}
    97  
    98  	policies, err := ExtractPolicies(r)
    99  	return len(policies) == 0, err
   100  }
   101  
   102  // NextPageURL extracts the "next" link from the links section of the result.
   103  func (r PolicyPage) NextPageURL() (string, error) {
   104  	var s struct {
   105  		Links struct {
   106  			Next     string `json:"next"`
   107  			Previous string `json:"previous"`
   108  		} `json:"links"`
   109  	}
   110  	err := r.ExtractInto(&s)
   111  	if err != nil {
   112  		return "", err
   113  	}
   114  	return s.Links.Next, err
   115  }
   116  
   117  // ExtractPolicies returns a slice of Policies
   118  // contained in a single page of results.
   119  func ExtractPolicies(r pagination.Page) ([]Policy, error) {
   120  	var s struct {
   121  		Policies []Policy `json:"policies"`
   122  	}
   123  	err := (r.(PolicyPage)).ExtractInto(&s)
   124  	return s.Policies, err
   125  }
   126  
   127  // Extract interprets any policyResults as a Policy.
   128  func (r policyResult) Extract() (*Policy, error) {
   129  	var s struct {
   130  		Policy *Policy `json:"policy"`
   131  	}
   132  	err := r.ExtractInto(&s)
   133  	return s.Policy, err
   134  }