github.com/gophercloud/gophercloud@v1.11.0/openstack/networking/v2/extensions/security/rules/results.go (about)

     1  package rules
     2  
     3  import (
     4  	"github.com/gophercloud/gophercloud"
     5  	"github.com/gophercloud/gophercloud/pagination"
     6  )
     7  
     8  // SecGroupRule represents a rule to dictate the behaviour of incoming or
     9  // outgoing traffic for a particular security group.
    10  type SecGroupRule struct {
    11  	// The UUID for this security group rule.
    12  	ID string
    13  
    14  	// The direction in which the security group rule is applied. The only values
    15  	// allowed are "ingress" or "egress". For a compute instance, an ingress
    16  	// security group rule is applied to incoming (ingress) traffic for that
    17  	// instance. An egress rule is applied to traffic leaving the instance.
    18  	Direction string
    19  
    20  	// Description of the rule
    21  	Description string `json:"description"`
    22  
    23  	// Must be IPv4 or IPv6, and addresses represented in CIDR must match the
    24  	// ingress or egress rules.
    25  	EtherType string `json:"ethertype"`
    26  
    27  	// The security group ID to associate with this security group rule.
    28  	SecGroupID string `json:"security_group_id"`
    29  
    30  	// The minimum port number in the range that is matched by the security group
    31  	// rule. If the protocol is TCP or UDP, this value must be less than or equal
    32  	// to the value of the PortRangeMax attribute. If the protocol is ICMP, this
    33  	// value must be an ICMP type.
    34  	PortRangeMin int `json:"port_range_min"`
    35  
    36  	// The maximum port number in the range that is matched by the security group
    37  	// rule. The PortRangeMin attribute constrains the PortRangeMax attribute. If
    38  	// the protocol is ICMP, this value must be an ICMP type.
    39  	PortRangeMax int `json:"port_range_max"`
    40  
    41  	// The protocol that is matched by the security group rule. Valid values are
    42  	// "tcp", "udp", "icmp" or an empty string.
    43  	Protocol string
    44  
    45  	// The remote group ID to be associated with this security group rule. You
    46  	// can specify either RemoteGroupID or RemoteIPPrefix.
    47  	RemoteGroupID string `json:"remote_group_id"`
    48  
    49  	// The remote IP prefix to be associated with this security group rule. You
    50  	// can specify either RemoteGroupID or RemoteIPPrefix . This attribute
    51  	// matches the specified IP prefix as the source IP address of the IP packet.
    52  	RemoteIPPrefix string `json:"remote_ip_prefix"`
    53  
    54  	// TenantID is the project owner of this security group rule.
    55  	TenantID string `json:"tenant_id"`
    56  
    57  	// ProjectID is the project owner of this security group rule.
    58  	ProjectID string `json:"project_id"`
    59  }
    60  
    61  // SecGroupRulePage is the page returned by a pager when traversing over a
    62  // collection of security group rules.
    63  type SecGroupRulePage struct {
    64  	pagination.LinkedPageBase
    65  }
    66  
    67  // NextPageURL is invoked when a paginated collection of security group rules has
    68  // reached the end of a page and the pager seeks to traverse over a new one. In
    69  // order to do this, it needs to construct the next page's URL.
    70  func (r SecGroupRulePage) NextPageURL() (string, error) {
    71  	var s struct {
    72  		Links []gophercloud.Link `json:"security_group_rules_links"`
    73  	}
    74  	err := r.ExtractInto(&s)
    75  	if err != nil {
    76  		return "", err
    77  	}
    78  	return gophercloud.ExtractNextURL(s.Links)
    79  }
    80  
    81  // IsEmpty checks whether a SecGroupRulePage struct is empty.
    82  func (r SecGroupRulePage) IsEmpty() (bool, error) {
    83  	if r.StatusCode == 204 {
    84  		return true, nil
    85  	}
    86  
    87  	is, err := ExtractRules(r)
    88  	return len(is) == 0, err
    89  }
    90  
    91  // ExtractRules accepts a Page struct, specifically a SecGroupRulePage struct,
    92  // and extracts the elements into a slice of SecGroupRule structs. In other words,
    93  // a generic collection is mapped into a relevant slice.
    94  func ExtractRules(r pagination.Page) ([]SecGroupRule, error) {
    95  	var s struct {
    96  		SecGroupRules []SecGroupRule `json:"security_group_rules"`
    97  	}
    98  	err := (r.(SecGroupRulePage)).ExtractInto(&s)
    99  	return s.SecGroupRules, err
   100  }
   101  
   102  type commonResult struct {
   103  	gophercloud.Result
   104  }
   105  
   106  // Extract is a function that accepts a result and extracts a security rule.
   107  func (r commonResult) Extract() (*SecGroupRule, error) {
   108  	var s struct {
   109  		SecGroupRule *SecGroupRule `json:"security_group_rule"`
   110  	}
   111  	err := r.ExtractInto(&s)
   112  	return s.SecGroupRule, err
   113  }
   114  
   115  // CreateResult represents the result of a create operation. Call its Extract
   116  // method to interpret it as a SecGroupRule.
   117  type CreateResult struct {
   118  	commonResult
   119  }
   120  
   121  // GetResult represents the result of a get operation. Call its Extract
   122  // method to interpret it as a SecGroupRule.
   123  type GetResult struct {
   124  	commonResult
   125  }
   126  
   127  // DeleteResult represents the result of a delete operation. Call its
   128  // ExtractErr method to determine if the request succeeded or failed.
   129  type DeleteResult struct {
   130  	gophercloud.ErrResult
   131  }