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