candidate.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. // Copyright 2019 Yunion
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package api
  15. import (
  16. "encoding/json"
  17. "io"
  18. "strconv"
  19. simplejson "github.com/bitly/go-simplejson"
  20. )
  21. // CandidateListArgs is a struct just for parsing candidate
  22. // resource list parameters.
  23. type CandidateListArgs struct {
  24. Type string `json:"type"`
  25. Region string `json:"region"`
  26. Zone string `json:"zone"`
  27. //Pool string
  28. Limit int64 `json:"limit"`
  29. Offset int64 `json:"offset"`
  30. Avaliable bool `json:"available"`
  31. }
  32. type ResultResource struct {
  33. Free float64 `json:"free"`
  34. Reserved float64 `json:"reserverd"`
  35. Total float64 `json:"total"`
  36. }
  37. func NewResultResourceString(free, reserverd, total string) (*ResultResource, error) {
  38. f, err := strconv.ParseFloat(free, 64)
  39. r, err := strconv.ParseFloat(reserverd, 64)
  40. t, err := strconv.ParseFloat(total, 64)
  41. if err != nil {
  42. return nil, err
  43. }
  44. return NewResultResource(f, r, t), nil
  45. }
  46. func NewResultResource(f, r, t float64) *ResultResource {
  47. return &ResultResource{
  48. Free: f,
  49. Reserved: r,
  50. Total: t,
  51. }
  52. }
  53. func NewResultResourceInt64(f, r, t int64) *ResultResource {
  54. free := float64(f)
  55. reserverd := float64(r)
  56. total := float64(t)
  57. return NewResultResource(free, reserverd, total)
  58. }
  59. type CandidateListResultItem struct {
  60. ID string `json:"id"`
  61. Name string `json:"name"`
  62. Cpu ResultResource `json:"cpu"`
  63. Mem ResultResource `json:"mem"`
  64. Storage ResultResource `json:"storage"`
  65. Status string `json:"status"`
  66. HostStatus string `json:"host_status"`
  67. EnableStatus string `json:"enable_status"`
  68. HostType string `json:"host_type"`
  69. PendingUsage map[string]interface{} `json:"pending_usage"`
  70. }
  71. type CandidateListResult struct {
  72. Data []CandidateListResultItem `json:"data"`
  73. Total int64 `json:"total"`
  74. Limit int64 `json:"limit"`
  75. Offset int64 `json:"offset"`
  76. }
  77. const (
  78. DefaultCandidateListArgsLimit = 20
  79. )
  80. // NewCandidateListArgs provides a function that
  81. // will parse candidate's list args from a json data.
  82. func NewCandidateListArgs(r io.Reader) (*CandidateListArgs, error) {
  83. args := CandidateListArgs{}
  84. err := json.NewDecoder(r).Decode(&args)
  85. if err != nil {
  86. return nil, err
  87. }
  88. if args.Limit == 0 {
  89. args.Limit = DefaultCandidateListArgsLimit
  90. }
  91. if args.Type == "" {
  92. args.Type = "all"
  93. }
  94. return &args, nil
  95. }
  96. // CandidateDetailArgs is a struct just for parsing candidate
  97. // resource parameters.
  98. type CandidateDetailArgs struct {
  99. ID string
  100. Type string
  101. }
  102. type CandidateDetailResult struct {
  103. Candidate interface{} `json:"candidate"`
  104. }
  105. // NewCandidateDetailArgs provides a function that
  106. // will parse candidate's args from a json data.
  107. func NewCandidateDetailArgs(sjson *simplejson.Json, id string) (*CandidateDetailArgs, error) {
  108. args := new(CandidateDetailArgs)
  109. args.ID = id
  110. if argsType, ok := sjson.CheckGet("type"); ok {
  111. args.Type = argsType.MustString()
  112. } else {
  113. args.Type = HostTypeHost
  114. }
  115. return args, nil
  116. }