github.com/gophercloud/gophercloud@v1.11.0/openstack/networking/v2/extensions/fwaas_v2/rules/requests.go (about) 1 package rules 2 3 import ( 4 "github.com/gophercloud/gophercloud" 5 "github.com/gophercloud/gophercloud/pagination" 6 ) 7 8 type ( 9 // Protocol represents a valid rule protocol 10 Protocol string 11 ) 12 13 const ( 14 // ProtocolAny is to allow any protocol 15 ProtocolAny Protocol = "any" 16 17 // ProtocolICMP is to allow the ICMP protocol 18 ProtocolICMP Protocol = "icmp" 19 20 // ProtocolTCP is to allow the TCP protocol 21 ProtocolTCP Protocol = "tcp" 22 23 // ProtocolUDP is to allow the UDP protocol 24 ProtocolUDP Protocol = "udp" 25 ) 26 27 type ( 28 // Action represents a valid rule protocol 29 Action string 30 ) 31 32 const ( 33 // ActionAllow is to allow traffic 34 ActionAllow Action = "allow" 35 36 // ActionDeny is to deny traffic 37 ActionDeny Action = "deny" 38 39 // ActionTCP is to reject traffic 40 ActionReject Action = "reject" 41 ) 42 43 // ListOptsBuilder allows extensions to add additional parameters to the 44 // List request. 45 type ListOptsBuilder interface { 46 ToRuleListQuery() (string, error) 47 } 48 49 // ListOpts allows the filtering and sorting of paginated collections through 50 // the API. Filtering is achieved by passing in struct field values that map to 51 // the Firewall rule attributes you want to see returned. SortKey allows you to 52 // sort by a particular firewall rule attribute. SortDir sets the direction, and is 53 // either `asc' or `desc'. Marker and Limit are used for pagination. 54 type ListOpts struct { 55 TenantID string `q:"tenant_id"` 56 Name string `q:"name"` 57 Description string `q:"description"` 58 Protocol Protocol `q:"protocol"` 59 Action Action `q:"action"` 60 IPVersion int `q:"ip_version"` 61 SourceIPAddress string `q:"source_ip_address"` 62 DestinationIPAddress string `q:"destination_ip_address"` 63 SourcePort string `q:"source_port"` 64 DestinationPort string `q:"destination_port"` 65 Enabled *bool `q:"enabled"` 66 ID string `q:"id"` 67 Shared *bool `q:"shared"` 68 ProjectID string `q:"project_id"` 69 FirewallPolicyID string `q:"firewall_policy_id"` 70 Limit int `q:"limit"` 71 Marker string `q:"marker"` 72 SortKey string `q:"sort_key"` 73 SortDir string `q:"sort_dir"` 74 } 75 76 // ToRuleListQuery formats a ListOpts into a query string. 77 func (opts ListOpts) ToRuleListQuery() (string, error) { 78 q, err := gophercloud.BuildQueryString(opts) 79 if err != nil { 80 return "", err 81 } 82 return q.String(), nil 83 } 84 85 // List returns a Pager which allows you to iterate over a collection of 86 // firewall rules. It accepts a ListOpts struct, which allows you to filter 87 // and sort the returned collection for greater efficiency. 88 // 89 // Default policy settings return only those firewall rules that are owned by the 90 // tenant who submits the request, unless an admin user submits the request. 91 func List(c *gophercloud.ServiceClient, opts ListOptsBuilder) pagination.Pager { 92 url := rootURL(c) 93 94 if opts != nil { 95 query, err := opts.ToRuleListQuery() 96 if err != nil { 97 return pagination.Pager{Err: err} 98 } 99 url += query 100 } 101 102 return pagination.NewPager(c, url, func(r pagination.PageResult) pagination.Page { 103 return RulePage{pagination.LinkedPageBase{PageResult: r}} 104 }) 105 } 106 107 // CreateOptsBuilder is the interface options structs have to satisfy in order 108 // to be used in the main Create operation in this package. Since many 109 // extensions decorate or modify the common logic, it is useful for them to 110 // satisfy a basic interface in order for them to be used. 111 type CreateOptsBuilder interface { 112 ToRuleCreateMap() (map[string]interface{}, error) 113 } 114 115 // CreateOpts contains all the values needed to create a new firewall rule. 116 type CreateOpts struct { 117 Protocol Protocol `json:"protocol" required:"true"` 118 Action Action `json:"action" required:"true"` 119 TenantID string `json:"tenant_id,omitempty"` 120 ProjectID string `json:"project_id,omitempty"` 121 Name string `json:"name,omitempty"` 122 Description string `json:"description,omitempty"` 123 IPVersion gophercloud.IPVersion `json:"ip_version,omitempty"` 124 SourceIPAddress string `json:"source_ip_address,omitempty"` 125 DestinationIPAddress string `json:"destination_ip_address,omitempty"` 126 SourcePort string `json:"source_port,omitempty"` 127 DestinationPort string `json:"destination_port,omitempty"` 128 Shared *bool `json:"shared,omitempty"` 129 Enabled *bool `json:"enabled,omitempty"` 130 } 131 132 // ToRuleCreateMap casts a CreateOpts struct to a map. 133 func (opts CreateOpts) ToRuleCreateMap() (map[string]interface{}, error) { 134 b, err := gophercloud.BuildRequestBody(opts, "firewall_rule") 135 if err != nil { 136 return nil, err 137 } 138 139 if m := b["firewall_rule"].(map[string]interface{}); m["protocol"] == "any" { 140 m["protocol"] = nil 141 } 142 143 return b, nil 144 } 145 146 // Create accepts a CreateOpts struct and uses the values to create a new firewall rule 147 func Create(c *gophercloud.ServiceClient, opts CreateOptsBuilder) (r CreateResult) { 148 b, err := opts.ToRuleCreateMap() 149 if err != nil { 150 r.Err = err 151 return 152 } 153 resp, err := c.Post(rootURL(c), b, &r.Body, nil) 154 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 155 return 156 } 157 158 // Get retrieves a particular firewall rule based on its unique ID. 159 func Get(c *gophercloud.ServiceClient, id string) (r GetResult) { 160 resp, err := c.Get(resourceURL(c, id), &r.Body, nil) 161 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 162 return 163 } 164 165 // UpdateOptsBuilder is the interface options structs have to satisfy in order 166 // to be used in the main Update operation in this package. Since many 167 // extensions decorate or modify the common logic, it is useful for them to 168 // satisfy a basic interface in order for them to be used. 169 type UpdateOptsBuilder interface { 170 ToRuleUpdateMap() (map[string]interface{}, error) 171 } 172 173 // UpdateOpts contains the values used when updating a firewall rule. 174 type UpdateOpts struct { 175 Protocol *Protocol `json:"protocol,omitempty"` 176 Action *Action `json:"action,omitempty"` 177 Name *string `json:"name,omitempty"` 178 Description *string `json:"description,omitempty"` 179 IPVersion *gophercloud.IPVersion `json:"ip_version,omitempty"` 180 SourceIPAddress *string `json:"source_ip_address,omitempty"` 181 DestinationIPAddress *string `json:"destination_ip_address,omitempty"` 182 SourcePort *string `json:"source_port,omitempty"` 183 DestinationPort *string `json:"destination_port,omitempty"` 184 Shared *bool `json:"shared,omitempty"` 185 Enabled *bool `json:"enabled,omitempty"` 186 } 187 188 // ToRuleUpdateMap casts a UpdateOpts struct to a map. 189 func (opts UpdateOpts) ToRuleUpdateMap() (map[string]interface{}, error) { 190 return gophercloud.BuildRequestBody(opts, "firewall_rule") 191 } 192 193 // Update allows firewall policies to be updated. 194 func Update(c *gophercloud.ServiceClient, id string, opts UpdateOptsBuilder) (r UpdateResult) { 195 b, err := opts.ToRuleUpdateMap() 196 if err != nil { 197 r.Err = err 198 return 199 } 200 resp, err := c.Put(resourceURL(c, id), b, &r.Body, &gophercloud.RequestOpts{ 201 OkCodes: []int{200}, 202 }) 203 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 204 return 205 } 206 207 // Delete will permanently delete a particular firewall rule based on its unique ID. 208 func Delete(c *gophercloud.ServiceClient, id string) (r DeleteResult) { 209 resp, err := c.Delete(resourceURL(c, id), nil) 210 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 211 return 212 }