securitygroup.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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 jdcloud
  15. import (
  16. "fmt"
  17. commodels "github.com/jdcloud-api/jdcloud-sdk-go/services/common/models"
  18. "github.com/jdcloud-api/jdcloud-sdk-go/services/vpc/apis"
  19. "github.com/jdcloud-api/jdcloud-sdk-go/services/vpc/client"
  20. "github.com/jdcloud-api/jdcloud-sdk-go/services/vpc/models"
  21. api "yunion.io/x/cloudmux/pkg/apis/compute"
  22. "yunion.io/x/cloudmux/pkg/cloudprovider"
  23. "yunion.io/x/cloudmux/pkg/multicloud"
  24. )
  25. type SSecurityGroup struct {
  26. multicloud.SSecurityGroup
  27. JdcloudTags
  28. vpc *SVpc
  29. models.NetworkSecurityGroup
  30. }
  31. func (sg *SSecurityGroup) GetVpcId() string {
  32. return sg.VpcId
  33. }
  34. func (sg *SSecurityGroup) GetId() string {
  35. return sg.NetworkSecurityGroupId
  36. }
  37. func (sg *SSecurityGroup) GetGlobalId() string {
  38. return sg.GetId()
  39. }
  40. func (sg *SSecurityGroup) GetName() string {
  41. return sg.NetworkSecurityGroupName
  42. }
  43. func (sg *SSecurityGroup) GetDescription() string {
  44. return sg.Description
  45. }
  46. func (sg *SSecurityGroup) GetRules() ([]cloudprovider.ISecurityGroupRule, error) {
  47. return nil, cloudprovider.ErrNotImplemented
  48. }
  49. func (sg *SSecurityGroup) GetStatus() string {
  50. return api.SECGROUP_STATUS_READY
  51. }
  52. func (sg *SSecurityGroup) Delete() error {
  53. return cloudprovider.ErrNotImplemented
  54. }
  55. func (r *SRegion) GetSecurityGroups(vpcId string, securityGroupIds []string, pageNumber int, pageSize int) ([]SSecurityGroup, int, error) {
  56. filters := []commodels.Filter{}
  57. if vpcId != "" {
  58. filters = append(filters, commodels.Filter{
  59. Name: "vpcId",
  60. Values: []string{vpcId},
  61. })
  62. }
  63. if len(securityGroupIds) > 0 {
  64. filters = append(filters, commodels.Filter{
  65. Name: "networkSecurityGroupIds",
  66. Values: securityGroupIds,
  67. })
  68. }
  69. req := apis.NewDescribeNetworkSecurityGroupsRequestWithAllParams(r.ID, &pageNumber, &pageSize, filters)
  70. client := client.NewVpcClient(r.getCredential())
  71. client.Logger = Logger{debug: r.client.debug}
  72. resp, err := client.DescribeNetworkSecurityGroups(req)
  73. if err != nil {
  74. return nil, 0, err
  75. }
  76. if resp.Error.Code >= 400 {
  77. err = fmt.Errorf("%s", resp.Error.Message)
  78. return nil, 0, err
  79. }
  80. total := resp.Result.TotalCount
  81. sgs := make([]SSecurityGroup, 0, len(resp.Result.NetworkSecurityGroups))
  82. for i := range resp.Result.NetworkSecurityGroups {
  83. sgs = append(sgs, SSecurityGroup{
  84. NetworkSecurityGroup: resp.Result.NetworkSecurityGroups[i],
  85. })
  86. }
  87. return sgs, total, nil
  88. }