fc3.go 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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 aliyun
  15. import (
  16. "fmt"
  17. "yunion.io/x/jsonutils"
  18. "yunion.io/x/pkg/errors"
  19. )
  20. const (
  21. FC3_API_VERSION = "2023-03-30"
  22. )
  23. type SFunction struct {
  24. FunctionId string
  25. FunctionName string
  26. }
  27. func (self *SRegion) fc3Request(apiName string, params map[string]string, body interface{}) (jsonutils.JSONObject, error) {
  28. client, err := self.getSdkClient()
  29. if err != nil {
  30. return nil, err
  31. }
  32. params = self.client.SetResourceGropuId(params)
  33. return doRequest(client, fmt.Sprintf("fcv3.%s.aliyuncs.com", self.RegionId), FC3_API_VERSION, apiName, params, body, self.client.debug)
  34. }
  35. func (region *SRegion) GetFunctions() ([]SFunction, error) {
  36. params := map[string]string{
  37. "PathPattern": "/functions",
  38. "limit": "100",
  39. }
  40. ret := []SFunction{}
  41. for {
  42. resp, err := region.fc3Request("ListFunctions", params, nil)
  43. if err != nil {
  44. return nil, errors.Wrapf(err, "ListFunctions")
  45. }
  46. part := struct {
  47. Functions []SFunction
  48. NextToken string
  49. }{}
  50. err = resp.Unmarshal(&part)
  51. if err != nil {
  52. return nil, errors.Wrapf(err, "resp.Unmarshal")
  53. }
  54. ret = append(ret, part.Functions...)
  55. if len(part.NextToken) == 0 || len(part.Functions) == 0 {
  56. break
  57. }
  58. params["nextToken"] = part.NextToken
  59. }
  60. return ret, nil
  61. }
  62. type SFunctionInstance struct {
  63. InstanceId string
  64. VersionId string
  65. }
  66. func (region *SRegion) GetFunctionInstances(funName string) ([]SFunctionInstance, error) {
  67. params := map[string]string{
  68. "PathPattern": fmt.Sprintf("/functions/%s/instances", funName),
  69. "withAllActive": "true",
  70. }
  71. resp, err := region.fc3Request("ListInstances", params, nil)
  72. if err != nil {
  73. return nil, errors.Wrapf(err, "ListInstances")
  74. }
  75. ret := []SFunctionInstance{}
  76. return ret, resp.Unmarshal(&ret, "instances")
  77. }