github.com/opentelekomcloud/gophertelekomcloud@v0.9.3/openstack/bms/v2/flavors/list.go (about) 1 package flavors 2 3 import ( 4 "reflect" 5 "strings" 6 7 "github.com/opentelekomcloud/gophertelekomcloud" 8 "github.com/opentelekomcloud/gophertelekomcloud/pagination" 9 ) 10 11 // The AccessType arguement is optional, and if it is not supplied, OpenStack 12 // returns the PublicAccess flavors. 13 type AccessType string 14 15 const ( 16 // PublicAccess returns public flavors and private flavors associated with that project. 17 PublicAccess AccessType = "true" 18 // PrivateAccess (admin only) returns private flavors, across all projects. 19 PrivateAccess AccessType = "false" 20 // AllAccess (admin only) returns public and private flavors across all projects. 21 AllAccess AccessType = "None" 22 ) 23 24 // ListOpts allows the filtering and sorting of paginated collections through 25 // the API. Filtering is achieved by passing in struct field values that map to 26 // the flavor attributes you want to see returned. 27 type ListOpts struct { 28 // Specifies the name of the BMS flavor 29 Name string 30 // Specifies the ID of the BMS flavor 31 ID string 32 // MinDisk and MinRAM, if provided, elides flavors which do not meet your criteria. 33 MinDisk int `q:"minDisk"` 34 MinRAM int `q:"minRam"` 35 // AccessType, if provided, instructs List which set of flavors to return. 36 // If IsPublic not provided, flavors for the current project are returned. 37 AccessType AccessType `q:"is_public"` 38 // SortKey allows you to sort by a particular attribute 39 SortKey string `q:"sort_key"` 40 // SortDir sets the direction, and is either `asc' or `desc' 41 SortDir string `q:"sort_dir"` 42 } 43 44 func List(client *golangsdk.ServiceClient, opts ListOpts) ([]Flavor, error) { 45 url, err := golangsdk.NewURLBuilder().WithEndpoints("flavors", "detail").WithQueryParams(&opts).Build() 46 if err != nil { 47 return nil, err 48 } 49 50 pages, err := pagination.NewPager(client, client.ServiceURL(url.String()), func(r pagination.PageResult) pagination.Page { 51 return FlavorPage{pagination.LinkedPageBase{PageResult: r}} 52 }).AllPages() 53 if err != nil { 54 return nil, err 55 } 56 57 allFlavors, err := ExtractFlavors(pages) 58 if err != nil { 59 return nil, err 60 } 61 62 return filterFlavors(allFlavors, opts) 63 } 64 65 // filterFlavors used to filter flavors using Id and Name 66 func filterFlavors(flavors []Flavor, opts ListOpts) ([]Flavor, error) { 67 var refinedFlavors []Flavor 68 var matched bool 69 m := map[string]interface{}{} 70 71 if opts.ID != "" { 72 m["ID"] = opts.ID 73 } 74 if opts.Name != "" { 75 m["Name"] = opts.Name 76 } 77 if len(m) > 0 && len(flavors) > 0 { 78 for _, flavor := range flavors { 79 matched = true 80 81 for key, value := range m { 82 if sVal := getStructField(&flavor, key); !(sVal == value) { 83 matched = false 84 } 85 } 86 if matched { 87 refinedFlavors = append(refinedFlavors, flavor) 88 } 89 } 90 } else { 91 refinedFlavors = flavors 92 } 93 var flavorList []Flavor 94 95 for i := 0; i < len(refinedFlavors); i++ { 96 if strings.Contains(refinedFlavors[i].Name, "physical") { 97 flavorList = append(flavorList, refinedFlavors[i]) 98 } 99 100 } 101 102 return flavorList, nil 103 } 104 105 func getStructField(v *Flavor, field string) string { 106 r := reflect.ValueOf(v) 107 f := reflect.Indirect(r).FieldByName(field) 108 return f.String() 109 }