fc.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. FC_API_VERSION = "2021-04-06"
  22. )
  23. type SFcService struct {
  24. ServiceId string
  25. ServiceName string
  26. }
  27. func (self *SRegion) fcRequest(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("fc.%s.aliyuncs.com", self.RegionId), FC_API_VERSION, apiName, params, body, self.client.debug)
  34. }
  35. func (self *SRegion) GetFcServices() ([]SFcService, error) {
  36. params := map[string]string{
  37. "PathPattern": "/services",
  38. "limit": "100",
  39. }
  40. ret := []SFcService{}
  41. for {
  42. resp, err := self.fc3Request("ListServices", params, nil)
  43. if err != nil {
  44. return nil, errors.Wrapf(err, "ListServices")
  45. }
  46. part := struct {
  47. Services []SFcService
  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.Services...)
  55. if len(part.NextToken) == 0 || len(part.Services) == 0 {
  56. break
  57. }
  58. params["nextToken"] = part.NextToken
  59. }
  60. return ret, nil
  61. }
  62. type SFcFunction struct {
  63. FunctionId string
  64. FunctionName string
  65. }
  66. func (self *SRegion) GetFcFunctions(service string) ([]SFcFunction, error) {
  67. params := map[string]string{
  68. "PathPattern": fmt.Sprintf("/services/%s/functions", service),
  69. "limit": "100",
  70. }
  71. ret := []SFcFunction{}
  72. for {
  73. resp, err := self.fc3Request("ListFunctions", params, nil)
  74. if err != nil {
  75. return nil, errors.Wrapf(err, "ListFunctions")
  76. }
  77. part := struct {
  78. Functions []SFcFunction
  79. NextToken string
  80. }{}
  81. err = resp.Unmarshal(&part)
  82. if err != nil {
  83. return nil, errors.Wrapf(err, "resp.Unmarshal")
  84. }
  85. ret = append(ret, part.Functions...)
  86. if len(part.NextToken) == 0 || len(part.Functions) == 0 {
  87. break
  88. }
  89. params["nextToken"] = part.NextToken
  90. }
  91. return ret, nil
  92. }
  93. type SFcInstance struct {
  94. InstanceId string
  95. VersionId string
  96. }
  97. func (self *SRegion) GetFcInstances(service, funcName string) ([]SFcInstance, error) {
  98. params := map[string]string{
  99. "PathPattern": fmt.Sprintf("/services/%s/functions/%s/instances", service, funcName),
  100. "limit": "100",
  101. }
  102. resp, err := self.fc3Request("ListFunctions", params, nil)
  103. if err != nil {
  104. return nil, errors.Wrapf(err, "ListFunctions")
  105. }
  106. ret := []SFcInstance{}
  107. return ret, resp.Unmarshal(&ret, "instances")
  108. }