github.com/opentelekomcloud/gophertelekomcloud@v0.9.3/openstack/networking/v2/extensions/security/rules/requests.go (about)

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