cloudpods.go 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. "yunion.io/x/pkg/utils"
  18. "yunion.io/x/sqlchemy"
  19. api "yunion.io/x/onecloud/pkg/apis/compute"
  20. "yunion.io/x/onecloud/pkg/compute/models"
  21. "yunion.io/x/onecloud/pkg/httperrors"
  22. "yunion.io/x/onecloud/pkg/mcclient"
  23. )
  24. type SCloudpodsRegionDriver struct {
  25. SManagedVirtualizationRegionDriver
  26. }
  27. func init() {
  28. driver := SCloudpodsRegionDriver{}
  29. models.RegisterRegionDriver(&driver)
  30. }
  31. func (self *SCloudpodsRegionDriver) GetProvider() string {
  32. return api.CLOUD_PROVIDER_CLOUDPODS
  33. }
  34. func (self *SCloudpodsRegionDriver) ValidateCreateVpcData(ctx context.Context, userCred mcclient.TokenCredential, input api.VpcCreateInput) (api.VpcCreateInput, error) {
  35. if !utils.IsInStringArray(input.CidrBlock, []string{"192.168.0.0/16", "10.0.0.0/8", "172.16.0.0/12"}) {
  36. return input, httperrors.NewInputParameterError("Invalid cidr_block, want 192.168.0.0/16|10.0.0.0/8|172.16.0.0/12, got %s", input.CidrBlock)
  37. }
  38. return input, nil
  39. }
  40. func (self *SCloudpodsRegionDriver) ValidateCreateSecurityGroupInput(ctx context.Context, userCred mcclient.TokenCredential, input *api.SSecgroupCreateInput) (*api.SSecgroupCreateInput, error) {
  41. for i := range input.Rules {
  42. rule := input.Rules[i]
  43. if rule.Priority == nil {
  44. return nil, httperrors.NewMissingParameterError("priority")
  45. }
  46. if *rule.Priority < 1 || *rule.Priority > 100 {
  47. return nil, httperrors.NewInputParameterError("invalid priority %d, range 1-100", *rule.Priority)
  48. }
  49. }
  50. return self.SManagedVirtualizationRegionDriver.ValidateCreateSecurityGroupInput(ctx, userCred, input)
  51. }
  52. func (self *SCloudpodsRegionDriver) ValidateUpdateSecurityGroupRuleInput(ctx context.Context, userCred mcclient.TokenCredential, input *api.SSecgroupRuleUpdateInput) (*api.SSecgroupRuleUpdateInput, error) {
  53. if input.Priority != nil && (*input.Priority < 1 || *input.Priority > 100) {
  54. return nil, httperrors.NewInputParameterError("invalid priority %d, range 1-100", *input.Priority)
  55. }
  56. return self.SManagedVirtualizationRegionDriver.ValidateUpdateSecurityGroupRuleInput(ctx, userCred, input)
  57. }
  58. func (self *SCloudpodsRegionDriver) GetSecurityGroupFilter(vpc *models.SVpc) (func(q *sqlchemy.SQuery) *sqlchemy.SQuery, error) {
  59. return func(q *sqlchemy.SQuery) *sqlchemy.SQuery {
  60. return q.Equals("cloudregion_id", vpc.CloudregionId)
  61. }, nil
  62. }