zstack.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. "database/sql"
  18. "strings"
  19. "yunion.io/x/sqlchemy"
  20. api "yunion.io/x/onecloud/pkg/apis/compute"
  21. "yunion.io/x/onecloud/pkg/compute/models"
  22. "yunion.io/x/onecloud/pkg/httperrors"
  23. "yunion.io/x/onecloud/pkg/mcclient"
  24. )
  25. type SZStackRegionDriver struct {
  26. SManagedVirtualizationRegionDriver
  27. }
  28. func init() {
  29. driver := SZStackRegionDriver{}
  30. models.RegisterRegionDriver(&driver)
  31. }
  32. func (self *SZStackRegionDriver) GetProvider() string {
  33. return api.CLOUD_PROVIDER_ZSTACK
  34. }
  35. func (self *SZStackRegionDriver) ValidateCreateEipData(ctx context.Context, userCred mcclient.TokenCredential, input *api.SElasticipCreateInput) error {
  36. if len(input.NetworkId) == 0 {
  37. return httperrors.NewMissingParameterError("network_id")
  38. }
  39. _network, err := models.NetworkManager.FetchByIdOrName(ctx, userCred, input.NetworkId)
  40. if err != nil {
  41. if err == sql.ErrNoRows {
  42. return httperrors.NewResourceNotFoundError2("network", input.NetworkId)
  43. }
  44. return httperrors.NewGeneralError(err)
  45. }
  46. network := _network.(*models.SNetwork)
  47. input.NetworkId = network.Id
  48. vpc, _ := network.GetVpc()
  49. if vpc == nil {
  50. return httperrors.NewInputParameterError("failed to found vpc for network %s(%s)", network.Name, network.Id)
  51. }
  52. input.ManagerId = vpc.ManagerId
  53. region, err := vpc.GetRegion()
  54. if err != nil {
  55. return err
  56. }
  57. if region.Id != input.CloudregionId {
  58. return httperrors.NewUnsupportOperationError("network %s(%s) does not belong to %s", network.Name, network.Id, self.GetProvider())
  59. }
  60. return nil
  61. }
  62. func (self *SZStackRegionDriver) ValidateCreateSecurityGroupInput(ctx context.Context, userCred mcclient.TokenCredential, input *api.SSecgroupCreateInput) (*api.SSecgroupCreateInput, error) {
  63. for i := range input.Rules {
  64. rule := input.Rules[i]
  65. if len(rule.Ports) > 0 && strings.Contains(input.Rules[i].Ports, ",") {
  66. return nil, httperrors.NewInputParameterError("invalid ports %s", input.Rules[i].Ports)
  67. }
  68. }
  69. return self.SManagedVirtualizationRegionDriver.ValidateCreateSecurityGroupInput(ctx, userCred, input)
  70. }
  71. func (self *SZStackRegionDriver) ValidateUpdateSecurityGroupRuleInput(ctx context.Context, userCred mcclient.TokenCredential, input *api.SSecgroupRuleUpdateInput) (*api.SSecgroupRuleUpdateInput, error) {
  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 *SZStackRegionDriver) 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)
  80. }, nil
  81. }