github.com/gophercloud/gophercloud@v1.11.0/openstack/networking/v2/extensions/fwaas/firewalls/requests.go (about) 1 package firewalls 2 3 import ( 4 "github.com/gophercloud/gophercloud" 5 "github.com/gophercloud/gophercloud/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 := gophercloud.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 *gophercloud.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 gophercloud.BuildRequestBody(opts, "firewall") 83 } 84 85 // Create accepts a CreateOpts struct and uses the values to create a new firewall. 86 func Create(c *gophercloud.ServiceClient, opts CreateOptsBuilder) (r CreateResult) { 87 b, err := opts.ToFirewallCreateMap() 88 if err != nil { 89 r.Err = err 90 return 91 } 92 resp, err := c.Post(rootURL(c), b, &r.Body, nil) 93 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 94 return 95 } 96 97 // Get retrieves a particular firewall based on its unique ID. 98 func Get(c *gophercloud.ServiceClient, id string) (r GetResult) { 99 resp, err := c.Get(resourceURL(c, id), &r.Body, nil) 100 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 101 return 102 } 103 104 // UpdateOptsBuilder allows extensions to add additional parameters to the 105 // Update request. 106 type UpdateOptsBuilder interface { 107 ToFirewallUpdateMap() (map[string]interface{}, error) 108 } 109 110 // UpdateOpts contains the values used when updating a firewall. 111 type UpdateOpts struct { 112 PolicyID string `json:"firewall_policy_id" required:"true"` 113 Name *string `json:"name,omitempty"` 114 Description *string `json:"description,omitempty"` 115 AdminStateUp *bool `json:"admin_state_up,omitempty"` 116 Shared *bool `json:"shared,omitempty"` 117 } 118 119 // ToFirewallUpdateMap casts a CreateOpts struct to a map. 120 func (opts UpdateOpts) ToFirewallUpdateMap() (map[string]interface{}, error) { 121 return gophercloud.BuildRequestBody(opts, "firewall") 122 } 123 124 // Update allows firewalls to be updated. 125 func Update(c *gophercloud.ServiceClient, id string, opts UpdateOptsBuilder) (r UpdateResult) { 126 b, err := opts.ToFirewallUpdateMap() 127 if err != nil { 128 r.Err = err 129 return 130 } 131 resp, err := c.Put(resourceURL(c, id), b, &r.Body, &gophercloud.RequestOpts{ 132 OkCodes: []int{200}, 133 }) 134 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 135 return 136 } 137 138 // Delete will permanently delete a particular firewall based on its unique ID. 139 func Delete(c *gophercloud.ServiceClient, id string) (r DeleteResult) { 140 resp, err := c.Delete(resourceURL(c, id), nil) 141 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 142 return 143 }