github.com/opentelekomcloud/gophertelekomcloud@v0.9.3/openstack/apigw/v2/gateway/List.go (about)

     1  package gateway
     2  
     3  import (
     4  	"bytes"
     5  
     6  	golangsdk "github.com/opentelekomcloud/gophertelekomcloud"
     7  	"github.com/opentelekomcloud/gophertelekomcloud/internal/extract"
     8  	"github.com/opentelekomcloud/gophertelekomcloud/pagination"
     9  )
    10  
    11  type ListOpts struct {
    12  	Limit        int    `q:"limit"`
    13  	InstanceID   string `q:"instance_id"`
    14  	InstanceName string `q:"instance_name"`
    15  	Status       string `q:"status"`
    16  }
    17  
    18  func List(client *golangsdk.ServiceClient, opts ListOpts) ([]Gateway, error) {
    19  	q, err := golangsdk.BuildQueryString(&opts)
    20  	if err != nil {
    21  		return nil, err
    22  	}
    23  	pages, err := pagination.Pager{
    24  		Client:     client,
    25  		InitialURL: client.ServiceURL("apigw", "instances") + q.String(),
    26  		CreatePage: func(r pagination.NewPageResult) pagination.NewPage {
    27  			return GatewayPage{NewSinglePageBase: pagination.NewSinglePageBase{NewPageResult: r}}
    28  		},
    29  	}.NewAllPages()
    30  
    31  	if err != nil {
    32  		return nil, err
    33  	}
    34  	return ExtractGateways(pages)
    35  }
    36  
    37  type GatewayPage struct {
    38  	pagination.NewSinglePageBase
    39  }
    40  
    41  func ExtractGateways(r pagination.NewPage) ([]Gateway, error) {
    42  	var s struct {
    43  		Gateways []Gateway `json:"instances"`
    44  	}
    45  	err := extract.Into(bytes.NewReader((r.(GatewayPage)).Body), &s)
    46  	return s.Gateways, err
    47  }