github.com/gophercloud/gophercloud@v1.11.0/openstack/compute/v2/extensions/secgroups/results.go (about)

     1  package secgroups
     2  
     3  import (
     4  	"encoding/json"
     5  	"strconv"
     6  
     7  	"github.com/gophercloud/gophercloud"
     8  	"github.com/gophercloud/gophercloud/pagination"
     9  )
    10  
    11  // SecurityGroup represents a security group.
    12  type SecurityGroup struct {
    13  	// The unique ID of the group. If Neutron is installed, this ID will be
    14  	// represented as a string UUID; if Neutron is not installed, it will be a
    15  	// numeric ID. For the sake of consistency, we always cast it to a string.
    16  	ID string `json:"-"`
    17  
    18  	// The human-readable name of the group, which needs to be unique.
    19  	Name string `json:"name"`
    20  
    21  	// The human-readable description of the group.
    22  	Description string `json:"description"`
    23  
    24  	// The rules which determine how this security group operates.
    25  	Rules []Rule `json:"rules"`
    26  
    27  	// The ID of the tenant to which this security group belongs.
    28  	TenantID string `json:"tenant_id"`
    29  }
    30  
    31  func (r *SecurityGroup) UnmarshalJSON(b []byte) error {
    32  	type tmp SecurityGroup
    33  	var s struct {
    34  		tmp
    35  		ID interface{} `json:"id"`
    36  	}
    37  	err := json.Unmarshal(b, &s)
    38  	if err != nil {
    39  		return err
    40  	}
    41  
    42  	*r = SecurityGroup(s.tmp)
    43  
    44  	switch t := s.ID.(type) {
    45  	case float64:
    46  		r.ID = strconv.FormatFloat(t, 'f', -1, 64)
    47  	case string:
    48  		r.ID = t
    49  	}
    50  
    51  	return err
    52  }
    53  
    54  // Rule represents a security group rule, a policy which determines how a
    55  // security group operates and what inbound traffic it allows in.
    56  type Rule struct {
    57  	// The unique ID. If Neutron is installed, this ID will be
    58  	// represented as a string UUID; if Neutron is not installed, it will be a
    59  	// numeric ID. For the sake of consistency, we always cast it to a string.
    60  	ID string `json:"-"`
    61  
    62  	// The lower bound of the port range which this security group should open up.
    63  	FromPort int `json:"from_port"`
    64  
    65  	// The upper bound of the port range which this security group should open up.
    66  	ToPort int `json:"to_port"`
    67  
    68  	// The IP protocol (e.g. TCP) which the security group accepts.
    69  	IPProtocol string `json:"ip_protocol"`
    70  
    71  	// The CIDR IP range whose traffic can be received.
    72  	IPRange IPRange `json:"ip_range"`
    73  
    74  	// The security group ID to which this rule belongs.
    75  	ParentGroupID string `json:"-"`
    76  
    77  	// Not documented.
    78  	Group Group
    79  }
    80  
    81  func (r *Rule) UnmarshalJSON(b []byte) error {
    82  	type tmp Rule
    83  	var s struct {
    84  		tmp
    85  		ID            interface{} `json:"id"`
    86  		ParentGroupID interface{} `json:"parent_group_id"`
    87  	}
    88  	err := json.Unmarshal(b, &s)
    89  	if err != nil {
    90  		return err
    91  	}
    92  
    93  	*r = Rule(s.tmp)
    94  
    95  	switch t := s.ID.(type) {
    96  	case float64:
    97  		r.ID = strconv.FormatFloat(t, 'f', -1, 64)
    98  	case string:
    99  		r.ID = t
   100  	}
   101  
   102  	switch t := s.ParentGroupID.(type) {
   103  	case float64:
   104  		r.ParentGroupID = strconv.FormatFloat(t, 'f', -1, 64)
   105  	case string:
   106  		r.ParentGroupID = t
   107  	}
   108  
   109  	return err
   110  }
   111  
   112  // IPRange represents the IP range whose traffic will be accepted by the
   113  // security group.
   114  type IPRange struct {
   115  	CIDR string
   116  }
   117  
   118  // Group represents a group.
   119  type Group struct {
   120  	TenantID string `json:"tenant_id"`
   121  	Name     string
   122  }
   123  
   124  // SecurityGroupPage is a single page of a SecurityGroup collection.
   125  type SecurityGroupPage struct {
   126  	pagination.SinglePageBase
   127  }
   128  
   129  // IsEmpty determines whether or not a page of Security Groups contains any
   130  // results.
   131  func (page SecurityGroupPage) IsEmpty() (bool, error) {
   132  	if page.StatusCode == 204 {
   133  		return true, nil
   134  	}
   135  
   136  	users, err := ExtractSecurityGroups(page)
   137  	return len(users) == 0, err
   138  }
   139  
   140  // ExtractSecurityGroups returns a slice of SecurityGroups contained in a
   141  // single page of results.
   142  func ExtractSecurityGroups(r pagination.Page) ([]SecurityGroup, error) {
   143  	var s struct {
   144  		SecurityGroups []SecurityGroup `json:"security_groups"`
   145  	}
   146  	err := (r.(SecurityGroupPage)).ExtractInto(&s)
   147  	return s.SecurityGroups, err
   148  }
   149  
   150  type commonResult struct {
   151  	gophercloud.Result
   152  }
   153  
   154  // CreateResult represents the result of a create operation. Call its Extract
   155  // method to interpret the result as a SecurityGroup.
   156  type CreateResult struct {
   157  	commonResult
   158  }
   159  
   160  // GetResult represents the result of a get operation. Call its Extract
   161  // method to interpret the result as a SecurityGroup.
   162  type GetResult struct {
   163  	commonResult
   164  }
   165  
   166  // UpdateResult represents the result of an update operation. Call its Extract
   167  // method to interpret the result as a SecurityGroup.
   168  type UpdateResult struct {
   169  	commonResult
   170  }
   171  
   172  // Extract will extract a SecurityGroup struct from most responses.
   173  func (r commonResult) Extract() (*SecurityGroup, error) {
   174  	var s struct {
   175  		SecurityGroup *SecurityGroup `json:"security_group"`
   176  	}
   177  	err := r.ExtractInto(&s)
   178  	return s.SecurityGroup, err
   179  }
   180  
   181  // CreateRuleResult represents the result when adding rules to a security group.
   182  // Call its Extract method to interpret the result as a Rule.
   183  type CreateRuleResult struct {
   184  	gophercloud.Result
   185  }
   186  
   187  // Extract will extract a Rule struct from a CreateRuleResult.
   188  func (r CreateRuleResult) Extract() (*Rule, error) {
   189  	var s struct {
   190  		Rule *Rule `json:"security_group_rule"`
   191  	}
   192  	err := r.ExtractInto(&s)
   193  	return s.Rule, err
   194  }
   195  
   196  // DeleteResult is the response from delete operation. Call its ExtractErr
   197  // method to determine if the request succeeded or failed.
   198  type DeleteResult struct {
   199  	gophercloud.ErrResult
   200  }
   201  
   202  // DeleteRuleResult is the response from a DeleteRule operation. Call its
   203  // ExtractErr method to determine if the request succeeded or failed.
   204  type DeleteRuleResult struct {
   205  	gophercloud.ErrResult
   206  }
   207  
   208  // AddServerResult is the response from an AddServer operation. Call its
   209  // ExtractErr method to determine if the request succeeded or failed.
   210  type AddServerResult struct {
   211  	gophercloud.ErrResult
   212  }
   213  
   214  // RemoveServerResult is the response from a RemoveServer operation. Call its
   215  // ExtractErr method to determine if the request succeeded or failed.
   216  type RemoveServerResult struct {
   217  	gophercloud.ErrResult
   218  }