service.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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 service
  15. import (
  16. "context"
  17. "net/http"
  18. "yunion.io/x/jsonutils"
  19. "yunion.io/x/onecloud/pkg/apimap/models/vpcagent"
  20. "yunion.io/x/onecloud/pkg/apimap/options"
  21. compute_api "yunion.io/x/onecloud/pkg/apis/compute"
  22. "yunion.io/x/onecloud/pkg/appsrv"
  23. common_app "yunion.io/x/onecloud/pkg/cloudcommon/app"
  24. common_options "yunion.io/x/onecloud/pkg/cloudcommon/options"
  25. "yunion.io/x/onecloud/pkg/cloudcommon/policy"
  26. "yunion.io/x/onecloud/pkg/compute/models"
  27. "yunion.io/x/onecloud/pkg/httperrors"
  28. "yunion.io/x/onecloud/pkg/mcclient/auth"
  29. "yunion.io/x/onecloud/pkg/scheduler/service"
  30. )
  31. func StartService() {
  32. options.Init()
  33. opt := options.GetOptions()
  34. // hack: set GuestManager recordChecksum to false
  35. models.GuestManager.SetEnableRecordChecksum(false)
  36. service.StartServiceWrapper(&opt.DBOptions, &opt.CommonOptions, func(app *appsrv.Application) error {
  37. common_options.StartOptionManager(&opt, opt.ConfigSyncPeriodSeconds, compute_api.SERVICE_TYPE, compute_api.SERVICE_VERSION, options.OnOptionsChange)
  38. InitHandlers(app)
  39. common_app.ServeForever(app, &opt.BaseOptions)
  40. return nil
  41. })
  42. }
  43. func InitHandlers(app *appsrv.Application) {
  44. app.AddHandler2("GET", "/vpcagent", auth.Authenticate(vpcAgentHandler), nil, "get_vpcagent_topo", nil)
  45. }
  46. func vpcAgentHandler(ctx context.Context, w http.ResponseWriter, r *http.Request) {
  47. userCred := auth.FetchUserCredential(ctx, policy.FilterPolicyCredential)
  48. query, err := jsonutils.ParseQueryString(r.URL.RawQuery)
  49. if err != nil {
  50. httperrors.GeneralServerError(ctx, w, err)
  51. return
  52. }
  53. result, err := vpcagent.GetTopoResult(ctx, userCred, query)
  54. if err != nil {
  55. httperrors.GeneralServerError(ctx, w, err)
  56. return
  57. }
  58. appsrv.SendJSON(w, jsonutils.Marshal(result))
  59. }