secgrouprule.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. package ecloud
  2. import (
  3. "fmt"
  4. "strings"
  5. "yunion.io/x/pkg/errors"
  6. "yunion.io/x/cloudmux/pkg/cloudprovider"
  7. "yunion.io/x/cloudmux/pkg/multicloud"
  8. "yunion.io/x/pkg/util/secrules"
  9. )
  10. // SSecurityGroupRule 与 ecloudsdkvpc ListSecurityGroupRuleResponseContent 字段对应
  11. type SSecurityGroupRule struct {
  12. multicloud.SResourceBase
  13. region *SRegion
  14. SecgroupId string `json:"secgroupId"`
  15. Id string `json:"id"`
  16. Direction string `json:"direction"`
  17. Protocol string `json:"protocol"`
  18. MinPortRange *int32 `json:"minPortRange,omitempty"`
  19. MaxPortRange *int32 `json:"maxPortRange,omitempty"`
  20. RemoteIpPrefix string `json:"remoteIpPrefix"`
  21. Description string `json:"description"`
  22. EtherType string `json:"etherType,omitempty"`
  23. AimSgid *string `json:"aimSgid,omitempty"`
  24. DefaultRule *bool `json:"defaultRule,omitempty"`
  25. CreatedTime string `json:"createdTime,omitempty"`
  26. Status *int32 `json:"status,omitempty"`
  27. }
  28. func (r *SSecurityGroupRule) GetGlobalId() string {
  29. return r.Id
  30. }
  31. func (r *SSecurityGroupRule) GetDirection() secrules.TSecurityRuleDirection {
  32. if r.Direction == "ingress" {
  33. return secrules.DIR_IN
  34. }
  35. return secrules.DIR_OUT
  36. }
  37. func (r *SSecurityGroupRule) GetPriority() int {
  38. return 0
  39. }
  40. func (r *SSecurityGroupRule) GetAction() secrules.TSecurityRuleAction {
  41. return secrules.SecurityRuleAllow
  42. }
  43. func (r *SSecurityGroupRule) GetProtocol() string {
  44. if r.Protocol == "" || r.Protocol == "ANY" {
  45. return secrules.PROTO_ANY
  46. }
  47. return strings.ToLower(r.Protocol)
  48. }
  49. func (r *SSecurityGroupRule) GetPorts() string {
  50. if r.MinPortRange != nil && r.MaxPortRange != nil {
  51. min, max := *r.MinPortRange, *r.MaxPortRange
  52. if min == max {
  53. return fmt.Sprintf("%d", min)
  54. }
  55. return fmt.Sprintf("%d-%d", min, max)
  56. }
  57. return ""
  58. }
  59. func (r *SSecurityGroupRule) GetDescription() string {
  60. return r.Description
  61. }
  62. func (r *SSecurityGroupRule) GetCIDRs() []string {
  63. if r.RemoteIpPrefix == "" {
  64. return nil
  65. }
  66. return []string{r.RemoteIpPrefix}
  67. }
  68. func (r *SSecurityGroupRule) Update(opts *cloudprovider.SecurityGroupRuleUpdateOptions) error {
  69. return errors.ErrNotSupported
  70. }
  71. func (r *SSecurityGroupRule) Delete() error {
  72. return r.region.DeleteSecurityGroupRule(r.Id)
  73. }