github.com/huaweicloud/golangsdk@v0.0.0-20210831081626-d823fe11ceba/openstack/deh/v1/hosts/requests.go (about) 1 package hosts 2 3 import ( 4 "reflect" 5 6 "github.com/huaweicloud/golangsdk" 7 "github.com/huaweicloud/golangsdk/pagination" 8 ) 9 10 // AllocateOptsBuilder allows extensions to add additional parameters to the 11 // Allocate request. 12 type AllocateOptsBuilder interface { 13 ToDeHAllocateMap() (map[string]interface{}, error) 14 } 15 16 // AllocateOpts contains all the values needed to allocate a new DeH. 17 type AllocateOpts struct { 18 Name string `json:"name" required:"true"` 19 Az string `json:"availability_zone" required:"true"` 20 AutoPlacement string `json:"auto_placement,omitempty"` 21 HostType string `json:"host_type" required:"true"` 22 Quantity int `json:"quantity" required:"true"` 23 } 24 25 // ToDeHAllocateMap builds a allocate request body from AllocateOpts. 26 func (opts AllocateOpts) ToDeHAllocateMap() (map[string]interface{}, error) { 27 return golangsdk.BuildRequestBody(opts, "") 28 } 29 30 // Allocate accepts a AllocateOpts struct and uses the values to allocate a new DeH. 31 func Allocate(c *golangsdk.ServiceClient, opts AllocateOptsBuilder) (r AllocateResult) { 32 b, err := opts.ToDeHAllocateMap() 33 if err != nil { 34 r.Err = err 35 return 36 } 37 reqOpt := &golangsdk.RequestOpts{OkCodes: []int{200, 201}} 38 _, r.Err = c.Post(rootURL(c), b, &r.Body, reqOpt) 39 return 40 } 41 42 // UpdateOptsBuilder allows extensions to add additional parameters to the 43 // Update request. 44 type UpdateOptsBuilder interface { 45 ToDeHUpdateMap() (map[string]interface{}, error) 46 } 47 48 // UpdateOpts contains all the values needed to update a DeH. 49 type UpdateOpts struct { 50 Name string `json:"name,omitempty"` 51 AutoPlacement string `json:"auto_placement,omitempty"` 52 } 53 54 // ToDeHUpdateMap builds a update request body from UpdateOpts. 55 func (opts UpdateOpts) ToDeHUpdateMap() (map[string]interface{}, error) { 56 return golangsdk.BuildRequestBody(opts, "dedicated_host") 57 } 58 59 // Update accepts a UpdateOpts struct and uses the values to update a DeH.The response code from api is 204 60 func Update(c *golangsdk.ServiceClient, hostID string, opts UpdateOptsBuilder) (r UpdateResult) { 61 b, err := opts.ToDeHUpdateMap() 62 if err != nil { 63 r.Err = err 64 return 65 } 66 reqOpt := &golangsdk.RequestOpts{OkCodes: []int{204}} 67 _, r.Err = c.Put(resourceURL(c, hostID), b, nil, reqOpt) 68 return 69 } 70 71 //Deletes the DeH using the specified hostID. 72 func Delete(c *golangsdk.ServiceClient, hostid string) (r DeleteResult) { 73 _, r.Err = c.Delete(resourceURL(c, hostid), nil) 74 return 75 } 76 77 // ListOpts allows the filtering and sorting of paginated collections through 78 // the API. 79 type ListOpts struct { 80 // Specifies Dedicated Host ID. 81 ID string `q:"dedicated_host_id"` 82 // Specifies the Dedicated Host name. 83 Name string `q:"name"` 84 // Specifes the Dedicated Host type. 85 HostType string `q:"host_type"` 86 // Specifes the Dedicated Host name of type. 87 HostTypeName string `q:"host_type_name"` 88 // Specifies flavor ID. 89 Flavor string `q:"flavor"` 90 // Specifies the Dedicated Host status. 91 // The value can be available, fault or released. 92 State string `q:"state"` 93 // Specifies the AZ to which the Dedicated Host belongs. 94 Az string `q:"availability_zone"` 95 // Specifies the number of entries displayed on each page. 96 Limit string `q:"limit"` 97 // The value is the ID of the last record on the previous page. 98 Marker string `q:"marker"` 99 // Filters the response by a date and time stamp when the dedicated host last changed status. 100 ChangesSince string `q:"changes-since"` 101 // Specifies the UUID of the tenant in a multi-tenancy cloud. 102 TenantId string `q:"tenant"` 103 } 104 105 // ListOptsBuilder allows extensions to add parameters to the List request. 106 type ListOptsBuilder interface { 107 ToHostListQuery() (string, error) 108 } 109 110 // ToRegionListQuery formats a ListOpts into a query string. 111 func (opts ListOpts) ToHostListQuery() (string, error) { 112 q, err := golangsdk.BuildQueryString(opts) 113 return q.String(), err 114 } 115 116 // List returns a Pager which allows you to iterate over a collection of 117 // dedicated hosts resources. It accepts a ListOpts struct, which allows you to 118 // filter the returned collection for greater efficiency. 119 func List(c *golangsdk.ServiceClient, opts ListOptsBuilder) pagination.Pager { 120 url := rootURL(c) 121 if opts != nil { 122 query, err := opts.ToHostListQuery() 123 if err != nil { 124 return pagination.Pager{Err: err} 125 } 126 url += query 127 } 128 return pagination.NewPager(c, url, func(r pagination.PageResult) pagination.Page { 129 return HostPage{pagination.LinkedPageBase{PageResult: r}} 130 }) 131 132 } 133 134 // Get retrieves a particular host based on its unique ID. 135 func Get(c *golangsdk.ServiceClient, id string) (r GetResult) { 136 _, r.Err = c.Get(resourceURL(c, id), &r.Body, nil) 137 return 138 } 139 140 // ListServerOpts allows the filtering and sorting of paginated collections through 141 // the API. Filtering is achieved by passing in struct field values that map to 142 // the server attributes you want to see returned. Marker and Limit are used 143 // for pagination. 144 type ListServerOpts struct { 145 // Specifies the number of entries displayed on each page. 146 Limit int `q:"limit"` 147 // The value is the ID of the last record on the previous page. 148 // If the marker value is invalid, error code 400 will be returned. 149 Marker string `q:"marker"` 150 // ID uniquely identifies this server amongst all other servers, 151 // including those not accessible to the current tenant. 152 ID string `json:"id"` 153 // Name contains the human-readable name for the server. 154 Name string `json:"name"` 155 // Status contains the current operational status of the server, 156 // such as IN_PROGRESS or ACTIVE. 157 Status string `json:"status"` 158 // UserID uniquely identifies the user account owning the tenant. 159 UserID string `json:"user_id"` 160 } 161 162 // ListServer returns a Pager which allows you to iterate over a collection of 163 // dedicated hosts Server resources. It accepts a ListServerOpts struct, which allows you to 164 // filter the returned collection for greater efficiency. 165 func ListServer(c *golangsdk.ServiceClient, id string, opts ListServerOpts) ([]Server, error) { 166 q, err := golangsdk.BuildQueryString(&opts) 167 if err != nil { 168 return nil, err 169 } 170 u := listServerURL(c, id) + q.String() 171 pages, err := pagination.NewPager(c, u, func(r pagination.PageResult) pagination.Page { 172 return ServerPage{pagination.LinkedPageBase{PageResult: r}} 173 }).AllPages() 174 if err != nil { 175 return nil, err 176 } 177 178 allservers, err := ExtractServers(pages) 179 if err != nil { 180 return nil, err 181 } 182 183 return FilterServers(allservers, opts) 184 } 185 186 func FilterServers(servers []Server, opts ListServerOpts) ([]Server, error) { 187 188 var refinedServers []Server 189 var matched bool 190 m := map[string]interface{}{} 191 192 if opts.ID != "" { 193 m["ID"] = opts.ID 194 } 195 if opts.Name != "" { 196 m["Name"] = opts.Name 197 } 198 if opts.Status != "" { 199 m["Status"] = opts.Status 200 } 201 if opts.UserID != "" { 202 m["UserID"] = opts.UserID 203 } 204 205 if len(m) > 0 && len(servers) > 0 { 206 for _, server := range servers { 207 matched = true 208 209 for key, value := range m { 210 if sVal := getStructServerField(&server, key); !(sVal == value) { 211 matched = false 212 } 213 } 214 215 if matched { 216 refinedServers = append(refinedServers, server) 217 } 218 } 219 220 } else { 221 refinedServers = servers 222 } 223 224 return refinedServers, nil 225 } 226 227 func getStructServerField(v *Server, field string) string { 228 r := reflect.ValueOf(v) 229 f := reflect.Indirect(r).FieldByName(field) 230 return string(f.String()) 231 }