github.com/vnpaycloud-console/gophercloud/v2@v2.0.5/openstack/networking/v2/extensions/fwaas_v2/rules/requests.go (about)

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