github.com/huaweicloud/golangsdk@v0.0.0-20210831081626-d823fe11ceba/openstack/compute/v2/extensions/defsecrules/results.go (about)

     1  package defsecrules
     2  
     3  import (
     4  	"encoding/json"
     5  
     6  	"github.com/huaweicloud/golangsdk"
     7  	"github.com/huaweicloud/golangsdk/openstack/compute/v2/extensions/secgroups"
     8  	"github.com/huaweicloud/golangsdk/pagination"
     9  )
    10  
    11  // DefaultRule represents a rule belonging to the "default" security group.
    12  // It is identical to an openstack/compute/v2/extensions/secgroups.Rule.
    13  type DefaultRule secgroups.Rule
    14  
    15  func (r *DefaultRule) UnmarshalJSON(b []byte) error {
    16  	var s secgroups.Rule
    17  	err := json.Unmarshal(b, &s)
    18  	if err != nil {
    19  		return err
    20  	}
    21  	*r = DefaultRule(s)
    22  	return nil
    23  }
    24  
    25  // DefaultRulePage is a single page of a DefaultRule collection.
    26  type DefaultRulePage struct {
    27  	pagination.SinglePageBase
    28  }
    29  
    30  // IsEmpty determines whether or not a page of default rules contains any results.
    31  func (page DefaultRulePage) IsEmpty() (bool, error) {
    32  	users, err := ExtractDefaultRules(page)
    33  	return len(users) == 0, err
    34  }
    35  
    36  // ExtractDefaultRules returns a slice of DefaultRules contained in a single
    37  // page of results.
    38  func ExtractDefaultRules(r pagination.Page) ([]DefaultRule, error) {
    39  	var s struct {
    40  		DefaultRules []DefaultRule `json:"security_group_default_rules"`
    41  	}
    42  	err := (r.(DefaultRulePage)).ExtractInto(&s)
    43  	return s.DefaultRules, err
    44  }
    45  
    46  type commonResult struct {
    47  	golangsdk.Result
    48  }
    49  
    50  // CreateResult represents the result of a create operation.
    51  type CreateResult struct {
    52  	commonResult
    53  }
    54  
    55  // GetResult represents the result of a get operation.
    56  type GetResult struct {
    57  	commonResult
    58  }
    59  
    60  // Extract will extract a DefaultRule struct from a Create or Get response.
    61  func (r commonResult) Extract() (*DefaultRule, error) {
    62  	var s struct {
    63  		DefaultRule DefaultRule `json:"security_group_default_rule"`
    64  	}
    65  	err := r.ExtractInto(&s)
    66  	return &s.DefaultRule, err
    67  }
    68  
    69  // DeleteResult is the response from a delete operation. Call its ExtractErr
    70  // method to determine if the request succeeded or failed.
    71  type DeleteResult struct {
    72  	golangsdk.ErrResult
    73  }