github.com/opentelekomcloud/gophertelekomcloud@v0.9.3/openstack/networking/v1/routetables/List.go (about)

     1  package routetables
     2  
     3  import (
     4  	"bytes"
     5  
     6  	golangsdk "github.com/opentelekomcloud/gophertelekomcloud"
     7  	"github.com/opentelekomcloud/gophertelekomcloud/internal/build"
     8  	"github.com/opentelekomcloud/gophertelekomcloud/internal/extract"
     9  	"github.com/opentelekomcloud/gophertelekomcloud/pagination"
    10  )
    11  
    12  // RouteTablePage is the page returned by a pager when traversing over a
    13  // collection of route tables
    14  type RouteTablePage struct {
    15  	pagination.NewSinglePageBase
    16  }
    17  
    18  // LastMarker returns the last route table ID in a ListResult
    19  func (r RouteTablePage) LastMarker() (string, error) {
    20  	tables, err := ExtractRouteTables(r)
    21  	if err != nil {
    22  		return "", err
    23  	}
    24  	if len(tables) == 0 {
    25  		return "", nil
    26  	}
    27  	return tables[len(tables)-1].ID, nil
    28  }
    29  
    30  // IsEmpty checks whether a RouteTablePage struct is empty.
    31  func (r RouteTablePage) IsEmpty() (bool, error) {
    32  	tables, err := ExtractRouteTables(r)
    33  	return len(tables) == 0, err
    34  }
    35  
    36  // ExtractRouteTables accepts a Page struct, specifically a RouteTablePage struct,
    37  // and extracts the elements into a slice of RouteTable structs.
    38  func ExtractRouteTables(r pagination.NewPage) ([]RouteTable, error) {
    39  	var s struct {
    40  		RouteTables []RouteTable `json:"routetables"`
    41  	}
    42  	err := extract.Into(bytes.NewReader((r.(RouteTablePage)).Body), &s)
    43  	return s.RouteTables, err
    44  }
    45  
    46  // ListOpts allows to query all route tables or filter collections by parameters
    47  // Marker and Limit are used for pagination.
    48  type ListOpts struct {
    49  	// ID is the unique identifier for the route table
    50  	ID string `q:"id"`
    51  	// VpcID is the unique identifier for the vpc
    52  	VpcID string `q:"vpc_id"`
    53  	// SubnetID the unique identifier for the subnet
    54  	SubnetID string `q:"subnet_id"`
    55  	// Limit is the number of records returned for each page query, the value range is 0~intmax
    56  	Limit int `q:"limit"`
    57  	// Marker is the starting resource ID of the paging query,
    58  	// which means that the query starts from the next record of the specified resource
    59  	Marker string `q:"marker"`
    60  }
    61  
    62  // List returns a Pager which allows you to iterate over a collection of
    63  // vpc route tables. It accepts a ListOpts struct, which allows you to
    64  // filter  the returned collection for greater efficiency.
    65  func List(client *golangsdk.ServiceClient, opts ListOpts) ([]RouteTable, error) {
    66  	q, err := build.QueryString(opts)
    67  	if err != nil {
    68  		return nil, err
    69  	}
    70  
    71  	pages, err := pagination.Pager{
    72  		Client:     client,
    73  		InitialURL: client.ServiceURL(client.ProjectID, "routetables") + q.String(),
    74  		CreatePage: func(r pagination.NewPageResult) pagination.NewPage {
    75  			return RouteTablePage{NewSinglePageBase: pagination.NewSinglePageBase{NewPageResult: r}}
    76  		},
    77  	}.NewAllPages()
    78  	if err != nil {
    79  		return nil, err
    80  	}
    81  	return ExtractRouteTables(pages)
    82  }