github.com/leeclow-ops/gophercloud@v1.2.1/openstack/networking/v2/extensions/fwaas_v2/rules/requests.go (about) 1 package rules 2 3 import ( 4 "github.com/leeclow-ops/gophercloud" 5 "github.com/leeclow-ops/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 Name string `json:"name,omitempty"` 121 Description string `json:"description,omitempty"` 122 IPVersion gophercloud.IPVersion `json:"ip_version,omitempty"` 123 SourceIPAddress string `json:"source_ip_address,omitempty"` 124 DestinationIPAddress string `json:"destination_ip_address,omitempty"` 125 SourcePort string `json:"source_port,omitempty"` 126 DestinationPort string `json:"destination_port,omitempty"` 127 Shared *bool `json:"shared,omitempty"` 128 Enabled *bool `json:"enabled,omitempty"` 129 } 130 131 // ToRuleCreateMap casts a CreateOpts struct to a map. 132 func (opts CreateOpts) ToRuleCreateMap() (map[string]interface{}, error) { 133 b, err := gophercloud.BuildRequestBody(opts, "firewall_rule") 134 if err != nil { 135 return nil, err 136 } 137 138 if m := b["firewall_rule"].(map[string]interface{}); m["protocol"] == "any" { 139 m["protocol"] = nil 140 } 141 142 return b, nil 143 } 144 145 // Create accepts a CreateOpts struct and uses the values to create a new firewall rule 146 func Create(c *gophercloud.ServiceClient, opts CreateOptsBuilder) (r CreateResult) { 147 b, err := opts.ToRuleCreateMap() 148 if err != nil { 149 r.Err = err 150 return 151 } 152 resp, err := c.Post(rootURL(c), b, &r.Body, nil) 153 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 154 return 155 } 156 157 // Get retrieves a particular firewall rule based on its unique ID. 158 func Get(c *gophercloud.ServiceClient, id string) (r GetResult) { 159 resp, err := c.Get(resourceURL(c, id), &r.Body, nil) 160 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 161 return 162 } 163 164 // UpdateOptsBuilder is the interface options structs have to satisfy in order 165 // to be used in the main Update operation in this package. Since many 166 // extensions decorate or modify the common logic, it is useful for them to 167 // satisfy a basic interface in order for them to be used. 168 type UpdateOptsBuilder interface { 169 ToRuleUpdateMap() (map[string]interface{}, error) 170 } 171 172 // UpdateOpts contains the values used when updating a firewall rule. 173 type UpdateOpts struct { 174 Protocol *Protocol `json:"protocol,omitempty"` 175 Action *Action `json:"action,omitempty"` 176 Name *string `json:"name,omitempty"` 177 Description *string `json:"description,omitempty"` 178 IPVersion *gophercloud.IPVersion `json:"ip_version,omitempty"` 179 SourceIPAddress *string `json:"source_ip_address,omitempty"` 180 DestinationIPAddress *string `json:"destination_ip_address,omitempty"` 181 SourcePort *string `json:"source_port,omitempty"` 182 DestinationPort *string `json:"destination_port,omitempty"` 183 Shared *bool `json:"shared,omitempty"` 184 Enabled *bool `json:"enabled,omitempty"` 185 } 186 187 // ToRuleUpdateMap casts a UpdateOpts struct to a map. 188 func (opts UpdateOpts) ToRuleUpdateMap() (map[string]interface{}, error) { 189 return gophercloud.BuildRequestBody(opts, "firewall_rule") 190 } 191 192 // Update allows firewall policies to be updated. 193 func Update(c *gophercloud.ServiceClient, id string, opts UpdateOptsBuilder) (r UpdateResult) { 194 b, err := opts.ToRuleUpdateMap() 195 if err != nil { 196 r.Err = err 197 return 198 } 199 resp, err := c.Put(resourceURL(c, id), b, &r.Body, &gophercloud.RequestOpts{ 200 OkCodes: []int{200}, 201 }) 202 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 203 return 204 } 205 206 // Delete will permanently delete a particular firewall rule based on its unique ID. 207 func Delete(c *gophercloud.ServiceClient, id string) (r DeleteResult) { 208 resp, err := c.Delete(resourceURL(c, id), nil) 209 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 210 return 211 }