github.com/opentelekomcloud/gophertelekomcloud@v0.9.3/openstack/vpc/v3/vpcs/List.go (about)

     1  package vpcs
     2  
     3  import (
     4  	"bytes"
     5  
     6  	"github.com/opentelekomcloud/gophertelekomcloud"
     7  	"github.com/opentelekomcloud/gophertelekomcloud/internal/extract"
     8  	"github.com/opentelekomcloud/gophertelekomcloud/pagination"
     9  )
    10  
    11  type ListOpts struct {
    12  	// Number of records displayed on each page.
    13  	// Value range: 0 to 2000
    14  	Limit int `q:"limit,omitempty"`
    15  	// Start resource ID of pagination query.
    16  	// If the parameter is left blank, only resources on the first page are queried.
    17  	Marker string `q:"marker,omitempty"`
    18  	// VPC ID, which can be used to filter VPCs.
    19  	Id []string `q:"id,omitempty"`
    20  	// VPC name, which can be used to filter VPCs.
    21  	Name []string `q:"name,omitempty"`
    22  	// Supplementary information about the VPC, which can be used to filter VPCs.
    23  	Description []string `q:"description,omitempty"`
    24  	// VPC CIDR block, which can be used to filter VPCs.
    25  	Cidr []string `q:"cidr,omitempty"`
    26  }
    27  
    28  // List is used to query VPCs.
    29  func List(client *golangsdk.ServiceClient, opts ListOpts) ([]Vpc, error) {
    30  	q, err := golangsdk.BuildQueryString(opts)
    31  	if err != nil {
    32  		return nil, err
    33  	}
    34  
    35  	pages, err := pagination.Pager{
    36  		Client:     client,
    37  		InitialURL: client.ServiceURL("vpcs") + q.String(),
    38  		CreatePage: func(r pagination.NewPageResult) pagination.NewPage {
    39  			return VpcPage{NewSinglePageBase: pagination.NewSinglePageBase{NewPageResult: r}}
    40  		},
    41  	}.NewAllPages()
    42  	if err != nil {
    43  		return nil, err
    44  	}
    45  	return ExtractVpcs(pages)
    46  }
    47  
    48  // VpcPage is the page returned by a pager when traversing over a
    49  // collection of vpcs
    50  type VpcPage struct {
    51  	pagination.NewSinglePageBase
    52  }
    53  
    54  // ExtractVpcs accepts a Page struct, specifically a VpcPage struct,
    55  // and extracts the elements into a slice of Vpc structs.
    56  func ExtractVpcs(r pagination.NewPage) ([]Vpc, error) {
    57  	var s struct {
    58  		Vpcs []Vpc `json:"vpcs"`
    59  	}
    60  	err := extract.Into(bytes.NewReader((r.(VpcPage)).Body), &s)
    61  	return s.Vpcs, err
    62  }