github.com/opentelekomcloud/gophertelekomcloud@v0.9.3/openstack/rts/v1/stackresources/requests.go (about) 1 package stackresources 2 3 import ( 4 "reflect" 5 6 "github.com/opentelekomcloud/gophertelekomcloud" 7 "github.com/opentelekomcloud/gophertelekomcloud/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 if err != nil { 48 return nil, err 49 } 50 51 allResources, err := ExtractResources(pages) 52 if err != nil { 53 return nil, err 54 } 55 56 return FilterResources(allResources, opts) 57 } 58 59 func FilterResources(resources []Resource, opts ListOpts) ([]Resource, error) { 60 61 var refinedResources []Resource 62 var matched bool 63 m := map[string]interface{}{} 64 65 if opts.LogicalID != "" { 66 m["LogicalID"] = opts.LogicalID 67 } 68 if opts.Name != "" { 69 m["Name"] = opts.Name 70 } 71 if opts.PhysicalID != "" { 72 m["PhysicalID"] = opts.PhysicalID 73 } 74 if opts.Status != "" { 75 m["Status"] = opts.Status 76 } 77 if opts.Type != "" { 78 m["Type"] = opts.Type 79 } 80 81 if len(m) > 0 && len(resources) > 0 { 82 for _, resource := range resources { 83 matched = true 84 85 for key, value := range m { 86 if sVal := getStructField(&resource, key); !(sVal == value) { 87 matched = false 88 } 89 } 90 91 if matched { 92 refinedResources = append(refinedResources, resource) 93 } 94 } 95 96 } else { 97 refinedResources = resources 98 } 99 100 return refinedResources, nil 101 } 102 103 func getStructField(v *Resource, field string) string { 104 r := reflect.ValueOf(v) 105 f := reflect.Indirect(r).FieldByName(field) 106 return f.String() 107 }