1package rules
2
3import (
4	"github.com/gophercloud/gophercloud"
5	"github.com/gophercloud/gophercloud/pagination"
6)
7
8// SecGroupRule represents a rule to dictate the behaviour of incoming or
9// outgoing traffic for a particular security group.
10type SecGroupRule struct {
11	// The UUID for this security group rule.
12	ID string
13
14	// The direction in which the security group rule is applied. The only values
15	// allowed are "ingress" or "egress". For a compute instance, an ingress
16	// security group rule is applied to incoming (ingress) traffic for that
17	// instance. An egress rule is applied to traffic leaving the instance.
18	Direction string
19
20	// Descripton of the rule
21	Description string `json:"description"`
22
23	// Must be IPv4 or IPv6, and addresses represented in CIDR must match the
24	// ingress or egress rules.
25	EtherType string `json:"ethertype"`
26
27	// The security group ID to associate with this security group rule.
28	SecGroupID string `json:"security_group_id"`
29
30	// The minimum port number in the range that is matched by the security group
31	// rule. If the protocol is TCP or UDP, this value must be less than or equal
32	// to the value of the PortRangeMax attribute. If the protocol is ICMP, this
33	// value must be an ICMP type.
34	PortRangeMin int `json:"port_range_min"`
35
36	// The maximum port number in the range that is matched by the security group
37	// rule. The PortRangeMin attribute constrains the PortRangeMax attribute. If
38	// the protocol is ICMP, this value must be an ICMP type.
39	PortRangeMax int `json:"port_range_max"`
40
41	// The protocol that is matched by the security group rule. Valid values are
42	// "tcp", "udp", "icmp" or an empty string.
43	Protocol string
44
45	// The remote group ID to be associated with this security group rule. You
46	// can specify either RemoteGroupID or RemoteIPPrefix.
47	RemoteGroupID string `json:"remote_group_id"`
48
49	// The remote IP prefix to be associated with this security group rule. You
50	// can specify either RemoteGroupID or RemoteIPPrefix . This attribute
51	// matches the specified IP prefix as the source IP address of the IP packet.
52	RemoteIPPrefix string `json:"remote_ip_prefix"`
53
54	// TenantID is the project owner of this security group rule.
55	TenantID string `json:"tenant_id"`
56
57	// ProjectID is the project owner of this security group rule.
58	ProjectID string `json:"project_id"`
59}
60
61// SecGroupRulePage is the page returned by a pager when traversing over a
62// collection of security group rules.
63type SecGroupRulePage struct {
64	pagination.LinkedPageBase
65}
66
67// NextPageURL is invoked when a paginated collection of security group rules has
68// reached the end of a page and the pager seeks to traverse over a new one. In
69// order to do this, it needs to construct the next page's URL.
70func (r SecGroupRulePage) NextPageURL() (string, error) {
71	var s struct {
72		Links []gophercloud.Link `json:"security_group_rules_links"`
73	}
74	err := r.ExtractInto(&s)
75	if err != nil {
76		return "", err
77	}
78	return gophercloud.ExtractNextURL(s.Links)
79}
80
81// IsEmpty checks whether a SecGroupRulePage struct is empty.
82func (r SecGroupRulePage) IsEmpty() (bool, error) {
83	is, err := ExtractRules(r)
84	return len(is) == 0, err
85}
86
87// ExtractRules accepts a Page struct, specifically a SecGroupRulePage struct,
88// and extracts the elements into a slice of SecGroupRule structs. In other words,
89// a generic collection is mapped into a relevant slice.
90func ExtractRules(r pagination.Page) ([]SecGroupRule, error) {
91	var s struct {
92		SecGroupRules []SecGroupRule `json:"security_group_rules"`
93	}
94	err := (r.(SecGroupRulePage)).ExtractInto(&s)
95	return s.SecGroupRules, err
96}
97
98type commonResult struct {
99	gophercloud.Result
100}
101
102// Extract is a function that accepts a result and extracts a security rule.
103func (r commonResult) Extract() (*SecGroupRule, error) {
104	var s struct {
105		SecGroupRule *SecGroupRule `json:"security_group_rule"`
106	}
107	err := r.ExtractInto(&s)
108	return s.SecGroupRule, err
109}
110
111// CreateResult represents the result of a create operation. Call its Extract
112// method to interpret it as a SecGroupRule.
113type CreateResult struct {
114	commonResult
115}
116
117// GetResult represents the result of a get operation. Call its Extract
118// method to interpret it as a SecGroupRule.
119type GetResult struct {
120	commonResult
121}
122
123// DeleteResult represents the result of a delete operation. Call its
124// ExtractErr method to determine if the request succeeded or failed.
125type DeleteResult struct {
126	gophercloud.ErrResult
127}
128