waf.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. "yunion.io/x/pkg/errors"
  17. "yunion.io/x/cloudmux/pkg/cloudprovider"
  18. )
  19. type SInstanceSpecs struct {
  20. Code int
  21. Value bool
  22. }
  23. type SWafInstance struct {
  24. Version string
  25. InstanceSpecInfos []SInstanceSpecs
  26. InstanceId string
  27. ExpireTime uint64
  28. }
  29. func (self *SRegion) DescribeInstanceSpecInfo() (*SWafInstance, error) {
  30. params := map[string]string{
  31. "RegionId": self.RegionId,
  32. }
  33. resp, err := self.wafRequest("DescribeInstanceSpecInfo", params)
  34. if err != nil {
  35. return nil, errors.Wrapf(err, "DescribeInstanceSpecInfo")
  36. }
  37. ret := &SWafInstance{}
  38. err = resp.Unmarshal(&ret)
  39. if err != nil {
  40. return nil, errors.Wrapf(err, "resp.Unmarshal")
  41. }
  42. if len(ret.InstanceId) == 0 {
  43. return nil, cloudprovider.ErrNotFound
  44. }
  45. return ret, nil
  46. }
  47. func (self *SRegion) DeleteInstance(id string) error {
  48. params := map[string]string{
  49. "RegionId": self.RegionId,
  50. "InstanceId": id,
  51. }
  52. _, err := self.wafRequest("DeleteInstance", params)
  53. return errors.Wrapf(err, "DeleteInstance")
  54. }
  55. func (self *SRegion) GetICloudWafInstancesV1() ([]cloudprovider.ICloudWafInstance, error) {
  56. ins, err := self.DescribeInstanceSpecInfo()
  57. if err != nil {
  58. if errors.Cause(err) == cloudprovider.ErrNotFound {
  59. return []cloudprovider.ICloudWafInstance{}, nil
  60. }
  61. return nil, errors.Wrapf(err, "DescribeInstanceSpecInfo")
  62. }
  63. domains, err := self.DescribeDomainNames(ins.InstanceId)
  64. if err != nil {
  65. return nil, errors.Wrapf(err, "DescribeDomainNames")
  66. }
  67. ret := []cloudprovider.ICloudWafInstance{}
  68. for i := range domains {
  69. domain, err := self.DescribeDomain(ins.InstanceId, domains[i])
  70. if err != nil {
  71. return nil, errors.Wrapf(err, "DescribeDomain %s", domains[i])
  72. }
  73. domain.region = self
  74. domain.insId = ins.InstanceId
  75. domain.name = domains[i]
  76. ret = append(ret, domain)
  77. }
  78. return ret, nil
  79. }