github.com/huaweicloud/golangsdk@v0.0.0-20210831081626-d823fe11ceba/openstack/networking/v2/extensions/fwaas/firewalls/requests.go (about) 1 package firewalls 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 ToFirewallListQuery() (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 attributes you want to see returned. SortKey allows you to sort 17 // by a particular firewall attribute. SortDir sets the direction, and is either 18 // `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 AdminStateUp bool `q:"admin_state_up"` 25 Shared bool `q:"shared"` 26 PolicyID string `q:"firewall_policy_id"` 27 ID string `q:"id"` 28 Limit int `q:"limit"` 29 Marker string `q:"marker"` 30 SortKey string `q:"sort_key"` 31 SortDir string `q:"sort_dir"` 32 } 33 34 // ToFirewallListQuery formats a ListOpts into a query string. 35 func (opts ListOpts) ToFirewallListQuery() (string, error) { 36 q, err := golangsdk.BuildQueryString(opts) 37 return q.String(), err 38 } 39 40 // List returns a Pager which allows you to iterate over a collection of 41 // firewalls. It accepts a ListOpts struct, which allows you to filter 42 // and sort the returned collection for greater efficiency. 43 // 44 // Default policy settings return only those firewalls that are owned by the 45 // tenant who submits the request, unless an admin user submits the request. 46 func List(c *golangsdk.ServiceClient, opts ListOptsBuilder) pagination.Pager { 47 url := rootURL(c) 48 if opts != nil { 49 query, err := opts.ToFirewallListQuery() 50 if err != nil { 51 return pagination.Pager{Err: err} 52 } 53 url += query 54 } 55 return pagination.NewPager(c, url, func(r pagination.PageResult) pagination.Page { 56 return FirewallPage{pagination.LinkedPageBase{PageResult: r}} 57 }) 58 } 59 60 // CreateOptsBuilder allows extensions to add additional parameters to the 61 // Create request. 62 type CreateOptsBuilder interface { 63 ToFirewallCreateMap() (map[string]interface{}, error) 64 } 65 66 // CreateOpts contains all the values needed to create a new firewall. 67 type CreateOpts struct { 68 PolicyID string `json:"firewall_policy_id" required:"true"` 69 // TenantID specifies a tenant to own the firewall. The caller must have 70 // an admin role in order to set this. Otherwise, this field is left unset 71 // and the caller will be the owner. 72 TenantID string `json:"tenant_id,omitempty"` 73 ProjectID string `json:"project_id,omitempty"` 74 Name string `json:"name,omitempty"` 75 Description string `json:"description,omitempty"` 76 AdminStateUp *bool `json:"admin_state_up,omitempty"` 77 Shared *bool `json:"shared,omitempty"` 78 } 79 80 // ToFirewallCreateMap casts a CreateOpts struct to a map. 81 func (opts CreateOpts) ToFirewallCreateMap() (map[string]interface{}, error) { 82 return golangsdk.BuildRequestBody(opts, "firewall") 83 } 84 85 // Create accepts a CreateOpts struct and uses the values to create a new firewall. 86 func Create(c *golangsdk.ServiceClient, opts CreateOptsBuilder) (r CreateResult) { 87 b, err := opts.ToFirewallCreateMap() 88 if err != nil { 89 r.Err = err 90 return 91 } 92 _, r.Err = c.Post(rootURL(c), b, &r.Body, nil) 93 return 94 } 95 96 // Get retrieves a particular firewall based on its unique ID. 97 func Get(c *golangsdk.ServiceClient, id string) (r GetResult) { 98 _, r.Err = c.Get(resourceURL(c, id), &r.Body, nil) 99 return 100 } 101 102 // UpdateOptsBuilder allows extensions to add additional parameters to the 103 // Update request. 104 type UpdateOptsBuilder interface { 105 ToFirewallUpdateMap() (map[string]interface{}, error) 106 } 107 108 // UpdateOpts contains the values used when updating a firewall. 109 type UpdateOpts struct { 110 PolicyID string `json:"firewall_policy_id" required:"true"` 111 Name string `json:"name,omitempty"` 112 Description string `json:"description,omitempty"` 113 AdminStateUp *bool `json:"admin_state_up,omitempty"` 114 Shared *bool `json:"shared,omitempty"` 115 } 116 117 // ToFirewallUpdateMap casts a CreateOpts struct to a map. 118 func (opts UpdateOpts) ToFirewallUpdateMap() (map[string]interface{}, error) { 119 return golangsdk.BuildRequestBody(opts, "firewall") 120 } 121 122 // Update allows firewalls to be updated. 123 func Update(c *golangsdk.ServiceClient, id string, opts UpdateOptsBuilder) (r UpdateResult) { 124 b, err := opts.ToFirewallUpdateMap() 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 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 }