handler.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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 capabilities
  15. import (
  16. "context"
  17. "fmt"
  18. "net/http"
  19. "yunion.io/x/jsonutils"
  20. "yunion.io/x/onecloud/pkg/appsrv"
  21. "yunion.io/x/onecloud/pkg/cloudcommon/policy"
  22. "yunion.io/x/onecloud/pkg/compute/models"
  23. "yunion.io/x/onecloud/pkg/httperrors"
  24. "yunion.io/x/onecloud/pkg/mcclient/auth"
  25. )
  26. func AddCapabilityHandler(prefix string, app *appsrv.Application) {
  27. app.AddHandler2("GET", fmt.Sprintf("%s/capabilities", prefix), auth.Authenticate(capaHandler), nil, "get_capabilities", nil)
  28. }
  29. func capaHandler(ctx context.Context, w http.ResponseWriter, r *http.Request) {
  30. userCred := auth.FetchUserCredential(ctx, policy.FilterPolicyCredential)
  31. query, err := jsonutils.ParseQueryString(r.URL.RawQuery)
  32. if err != nil {
  33. httperrors.GeneralServerError(ctx, w, err)
  34. return
  35. }
  36. capa, err := models.GetCapabilities(ctx, userCred, query, nil, nil)
  37. if err != nil {
  38. httperrors.GeneralServerError(ctx, w, err)
  39. return
  40. }
  41. appsrv.SendJSON(w, jsonutils.Marshal(capa))
  42. }