github.com/huaweicloud/golangsdk@v0.0.0-20210831081626-d823fe11ceba/openstack/rts/v1/stackresources/requests.go (about)

     1  package stackresources
     2  
     3  import (
     4  	"reflect"
     5  
     6  	"github.com/huaweicloud/golangsdk"
     7  	"github.com/huaweicloud/golangsdk/pagination"
     8  )
     9  
    10  // ListOptsBuilder allows extensions to add additional parameters to the
    11  // List request.
    12  type ListOptsBuilder interface {
    13  	ToStackResourceListQuery() (string, error)
    14  }
    15  
    16  // ListOpts allows the filtering and sorting of paginated collections through
    17  // the API. Filtering is achieved by passing in struct field values that map to
    18  // the rts attributes you want to see returned.
    19  type ListOpts struct {
    20  	//Specifies the logical resource ID of the resource.
    21  	LogicalID string `q:"logical_resource_id"`
    22  
    23  	//Name is the human readable name for the Resource.
    24  	Name string `q:"resource_name"`
    25  
    26  	//Specifies the Physical resource ID of the resource.
    27  	PhysicalID string `q:"physical_resource_id"`
    28  
    29  	//Status indicates whether or not a subnet is currently operational.
    30  	Status string `q:"resource_status"`
    31  
    32  	//Specifies the resource type that are defined in the template.
    33  	Type string `q:"resource_type"`
    34  }
    35  
    36  // List returns collection of
    37  // resources. It accepts a ListOpts struct, which allows you to filter and sort
    38  // the returned collection for greater efficiency.
    39  //
    40  // Default policy settings return only those resources that are owned by the
    41  // tenant who submits the request, unless an admin user submits the request.
    42  func List(client *golangsdk.ServiceClient, stackName string, opts ListOpts) ([]Resource, error) {
    43  	u := listURL(client, stackName)
    44  	pages, err := pagination.NewPager(client, u, func(r pagination.PageResult) pagination.Page {
    45  		return ResourcePage{pagination.LinkedPageBase{PageResult: r}}
    46  	}).AllPages()
    47  
    48  	allResources, err := ExtractResources(pages)
    49  	if err != nil {
    50  		return nil, err
    51  	}
    52  
    53  	return FilterResources(allResources, opts)
    54  }
    55  
    56  func FilterResources(resources []Resource, opts ListOpts) ([]Resource, error) {
    57  
    58  	var refinedResources []Resource
    59  	var matched bool
    60  	m := map[string]interface{}{}
    61  
    62  	if opts.LogicalID != "" {
    63  		m["LogicalID"] = opts.LogicalID
    64  	}
    65  	if opts.Name != "" {
    66  		m["Name"] = opts.Name
    67  	}
    68  	if opts.PhysicalID != "" {
    69  		m["PhysicalID"] = opts.PhysicalID
    70  	}
    71  	if opts.Status != "" {
    72  		m["Status"] = opts.Status
    73  	}
    74  	if opts.Type != "" {
    75  		m["Type"] = opts.Type
    76  	}
    77  
    78  	if len(m) > 0 && len(resources) > 0 {
    79  		for _, resource := range resources {
    80  			matched = true
    81  
    82  			for key, value := range m {
    83  				if sVal := getStructField(&resource, key); !(sVal == value) {
    84  					matched = false
    85  				}
    86  			}
    87  
    88  			if matched {
    89  				refinedResources = append(refinedResources, resource)
    90  			}
    91  		}
    92  
    93  	} else {
    94  		refinedResources = resources
    95  	}
    96  
    97  	return refinedResources, nil
    98  }
    99  
   100  func getStructField(v *Resource, field string) string {
   101  	r := reflect.ValueOf(v)
   102  	f := reflect.Indirect(r).FieldByName(field)
   103  	return string(f.String())
   104  }