ucloud.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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 regiondrivers
  15. import (
  16. "context"
  17. "strings"
  18. "yunion.io/x/jsonutils"
  19. "yunion.io/x/pkg/util/secrules"
  20. "yunion.io/x/sqlchemy"
  21. api "yunion.io/x/onecloud/pkg/apis/compute"
  22. "yunion.io/x/onecloud/pkg/cloudcommon/validators"
  23. "yunion.io/x/onecloud/pkg/compute/models"
  24. "yunion.io/x/onecloud/pkg/httperrors"
  25. "yunion.io/x/onecloud/pkg/mcclient"
  26. )
  27. type SUcloudRegionDriver struct {
  28. SManagedVirtualizationRegionDriver
  29. }
  30. func init() {
  31. driver := SUcloudRegionDriver{}
  32. models.RegisterRegionDriver(&driver)
  33. }
  34. func (self *SUcloudRegionDriver) GetProvider() string {
  35. return api.CLOUD_PROVIDER_UCLOUD
  36. }
  37. func (self *SUcloudRegionDriver) ValidateCreateVpcData(ctx context.Context, userCred mcclient.TokenCredential, input api.VpcCreateInput) (api.VpcCreateInput, error) {
  38. var cidrV = validators.NewIPv4PrefixValidator("cidr_block")
  39. if err := cidrV.Validate(ctx, jsonutils.Marshal(input).(*jsonutils.JSONDict)); err != nil {
  40. return input, err
  41. }
  42. err := IsInPrivateIpRange(cidrV.Value.ToIPRange())
  43. if err != nil {
  44. return input, err
  45. }
  46. if cidrV.Value.MaskLen > 29 {
  47. return input, httperrors.NewInputParameterError("%s request the mask range should be less than or equal to 29", self.GetProvider())
  48. }
  49. return input, nil
  50. }
  51. func (self *SUcloudRegionDriver) ValidateCreateSecurityGroupInput(ctx context.Context, userCred mcclient.TokenCredential, input *api.SSecgroupCreateInput) (*api.SSecgroupCreateInput, error) {
  52. for i := range input.Rules {
  53. if input.Rules[i].Protocol == secrules.PROTO_ANY {
  54. return nil, httperrors.NewNotSupportedError("protocol %s", input.Rules[i].Protocol)
  55. }
  56. if input.Rules[i].Priority == nil {
  57. return nil, httperrors.NewMissingParameterError("priority")
  58. }
  59. if *input.Rules[i].Priority < 1 || *input.Rules[i].Priority > 3 {
  60. return nil, httperrors.NewInputParameterError("invalid priority %d, range 1-3", *input.Rules[i].Priority)
  61. }
  62. }
  63. return self.SManagedVirtualizationRegionDriver.ValidateCreateSecurityGroupInput(ctx, userCred, input)
  64. }
  65. func (self *SUcloudRegionDriver) ValidateUpdateSecurityGroupRuleInput(ctx context.Context, userCred mcclient.TokenCredential, input *api.SSecgroupRuleUpdateInput) (*api.SSecgroupRuleUpdateInput, error) {
  66. if input.Priority != nil && (*input.Priority < 1 || *input.Priority > 3) {
  67. return nil, httperrors.NewInputParameterError("invalid priority %d, range 1-3", *input.Priority)
  68. }
  69. if input.Protocol != nil && *input.Protocol == secrules.PROTO_ANY {
  70. return nil, httperrors.NewNotSupportedError("protocol %s", *input.Protocol)
  71. }
  72. if input.Ports != nil && strings.Contains(*input.Ports, ",") {
  73. return nil, httperrors.NewInputParameterError("invalid ports %s", *input.Ports)
  74. }
  75. return self.SManagedVirtualizationRegionDriver.ValidateUpdateSecurityGroupRuleInput(ctx, userCred, input)
  76. }
  77. func (self *SUcloudRegionDriver) GetSecurityGroupFilter(vpc *models.SVpc) (func(q *sqlchemy.SQuery) *sqlchemy.SQuery, error) {
  78. return func(q *sqlchemy.SQuery) *sqlchemy.SQuery {
  79. return q.Equals("cloudregion_id", vpc.CloudregionId).Equals("manager_id", vpc.ManagerId)
  80. }, nil
  81. }