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