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

     1  package rules
     2  
     3  import (
     4  	"github.com/chnsz/golangsdk"
     5  	"github.com/chnsz/golangsdk/pagination"
     6  )
     7  
     8  // CreateOpts is a struct which will be used to create a new security group rule.
     9  type CreateOpts struct {
    10  	// Specifies the security group ID.
    11  	SecurityGroupId string `json:"security_group_id" required:"true"`
    12  	// Provides supplementary information about the security group rule.
    13  	// The value can contain no more than 255 characters, including letters and digits.
    14  	Description string `json:"description,omitempty"`
    15  	// Specifies the direction of access control.
    16  	// Possible values are as follows:
    17  	//   egress
    18  	//   ingress
    19  	Direction string `json:"direction" required:"true"`
    20  	// Specifies the IP protocol version. The value can be IPv4 or IPv6. The default value is IPv4.
    21  	Ethertype string `json:"ethertype,omitempty"`
    22  	// Specifies the protocol type. The value can be icmp, tcp, or udp.
    23  	// If the parameter is left blank, all protocols are supported.
    24  	Protocol string `json:"protocol,omitempty"`
    25  	// Specifies the port value range, which supports single port (80), continuous port (1-30) and discontinuous
    26  	// port (22, 3389, 80). The range of port values is range form 1 to 65,535.
    27  	MultiPort string `json:"multiport,omitempty"`
    28  	// Specifies the remote IP address.
    29  	// If the access control direction is set to egress, the parameter specifies the source IP address.
    30  	// If the access control direction is set to ingress, the parameter specifies the destination IP address.
    31  	// The value can be in the CIDR format or IP addresses.
    32  	// The parameter is exclusive with parameter remote_group_id.
    33  	RemoteIpPrefix string `json:"remote_ip_prefix,omitempty"`
    34  	// Specifies the ID of the peer security group.
    35  	// The value is exclusive with parameter remote_ip_prefix.
    36  	RemoteGroupId string `json:"remote_group_id,omitempty"`
    37  	// Specifies the ID of the peer security group.
    38  	// The value is exclusive with parameter remote_ip_prefix.
    39  	RemoteAddressGroupId string `json:"remote_address_group_id,omitempty"`
    40  	// Specifies the ID of the peer security group.
    41  	// The value is exclusive with parameter remote_ip_prefix.
    42  	Action string `json:"action,omitempty"`
    43  	// Specifies the ID of the peer security group.
    44  	// The value is exclusive with parameter remote_ip_prefix.
    45  	Priority int `json:"priority,omitempty"`
    46  }
    47  
    48  // Create is a method to create a new security group rule.
    49  func Create(c *golangsdk.ServiceClient, opts CreateOpts) (*SecurityGroupRule, error) {
    50  	b, err := golangsdk.BuildRequestBody(opts, "security_group_rule")
    51  	if err != nil {
    52  		return nil, err
    53  	}
    54  
    55  	var rst golangsdk.Result
    56  	_, err = c.Post(rootURL(c), b, &rst.Body, nil)
    57  	if err == nil {
    58  		var r SecurityGroupRule
    59  		err = rst.ExtractIntoStructPtr(&r, "security_group_rule")
    60  		return &r, err
    61  	}
    62  	return nil, err
    63  }
    64  
    65  // Get is a method to obtain the security group rule detail.
    66  func Get(c *golangsdk.ServiceClient, ruleId string) (*SecurityGroupRule, error) {
    67  	var rst golangsdk.Result
    68  	_, err := c.Get(resourceURL(c, ruleId), &rst.Body, nil)
    69  	if err == nil {
    70  		var r SecurityGroupRule
    71  		err = rst.ExtractIntoStructPtr(&r, "security_group_rule")
    72  		return &r, err
    73  	}
    74  	return nil, err
    75  }
    76  
    77  // ListOpts allows to filter list data using given parameters.
    78  type ListOpts struct {
    79  	// Specifies a resource ID for pagination query, indicating that the query starts from the next record of the
    80  	// specified resource ID. This parameter can work together with the parameter limit.
    81  	//   1. If parameters marker and limit are not passed, all resource records will be returned.
    82  	//   2. If the parameter marker is not passed and the value of parameter limit is set to 10, the first 10 resource
    83  	//     records will be returned.
    84  	//   3. If the value of the parameter marker is set to the resource ID of the 10th record and the value of parameter
    85  	//     limit is set to 10, the 11th to 20th resource records will be returned.
    86  	//   4. If the value of the parameter marker is set to the resource ID of the 10th record and the parameter limit is
    87  	//     not passed, resource records starting from the 11th records (including 11th) will be returned.
    88  	Marker string `q:"marker"`
    89  	// Specifies the number of records that will be returned on each page. The value is from 0 to intmax.
    90  	// limit can be used together with marker. For details, see the parameter description of marker.
    91  	Limit int `q:"limit"`
    92  	// Specifies the security group rule ID.
    93  	ID string `q:"id"`
    94  	// Specifies the security group ID.
    95  	SecurityGroupId string `q:"security_group_id"`
    96  	// Security group rule protocol, support multiple filtering
    97  	Protocol string `q:"protocol"`
    98  	// Security group description added. You can use this field to filter security groups precisely, and support
    99  	// multiple descriptions for filtering.
   100  	Description string `q:"description"`
   101  	// Remote security group ID, support multiple ID filtering.
   102  	RemoteGroupId string `q:"remote_group_id"`
   103  	// Security group rule direction.
   104  	Direction string `q:"direction"`
   105  	// Security group rules take effect policy.
   106  	Action string `q:"action"`
   107  }
   108  
   109  // List is a method to obtain the list of the security group rules.
   110  func List(c *golangsdk.ServiceClient, opts ListOpts) ([]SecurityGroupRule, error) {
   111  	url := rootURL(c)
   112  	query, err := golangsdk.BuildQueryString(opts)
   113  	if err != nil {
   114  		return nil, err
   115  	}
   116  	url += query.String()
   117  
   118  	pages, err := pagination.NewPager(c, url, func(r pagination.PageResult) pagination.Page {
   119  		p := SecurityGroupRulePage{pagination.MarkerPageBase{PageResult: r}}
   120  		p.MarkerPageBase.Owner = p
   121  		return p
   122  	}).AllPages()
   123  
   124  	if err != nil {
   125  		return nil, err
   126  	}
   127  	return ExtractSecurityGroupRules(pages)
   128  }
   129  
   130  // Delete is a method to delete an existing security group rule.
   131  func Delete(c *golangsdk.ServiceClient, securityGroupRuleId string) *golangsdk.ErrResult {
   132  	var r golangsdk.ErrResult
   133  	_, r.Err = c.Delete(resourceURL(c, securityGroupRuleId), nil)
   134  	return &r
   135  }