|
| 1 | +package groups |
| 2 | + |
| 3 | +import ( |
| 4 | + "github.com/gophercloud/gophercloud" |
| 5 | + "github.com/gophercloud/gophercloud/pagination" |
| 6 | +) |
| 7 | + |
| 8 | +// ListOptsBuilder allows extensions to add additional parameters to the |
| 9 | +// List request. |
| 10 | +type ListOptsBuilder interface { |
| 11 | + ToGroupListQuery() (string, error) |
| 12 | +} |
| 13 | + |
| 14 | +// ListOpts allows the filtering and sorting of paginated collections through |
| 15 | +// the API. Filtering is achieved by passing in struct field values that map to |
| 16 | +// the firewall group attributes you want to see returned. SortKey allows you |
| 17 | +// to sort by a particular firewall group attribute. SortDir sets the direction, |
| 18 | +// and is either `asc' or `desc'. Marker and Limit are used for pagination. |
| 19 | +type ListOpts struct { |
| 20 | + TenantID string `q:"tenant_id"` |
| 21 | + Name string `q:"name"` |
| 22 | + Description string `q:"description"` |
| 23 | + IngressFirewallPolicyID string `q:"ingress_firewall_policy_id"` |
| 24 | + EgressFirewallPolicyID string `q:"egress_firewall_policy_id"` |
| 25 | + AdminStateUp *bool `q:"admin_state_up"` |
| 26 | + Ports *[]string `q:"ports"` |
| 27 | + Status string `q:"status"` |
| 28 | + ID string `q:"id"` |
| 29 | + Shared *bool `q:"shared"` |
| 30 | + ProjectID string `q:"project_id"` |
| 31 | + Limit int `q:"limit"` |
| 32 | + Marker string `q:"marker"` |
| 33 | + SortKey string `q:"sort_key"` |
| 34 | + SortDir string `q:"sort_dir"` |
| 35 | +} |
| 36 | + |
| 37 | +// ToGroupListQuery formats a ListOpts into a query string. |
| 38 | +func (opts ListOpts) ToGroupListQuery() (string, error) { |
| 39 | + q, err := gophercloud.BuildQueryString(opts) |
| 40 | + if err != nil { |
| 41 | + return "", err |
| 42 | + } |
| 43 | + return q.String(), err |
| 44 | +} |
| 45 | + |
| 46 | +// List returns a Pager which allows you to iterate over a collection of |
| 47 | +// firewall groups. It accepts a ListOpts struct, which allows you to filter |
| 48 | +// and sort the returned collection for greater efficiency. |
| 49 | +// |
| 50 | +// Default group settings return only those firewall groups that are owned by the |
| 51 | +// tenant who submits the request, unless an admin user submits the request. |
| 52 | +func List(c *gophercloud.ServiceClient, opts ListOptsBuilder) pagination.Pager { |
| 53 | + url := rootURL(c) |
| 54 | + |
| 55 | + if opts != nil { |
| 56 | + query, err := opts.ToGroupListQuery() |
| 57 | + if err != nil { |
| 58 | + return pagination.Pager{Err: err} |
| 59 | + } |
| 60 | + url += query |
| 61 | + } |
| 62 | + |
| 63 | + return pagination.NewPager(c, url, func(r pagination.PageResult) pagination.Page { |
| 64 | + return GroupPage{pagination.LinkedPageBase{PageResult: r}} |
| 65 | + }) |
| 66 | +} |
| 67 | + |
| 68 | +// Get retrieves a particular firewall group based on its unique ID. |
| 69 | +func Get(c *gophercloud.ServiceClient, id string) (r GetResult) { |
| 70 | + _, r.Err = c.Get(resourceURL(c, id), &r.Body, nil) |
| 71 | + return |
| 72 | +} |
| 73 | + |
| 74 | +// CreateOptsBuilder is the interface options structs have to satisfy in order |
| 75 | +// to be used in the main Create operation in this package. Since many |
| 76 | +// extensions decorate or modify the common logic, it is useful for them to |
| 77 | +// satisfy a basic interface in order for them to be used. |
| 78 | +type CreateOptsBuilder interface { |
| 79 | + ToFirewallGroupCreateMap() (map[string]interface{}, error) |
| 80 | +} |
| 81 | + |
| 82 | +// CreateOpts contains all the values needed to create a new firewall group. |
| 83 | +type CreateOpts struct { |
| 84 | + ID string `json:"id,omitempty"` |
| 85 | + TenantID string `json:"tenant_id,omitempty"` |
| 86 | + Name string `json:"name,omitempty"` |
| 87 | + Description string `json:"description,omitempty"` |
| 88 | + IngressFirewallPolicyID string `json:"ingress_firewall_policy_id,omitempty"` |
| 89 | + EgressFirewallPolicyID string `json:"egress_firewall_policy_id,omitempty"` |
| 90 | + AdminStateUp *bool `json:"admin_state_up,omitempty"` |
| 91 | + Ports []string `json:"ports,omitempty"` |
| 92 | + Shared *bool `json:"shared,omitempty"` |
| 93 | + ProjectID string `json:"project_id,omitempty"` |
| 94 | +} |
| 95 | + |
| 96 | +// ToFirewallGroupCreateMap casts a CreateOpts struct to a map. |
| 97 | +func (opts CreateOpts) ToFirewallGroupCreateMap() (map[string]interface{}, error) { |
| 98 | + return gophercloud.BuildRequestBody(opts, "firewall_group") |
| 99 | +} |
| 100 | + |
| 101 | +// Create accepts a CreateOpts struct and uses the values to create a new firewall group |
| 102 | +func Create(c *gophercloud.ServiceClient, opts CreateOptsBuilder) (r CreateResult) { |
| 103 | + b, err := opts.ToFirewallGroupCreateMap() |
| 104 | + if err != nil { |
| 105 | + r.Err = err |
| 106 | + return |
| 107 | + } |
| 108 | + _, r.Err = c.Post(rootURL(c), b, &r.Body, nil) |
| 109 | + return |
| 110 | +} |
| 111 | + |
| 112 | +// UpdateOptsBuilder is the interface options structs have to satisfy in order |
| 113 | +// to be used in the main Update operation in this package. Since many |
| 114 | +// extensions decorate or modify the common logic, it is useful for them to |
| 115 | +// satisfy a basic interface in order for them to be used. |
| 116 | +type UpdateOptsBuilder interface { |
| 117 | + ToFirewallGroupUpdateMap() (map[string]interface{}, error) |
| 118 | +} |
| 119 | + |
| 120 | +// UpdateOpts contains the values used when updating a firewall group. |
| 121 | +type UpdateOpts struct { |
| 122 | + Name *string `json:"name,omitempty"` |
| 123 | + Description *string `json:"description,omitempty"` |
| 124 | + IngressFirewallPolicyID *string `json:"ingress_firewall_policy_id,omitempty"` |
| 125 | + EgressFirewallPolicyID *string `json:"egress_firewall_policy_id,omitempty"` |
| 126 | + AdminStateUp *bool `json:"admin_state_up,omitempty"` |
| 127 | + Ports []string `json:"ports,omitempty"` |
| 128 | + Shared *bool `json:"shared,omitempty"` |
| 129 | +} |
| 130 | + |
| 131 | +// ToFirewallGroupUpdateMap casts a CreateOpts struct to a map. |
| 132 | +func (opts UpdateOpts) ToFirewallGroupUpdateMap() (map[string]interface{}, error) { |
| 133 | + return gophercloud.BuildRequestBody(opts, "firewall_group") |
| 134 | +} |
| 135 | + |
| 136 | +// Update allows firewall groups to be updated. |
| 137 | +func Update(c *gophercloud.ServiceClient, id string, opts UpdateOptsBuilder) (r UpdateResult) { |
| 138 | + b, err := opts.ToFirewallGroupUpdateMap() |
| 139 | + if err != nil { |
| 140 | + r.Err = err |
| 141 | + return |
| 142 | + } |
| 143 | + _, r.Err = c.Put(resourceURL(c, id), b, &r.Body, &gophercloud.RequestOpts{ |
| 144 | + OkCodes: []int{200}, |
| 145 | + }) |
| 146 | + return |
| 147 | +} |
| 148 | + |
| 149 | +// Delete will permanently delete a particular firewall group based on its unique ID. |
| 150 | +func Delete(c *gophercloud.ServiceClient, id string) (r DeleteResult) { |
| 151 | + _, r.Err = c.Delete(resourceURL(c, id), nil) |
| 152 | + return |
| 153 | +} |
0 commit comments