azure.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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/cloudmux/pkg/cloudprovider"
  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 SAzureRegionDriver struct {
  27. SManagedVirtualizationRegionDriver
  28. }
  29. func init() {
  30. driver := SAzureRegionDriver{}
  31. models.RegisterRegionDriver(&driver)
  32. }
  33. func (self *SAzureRegionDriver) GetProvider() string {
  34. return api.CLOUD_PROVIDER_AZURE
  35. }
  36. func (self *SAzureRegionDriver) 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. if cidrV.Value.MaskLen < 8 || cidrV.Value.MaskLen > 29 {
  42. return input, httperrors.NewInputParameterError("%s request the mask range should be between 8 and 29", self.GetProvider())
  43. }
  44. return input, nil
  45. }
  46. func (self *SAzureRegionDriver) ValidateCreateWafInstanceData(ctx context.Context, userCred mcclient.TokenCredential, input api.WafInstanceCreateInput) (api.WafInstanceCreateInput, error) {
  47. if len(input.Type) == 0 {
  48. input.Type = cloudprovider.WafTypeAppGateway
  49. }
  50. switch input.Type {
  51. case cloudprovider.WafTypeAppGateway:
  52. default:
  53. return input, httperrors.NewInputParameterError("Invalid azure waf type %s", input.Type)
  54. }
  55. if input.DefaultAction == nil {
  56. input.DefaultAction = &cloudprovider.DefaultAction{}
  57. }
  58. if len(input.DefaultAction.Action) == 0 {
  59. input.DefaultAction.Action = cloudprovider.WafActionDetection
  60. }
  61. switch input.DefaultAction.Action {
  62. case cloudprovider.WafActionPrevention:
  63. case cloudprovider.WafActionDetection:
  64. default:
  65. return input, httperrors.NewInputParameterError("invalid default action %s", input.DefaultAction.Action)
  66. }
  67. return input, nil
  68. }
  69. func (self *SAzureRegionDriver) ValidateCreateSecurityGroupInput(ctx context.Context, userCred mcclient.TokenCredential, input *api.SSecgroupCreateInput) (*api.SSecgroupCreateInput, error) {
  70. for i := range input.Rules {
  71. rule := input.Rules[i]
  72. if rule.Priority == nil {
  73. return nil, httperrors.NewMissingParameterError("priority")
  74. }
  75. if *rule.Priority < 100 || *rule.Priority > 4096 {
  76. return nil, httperrors.NewInputParameterError("invalid priority %d, range 100-4096", *rule.Priority)
  77. }
  78. }
  79. return self.SManagedVirtualizationRegionDriver.ValidateCreateSecurityGroupInput(ctx, userCred, input)
  80. }
  81. func (self *SAzureRegionDriver) ValidateUpdateSecurityGroupRuleInput(ctx context.Context, userCred mcclient.TokenCredential, input *api.SSecgroupRuleUpdateInput) (*api.SSecgroupRuleUpdateInput, error) {
  82. if input.Priority != nil && (*input.Priority < 100 || *input.Priority > 4096) {
  83. return nil, httperrors.NewInputParameterError("invalid priority %d, range 100-4096", *input.Priority)
  84. }
  85. return self.SManagedVirtualizationRegionDriver.ValidateUpdateSecurityGroupRuleInput(ctx, userCred, input)
  86. }
  87. func (self *SAzureRegionDriver) GetSecurityGroupFilter(vpc *models.SVpc) (func(q *sqlchemy.SQuery) *sqlchemy.SQuery, error) {
  88. return func(q *sqlchemy.SQuery) *sqlchemy.SQuery {
  89. return q.Equals("cloudregion_id", vpc.CloudregionId).Equals("manager_id", vpc.ManagerId)
  90. }, nil
  91. }