firewall_rule.go raw

   1  package govultr
   2  
   3  import (
   4  	"context"
   5  	"fmt"
   6  	"net/http"
   7  
   8  	"github.com/google/go-querystring/query"
   9  )
  10  
  11  // FireWallRuleService is the interface to interact with the firewall rule endpoints on the Vultr API
  12  // Link : https://www.vultr.com/api/#tag/firewall
  13  type FireWallRuleService interface {
  14  	Create(ctx context.Context, fwGroupID string, fwRuleReq *FirewallRuleReq) (*FirewallRule, *http.Response, error)
  15  	Get(ctx context.Context, fwGroupID string, fwRuleID int) (*FirewallRule, *http.Response, error)
  16  	Delete(ctx context.Context, fwGroupID string, fwRuleID int) error
  17  	List(ctx context.Context, fwGroupID string, options *ListOptions) ([]FirewallRule, *Meta, *http.Response, error)
  18  }
  19  
  20  // FireWallRuleServiceHandler handles interaction with the firewall rule methods for the Vultr API
  21  type FireWallRuleServiceHandler struct {
  22  	client *Client
  23  }
  24  
  25  // FirewallRule represents a Vultr firewall rule
  26  type FirewallRule struct {
  27  	ID         int    `json:"id"`
  28  	Action     string `json:"action"`
  29  	IPType     string `json:"ip_type"`
  30  	Protocol   string `json:"protocol"`
  31  	Port       string `json:"port"`
  32  	Subnet     string `json:"subnet"`
  33  	SubnetSize int    `json:"subnet_size"`
  34  	Source     string `json:"source"`
  35  	Notes      string `json:"notes"`
  36  }
  37  
  38  // FirewallRuleReq struct used to create a FirewallRule.
  39  type FirewallRuleReq struct {
  40  	IPType     string `json:"ip_type"`
  41  	Protocol   string `json:"protocol"`
  42  	Subnet     string `json:"subnet"`
  43  	SubnetSize int    `json:"subnet_size"`
  44  	Port       string `json:"port,omitempty"`
  45  	Source     string `json:"source,omitempty"`
  46  	Notes      string `json:"notes,omitempty"`
  47  }
  48  
  49  type firewallRulesBase struct {
  50  	FirewallRules []FirewallRule `json:"firewall_rules"`
  51  	Meta          *Meta          `json:"meta"`
  52  }
  53  
  54  type firewallRuleBase struct {
  55  	FirewallRule *FirewallRule `json:"firewall_rule"`
  56  }
  57  
  58  // Create will create a rule in a firewall group.
  59  func (f *FireWallRuleServiceHandler) Create(ctx context.Context, fwGroupID string, fwRuleReq *FirewallRuleReq) (*FirewallRule, *http.Response, error) { //nolint:lll
  60  	uri := fmt.Sprintf("/v2/firewalls/%s/rules", fwGroupID)
  61  
  62  	req, err := f.client.NewRequest(ctx, http.MethodPost, uri, fwRuleReq)
  63  	if err != nil {
  64  		return nil, nil, err
  65  	}
  66  
  67  	firewallRule := new(firewallRuleBase)
  68  	resp, err := f.client.DoWithContext(ctx, req, firewallRule)
  69  	if err != nil {
  70  		return nil, resp, err
  71  	}
  72  
  73  	return firewallRule.FirewallRule, resp, nil
  74  }
  75  
  76  // Get will get a rule in a firewall group.
  77  func (f *FireWallRuleServiceHandler) Get(ctx context.Context, fwGroupID string, fwRuleID int) (*FirewallRule, *http.Response, error) {
  78  	uri := fmt.Sprintf("/v2/firewalls/%s/rules/%d", fwGroupID, fwRuleID)
  79  
  80  	req, err := f.client.NewRequest(ctx, http.MethodGet, uri, nil)
  81  	if err != nil {
  82  		return nil, nil, err
  83  	}
  84  
  85  	firewallRule := new(firewallRuleBase)
  86  	resp, err := f.client.DoWithContext(ctx, req, firewallRule)
  87  	if err != nil {
  88  		return nil, resp, err
  89  	}
  90  
  91  	return firewallRule.FirewallRule, resp, nil
  92  }
  93  
  94  // Delete will delete a firewall rule on your Vultr account
  95  func (f *FireWallRuleServiceHandler) Delete(ctx context.Context, fwGroupID string, fwRuleID int) error {
  96  	uri := fmt.Sprintf("/v2/firewalls/%s/rules/%d", fwGroupID, fwRuleID)
  97  
  98  	req, err := f.client.NewRequest(ctx, http.MethodDelete, uri, nil)
  99  	if err != nil {
 100  		return err
 101  	}
 102  	_, err = f.client.DoWithContext(ctx, req, nil)
 103  	return err
 104  }
 105  
 106  // List will return both ipv4 an ipv6 firewall rules that are defined within a firewall group
 107  func (f *FireWallRuleServiceHandler) List(ctx context.Context, fwGroupID string, options *ListOptions) ([]FirewallRule, *Meta, *http.Response, error) { //nolint:lll,dupl
 108  	uri := fmt.Sprintf("/v2/firewalls/%s/rules", fwGroupID)
 109  
 110  	req, err := f.client.NewRequest(ctx, http.MethodGet, uri, nil)
 111  	if err != nil {
 112  		return nil, nil, nil, err
 113  	}
 114  
 115  	newValues, err := query.Values(options)
 116  	if err != nil {
 117  		return nil, nil, nil, err
 118  	}
 119  
 120  	req.URL.RawQuery = newValues.Encode()
 121  
 122  	firewallRule := new(firewallRulesBase)
 123  	resp, err := f.client.DoWithContext(ctx, req, firewallRule)
 124  	if err != nil {
 125  		return nil, nil, resp, err
 126  	}
 127  
 128  	return firewallRule.FirewallRules, firewallRule.Meta, resp, nil
 129  }
 130