ping.go 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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 handler
  15. import (
  16. "fmt"
  17. "net/http"
  18. gin "github.com/gin-gonic/gin"
  19. "yunion.io/x/log"
  20. schedman "yunion.io/x/onecloud/pkg/scheduler/manager"
  21. o "yunion.io/x/onecloud/pkg/scheduler/options"
  22. )
  23. var counter = 0
  24. func InstallPingHandler(r *gin.Engine) {
  25. r.GET("/ping", pingHandler)
  26. r.GET("/switch", switchHandler)
  27. }
  28. // pingHandler is a handler of ping-pong service that just for
  29. // testing scheduler serevice.
  30. func pingHandler(c *gin.Context) {
  31. if !schedman.IsReady() {
  32. c.AbortWithError(http.StatusBadRequest, fmt.Errorf("Global scheduler not init"))
  33. return
  34. }
  35. log.Infof("%v", c.Request.Body)
  36. c.String(http.StatusOK, "pong")
  37. }
  38. // switchHandler is a handler of switch service that just for
  39. // testing scheduler serevice.
  40. func switchHandler(c *gin.Context) {
  41. if !schedman.IsReady() {
  42. c.AbortWithError(http.StatusBadRequest, fmt.Errorf("Global scheduler not init"))
  43. return
  44. }
  45. log.Infof("%v", c.Request.Body)
  46. counter++
  47. var result string
  48. if counter%2 == 1 {
  49. o.Options.DisableBaremetalPredicates = true
  50. result = "ignore_baremetal_filter_switch is true"
  51. } else {
  52. o.Options.DisableBaremetalPredicates = false
  53. result = "ignore_baremetal_filter_switch is false"
  54. }
  55. c.JSON(http.StatusOK, result)
  56. }