ctyun.go 3.2 KB

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