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

     1  package subnetpools
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  	"strconv"
     7  	"time"
     8  
     9  	"github.com/huaweicloud/golangsdk"
    10  	"github.com/huaweicloud/golangsdk/pagination"
    11  )
    12  
    13  type commonResult struct {
    14  	golangsdk.Result
    15  }
    16  
    17  // Extract is a function that accepts a result and extracts a subnetpool resource.
    18  func (r commonResult) Extract() (*SubnetPool, error) {
    19  	var s struct {
    20  		SubnetPool *SubnetPool `json:"subnetpool"`
    21  	}
    22  	err := r.ExtractInto(&s)
    23  	return s.SubnetPool, err
    24  }
    25  
    26  // GetResult represents the result of a get operation. Call its Extract
    27  // method to interpret it as a SubnetPool.
    28  type GetResult struct {
    29  	commonResult
    30  }
    31  
    32  // CreateResult represents the result of a create operation. Call its Extract
    33  // method to interpret it as a SubnetPool.
    34  type CreateResult struct {
    35  	commonResult
    36  }
    37  
    38  // UpdateResult represents the result of an update operation. Call its Extract
    39  // method to interpret it as a SubnetPool.
    40  type UpdateResult struct {
    41  	commonResult
    42  }
    43  
    44  // DeleteResult represents the result of a delete operation. Call its
    45  // ExtractErr method to determine if the request succeeded or failed.
    46  type DeleteResult struct {
    47  	golangsdk.ErrResult
    48  }
    49  
    50  // SubnetPool represents a Neutron subnetpool.
    51  // A subnetpool is a pool of addresses from which subnets can be allocated.
    52  type SubnetPool struct {
    53  	// ID is the id of the subnetpool.
    54  	ID string `json:"id"`
    55  
    56  	// Name is the human-readable name of the subnetpool.
    57  	Name string `json:"name"`
    58  
    59  	// DefaultQuota is the per-project quota on the prefix space
    60  	// that can be allocated from the subnetpool for project subnets.
    61  	DefaultQuota int `json:"default_quota"`
    62  
    63  	// TenantID is the id of the Identity project.
    64  	TenantID string `json:"tenant_id"`
    65  
    66  	// ProjectID is the id of the Identity project.
    67  	ProjectID string `json:"project_id"`
    68  
    69  	// CreatedAt is the time at which subnetpool has been created.
    70  	CreatedAt time.Time `json:"created_at"`
    71  
    72  	// UpdatedAt is the time at which subnetpool has been created.
    73  	UpdatedAt time.Time `json:"updated_at"`
    74  
    75  	// Prefixes is the list of subnet prefixes to assign to the subnetpool.
    76  	// Neutron API merges adjacent prefixes and treats them as a single prefix.
    77  	// Each subnet prefix must be unique among all subnet prefixes in all subnetpools
    78  	// that are associated with the address scope.
    79  	Prefixes []string `json:"prefixes"`
    80  
    81  	// DefaultPrefixLen is yhe size of the prefix to allocate when the cidr
    82  	// or prefixlen attributes are omitted when you create the subnet.
    83  	// Defaults to the MinPrefixLen.
    84  	DefaultPrefixLen int `json:"-"`
    85  
    86  	// MinPrefixLen is the smallest prefix that can be allocated from a subnetpool.
    87  	// For IPv4 subnetpools, default is 8.
    88  	// For IPv6 subnetpools, default is 64.
    89  	MinPrefixLen int `json:"-"`
    90  
    91  	// MaxPrefixLen is the maximum prefix size that can be allocated from the subnetpool.
    92  	// For IPv4 subnetpools, default is 32.
    93  	// For IPv6 subnetpools, default is 128.
    94  	MaxPrefixLen int `json:"-"`
    95  
    96  	// AddressScopeID is the Neutron address scope to assign to the subnetpool.
    97  	AddressScopeID string `json:"address_scope_id"`
    98  
    99  	// IPversion is the IP protocol version.
   100  	// Valid value is 4 or 6. Default is 4.
   101  	IPversion int `json:"ip_version"`
   102  
   103  	// Shared indicates whether this network is shared across all projects.
   104  	Shared bool `json:"shared"`
   105  
   106  	// Description is thehuman-readable description for the resource.
   107  	Description string `json:"description"`
   108  
   109  	// IsDefault indicates if the subnetpool is default pool or not.
   110  	IsDefault bool `json:"is_default"`
   111  
   112  	// RevisionNumber is the revision number of the subnetpool.
   113  	RevisionNumber int `json:"revision_number"`
   114  }
   115  
   116  func (r *SubnetPool) UnmarshalJSON(b []byte) error {
   117  	type tmp SubnetPool
   118  	var s struct {
   119  		tmp
   120  		DefaultPrefixLen interface{} `json:"default_prefixlen"`
   121  		MinPrefixLen     interface{} `json:"min_prefixlen"`
   122  		MaxPrefixLen     interface{} `json:"max_prefixlen"`
   123  	}
   124  	err := json.Unmarshal(b, &s)
   125  	if err != nil {
   126  		return err
   127  	}
   128  
   129  	*r = SubnetPool(s.tmp)
   130  
   131  	switch t := s.DefaultPrefixLen.(type) {
   132  	case string:
   133  		if r.DefaultPrefixLen, err = strconv.Atoi(t); err != nil {
   134  			return err
   135  		}
   136  	case float64:
   137  		r.DefaultPrefixLen = int(t)
   138  	default:
   139  		return fmt.Errorf("DefaultPrefixLen has unexpected type: %T", t)
   140  	}
   141  
   142  	switch t := s.MinPrefixLen.(type) {
   143  	case string:
   144  		if r.MinPrefixLen, err = strconv.Atoi(t); err != nil {
   145  			return err
   146  		}
   147  	case float64:
   148  		r.MinPrefixLen = int(t)
   149  	default:
   150  		return fmt.Errorf("MinPrefixLen has unexpected type: %T", t)
   151  	}
   152  
   153  	switch t := s.MaxPrefixLen.(type) {
   154  	case string:
   155  		if r.MaxPrefixLen, err = strconv.Atoi(t); err != nil {
   156  			return err
   157  		}
   158  	case float64:
   159  		r.MaxPrefixLen = int(t)
   160  	default:
   161  		return fmt.Errorf("MaxPrefixLen has unexpected type: %T", t)
   162  	}
   163  
   164  	return nil
   165  }
   166  
   167  // SubnetPoolPage stores a single page of SubnetPools from a List() API call.
   168  type SubnetPoolPage struct {
   169  	pagination.LinkedPageBase
   170  }
   171  
   172  // NextPageURL is invoked when a paginated collection of subnetpools has reached
   173  // the end of a page and the pager seeks to traverse over a new one.
   174  // In order to do this, it needs to construct the next page's URL.
   175  func (r SubnetPoolPage) NextPageURL() (string, error) {
   176  	var s struct {
   177  		Links []golangsdk.Link `json:"subnetpools_links"`
   178  	}
   179  	err := r.ExtractInto(&s)
   180  	if err != nil {
   181  		return "", err
   182  	}
   183  	return golangsdk.ExtractNextURL(s.Links)
   184  }
   185  
   186  // IsEmpty determines whether or not a SubnetPoolPage is empty.
   187  func (r SubnetPoolPage) IsEmpty() (bool, error) {
   188  	subnetpools, err := ExtractSubnetPools(r)
   189  	return len(subnetpools) == 0, err
   190  }
   191  
   192  // ExtractSubnetPools interprets the results of a single page from a List() API call,
   193  // producing a slice of SubnetPools structs.
   194  func ExtractSubnetPools(r pagination.Page) ([]SubnetPool, error) {
   195  	var s struct {
   196  		SubnetPools []SubnetPool `json:"subnetpools"`
   197  	}
   198  	err := (r.(SubnetPoolPage)).ExtractInto(&s)
   199  	return s.SubnetPools, err
   200  }