github.com/huaweicloud/golangsdk@v0.0.0-20210831081626-d823fe11ceba/openstack/iec/v1/firewalls/requests.go (about)

     1  package firewalls
     2  
     3  import (
     4  	"net/http"
     5  
     6  	"github.com/huaweicloud/golangsdk"
     7  )
     8  
     9  type CreateOpts struct {
    10  	Name        string `json:"name,true"`
    11  	Description string `json:"description,omitempty"`
    12  }
    13  
    14  type CreateOptsBuilder interface {
    15  	ToFirewallCreateMap() (map[string]interface{}, error)
    16  }
    17  
    18  func (opts CreateOpts) ToFirewallCreateMap() (map[string]interface{}, error) {
    19  	b, err := golangsdk.BuildRequestBody(&opts, "firewall")
    20  	if err != nil {
    21  		return nil, err
    22  	}
    23  	return b, nil
    24  }
    25  
    26  func Create(client *golangsdk.ServiceClient, opts CreateOptsBuilder) (r CreateResult) {
    27  	b, err := opts.ToFirewallCreateMap()
    28  	if err != nil {
    29  		r.Err = err
    30  		return
    31  	}
    32  
    33  	_, r.Err = client.Post(rootURL(client), b, &r.Body, &golangsdk.RequestOpts{
    34  		OkCodes: []int{http.StatusOK},
    35  	})
    36  	return
    37  }
    38  
    39  func Delete(client *golangsdk.ServiceClient, firewallID string) (r DeleteResult) {
    40  	url := DeleteURL(client, firewallID)
    41  	_, r.Err = client.Delete(url, nil)
    42  	return
    43  }
    44  
    45  func Get(client *golangsdk.ServiceClient, fwID string) (r GetResult) {
    46  	url := GetURL(client, fwID)
    47  	_, r.Err = client.Get(url, &r.Body, &golangsdk.RequestOpts{
    48  		OkCodes: []int{http.StatusOK},
    49  	})
    50  	return
    51  }
    52  
    53  type ReqSubnet struct {
    54  	ID    string `json:"id"`
    55  	VpcID string `json:"vpc_id"`
    56  }
    57  
    58  type UpdateOpts struct {
    59  	Name         string       `json:"name,omitempty"`
    60  	Description  *string      `json:"description,omitempty"`
    61  	AdminStateUp *bool        `json:"admin_state_up,omitempty"`
    62  	Subnets      *[]ReqSubnet `json:"subnets,omitempty"`
    63  }
    64  
    65  type UpdateOptsBuilder interface {
    66  	ToUpdateFirewallMap() (map[string]interface{}, error)
    67  }
    68  
    69  func (opts UpdateOpts) ToUpdateFirewallMap() (map[string]interface{}, error) {
    70  	b, err := golangsdk.BuildRequestBody(opts, "firewall")
    71  	if err != nil {
    72  		return nil, err
    73  	}
    74  	return b, nil
    75  }
    76  
    77  func Update(client *golangsdk.ServiceClient, fwID string, opts UpdateOptsBuilder) (r UpdateResult) {
    78  	b, err := opts.ToUpdateFirewallMap()
    79  	if err != nil {
    80  		r.Err = err
    81  		return
    82  	}
    83  
    84  	_, r.Err = client.Put(UpdateURL(client, fwID), b, &r.Body, &golangsdk.RequestOpts{OkCodes: []int{http.StatusOK}})
    85  	return
    86  }
    87  
    88  type ReqFirewallRulesOpts struct {
    89  	ID          string `json:"id,omitempty"`
    90  	Description string `json:"description"`
    91  	Enabled     *bool  `json:"enabled,omitempty"`
    92  	Name        string `json:"name"`
    93  	Protocol    string `json:"protocol"`
    94  	Action      string `json:"action"`
    95  	IPVersion   int    `json:"ip_version,omitempty"`
    96  	DstIPAddr   string `json:"destination_ip_address"`
    97  	DstPort     string `json:"destination_port"`
    98  	SrcIPAddr   string `json:"source_ip_address"`
    99  	SrcPort     string `json:"source_port"`
   100  	OperateType string `json:"operate_type"`
   101  }
   102  
   103  type ReqPolicyOpts struct {
   104  	PolicyID      string                  `json:"id"`
   105  	FirewallRules *[]ReqFirewallRulesOpts `json:"firewall_rules,omitempty"`
   106  }
   107  
   108  type UpdateRuleOpts struct {
   109  	ReqFirewallOutPolicy *ReqPolicyOpts `json:"egress_firewall_policy,omitempty"`
   110  	ReqFirewallInPolicy  *ReqPolicyOpts `json:"ingress_firewall_policy,omitempty"`
   111  }
   112  
   113  type UpdateRuleOptsBuilder interface {
   114  	ToUpdateFirewallRuleMap() (map[string]interface{}, error)
   115  }
   116  
   117  func (opts UpdateRuleOpts) ToUpdateFirewallRuleMap() (map[string]interface{}, error) {
   118  	b, err := golangsdk.BuildRequestBody(opts, "firewall")
   119  	if err != nil {
   120  		return nil, err
   121  	}
   122  	return b, nil
   123  }
   124  
   125  func UpdateRule(client *golangsdk.ServiceClient, fwID string, opts UpdateRuleOptsBuilder) (r UpdateRuleResult) {
   126  	b, err := opts.ToUpdateFirewallRuleMap()
   127  	if err != nil {
   128  		r.Err = err
   129  		return
   130  	}
   131  
   132  	_, r.Err = client.Put(UpdateRuleURL(client, fwID), b, &r.Body, &golangsdk.RequestOpts{OkCodes: []int{http.StatusOK}})
   133  	return
   134  }
   135  
   136  type ListOpts struct {
   137  	Limit  int    `q:"limit"`
   138  	Offset int    `q:"offset"`
   139  	ID     string `q:"id"`
   140  	Name   string `q:"name"`
   141  }
   142  
   143  type ListFirewallsOptsBuilder interface {
   144  	ToListFirewallsQuery() (string, error)
   145  }
   146  
   147  func (opts ListOpts) ToListFirewallsQuery() (string, error) {
   148  	b, err := golangsdk.BuildQueryString(&opts)
   149  	if err != nil {
   150  		return "", err
   151  	}
   152  	return b.String(), nil
   153  }
   154  
   155  func List(client *golangsdk.ServiceClient, opts ListFirewallsOptsBuilder) (r ListResult) {
   156  	listURL := rootURL(client)
   157  	if opts != nil {
   158  		query, err := opts.ToListFirewallsQuery()
   159  		if err != nil {
   160  			r.Err = err
   161  			return r
   162  		}
   163  		listURL += query
   164  	}
   165  
   166  	_, r.Err = client.Get(listURL, &r.Body, &golangsdk.RequestOpts{
   167  		OkCodes: []int{http.StatusOK},
   168  	})
   169  	return
   170  }