github.com/opentelekomcloud/gophertelekomcloud@v0.9.3/openstack/networking/v2/extensions/fwaas_v2/firewall_groups/requests.go (about)

     1  package firewall_groups
     2  
     3  import (
     4  	"github.com/opentelekomcloud/gophertelekomcloud"
     5  	"github.com/opentelekomcloud/gophertelekomcloud/pagination"
     6  	// "fmt"
     7  )
     8  
     9  // ListOptsBuilder allows extensions to add additional parameters to the
    10  // List request.
    11  type ListOptsBuilder interface {
    12  	ToFirewallGroupListQuery() (string, error)
    13  }
    14  
    15  // ListOpts allows the filtering and sorting of paginated collections through
    16  // the API. Filtering is achieved by passing in struct field values that map to
    17  // the firewall attributes you want to see returned. SortKey allows you to sort
    18  // by a particular firewall attribute. SortDir sets the direction, and is either
    19  // `asc' or `desc'. Marker and Limit are used for pagination.
    20  type ListOpts struct {
    21  	TenantID        string `q:"tenant_id"`
    22  	Name            string `q:"name"`
    23  	Description     string `q:"description"`
    24  	AdminStateUp    bool   `q:"admin_state_up"`
    25  	Shared          bool   `q:"public"`
    26  	IngressPolicyID string `q:"ingress_firewall_policy_id"`
    27  	EgressPolicyID  string `q:"egress_firewall_policy_id"`
    28  	ID              string `q:"id"`
    29  	Limit           int    `q:"limit"`
    30  	Marker          string `q:"marker"`
    31  	SortKey         string `q:"sort_key"`
    32  	SortDir         string `q:"sort_dir"`
    33  }
    34  
    35  // ToFirewallListQuery formats a ListOpts into a query string.
    36  func (opts ListOpts) ToFirewallGroupListQuery() (string, error) {
    37  	q, err := golangsdk.BuildQueryString(opts)
    38  	if err != nil {
    39  		return "", err
    40  	}
    41  	return q.String(), err
    42  }
    43  
    44  // List returns a Pager which allows you to iterate over a collection of
    45  // firewall_groups. It accepts a ListOpts struct, which allows you to filter
    46  // and sort the returned collection for greater efficiency.
    47  //
    48  // Default policy settings return only those firewall_groups that are owned by the
    49  // tenant who submits the request, unless an admin user submits the request.
    50  func List(c *golangsdk.ServiceClient, opts ListOptsBuilder) pagination.Pager {
    51  	url := rootURL(c)
    52  	if opts != nil {
    53  		query, err := opts.ToFirewallGroupListQuery()
    54  		if err != nil {
    55  			return pagination.Pager{Err: err}
    56  		}
    57  		url += query
    58  	}
    59  	return pagination.NewPager(c, url, func(r pagination.PageResult) pagination.Page {
    60  		return FirewallGroupPage{pagination.LinkedPageBase{PageResult: r}}
    61  	})
    62  }
    63  
    64  // CreateOptsBuilder is the interface options structs have to satisfy in order
    65  // to be used in the main Create operation in this package. Since many
    66  // extensions decorate or modify the common logic, it is useful for them to
    67  // satisfy a basic interface in order for them to be used.
    68  type CreateOptsBuilder interface {
    69  	ToFirewallGroupCreateMap() (map[string]interface{}, error)
    70  }
    71  
    72  // CreateOpts contains all the values needed to create a new firewall_group.
    73  type CreateOpts struct {
    74  	IngressPolicyID string `json:"ingress_firewall_policy_id,omitempty"`
    75  	EgressPolicyID  string `json:"egress_firewall_policy_id,omitempty"`
    76  	// Only required if the caller has an admin role and wants to create a firewall
    77  	// for another tenant.
    78  	TenantID     string `json:"tenant_id,omitempty"`
    79  	Name         string `json:"name,omitempty"`
    80  	Description  string `json:"description,omitempty"`
    81  	AdminStateUp *bool  `json:"admin_state_up,omitempty"`
    82  	Shared       *bool  `json:"public,omitempty"`
    83  }
    84  
    85  // ToFirewallGroupCreateMap casts a CreateOpts struct to a map.
    86  func (opts CreateOpts) ToFirewallGroupCreateMap() (map[string]interface{}, error) {
    87  	return golangsdk.BuildRequestBody(opts, "firewall_group")
    88  }
    89  
    90  // Create accepts a CreateOpts struct and uses the values to create a new firewall group
    91  func Create(c *golangsdk.ServiceClient, opts CreateOptsBuilder) (r CreateResult) {
    92  	b, err := opts.ToFirewallGroupCreateMap()
    93  	if err != nil {
    94  		r.Err = err
    95  		return
    96  	}
    97  	// fmt.Printf("Creating %+v.\n", r)
    98  	_, r.Err = c.Post(rootURL(c), b, &r.Body, nil)
    99  	// fmt.Printf("Created %+v.\n", r)
   100  	return
   101  }
   102  
   103  // Get retrieves a particular firewall based on its unique ID.
   104  func Get(c *golangsdk.ServiceClient, id string) (r GetResult) {
   105  	_, r.Err = c.Get(resourceURL(c, id), &r.Body, nil)
   106  	return
   107  }
   108  
   109  // UpdateOptsBuilder is the interface options structs have to satisfy in order
   110  // to be used in the main Update operation in this package. Since many
   111  // extensions decorate or modify the common logic, it is useful for them to
   112  // satisfy a basic interface in order for them to be used.
   113  type UpdateOptsBuilder interface {
   114  	ToFirewallGroupUpdateMap() (map[string]interface{}, error)
   115  }
   116  
   117  // UpdateOpts contains the values used when updating a firewall.
   118  type UpdateOpts struct {
   119  	IngressPolicyID string `json:"ingress_firewall_policy_id,omitempty"`
   120  	EgressPolicyID  string `json:"egress_firewall_policy_id,omitempty"`
   121  	Name            string `json:"name,omitempty"`
   122  	Description     string `json:"description,omitempty"`
   123  	AdminStateUp    *bool  `json:"admin_state_up,omitempty"`
   124  	Shared          *bool  `json:"public,omitempty"`
   125  }
   126  
   127  // ToFirewallGroupUpdateMap casts a CreateOpts struct to a map.
   128  func (opts UpdateOpts) ToFirewallGroupUpdateMap() (map[string]interface{}, error) {
   129  	return golangsdk.BuildRequestBody(opts, "firewall_group")
   130  }
   131  
   132  // Update allows firewall_groups to be updated.
   133  func Update(c *golangsdk.ServiceClient, id string, opts UpdateOptsBuilder) (r UpdateResult) {
   134  	b, err := opts.ToFirewallGroupUpdateMap()
   135  	if err != nil {
   136  		r.Err = err
   137  		return
   138  	}
   139  	_, r.Err = c.Put(resourceURL(c, id), b, &r.Body, &golangsdk.RequestOpts{
   140  		OkCodes: []int{200},
   141  	})
   142  	return
   143  }
   144  
   145  // Delete will permanently delete a particular firewall based on its unique ID.
   146  func Delete(c *golangsdk.ServiceClient, id string) (r DeleteResult) {
   147  	_, r.Err = c.Delete(resourceURL(c, id), nil)
   148  	return
   149  }