github.com/opentelekomcloud/gophertelekomcloud@v0.9.3/openstack/networking/v2/extensions/fwaas/firewalls/requests.go (about) 1 package firewalls 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 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 if err != nil { 38 return "", err 39 } 40 return q.String(), err 41 } 42 43 // List returns a Pager which allows you to iterate over a collection of 44 // firewalls. It accepts a ListOpts struct, which allows you to filter 45 // and sort the returned collection for greater efficiency. 46 // 47 // Default policy settings return only those firewalls that are owned by the 48 // tenant who submits the request, unless an admin user submits the request. 49 func List(c *golangsdk.ServiceClient, opts ListOptsBuilder) pagination.Pager { 50 url := rootURL(c) 51 if opts != nil { 52 query, err := opts.ToFirewallListQuery() 53 if err != nil { 54 return pagination.Pager{Err: err} 55 } 56 url += query 57 } 58 return pagination.NewPager(c, url, func(r pagination.PageResult) pagination.Page { 59 return FirewallPage{pagination.LinkedPageBase{PageResult: r}} 60 }) 61 } 62 63 // CreateOptsBuilder allows extensions to add additional parameters to the 64 // Create request. 65 type CreateOptsBuilder interface { 66 ToFirewallCreateMap() (map[string]interface{}, error) 67 } 68 69 // CreateOpts contains all the values needed to create a new firewall. 70 type CreateOpts struct { 71 PolicyID string `json:"firewall_policy_id" required:"true"` 72 // TenantID specifies a tenant to own the firewall. The caller must have 73 // an admin role in order to set this. Otherwise, this field is left unset 74 // and the caller will be the owner. 75 TenantID string `json:"tenant_id,omitempty"` 76 ProjectID string `json:"project_id,omitempty"` 77 Name string `json:"name,omitempty"` 78 Description string `json:"description,omitempty"` 79 AdminStateUp *bool `json:"admin_state_up,omitempty"` 80 Shared *bool `json:"shared,omitempty"` 81 } 82 83 // ToFirewallCreateMap casts a CreateOpts struct to a map. 84 func (opts CreateOpts) ToFirewallCreateMap() (map[string]interface{}, error) { 85 return golangsdk.BuildRequestBody(opts, "firewall") 86 } 87 88 // Create accepts a CreateOpts struct and uses the values to create a new firewall. 89 func Create(c *golangsdk.ServiceClient, opts CreateOptsBuilder) (r CreateResult) { 90 b, err := opts.ToFirewallCreateMap() 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 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 ToFirewallUpdateMap() (map[string]interface{}, error) 109 } 110 111 // UpdateOpts contains the values used when updating a firewall. 112 type UpdateOpts struct { 113 PolicyID string `json:"firewall_policy_id" required:"true"` 114 Name string `json:"name,omitempty"` 115 Description string `json:"description,omitempty"` 116 AdminStateUp *bool `json:"admin_state_up,omitempty"` 117 Shared *bool `json:"shared,omitempty"` 118 } 119 120 // ToFirewallUpdateMap casts a CreateOpts struct to a map. 121 func (opts UpdateOpts) ToFirewallUpdateMap() (map[string]interface{}, error) { 122 return golangsdk.BuildRequestBody(opts, "firewall") 123 } 124 125 // Update allows firewalls to be updated. 126 func Update(c *golangsdk.ServiceClient, id string, opts UpdateOptsBuilder) (r UpdateResult) { 127 b, err := opts.ToFirewallUpdateMap() 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 based on its unique ID. 139 func Delete(c *golangsdk.ServiceClient, id string) (r DeleteResult) { 140 _, r.Err = c.Delete(resourceURL(c, id), nil) 141 return 142 }