github.com/chnsz/golangsdk@v0.0.0-20240506093406-85a3fbfa605b/openstack/aad/v1/rules/requests.go (about)

     1  package rules
     2  
     3  import (
     4  	"github.com/chnsz/golangsdk"
     5  )
     6  
     7  // BatchCreateOpts is the structure required by the BatchCreate method to batch create at least one forward rule.
     8  type BatchCreateOpts struct {
     9  	// Forwarding rules created in batches.
    10  	Rules []RuleOpts `json:"rules,omitempty"`
    11  }
    12  
    13  // RuleOpts is the object that represents the forwarding rule configuration structure.
    14  type RuleOpts struct {
    15  	// The forward protocol.
    16  	ForwardProtocol string `json:"forward_protocol" required:"true"`
    17  	// The forward port.
    18  	// The valid value is range from 1 to 65535.
    19  	ForwardPort int `json:"forward_port" required:"true"`
    20  	// The source port.
    21  	// The valid value is range from 1 to 65535.
    22  	SourcePort int `json:"source_port" required:"true"`
    23  	// The source IP list, separate the IPs with commas.
    24  	SourceIp string `json:"source_ip" required:"true"`
    25  }
    26  
    27  var requestOpts = golangsdk.RequestOpts{
    28  	MoreHeaders: map[string]string{"Content-Type": "application/json", "X-Language": "en-us"},
    29  }
    30  
    31  // BatchCreate is a method to to batch create at least one forward rule using given parameters.
    32  func BatchCreate(c *golangsdk.ServiceClient, instnaceId, ip string, opts BatchCreateOpts) (*BatchResp, error) {
    33  	b, err := golangsdk.BuildRequestBody(opts, "")
    34  	if err != nil {
    35  		return nil, err
    36  	}
    37  
    38  	var r BatchResp
    39  	_, err = c.Post(batchCreateURL(c, instnaceId, ip), b, &r, &golangsdk.RequestOpts{
    40  		MoreHeaders: requestOpts.MoreHeaders,
    41  	})
    42  	return &r, err
    43  }
    44  
    45  // List is a method to query the list of the forward rule using given opts.
    46  func List(c *golangsdk.ServiceClient, instnaceId, ip string) ([]Rule, error) {
    47  	var r ListResp
    48  	_, err := c.Get(listURL(c, instnaceId, ip), &r, &golangsdk.RequestOpts{
    49  		MoreHeaders: requestOpts.MoreHeaders,
    50  	})
    51  
    52  	return r.Rules, err
    53  }
    54  
    55  // UpdateOpts is the structure required by the Update method to update the configurations of the forward rule.
    56  type UpdateOpts struct {
    57  	// The forward protocol.
    58  	ForwardProtocol string `json:"forward_protocol" required:"true"`
    59  	// The forward port.
    60  	// The valid value is range from 1 to 65535.
    61  	ForwardPort int `json:"forward_port" required:"true"`
    62  	// The source port.
    63  	// The valid value is range from 1 to 65535.
    64  	SourcePort int `json:"source_port" required:"true"`
    65  	// The source IP list, separate the IPs with commas.
    66  	SourceIp string `json:"source_ip" required:"true"`
    67  }
    68  
    69  // Update is a method to update the configurations of the forward rule using given parameters.
    70  func Update(c *golangsdk.ServiceClient, instnaceId, ip, ruleId string, opts UpdateOpts) error {
    71  	b, err := golangsdk.BuildRequestBody(opts, "")
    72  	if err != nil {
    73  		return err
    74  	}
    75  
    76  	var r BatchResp
    77  	_, err = c.Put(updateURL(c, instnaceId, ip, ruleId), b, &r, &golangsdk.RequestOpts{
    78  		MoreHeaders: requestOpts.MoreHeaders,
    79  	})
    80  	return err
    81  }
    82  
    83  // BatchDeleteOpts is the structure required by the BatchDelete method to batch delete at least one forward rule.
    84  type BatchDeleteOpts struct {
    85  	RuleIds []string `json:"ids" required:"true"`
    86  }
    87  
    88  // BatchDelete is a method to to batch delete at least one forward rule using given parameters.
    89  func BatchDelete(c *golangsdk.ServiceClient, instnaceId, ip string, opts BatchDeleteOpts) (*BatchResp, error) {
    90  	b, err := golangsdk.BuildRequestBody(opts, "")
    91  	if err != nil {
    92  		return nil, err
    93  	}
    94  
    95  	var r BatchResp
    96  	_, err = c.Post(batchDeleteURL(c, instnaceId, ip), b, &r, &golangsdk.RequestOpts{
    97  		MoreHeaders: requestOpts.MoreHeaders,
    98  	})
    99  	return &r, err
   100  }