github.com/huaweicloud/golangsdk@v0.0.0-20210831081626-d823fe11ceba/openstack/deh/v1/hosts/results.go (about) 1 package hosts 2 3 import ( 4 "time" 5 6 "github.com/huaweicloud/golangsdk" 7 "github.com/huaweicloud/golangsdk/pagination" 8 ) 9 10 type Host struct { 11 // ID is the unique identifier for the dedicated host . 12 ID string `json:"dedicated_host_id"` 13 // Specifies the Dedicated Host name. 14 Name string `json:"name"` 15 // Specifies whether to allow a VM to be placed on this available host 16 // if its Dedicated Host ID is not specified during its creation. 17 AutoPlacement string `json:"auto_placement"` 18 // Specifies the AZ to which the Dedicated Host belongs. 19 Az string `json:"availability_zone"` 20 // Specifies the tenant who owns the Dedicated Host. 21 TenantId string `json:"project_id"` 22 // Specifies the host status. 23 State string `json:"state"` 24 // Specifies the number of available vCPUs for the Dedicated Host. 25 AvailableVcpus int `json:"available_vcpus"` 26 // Specifies the size of available memory for the Dedicated Host. 27 AvailableMemory int `json:"available_memory"` 28 // Time at which the dedicated host has been allocated. 29 AllocatedAt string `json:"allocated_at"` 30 // Time at which the dedicated host has been released. 31 ReleasedAt string `json:"released_at"` 32 // Specifies the number of the placed VMs. 33 InstanceTotal int `json:"instance_total"` 34 // Specifies the VMs started on the Dedicated Host. 35 InstanceUuids []string `json:"instance_uuids"` 36 // Specifies the property of host. 37 HostProperties HostPropertiesOpts `json:"host_properties"` 38 } 39 type HostPropertiesOpts struct { 40 // Specifies the property of host. 41 HostType string `json:"host_type"` 42 HostTypeName string `json:"host_type_name"` 43 Vcpus int `json:"vcpus"` 44 Cores int `json:"cores"` 45 Sockets int `json:"sockets"` 46 Memory int `json:"memory"` 47 InstanceCapacities []InstanceCapacities `json:"available_instance_capacities"` 48 } 49 type InstanceCapacities struct { 50 // Specifies the number of supported flavors. 51 Flavor string `json:"flavor"` 52 } 53 54 // HostPage is the page returned by a pager when traversing over a 55 // collection of Hosts. 56 type HostPage struct { 57 pagination.LinkedPageBase 58 } 59 60 // IsEmpty returns true if a ListResult contains no Dedicated Hosts. 61 func (r HostPage) IsEmpty() (bool, error) { 62 stacks, err := ExtractHosts(r) 63 return len(stacks) == 0, err 64 } 65 66 // ExtractHosts accepts a Page struct, specifically a HostPage struct, 67 // and extracts the elements into a slice of hosts structs. In other words, 68 // a generic collection is mapped into a relevant slice. 69 func ExtractHosts(r pagination.Page) ([]Host, error) { 70 var s struct { 71 ListedStacks []Host `json:"dedicated_hosts"` 72 } 73 err := (r.(HostPage)).ExtractInto(&s) 74 return s.ListedStacks, err 75 } 76 77 // NextPageURL is invoked when a paginated collection of hosts has reached 78 // the end of a page and the pager seeks to traverse over a new one. In order 79 // to do this, it needs to construct the next page's URL. 80 func (r HostPage) NextPageURL() (string, error) { 81 var s struct { 82 Links []golangsdk.Link `json:"dedicated_hostslinks"` 83 } 84 err := r.ExtractInto(&s) 85 if err != nil { 86 return "", err 87 } 88 return golangsdk.ExtractNextURL(s.Links) 89 } 90 91 type commonResult struct { 92 golangsdk.Result 93 } 94 95 // AllocateResult represents the result of a allocate operation. Call its Extract 96 // method to interpret it as a host. 97 type AllocateResult struct { 98 commonResult 99 } 100 101 // Extract is a function that accepts a result and extracts Allocated Hosts. 102 func (r AllocateResult) ExtractHost() (*AllocatedHosts, error) { 103 var response AllocatedHosts 104 err := r.ExtractInto(&response) 105 return &response, err 106 } 107 108 //AllocatedHosts is the response structure of the allocated DeH 109 type AllocatedHosts struct { 110 AllocatedHostIds []string `json:"dedicated_host_ids"` 111 } 112 113 // AllocateResult represents the result of a allocate operation. Call its Extract 114 // method to interpret it as a host. 115 type UpdateResult struct { 116 commonResult 117 } 118 119 type DeleteResult struct { 120 commonResult 121 } 122 123 // GetResult represents the result of a get operation. Call its Extract 124 // method to interpret it as a host. 125 type GetResult struct { 126 commonResult 127 } 128 129 // Extract is a function that accepts a result and extracts a host. 130 func (r commonResult) Extract() (*Host, error) { 131 var s struct { 132 Host *Host `json:"dedicated_host"` 133 } 134 err := r.ExtractInto(&s) 135 return s.Host, err 136 } 137 138 // Server represents a server/instance in the OpenStack cloud. 139 type Server struct { 140 // ID uniquely identifies this server amongst all other servers, 141 // including those not accessible to the current tenant. 142 ID string `json:"id"` 143 // TenantID identifies the tenant owning this server resource. 144 TenantID string `json:"tenant_id"` 145 // UserID uniquely identifies the user account owning the tenant. 146 UserID string `json:"user_id"` 147 // Name contains the human-readable name for the server. 148 Name string `json:"name"` 149 // Updated and Created contain ISO-8601 timestamps of when the state of the 150 // server last changed, and when it was created. 151 Updated time.Time `json:"updated"` 152 Created time.Time `json:"created"` 153 // Status contains the current operational status of the server, 154 // such as IN_PROGRESS or ACTIVE. 155 Status string `json:"status"` 156 // Image refers to a JSON object, which itself indicates the OS image used to 157 // deploy the server. 158 Image map[string]interface{} `json:"-"` 159 // Flavor refers to a JSON object, which itself indicates the hardware 160 // configuration of the deployed server. 161 Flavor map[string]interface{} `json:"flavor"` 162 // Addresses includes a list of all IP addresses assigned to the server, 163 // keyed by pool. 164 Addresses map[string]interface{} `json:"addresses"` 165 // Metadata includes a list of all user-specified key-value pairs attached 166 // to the server. 167 Metadata map[string]string `json:"metadata"` 168 } 169 170 type ServerPage struct { 171 pagination.LinkedPageBase 172 } 173 174 // IsEmpty returns true if a page contains no Server results. 175 func (r ServerPage) IsEmpty() (bool, error) { 176 s, err := ExtractServers(r) 177 return len(s) == 0, err 178 } 179 180 // NextPageURL uses the response's embedded link reference to navigate to the 181 // next page of results. 182 func (r ServerPage) NextPageURL() (string, error) { 183 var s struct { 184 Links []golangsdk.Link `json:"servers_links"` 185 } 186 err := r.ExtractInto(&s) 187 if err != nil { 188 return "", err 189 } 190 return golangsdk.ExtractNextURL(s.Links) 191 } 192 193 // ExtractServers accepts a Page struct, specifically a ServerPage struct, 194 // and extracts the elements into a slice of Server structs. In other words, 195 // a generic collection is mapped into a relevant slice. 196 func ExtractServers(r pagination.Page) ([]Server, error) { 197 var s struct { 198 ListedStacks []Server `json:"servers"` 199 } 200 err := (r.(ServerPage)).ExtractInto(&s) 201 return s.ListedStacks, err 202 }