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

     1  package rules
     2  
     3  import (
     4  	"github.com/huaweicloud/golangsdk"
     5  	"github.com/huaweicloud/golangsdk/pagination"
     6  )
     7  
     8  type (
     9  	// Protocol represents a valid rule protocol
    10  	Protocol string
    11  )
    12  
    13  const (
    14  	// ProtocolAny is to allow any protocol
    15  	ProtocolAny Protocol = "any"
    16  
    17  	// ProtocolICMP is to allow the ICMP protocol
    18  	ProtocolICMP Protocol = "icmp"
    19  
    20  	// ProtocolTCP is to allow the TCP protocol
    21  	ProtocolTCP Protocol = "tcp"
    22  
    23  	// ProtocolUDP is to allow the UDP protocol
    24  	ProtocolUDP Protocol = "udp"
    25  )
    26  
    27  // ListOptsBuilder allows extensions to add additional parameters to the
    28  // List request.
    29  type ListOptsBuilder interface {
    30  	ToRuleListQuery() (string, error)
    31  }
    32  
    33  // ListOpts allows the filtering and sorting of paginated collections through
    34  // the API. Filtering is achieved by passing in struct field values that map to
    35  // the Firewall rule attributes you want to see returned. SortKey allows you to
    36  // sort by a particular firewall rule attribute. SortDir sets the direction, and is
    37  // either `asc' or `desc'. Marker and Limit are used for pagination.
    38  type ListOpts struct {
    39  	TenantID             string `q:"tenant_id"`
    40  	Name                 string `q:"name"`
    41  	Description          string `q:"description"`
    42  	Protocol             string `q:"protocol"`
    43  	Action               string `q:"action"`
    44  	IPVersion            int    `q:"ip_version"`
    45  	SourceIPAddress      string `q:"source_ip_address"`
    46  	DestinationIPAddress string `q:"destination_ip_address"`
    47  	SourcePort           string `q:"source_port"`
    48  	DestinationPort      string `q:"destination_port"`
    49  	Enabled              bool   `q:"enabled"`
    50  	ID                   string `q:"id"`
    51  	Limit                int    `q:"limit"`
    52  	Marker               string `q:"marker"`
    53  	SortKey              string `q:"sort_key"`
    54  	SortDir              string `q:"sort_dir"`
    55  }
    56  
    57  // ToRuleListQuery formats a ListOpts into a query string.
    58  func (opts ListOpts) ToRuleListQuery() (string, error) {
    59  	q, err := golangsdk.BuildQueryString(opts)
    60  	if err != nil {
    61  		return "", err
    62  	}
    63  	return q.String(), nil
    64  }
    65  
    66  // List returns a Pager which allows you to iterate over a collection of
    67  // firewall rules. It accepts a ListOpts struct, which allows you to filter
    68  // and sort the returned collection for greater efficiency.
    69  //
    70  // Default policy settings return only those firewall rules that are owned by the
    71  // tenant who submits the request, unless an admin user submits the request.
    72  func List(c *golangsdk.ServiceClient, opts ListOptsBuilder) pagination.Pager {
    73  	url := rootURL(c)
    74  
    75  	if opts != nil {
    76  		query, err := opts.ToRuleListQuery()
    77  		if err != nil {
    78  			return pagination.Pager{Err: err}
    79  		}
    80  		url += query
    81  	}
    82  
    83  	return pagination.NewPager(c, url, func(r pagination.PageResult) pagination.Page {
    84  		return RulePage{pagination.LinkedPageBase{PageResult: r}}
    85  	})
    86  }
    87  
    88  // CreateOptsBuilder is the interface options structs have to satisfy in order
    89  // to be used in the main Create operation in this package. Since many
    90  // extensions decorate or modify the common logic, it is useful for them to
    91  // satisfy a basic interface in order for them to be used.
    92  type CreateOptsBuilder interface {
    93  	ToRuleCreateMap() (map[string]interface{}, error)
    94  }
    95  
    96  // CreateOpts contains all the values needed to create a new firewall rule.
    97  type CreateOpts struct {
    98  	Protocol             Protocol            `json:"protocol" required:"true"`
    99  	Action               string              `json:"action" required:"true"`
   100  	TenantID             string              `json:"tenant_id,omitempty"`
   101  	Name                 string              `json:"name,omitempty"`
   102  	Description          string              `json:"description,omitempty"`
   103  	IPVersion            golangsdk.IPVersion `json:"ip_version,omitempty"`
   104  	SourceIPAddress      string              `json:"source_ip_address,omitempty"`
   105  	DestinationIPAddress string              `json:"destination_ip_address,omitempty"`
   106  	SourcePort           string              `json:"source_port,omitempty"`
   107  	DestinationPort      string              `json:"destination_port,omitempty"`
   108  	Shared               *bool               `json:"shared,omitempty"`
   109  	Enabled              *bool               `json:"enabled,omitempty"`
   110  }
   111  
   112  // ToRuleCreateMap casts a CreateOpts struct to a map.
   113  func (opts CreateOpts) ToRuleCreateMap() (map[string]interface{}, error) {
   114  	b, err := golangsdk.BuildRequestBody(opts, "firewall_rule")
   115  	if err != nil {
   116  		return nil, err
   117  	}
   118  
   119  	if m := b["firewall_rule"].(map[string]interface{}); m["protocol"] == "any" {
   120  		m["protocol"] = nil
   121  	}
   122  
   123  	return b, nil
   124  }
   125  
   126  // Create accepts a CreateOpts struct and uses the values to create a new firewall rule
   127  func Create(c *golangsdk.ServiceClient, opts CreateOptsBuilder) (r CreateResult) {
   128  	b, err := opts.ToRuleCreateMap()
   129  	if err != nil {
   130  		r.Err = err
   131  		return
   132  	}
   133  	_, r.Err = c.Post(rootURL(c), b, &r.Body, nil)
   134  	return
   135  }
   136  
   137  // Get retrieves a particular firewall rule based on its unique ID.
   138  func Get(c *golangsdk.ServiceClient, id string) (r GetResult) {
   139  	_, r.Err = c.Get(resourceURL(c, id), &r.Body, nil)
   140  	return
   141  }
   142  
   143  // UpdateOptsBuilder is the interface options structs have to satisfy in order
   144  // to be used in the main Update operation in this package. Since many
   145  // extensions decorate or modify the common logic, it is useful for them to
   146  // satisfy a basic interface in order for them to be used.
   147  type UpdateOptsBuilder interface {
   148  	ToRuleUpdateMap() (map[string]interface{}, error)
   149  }
   150  
   151  // UpdateOpts contains the values used when updating a firewall rule.
   152  type UpdateOpts struct {
   153  	Protocol             *string              `json:"protocol,omitempty"`
   154  	Action               *string              `json:"action,omitempty"`
   155  	Name                 *string              `json:"name,omitempty"`
   156  	Description          *string              `json:"description,omitempty"`
   157  	IPVersion            *golangsdk.IPVersion `json:"ip_version,omitempty"`
   158  	SourceIPAddress      *string              `json:"source_ip_address,omitempty"`
   159  	DestinationIPAddress *string              `json:"destination_ip_address,omitempty"`
   160  	SourcePort           *string              `json:"source_port,omitempty"`
   161  	DestinationPort      *string              `json:"destination_port,omitempty"`
   162  	Shared               *bool                `json:"shared,omitempty"`
   163  	Enabled              *bool                `json:"enabled,omitempty"`
   164  }
   165  
   166  // ToRuleUpdateMap casts a UpdateOpts struct to a map.
   167  func (opts UpdateOpts) ToRuleUpdateMap() (map[string]interface{}, error) {
   168  	return golangsdk.BuildRequestBody(opts, "firewall_rule")
   169  }
   170  
   171  // Update allows firewall policies to be updated.
   172  func Update(c *golangsdk.ServiceClient, id string, opts UpdateOptsBuilder) (r UpdateResult) {
   173  	b, err := opts.ToRuleUpdateMap()
   174  	if err != nil {
   175  		r.Err = err
   176  		return
   177  	}
   178  	_, r.Err = c.Put(resourceURL(c, id), b, &r.Body, &golangsdk.RequestOpts{
   179  		OkCodes: []int{200},
   180  	})
   181  	return
   182  }
   183  
   184  // Delete will permanently delete a particular firewall rule based on its unique ID.
   185  func Delete(c *golangsdk.ServiceClient, id string) (r DeleteResult) {
   186  	_, r.Err = c.Delete(resourceURL(c, id), nil)
   187  	return
   188  }