github.com/chnsz/golangsdk@v0.0.0-20240506093406-85a3fbfa605b/openstack/networking/v1/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 start port number.
    26  	// The value ranges from 1 to 65535.
    27  	// The value cannot be greater than the port_range_max value. An empty value indicates all ports.
    28  	PortRangeMin int `json:"port_range_min,omitempty"`
    29  	// Specifies the end port number.
    30  	// The value ranges from 1 to 65535.
    31  	// The value cannot be smaller than the port_range_min value. An empty value indicates all ports.
    32  	PortRangeMax int `json:"port_range_max,omitempty"`
    33  	// Specifies the remote IP address.
    34  	// If the access control direction is set to egress, the parameter specifies the source IP address.
    35  	// If the access control direction is set to ingress, the parameter specifies the destination IP address.
    36  	// The value can be in the CIDR format or IP addresses.
    37  	// The parameter is exclusive with parameter remote_group_id.
    38  	RemoteIpPrefix string `json:"remote_ip_prefix,omitempty"`
    39  	// Specifies the ID of the peer security group.
    40  	// The value is exclusive with parameter remote_ip_prefix.
    41  	RemoteGroupId string `json:"remote_group_id,omitempty"`
    42  }
    43  
    44  // Create is a method to create a new security group rule.
    45  func Create(c *golangsdk.ServiceClient, opts CreateOpts) (*SecurityGroupRule, error) {
    46  	b, err := golangsdk.BuildRequestBody(opts, "security_group_rule")
    47  	if err != nil {
    48  		return nil, err
    49  	}
    50  
    51  	var rst golangsdk.Result
    52  	_, err = c.Post(rootURL(c), b, &rst.Body, nil)
    53  	if err == nil {
    54  		var r SecurityGroupRule
    55  		rst.ExtractIntoStructPtr(&r, "security_group_rule")
    56  		return &r, nil
    57  	}
    58  	return nil, err
    59  }
    60  
    61  // Get is a method to obtain the security group rule detail.
    62  func Get(c *golangsdk.ServiceClient, ruleId string) (*SecurityGroupRule, error) {
    63  	var rst golangsdk.Result
    64  	_, err := c.Get(resourceURL(c, ruleId), &rst.Body, nil)
    65  	if err == nil {
    66  		var r SecurityGroupRule
    67  		rst.ExtractIntoStructPtr(&r, "security_group_rule")
    68  		return &r, nil
    69  	}
    70  	return nil, err
    71  }
    72  
    73  // ListOpts allows to filter list data using given parameters.
    74  type ListOpts struct {
    75  	// Specifies a resource ID for pagination query, indicating that the query starts from the next record of the
    76  	// specified resource ID. This parameter can work together with the parameter limit.
    77  	//   1. If parameters marker and limit are not passed, all resource records will be returned.
    78  	//   2. If the parameter marker is not passed and the value of parameter limit is set to 10, the first 10 resource
    79  	//     records will be returned.
    80  	//   3. If the value of the parameter marker is set to the resource ID of the 10th record and the value of parameter
    81  	//     limit is set to 10, the 11th to 20th resource records will be returned.
    82  	//   4. If the value of the parameter marker is set to the resource ID of the 10th record and the parameter limit is
    83  	//     not passed, resource records starting from the 11th records (including 11th) will be returned.
    84  	Marker string `q:"marker"`
    85  	// Specifies the number of records that will be returned on each page. The value is from 0 to intmax.
    86  	// limit can be used together with marker. For details, see the parameter description of marker.
    87  	Limit int `q:"limit"`
    88  	// Specifies the security group ID.
    89  	SecurityGroupId string `q:"security_group_id"`
    90  	// Specifies the project ID.
    91  	ProjectId string `q:"project_id"`
    92  }
    93  
    94  // List is a method to obtain the list of the security group rules.
    95  func List(c *golangsdk.ServiceClient, opts ListOpts) ([]SecurityGroupRule, error) {
    96  	url := rootURL(c)
    97  	query, err := golangsdk.BuildQueryString(opts)
    98  	if err != nil {
    99  		return nil, err
   100  	}
   101  	url += query.String()
   102  
   103  	pages, err := pagination.NewPager(c, url, func(r pagination.PageResult) pagination.Page {
   104  		p := SecurityGroupRulePage{pagination.MarkerPageBase{PageResult: r}}
   105  		p.MarkerPageBase.Owner = p
   106  		return p
   107  	}).AllPages()
   108  
   109  	if err != nil {
   110  		return nil, err
   111  	}
   112  	return ExtractSecurityGroupRules(pages)
   113  }
   114  
   115  // Delete is a method to delete an existing security group rule.
   116  func Delete(c *golangsdk.ServiceClient, securityGroupRuleId string) *golangsdk.ErrResult {
   117  	var r golangsdk.ErrResult
   118  	_, r.Err = c.Delete(resourceURL(c, securityGroupRuleId), nil)
   119  	return &r
   120  }