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

     1  package quotas
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  	"strconv"
     7  
     8  	"github.com/gophercloud/gophercloud"
     9  )
    10  
    11  type commonResult struct {
    12  	gophercloud.Result
    13  }
    14  
    15  type detailResult struct {
    16  	gophercloud.Result
    17  }
    18  
    19  // Extract is a function that accepts a result and extracts a Quota resource.
    20  func (r commonResult) Extract() (*Quota, error) {
    21  	var s struct {
    22  		Quota *Quota `json:"quota"`
    23  	}
    24  	err := r.ExtractInto(&s)
    25  	return s.Quota, err
    26  }
    27  
    28  // Extract is a function that accepts a result and extracts a QuotaDetailSet resource.
    29  func (r detailResult) Extract() (*QuotaDetailSet, error) {
    30  	var s struct {
    31  		Quota *QuotaDetailSet `json:"quota"`
    32  	}
    33  	err := r.ExtractInto(&s)
    34  	return s.Quota, err
    35  }
    36  
    37  // GetResult represents the result of a get operation. Call its Extract
    38  // method to interpret it as a Quota.
    39  type GetResult struct {
    40  	commonResult
    41  }
    42  
    43  // GetDetailResult represents the detailed result of a get operation. Call its Extract
    44  // method to interpret it as a Quota.
    45  type GetDetailResult struct {
    46  	detailResult
    47  }
    48  
    49  // UpdateResult represents the result of an update operation. Call its Extract
    50  // method to interpret it as a Quota.
    51  type UpdateResult struct {
    52  	commonResult
    53  }
    54  
    55  // Quota contains Networking quotas for a project.
    56  type Quota struct {
    57  	// FloatingIP represents a number of floating IPs. A "-1" value means no limit.
    58  	FloatingIP int `json:"floatingip"`
    59  
    60  	// Network represents a number of networks. A "-1" value means no limit.
    61  	Network int `json:"network"`
    62  
    63  	// Port represents a number of ports. A "-1" value means no limit.
    64  	Port int `json:"port"`
    65  
    66  	// RBACPolicy represents a number of RBAC policies. A "-1" value means no limit.
    67  	RBACPolicy int `json:"rbac_policy"`
    68  
    69  	// Router represents a number of routers. A "-1" value means no limit.
    70  	Router int `json:"router"`
    71  
    72  	// SecurityGroup represents a number of security groups. A "-1" value means no limit.
    73  	SecurityGroup int `json:"security_group"`
    74  
    75  	// SecurityGroupRule represents a number of security group rules. A "-1" value means no limit.
    76  	SecurityGroupRule int `json:"security_group_rule"`
    77  
    78  	// Subnet represents a number of subnets. A "-1" value means no limit.
    79  	Subnet int `json:"subnet"`
    80  
    81  	// SubnetPool represents a number of subnet pools. A "-1" value means no limit.
    82  	SubnetPool int `json:"subnetpool"`
    83  
    84  	// Trunk represents a number of trunks. A "-1" value means no limit.
    85  	Trunk int `json:"trunk"`
    86  }
    87  
    88  // QuotaDetailSet represents details of both operational limits of Networking resources for a project
    89  // and the current usage of those resources.
    90  type QuotaDetailSet struct {
    91  	// FloatingIP represents a number of floating IPs. A "-1" value means no limit.
    92  	FloatingIP QuotaDetail `json:"floatingip"`
    93  
    94  	// Network represents a number of networks. A "-1" value means no limit.
    95  	Network QuotaDetail `json:"network"`
    96  
    97  	// Port represents a number of ports. A "-1" value means no limit.
    98  	Port QuotaDetail `json:"port"`
    99  
   100  	// RBACPolicy represents a number of RBAC policies. A "-1" value means no limit.
   101  	RBACPolicy QuotaDetail `json:"rbac_policy"`
   102  
   103  	// Router represents a number of routers. A "-1" value means no limit.
   104  	Router QuotaDetail `json:"router"`
   105  
   106  	// SecurityGroup represents a number of security groups. A "-1" value means no limit.
   107  	SecurityGroup QuotaDetail `json:"security_group"`
   108  
   109  	// SecurityGroupRule represents a number of security group rules. A "-1" value means no limit.
   110  	SecurityGroupRule QuotaDetail `json:"security_group_rule"`
   111  
   112  	// Subnet represents a number of subnets. A "-1" value means no limit.
   113  	Subnet QuotaDetail `json:"subnet"`
   114  
   115  	// SubnetPool represents a number of subnet pools. A "-1" value means no limit.
   116  	SubnetPool QuotaDetail `json:"subnetpool"`
   117  
   118  	// Trunk represents a number of trunks. A "-1" value means no limit.
   119  	Trunk QuotaDetail `json:"trunk"`
   120  }
   121  
   122  // QuotaDetail is a set of details about a single operational limit that allows
   123  // for control of networking usage.
   124  type QuotaDetail struct {
   125  	// Used is the current number of provisioned/allocated resources of the
   126  	// given type.
   127  	Used int `json:"used"`
   128  
   129  	// Reserved is a transitional state when a claim against quota has been made
   130  	// but the resource is not yet fully online.
   131  	Reserved int `json:"reserved"`
   132  
   133  	// Limit is the maximum number of a given resource that can be
   134  	// allocated/provisioned.  This is what "quota" usually refers to.
   135  	Limit int `json:"limit"`
   136  }
   137  
   138  // UnmarshalJSON overrides the default unmarshalling function to accept
   139  // Reserved as a string.
   140  //
   141  // Due to a bug in Neutron, under some conditions Reserved is returned as a
   142  // string.
   143  //
   144  // This method is left for compatibility with unpatched versions of Neutron.
   145  //
   146  // cf. https://bugs.launchpad.net/neutron/+bug/1918565
   147  func (q *QuotaDetail) UnmarshalJSON(b []byte) error {
   148  	type tmp QuotaDetail
   149  	var s struct {
   150  		tmp
   151  		Reserved interface{} `json:"reserved"`
   152  	}
   153  
   154  	err := json.Unmarshal(b, &s)
   155  	if err != nil {
   156  		return err
   157  	}
   158  
   159  	*q = QuotaDetail(s.tmp)
   160  
   161  	switch t := s.Reserved.(type) {
   162  	case float64:
   163  		q.Reserved = int(t)
   164  	case string:
   165  		if q.Reserved, err = strconv.Atoi(t); err != nil {
   166  			return err
   167  		}
   168  	default:
   169  		return fmt.Errorf("reserved has unexpected type: %T", t)
   170  	}
   171  
   172  	return nil
   173  }