github.com/gophercloud/gophercloud@v1.14.1/openstack/networking/v2/extensions/security/groups/results.go (about)

     1  package groups
     2  
     3  import (
     4  	"encoding/json"
     5  	"time"
     6  
     7  	"github.com/gophercloud/gophercloud"
     8  	"github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/security/rules"
     9  	"github.com/gophercloud/gophercloud/pagination"
    10  )
    11  
    12  // SecGroup represents a container for security group rules.
    13  type SecGroup struct {
    14  	// The UUID for the security group.
    15  	ID string
    16  
    17  	// Human-readable name for the security group. Might not be unique.
    18  	// Cannot be named "default" as that is automatically created for a tenant.
    19  	Name string
    20  
    21  	// The security group description.
    22  	Description string
    23  
    24  	// A slice of security group rules that dictate the permitted behaviour for
    25  	// traffic entering and leaving the group.
    26  	Rules []rules.SecGroupRule `json:"security_group_rules"`
    27  
    28  	// Indicates if the security group is stateful or stateless.
    29  	Stateful bool `json:"stateful"`
    30  
    31  	// TenantID is the project owner of the security group.
    32  	TenantID string `json:"tenant_id"`
    33  
    34  	// UpdatedAt and CreatedAt contain ISO-8601 timestamps of when the state of the
    35  	// security group last changed, and when it was created.
    36  	UpdatedAt time.Time `json:"-"`
    37  	CreatedAt time.Time `json:"-"`
    38  
    39  	// ProjectID is the project owner of the security group.
    40  	ProjectID string `json:"project_id"`
    41  
    42  	// Tags optionally set via extensions/attributestags
    43  	Tags []string `json:"tags"`
    44  }
    45  
    46  func (r *SecGroup) UnmarshalJSON(b []byte) error {
    47  	type tmp SecGroup
    48  
    49  	// Support for older neutron time format
    50  	var s1 struct {
    51  		tmp
    52  		CreatedAt gophercloud.JSONRFC3339NoZ `json:"created_at"`
    53  		UpdatedAt gophercloud.JSONRFC3339NoZ `json:"updated_at"`
    54  	}
    55  
    56  	err := json.Unmarshal(b, &s1)
    57  	if err == nil {
    58  		*r = SecGroup(s1.tmp)
    59  		r.CreatedAt = time.Time(s1.CreatedAt)
    60  		r.UpdatedAt = time.Time(s1.UpdatedAt)
    61  
    62  		return nil
    63  	}
    64  
    65  	// Support for newer neutron time format
    66  	var s2 struct {
    67  		tmp
    68  		CreatedAt time.Time `json:"created_at"`
    69  		UpdatedAt time.Time `json:"updated_at"`
    70  	}
    71  
    72  	err = json.Unmarshal(b, &s2)
    73  	if err != nil {
    74  		return err
    75  	}
    76  
    77  	*r = SecGroup(s2.tmp)
    78  	r.CreatedAt = time.Time(s2.CreatedAt)
    79  	r.UpdatedAt = time.Time(s2.UpdatedAt)
    80  
    81  	return nil
    82  }
    83  
    84  // SecGroupPage is the page returned by a pager when traversing over a
    85  // collection of security groups.
    86  type SecGroupPage struct {
    87  	pagination.LinkedPageBase
    88  }
    89  
    90  // NextPageURL is invoked when a paginated collection of security groups has
    91  // reached the end of a page and the pager seeks to traverse over a new one. In
    92  // order to do this, it needs to construct the next page's URL.
    93  func (r SecGroupPage) NextPageURL() (string, error) {
    94  	var s struct {
    95  		Links []gophercloud.Link `json:"security_groups_links"`
    96  	}
    97  	err := r.ExtractInto(&s)
    98  	if err != nil {
    99  		return "", err
   100  	}
   101  
   102  	return gophercloud.ExtractNextURL(s.Links)
   103  }
   104  
   105  // IsEmpty checks whether a SecGroupPage struct is empty.
   106  func (r SecGroupPage) IsEmpty() (bool, error) {
   107  	if r.StatusCode == 204 {
   108  		return true, nil
   109  	}
   110  
   111  	is, err := ExtractGroups(r)
   112  	return len(is) == 0, err
   113  }
   114  
   115  // ExtractGroups accepts a Page struct, specifically a SecGroupPage struct,
   116  // and extracts the elements into a slice of SecGroup structs. In other words,
   117  // a generic collection is mapped into a relevant slice.
   118  func ExtractGroups(r pagination.Page) ([]SecGroup, error) {
   119  	var s struct {
   120  		SecGroups []SecGroup `json:"security_groups"`
   121  	}
   122  	err := (r.(SecGroupPage)).ExtractInto(&s)
   123  	return s.SecGroups, err
   124  }
   125  
   126  type commonResult struct {
   127  	gophercloud.Result
   128  }
   129  
   130  // Extract is a function that accepts a result and extracts a security group.
   131  func (r commonResult) Extract() (*SecGroup, error) {
   132  	var s struct {
   133  		SecGroup *SecGroup `json:"security_group"`
   134  	}
   135  	err := r.ExtractInto(&s)
   136  	return s.SecGroup, err
   137  }
   138  
   139  // CreateResult represents the result of a create operation. Call its Extract
   140  // method to interpret it as a SecGroup.
   141  type CreateResult struct {
   142  	commonResult
   143  }
   144  
   145  // UpdateResult represents the result of an update operation. Call its Extract
   146  // method to interpret it as a SecGroup.
   147  type UpdateResult struct {
   148  	commonResult
   149  }
   150  
   151  // GetResult represents the result of a get operation. Call its Extract
   152  // method to interpret it as a SecGroup.
   153  type GetResult struct {
   154  	commonResult
   155  }
   156  
   157  // DeleteResult represents the result of a delete operation. Call its
   158  // ExtractErr method to determine if the request succeeded or failed.
   159  type DeleteResult struct {
   160  	gophercloud.ErrResult
   161  }