github.com/huaweicloud/golangsdk@v0.0.0-20210831081626-d823fe11ceba/openstack/networking/v2/extensions/security/rules/requests.go (about)

     1  package rules
     2  
     3  import (
     4  	"github.com/huaweicloud/golangsdk"
     5  	"github.com/huaweicloud/golangsdk/pagination"
     6  )
     7  
     8  // ListOpts allows the filtering and sorting of paginated collections through
     9  // the API. Filtering is achieved by passing in struct field values that map to
    10  // the security group rule attributes you want to see returned. SortKey allows
    11  // you to sort by a particular network attribute. SortDir sets the direction,
    12  // and is either `asc' or `desc'. Marker and Limit are used for pagination.
    13  type ListOpts struct {
    14  	Direction      string `q:"direction"`
    15  	EtherType      string `q:"ethertype"`
    16  	ID             string `q:"id"`
    17  	PortRangeMax   int    `q:"port_range_max"`
    18  	PortRangeMin   int    `q:"port_range_min"`
    19  	Protocol       string `q:"protocol"`
    20  	RemoteGroupID  string `q:"remote_group_id"`
    21  	RemoteIPPrefix string `q:"remote_ip_prefix"`
    22  	SecGroupID     string `q:"security_group_id"`
    23  	TenantID       string `q:"tenant_id"`
    24  	ProjectID      string `q:"project_id"`
    25  	Limit          int    `q:"limit"`
    26  	Marker         string `q:"marker"`
    27  	SortKey        string `q:"sort_key"`
    28  	SortDir        string `q:"sort_dir"`
    29  }
    30  
    31  // List returns a Pager which allows you to iterate over a collection of
    32  // security group rules. It accepts a ListOpts struct, which allows you to filter
    33  // and sort the returned collection for greater efficiency.
    34  func List(c *golangsdk.ServiceClient, opts ListOpts) pagination.Pager {
    35  	q, err := golangsdk.BuildQueryString(&opts)
    36  	if err != nil {
    37  		return pagination.Pager{Err: err}
    38  	}
    39  	u := rootURL(c) + q.String()
    40  	return pagination.NewPager(c, u, func(r pagination.PageResult) pagination.Page {
    41  		return SecGroupRulePage{pagination.LinkedPageBase{PageResult: r}}
    42  	})
    43  }
    44  
    45  type RuleDirection string
    46  type RuleProtocol string
    47  type RuleEtherType string
    48  
    49  // Constants useful for CreateOpts
    50  const (
    51  	DirIngress        RuleDirection = "ingress"
    52  	DirEgress         RuleDirection = "egress"
    53  	EtherType4        RuleEtherType = "IPv4"
    54  	EtherType6        RuleEtherType = "IPv6"
    55  	ProtocolAH        RuleProtocol  = "ah"
    56  	ProtocolDCCP      RuleProtocol  = "dccp"
    57  	ProtocolEGP       RuleProtocol  = "egp"
    58  	ProtocolESP       RuleProtocol  = "esp"
    59  	ProtocolGRE       RuleProtocol  = "gre"
    60  	ProtocolICMP      RuleProtocol  = "icmp"
    61  	ProtocolIGMP      RuleProtocol  = "igmp"
    62  	ProtocolIPv6Encap RuleProtocol  = "ipv6-encap"
    63  	ProtocolIPv6Frag  RuleProtocol  = "ipv6-frag"
    64  	ProtocolIPv6ICMP  RuleProtocol  = "ipv6-icmp"
    65  	ProtocolIPv6NoNxt RuleProtocol  = "ipv6-nonxt"
    66  	ProtocolIPv6Opts  RuleProtocol  = "ipv6-opts"
    67  	ProtocolIPv6Route RuleProtocol  = "ipv6-route"
    68  	ProtocolOSPF      RuleProtocol  = "ospf"
    69  	ProtocolPGM       RuleProtocol  = "pgm"
    70  	ProtocolRSVP      RuleProtocol  = "rsvp"
    71  	ProtocolSCTP      RuleProtocol  = "sctp"
    72  	ProtocolTCP       RuleProtocol  = "tcp"
    73  	ProtocolUDP       RuleProtocol  = "udp"
    74  	ProtocolUDPLite   RuleProtocol  = "udplite"
    75  	ProtocolVRRP      RuleProtocol  = "vrrp"
    76  )
    77  
    78  // CreateOptsBuilder allows extensions to add additional parameters to the
    79  // Create request.
    80  type CreateOptsBuilder interface {
    81  	ToSecGroupRuleCreateMap() (map[string]interface{}, error)
    82  }
    83  
    84  // CreateOpts contains all the values needed to create a new security group
    85  // rule.
    86  type CreateOpts struct {
    87  	// Must be either "ingress" or "egress": the direction in which the security
    88  	// group rule is applied.
    89  	Direction RuleDirection `json:"direction" required:"true"`
    90  
    91  	// String description of each rule, optional
    92  	Description string `json:"description,omitempty"`
    93  
    94  	// Must be "IPv4" or "IPv6", and addresses represented in CIDR must match the
    95  	// ingress or egress rules.
    96  	EtherType RuleEtherType `json:"ethertype" required:"true"`
    97  
    98  	// The security group ID to associate with this security group rule.
    99  	SecGroupID string `json:"security_group_id" required:"true"`
   100  
   101  	// The maximum port number in the range that is matched by the security group
   102  	// rule. The PortRangeMin attribute constrains the PortRangeMax attribute. If
   103  	// the protocol is ICMP, this value must be an ICMP type.
   104  	PortRangeMax int `json:"port_range_max,omitempty"`
   105  
   106  	// The minimum port number in the range that is matched by the security group
   107  	// rule. If the protocol is TCP or UDP, this value must be less than or equal
   108  	// to the value of the PortRangeMax attribute. If the protocol is ICMP, this
   109  	// value must be an ICMP type.
   110  	PortRangeMin int `json:"port_range_min,omitempty"`
   111  
   112  	// The protocol that is matched by the security group rule. Valid values are
   113  	// "tcp", "udp", "icmp" or an empty string.
   114  	Protocol RuleProtocol `json:"protocol,omitempty"`
   115  
   116  	// The remote group ID to be associated with this security group rule. You can
   117  	// specify either RemoteGroupID or RemoteIPPrefix.
   118  	RemoteGroupID string `json:"remote_group_id,omitempty"`
   119  
   120  	// The remote IP prefix to be associated with this security group rule. You can
   121  	// specify either RemoteGroupID or RemoteIPPrefix. This attribute matches the
   122  	// specified IP prefix as the source IP address of the IP packet.
   123  	RemoteIPPrefix string `json:"remote_ip_prefix,omitempty"`
   124  
   125  	// TenantID is the UUID of the project who owns the Rule.
   126  	// Only administrative users can specify a project UUID other than their own.
   127  	TenantID string `json:"tenant_id,omitempty"`
   128  }
   129  
   130  // ToSecGroupRuleCreateMap builds a request body from CreateOpts.
   131  func (opts CreateOpts) ToSecGroupRuleCreateMap() (map[string]interface{}, error) {
   132  	return golangsdk.BuildRequestBody(opts, "security_group_rule")
   133  }
   134  
   135  // Create is an operation which adds a new security group rule and associates it
   136  // with an existing security group (whose ID is specified in CreateOpts).
   137  func Create(c *golangsdk.ServiceClient, opts CreateOptsBuilder) (r CreateResult) {
   138  	b, err := opts.ToSecGroupRuleCreateMap()
   139  	if err != nil {
   140  		r.Err = err
   141  		return
   142  	}
   143  	_, r.Err = c.Post(rootURL(c), b, &r.Body, nil)
   144  	return
   145  }
   146  
   147  // Get retrieves a particular security group rule based on its unique ID.
   148  func Get(c *golangsdk.ServiceClient, id string) (r GetResult) {
   149  	_, r.Err = c.Get(resourceURL(c, id), &r.Body, nil)
   150  	return
   151  }
   152  
   153  // Delete will permanently delete a particular security group rule based on its
   154  // unique ID.
   155  func Delete(c *golangsdk.ServiceClient, id string) (r DeleteResult) {
   156  	_, r.Err = c.Delete(resourceURL(c, id), nil)
   157  	return
   158  }