1package rules
2
3import (
4	"github.com/gophercloud/gophercloud"
5	"github.com/gophercloud/gophercloud/pagination"
6)
7
8// Rule represents a firewall rule
9type Rule struct {
10	ID                   string `json:"id"`
11	Name                 string `json:"name,omitempty"`
12	Description          string `json:"description,omitempty"`
13	Protocol             string `json:"protocol"`
14	Action               string `json:"action"`
15	IPVersion            int    `json:"ip_version,omitempty"`
16	SourceIPAddress      string `json:"source_ip_address,omitempty"`
17	DestinationIPAddress string `json:"destination_ip_address,omitempty"`
18	SourcePort           string `json:"source_port,omitempty"`
19	DestinationPort      string `json:"destination_port,omitempty"`
20	Shared               bool   `json:"shared,omitempty"`
21	Enabled              bool   `json:"enabled,omitempty"`
22	FirewallPolicyID     string `json:"firewall_policy_id"`
23	TenantID             string `json:"tenant_id"`
24	ProjectID            string `json:"project_id"`
25}
26
27// RulePage is the page returned by a pager when traversing over a
28// collection of firewall rules.
29type RulePage struct {
30	pagination.LinkedPageBase
31}
32
33// NextPageURL is invoked when a paginated collection of firewall rules has
34// reached the end of a page and the pager seeks to traverse over a new one.
35// In order to do this, it needs to construct the next page's URL.
36func (r RulePage) NextPageURL() (string, error) {
37	var s struct {
38		Links []gophercloud.Link `json:"firewall_rules_links"`
39	}
40	err := r.ExtractInto(&s)
41	if err != nil {
42		return "", err
43	}
44	return gophercloud.ExtractNextURL(s.Links)
45}
46
47// IsEmpty checks whether a RulePage struct is empty.
48func (r RulePage) IsEmpty() (bool, error) {
49	is, err := ExtractRules(r)
50	return len(is) == 0, err
51}
52
53// ExtractRules accepts a Page struct, specifically a RouterPage struct,
54// and extracts the elements into a slice of Router structs. In other words,
55// a generic collection is mapped into a relevant slice.
56func ExtractRules(r pagination.Page) ([]Rule, error) {
57	var s struct {
58		Rules []Rule `json:"firewall_rules"`
59	}
60	err := (r.(RulePage)).ExtractInto(&s)
61	return s.Rules, err
62}
63
64type commonResult struct {
65	gophercloud.Result
66}
67
68// Extract is a function that accepts a result and extracts a firewall rule.
69func (r commonResult) Extract() (*Rule, error) {
70	var s struct {
71		Rule *Rule `json:"firewall_rule"`
72	}
73	err := r.ExtractInto(&s)
74	return s.Rule, err
75}
76
77// GetResult represents the result of a get operation.
78type GetResult struct {
79	commonResult
80}
81
82// UpdateResult represents the result of an update operation.
83type UpdateResult struct {
84	commonResult
85}
86
87// DeleteResult represents the result of a delete operation.
88type DeleteResult struct {
89	gophercloud.ErrResult
90}
91
92// CreateResult represents the result of a create operation.
93type CreateResult struct {
94	commonResult
95}
96