1package groups
2
3import (
4	"github.com/gophercloud/gophercloud"
5	"github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/security/rules"
6	"github.com/gophercloud/gophercloud/pagination"
7)
8
9// SecGroup represents a container for security group rules.
10type SecGroup struct {
11	// The UUID for the security group.
12	ID string
13
14	// Human-readable name for the security group. Might not be unique.
15	// Cannot be named "default" as that is automatically created for a tenant.
16	Name string
17
18	// The security group description.
19	Description string
20
21	// A slice of security group rules that dictate the permitted behaviour for
22	// traffic entering and leaving the group.
23	Rules []rules.SecGroupRule `json:"security_group_rules"`
24
25	// TenantID is the project owner of the security group.
26	TenantID string `json:"tenant_id"`
27
28	// ProjectID is the project owner of the security group.
29	ProjectID string `json:"project_id"`
30}
31
32// SecGroupPage is the page returned by a pager when traversing over a
33// collection of security groups.
34type SecGroupPage struct {
35	pagination.LinkedPageBase
36}
37
38// NextPageURL is invoked when a paginated collection of security groups has
39// reached the end of a page and the pager seeks to traverse over a new one. In
40// order to do this, it needs to construct the next page's URL.
41func (r SecGroupPage) NextPageURL() (string, error) {
42	var s struct {
43		Links []gophercloud.Link `json:"security_groups_links"`
44	}
45	err := r.ExtractInto(&s)
46	if err != nil {
47		return "", err
48	}
49
50	return gophercloud.ExtractNextURL(s.Links)
51}
52
53// IsEmpty checks whether a SecGroupPage struct is empty.
54func (r SecGroupPage) IsEmpty() (bool, error) {
55	is, err := ExtractGroups(r)
56	return len(is) == 0, err
57}
58
59// ExtractGroups accepts a Page struct, specifically a SecGroupPage struct,
60// and extracts the elements into a slice of SecGroup structs. In other words,
61// a generic collection is mapped into a relevant slice.
62func ExtractGroups(r pagination.Page) ([]SecGroup, error) {
63	var s struct {
64		SecGroups []SecGroup `json:"security_groups"`
65	}
66	err := (r.(SecGroupPage)).ExtractInto(&s)
67	return s.SecGroups, err
68}
69
70type commonResult struct {
71	gophercloud.Result
72}
73
74// Extract is a function that accepts a result and extracts a security group.
75func (r commonResult) Extract() (*SecGroup, error) {
76	var s struct {
77		SecGroup *SecGroup `json:"security_group"`
78	}
79	err := r.ExtractInto(&s)
80	return s.SecGroup, err
81}
82
83// CreateResult represents the result of a create operation. Call its Extract
84// method to interpret it as a SecGroup.
85type CreateResult struct {
86	commonResult
87}
88
89// UpdateResult represents the result of an update operation. Call its Extract
90// method to interpret it as a SecGroup.
91type UpdateResult struct {
92	commonResult
93}
94
95// GetResult represents the result of a get operation. Call its Extract
96// method to interpret it as a SecGroup.
97type GetResult struct {
98	commonResult
99}
100
101// DeleteResult represents the result of a delete operation. Call its
102// ExtractErr method to determine if the request succeeded or failed.
103type DeleteResult struct {
104	gophercloud.ErrResult
105}
106