github.com/huaweicloud/golangsdk@v0.0.0-20210831081626-d823fe11ceba/openstack/networking/v2/extensions/fwaas_v2/firewall_groups/requests.go (about)

     1  package firewall_groups
     2  
     3  import (
     4  	"github.com/huaweicloud/golangsdk"
     5  	"github.com/huaweicloud/golangsdk/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  	return q.String(), err
    39  }
    40  
    41  // List returns a Pager which allows you to iterate over a collection of
    42  // firewall_groups. It accepts a ListOpts struct, which allows you to filter
    43  // and sort the returned collection for greater efficiency.
    44  //
    45  // Default policy settings return only those firewall_groups that are owned by the
    46  // tenant who submits the request, unless an admin user submits the request.
    47  func List(c *golangsdk.ServiceClient, opts ListOptsBuilder) pagination.Pager {
    48  	url := rootURL(c)
    49  	if opts != nil {
    50  		query, err := opts.ToFirewallGroupListQuery()
    51  		if err != nil {
    52  			return pagination.Pager{Err: err}
    53  		}
    54  		url += query
    55  	}
    56  	return pagination.NewPager(c, url, func(r pagination.PageResult) pagination.Page {
    57  		return FirewallGroupPage{pagination.LinkedPageBase{PageResult: r}}
    58  	})
    59  }
    60  
    61  // CreateOptsBuilder is the interface options structs have to satisfy in order
    62  // to be used in the main Create operation in this package. Since many
    63  // extensions decorate or modify the common logic, it is useful for them to
    64  // satisfy a basic interface in order for them to be used.
    65  type CreateOptsBuilder interface {
    66  	ToFirewallGroupCreateMap() (map[string]interface{}, error)
    67  }
    68  
    69  // CreateOpts contains all the values needed to create a new firewall_group.
    70  type CreateOpts struct {
    71  	IngressPolicyID string `json:"ingress_firewall_policy_id,omitempty"`
    72  	EgressPolicyID  string `json:"egress_firewall_policy_id,omitempty"`
    73  	// Only required if the caller has an admin role and wants to create a firewall
    74  	// for another tenant.
    75  	TenantID     string `json:"tenant_id,omitempty"`
    76  	Name         string `json:"name,omitempty"`
    77  	Description  string `json:"description,omitempty"`
    78  	AdminStateUp *bool  `json:"admin_state_up,omitempty"`
    79  	Shared       *bool  `json:"public,omitempty"`
    80  }
    81  
    82  // ToFirewallGroupCreateMap casts a CreateOpts struct to a map.
    83  func (opts CreateOpts) ToFirewallGroupCreateMap() (map[string]interface{}, error) {
    84  	return golangsdk.BuildRequestBody(opts, "firewall_group")
    85  }
    86  
    87  // Create accepts a CreateOpts struct and uses the values to create a new firewall group
    88  func Create(c *golangsdk.ServiceClient, opts CreateOptsBuilder) (r CreateResult) {
    89  	b, err := opts.ToFirewallGroupCreateMap()
    90  	if err != nil {
    91  		r.Err = err
    92  		return
    93  	}
    94  	//fmt.Printf("Creating %+v.\n", r)
    95  	_, r.Err = c.Post(rootURL(c), b, &r.Body, nil)
    96  	//fmt.Printf("Created %+v.\n", r)
    97  	return
    98  }
    99  
   100  // Get retrieves a particular firewall based on its unique ID.
   101  func Get(c *golangsdk.ServiceClient, id string) (r GetResult) {
   102  	_, r.Err = c.Get(resourceURL(c, id), &r.Body, nil)
   103  	return
   104  }
   105  
   106  // UpdateOptsBuilder is the interface options structs have to satisfy in order
   107  // to be used in the main Update operation in this package. Since many
   108  // extensions decorate or modify the common logic, it is useful for them to
   109  // satisfy a basic interface in order for them to be used.
   110  type UpdateOptsBuilder interface {
   111  	ToFirewallGroupUpdateMap() (map[string]interface{}, error)
   112  }
   113  
   114  // UpdateOpts contains the values used when updating a firewall.
   115  type UpdateOpts struct {
   116  	IngressPolicyID string `json:"ingress_firewall_policy_id,omitempty"`
   117  	EgressPolicyID  string `json:"egress_firewall_policy_id,omitempty"`
   118  	Name            string `json:"name,omitempty"`
   119  	Description     string `json:"description,omitempty"`
   120  	AdminStateUp    *bool  `json:"admin_state_up,omitempty"`
   121  	Shared          *bool  `json:"public,omitempty"`
   122  }
   123  
   124  // ToFirewallGroupUpdateMap casts a CreateOpts struct to a map.
   125  func (opts UpdateOpts) ToFirewallGroupUpdateMap() (map[string]interface{}, error) {
   126  	return golangsdk.BuildRequestBody(opts, "firewall_group")
   127  }
   128  
   129  // Update allows firewall_groups to be updated.
   130  func Update(c *golangsdk.ServiceClient, id string, opts UpdateOptsBuilder) (r UpdateResult) {
   131  	b, err := opts.ToFirewallGroupUpdateMap()
   132  	if err != nil {
   133  		r.Err = err
   134  		return
   135  	}
   136  	_, r.Err = c.Put(resourceURL(c, id), b, &r.Body, &golangsdk.RequestOpts{
   137  		OkCodes: []int{200},
   138  	})
   139  	return
   140  }
   141  
   142  // Delete will permanently delete a particular firewall based on its unique ID.
   143  func Delete(c *golangsdk.ServiceClient, id string) (r DeleteResult) {
   144  	_, r.Err = c.Delete(resourceURL(c, id), nil)
   145  	return
   146  }