github.com/opentelekomcloud/gophertelekomcloud@v0.9.3/openstack/networking/v2/extensions/fwaas/policies/requests.go (about) 1 package policies 2 3 import ( 4 "github.com/opentelekomcloud/gophertelekomcloud" 5 "github.com/opentelekomcloud/gophertelekomcloud/pagination" 6 ) 7 8 // ListOptsBuilder allows extensions to add additional parameters to the 9 // List request. 10 type ListOptsBuilder interface { 11 ToPolicyListQuery() (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 policy attributes you want to see returned. SortKey allows you 17 // to sort by a particular firewall policy 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 ProjectID string `q:"project_id"` 22 Name string `q:"name"` 23 Description string `q:"description"` 24 Shared *bool `q:"shared"` 25 Audited *bool `q:"audited"` 26 ID string `q:"id"` 27 Limit int `q:"limit"` 28 Marker string `q:"marker"` 29 SortKey string `q:"sort_key"` 30 SortDir string `q:"sort_dir"` 31 } 32 33 // ToPolicyListQuery formats a ListOpts into a query string. 34 func (opts ListOpts) ToPolicyListQuery() (string, error) { 35 q, err := golangsdk.BuildQueryString(opts) 36 if err != nil { 37 return "", err 38 } 39 return q.String(), err 40 } 41 42 // List returns a Pager which allows you to iterate over a collection of 43 // firewall policies. It accepts a ListOpts struct, which allows you to filter 44 // and sort the returned collection for greater efficiency. 45 // 46 // Default policy settings return only those firewall policies that are owned by 47 // the tenant who submits the request, unless an admin user submits the request. 48 func List(c *golangsdk.ServiceClient, opts ListOptsBuilder) pagination.Pager { 49 url := rootURL(c) 50 if opts != nil { 51 query, err := opts.ToPolicyListQuery() 52 if err != nil { 53 return pagination.Pager{Err: err} 54 } 55 url += query 56 } 57 return pagination.NewPager(c, url, func(r pagination.PageResult) pagination.Page { 58 return PolicyPage{pagination.LinkedPageBase{PageResult: r}} 59 }) 60 } 61 62 // CreateOptsBuilder allows extensions to add additional parameters to the 63 // Create request. 64 type CreateOptsBuilder interface { 65 ToFirewallPolicyCreateMap() (map[string]interface{}, error) 66 } 67 68 // CreateOpts contains all the values needed to create a new firewall policy. 69 type CreateOpts struct { 70 // TenantID specifies a tenant to own the firewall. The caller must have 71 // an admin role in order to set this. Otherwise, this field is left unset 72 // and the caller will be the owner. 73 TenantID string `json:"tenant_id,omitempty"` 74 ProjectID string `json:"project_id,omitempty"` 75 Name string `json:"name,omitempty"` 76 Description string `json:"description,omitempty"` 77 Shared *bool `json:"shared,omitempty"` 78 Audited *bool `json:"audited,omitempty"` 79 Rules []string `json:"firewall_rules,omitempty"` 80 } 81 82 // ToFirewallPolicyCreateMap casts a CreateOpts struct to a map. 83 func (opts CreateOpts) ToFirewallPolicyCreateMap() (map[string]interface{}, error) { 84 return golangsdk.BuildRequestBody(opts, "firewall_policy") 85 } 86 87 // Create accepts a CreateOpts struct and uses the values to create a new 88 // firewall policy. 89 func Create(c *golangsdk.ServiceClient, opts CreateOptsBuilder) (r CreateResult) { 90 b, err := opts.ToFirewallPolicyCreateMap() 91 if err != nil { 92 r.Err = err 93 return 94 } 95 _, r.Err = c.Post(rootURL(c), b, &r.Body, nil) 96 return 97 } 98 99 // Get retrieves a particular firewall policy based on its unique ID. 100 func Get(c *golangsdk.ServiceClient, id string) (r GetResult) { 101 _, r.Err = c.Get(resourceURL(c, id), &r.Body, nil) 102 return 103 } 104 105 // UpdateOptsBuilder allows extensions to add additional parameters to the 106 // Update request. 107 type UpdateOptsBuilder interface { 108 ToFirewallPolicyUpdateMap() (map[string]interface{}, error) 109 } 110 111 // UpdateOpts contains the values used when updating a firewall policy. 112 type UpdateOpts struct { 113 Name string `json:"name,omitempty"` 114 Description string `json:"description,omitempty"` 115 Shared *bool `json:"shared,omitempty"` 116 Audited *bool `json:"audited,omitempty"` 117 Rules []string `json:"firewall_rules,omitempty"` 118 } 119 120 // ToFirewallPolicyUpdateMap casts a CreateOpts struct to a map. 121 func (opts UpdateOpts) ToFirewallPolicyUpdateMap() (map[string]interface{}, error) { 122 return golangsdk.BuildRequestBody(opts, "firewall_policy") 123 } 124 125 // Update allows firewall policies to be updated. 126 func Update(c *golangsdk.ServiceClient, id string, opts UpdateOptsBuilder) (r UpdateResult) { 127 b, err := opts.ToFirewallPolicyUpdateMap() 128 if err != nil { 129 r.Err = err 130 return 131 } 132 _, r.Err = c.Put(resourceURL(c, id), b, &r.Body, &golangsdk.RequestOpts{ 133 OkCodes: []int{200}, 134 }) 135 return 136 } 137 138 // Delete will permanently delete a particular firewall policy based on its 139 // unique ID. 140 func Delete(c *golangsdk.ServiceClient, id string) (r DeleteResult) { 141 _, r.Err = c.Delete(resourceURL(c, id), nil) 142 return 143 } 144 145 // InsertRuleOptsBuilder allows extensions to add additional parameters to the 146 // InsertRule request. 147 type InsertRuleOptsBuilder interface { 148 ToFirewallPolicyInsertRuleMap() (map[string]interface{}, error) 149 } 150 151 // InsertRuleOpts contains the values used when updating a policy's rules. 152 type InsertRuleOpts struct { 153 ID string `json:"firewall_rule_id" required:"true"` 154 BeforeRuleID string `json:"insert_before,omitempty"` 155 AfterRuleID string `json:"insert_after,omitempty"` 156 } 157 158 func (opts InsertRuleOpts) ToFirewallPolicyInsertRuleMap() (map[string]interface{}, error) { 159 return golangsdk.BuildRequestBody(opts, "") 160 } 161 162 // AddRule will add a rule to a policy. 163 func AddRule(c *golangsdk.ServiceClient, id string, opts InsertRuleOptsBuilder) (r InsertRuleResult) { 164 b, err := opts.ToFirewallPolicyInsertRuleMap() 165 if err != nil { 166 r.Err = err 167 return 168 } 169 _, r.Err = c.Put(insertURL(c, id), b, &r.Body, &golangsdk.RequestOpts{ 170 OkCodes: []int{200}, 171 }) 172 return 173 } 174 175 // RemoveRule will add a rule to a policy. 176 func RemoveRule(c *golangsdk.ServiceClient, id, ruleID string) (r RemoveRuleResult) { 177 b := map[string]interface{}{"firewall_rule_id": ruleID} 178 _, r.Err = c.Put(removeURL(c, id), b, &r.Body, &golangsdk.RequestOpts{ 179 OkCodes: []int{200}, 180 }) 181 return 182 }