github.com/gophercloud/gophercloud@v1.11.0/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  	// TenantID is the project owner of the security group.
    29  	TenantID string `json:"tenant_id"`
    30  
    31  	// UpdatedAt and CreatedAt contain ISO-8601 timestamps of when the state of the
    32  	// security group last changed, and when it was created.
    33  	UpdatedAt time.Time `json:"-"`
    34  	CreatedAt time.Time `json:"-"`
    35  
    36  	// ProjectID is the project owner of the security group.
    37  	ProjectID string `json:"project_id"`
    38  
    39  	// Tags optionally set via extensions/attributestags
    40  	Tags []string `json:"tags"`
    41  }
    42  
    43  func (r *SecGroup) UnmarshalJSON(b []byte) error {
    44  	type tmp SecGroup
    45  
    46  	// Support for older neutron time format
    47  	var s1 struct {
    48  		tmp
    49  		CreatedAt gophercloud.JSONRFC3339NoZ `json:"created_at"`
    50  		UpdatedAt gophercloud.JSONRFC3339NoZ `json:"updated_at"`
    51  	}
    52  
    53  	err := json.Unmarshal(b, &s1)
    54  	if err == nil {
    55  		*r = SecGroup(s1.tmp)
    56  		r.CreatedAt = time.Time(s1.CreatedAt)
    57  		r.UpdatedAt = time.Time(s1.UpdatedAt)
    58  
    59  		return nil
    60  	}
    61  
    62  	// Support for newer neutron time format
    63  	var s2 struct {
    64  		tmp
    65  		CreatedAt time.Time `json:"created_at"`
    66  		UpdatedAt time.Time `json:"updated_at"`
    67  	}
    68  
    69  	err = json.Unmarshal(b, &s2)
    70  	if err != nil {
    71  		return err
    72  	}
    73  
    74  	*r = SecGroup(s2.tmp)
    75  	r.CreatedAt = time.Time(s2.CreatedAt)
    76  	r.UpdatedAt = time.Time(s2.UpdatedAt)
    77  
    78  	return nil
    79  }
    80  
    81  // SecGroupPage is the page returned by a pager when traversing over a
    82  // collection of security groups.
    83  type SecGroupPage struct {
    84  	pagination.LinkedPageBase
    85  }
    86  
    87  // NextPageURL is invoked when a paginated collection of security groups has
    88  // reached the end of a page and the pager seeks to traverse over a new one. In
    89  // order to do this, it needs to construct the next page's URL.
    90  func (r SecGroupPage) NextPageURL() (string, error) {
    91  	var s struct {
    92  		Links []gophercloud.Link `json:"security_groups_links"`
    93  	}
    94  	err := r.ExtractInto(&s)
    95  	if err != nil {
    96  		return "", err
    97  	}
    98  
    99  	return gophercloud.ExtractNextURL(s.Links)
   100  }
   101  
   102  // IsEmpty checks whether a SecGroupPage struct is empty.
   103  func (r SecGroupPage) IsEmpty() (bool, error) {
   104  	if r.StatusCode == 204 {
   105  		return true, nil
   106  	}
   107  
   108  	is, err := ExtractGroups(r)
   109  	return len(is) == 0, err
   110  }
   111  
   112  // ExtractGroups accepts a Page struct, specifically a SecGroupPage struct,
   113  // and extracts the elements into a slice of SecGroup structs. In other words,
   114  // a generic collection is mapped into a relevant slice.
   115  func ExtractGroups(r pagination.Page) ([]SecGroup, error) {
   116  	var s struct {
   117  		SecGroups []SecGroup `json:"security_groups"`
   118  	}
   119  	err := (r.(SecGroupPage)).ExtractInto(&s)
   120  	return s.SecGroups, err
   121  }
   122  
   123  type commonResult struct {
   124  	gophercloud.Result
   125  }
   126  
   127  // Extract is a function that accepts a result and extracts a security group.
   128  func (r commonResult) Extract() (*SecGroup, error) {
   129  	var s struct {
   130  		SecGroup *SecGroup `json:"security_group"`
   131  	}
   132  	err := r.ExtractInto(&s)
   133  	return s.SecGroup, err
   134  }
   135  
   136  // CreateResult represents the result of a create operation. Call its Extract
   137  // method to interpret it as a SecGroup.
   138  type CreateResult struct {
   139  	commonResult
   140  }
   141  
   142  // UpdateResult represents the result of an update operation. Call its Extract
   143  // method to interpret it as a SecGroup.
   144  type UpdateResult struct {
   145  	commonResult
   146  }
   147  
   148  // GetResult represents the result of a get operation. Call its Extract
   149  // method to interpret it as a SecGroup.
   150  type GetResult struct {
   151  	commonResult
   152  }
   153  
   154  // DeleteResult represents the result of a delete operation. Call its
   155  // ExtractErr method to determine if the request succeeded or failed.
   156  type DeleteResult struct {
   157  	gophercloud.ErrResult
   158  }