github.com/huaweicloud/golangsdk@v0.0.0-20210831081626-d823fe11ceba/openstack/bms/v2/servers/requests.go (about) 1 package servers 2 3 import ( 4 "reflect" 5 "strings" 6 7 "github.com/huaweicloud/golangsdk" 8 "github.com/huaweicloud/golangsdk/pagination" 9 ) 10 11 // SortDir is a type for specifying in which direction to sort a list of servers. 12 type SortDir string 13 14 // SortKey is a type for specifying by which key to sort a list of servers. 15 type SortKey string 16 17 var ( 18 // SortAsc is used to sort a list of servers in ascending order. 19 SortAsc SortDir = "asc" 20 // SortDesc is used to sort a list of servers in descending order. 21 SortDesc SortDir = "desc" 22 // SortUUID is used to sort a list of servers by uuid. 23 SortUUID SortKey = "uuid" 24 // SortVMState is used to sort a list of servers by vm_state. 25 SortVMState SortKey = "vm_state" 26 // SortDisplayName is used to sort a list of servers by display_name. 27 SortDisplayName SortKey = "display_name" 28 // SortTaskState is used to sort a list of servers by task_state. 29 SortTaskState SortKey = "task_state" 30 // SortPowerState is used to sort a list of servers by power_state. 31 SortPowerState SortKey = "power_state" 32 // SortAvailabilityZone is used to sort a list of servers by availability_zone. 33 SortAvailabilityZone SortKey = "availability_zone" 34 ) 35 36 // ListServerOpts allows the filtering and sorting of paginated collections through 37 // the API. Filtering is achieved by passing in struct field values that map to 38 // the server attributes you want to see returned. 39 type ListOpts struct { 40 // ID uniquely identifies this server amongst all other servers, 41 // including those not accessible to the current tenant. 42 ID string 43 //ID of the user to which the BMS belongs. 44 UserID string 45 //Contains the nova-compute status 46 HostStatus string 47 //Contains the host ID of the BMS. 48 HostID string 49 // KeyName indicates which public key was injected into the server on launch. 50 KeyName string 51 // Specifies the BMS name, not added in query since returns like results. 52 Name string 53 // Specifies the BMS image ID. 54 ImageID string `q:"image"` 55 // Specifies flavor ID. 56 FlavorID string `q:"flavor"` 57 // Specifies the BMS status. 58 Status string `q:"status"` 59 //Filters out the BMSs that have been updated since the changes-since time. 60 // The parameter is in ISO 8601 time format, for example, 2013-06-09T06:42:18Z. 61 ChangesSince string `q:"changes-since"` 62 //Specifies whether to query the BMSs of all tenants. This parameter is available only to administrators. 63 // The value can be 0 (do not query the BMSs of all tenants) or 1 (query the BMSs of all tenants). 64 AllTenants int `q:"all_tenants"` 65 //Specifies the IP address. This parameter supports fuzzy matching. 66 IP string `q:"ip"` 67 //Specifies the tag list. Returns BMSs that match all tags. Use commas (,) to separate multiple tags 68 Tags string `q:"tags"` 69 //Specifies the tag list. Returns BMSs that match any tag 70 TagsAny string `q:"tags-any"` 71 //Specifies the tag list. Returns BMSs that do not match all tags. 72 NotTags string `q:"not-tags"` 73 //Specifies the tag list. Returns BMSs that do not match any of the tags. 74 NotTagsAny string `q:"not-tags-any"` 75 //Specifies the BMS sorting attribute, which can be the BMS UUID (uuid), BMS status (vm_state), 76 // BMS name (display_name), BMS task status (task_state), power status (power_state), 77 // creation time (created_at), last time when the BMS is updated (updated_at), and availability zone 78 // (availability_zone). You can specify multiple sort_key and sort_dir pairs. 79 SortKey SortKey `q:"sort_key"` 80 //Specifies the sorting direction, i.e. asc or desc. 81 SortDir SortDir `q:"sort_dir"` 82 } 83 84 // ListServer returns a Pager which allows you to iterate over a collection of 85 // BMS Server resources. It accepts a ListServerOpts struct, which allows you to 86 // filter the returned collection for greater efficiency. 87 func List(c *golangsdk.ServiceClient, opts ListOpts) ([]Server, error) { 88 c.Microversion = "2.26" 89 q, err := golangsdk.BuildQueryString(&opts) 90 if err != nil { 91 return nil, err 92 } 93 u := listURL(c) + q.String() 94 pages, err := pagination.NewPager(c, u, func(r pagination.PageResult) pagination.Page { 95 return ServerPage{pagination.LinkedPageBase{PageResult: r}} 96 }).AllPages() 97 if err != nil { 98 return nil, err 99 } 100 101 allservers, err := ExtractServers(pages) 102 if err != nil { 103 return nil, err 104 } 105 return FilterServers(allservers, opts) 106 } 107 108 func FilterServers(servers []Server, opts ListOpts) ([]Server, error) { 109 var refinedServers []Server 110 var matched bool 111 m := map[string]interface{}{} 112 113 if opts.ID != "" { 114 m["ID"] = opts.ID 115 } 116 if opts.Name != "" { 117 m["Name"] = opts.Name 118 } 119 if opts.UserID != "" { 120 m["UserID"] = opts.UserID 121 } 122 if opts.HostStatus != "" { 123 m["HostStatus"] = opts.HostStatus 124 } 125 if opts.HostID != "" { 126 m["HostID"] = opts.HostID 127 } 128 if opts.KeyName != "" { 129 m["KeyName"] = opts.KeyName 130 } 131 if len(m) > 0 && len(servers) > 0 { 132 for _, server := range servers { 133 matched = true 134 135 for key, value := range m { 136 if sVal := getStructServerField(&server, key); !(sVal == value) { 137 matched = false 138 } 139 } 140 if matched { 141 refinedServers = append(refinedServers, server) 142 } 143 } 144 } else { 145 refinedServers = servers 146 } 147 var serverList []Server 148 149 for i := 0; i < len(refinedServers); i++ { 150 if strings.Contains(refinedServers[i].Flavor.ID, "physical") { 151 serverList = append(serverList, refinedServers[i]) 152 } 153 154 } 155 return serverList, nil 156 } 157 158 func getStructServerField(v *Server, field string) string { 159 r := reflect.ValueOf(v) 160 f := reflect.Indirect(r).FieldByName(field) 161 return string(f.String()) 162 } 163 164 // Get requests details on a single server, by ID. 165 func Get(client *golangsdk.ServiceClient, id string) (r GetResult) { 166 _, r.Err = client.Get(getURL(client, id), &r.Body, &golangsdk.RequestOpts{ 167 MoreHeaders: map[string]string{"X-OpenStack-Nova-API-Version": "2.26"}, 168 }) 169 return 170 }