zettakit.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. "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 SZettaKitRegionDriver struct {
  25. SManagedVirtualizationRegionDriver
  26. }
  27. func init() {
  28. driver := SZettaKitRegionDriver{}
  29. models.RegisterRegionDriver(&driver)
  30. }
  31. func (self *SZettaKitRegionDriver) GetProvider() string {
  32. return api.CLOUD_PROVIDER_ZETTAKIT
  33. }
  34. func (self *SZettaKitRegionDriver) IsVpcCreateNeedInputCidr() bool {
  35. return false
  36. }
  37. func (self *SZettaKitRegionDriver) IsSupportedElasticcacheSecgroup() bool {
  38. return false
  39. }
  40. func (self *SZettaKitRegionDriver) GetMaxElasticcacheSecurityGroupCount() int {
  41. return 1
  42. }
  43. func (self *SZettaKitRegionDriver) ValidateCreateEipData(ctx context.Context, userCred mcclient.TokenCredential, input *api.SElasticipCreateInput) error {
  44. if len(input.NetworkId) == 0 {
  45. return httperrors.NewMissingParameterError("network_id")
  46. }
  47. _network, err := models.NetworkManager.FetchByIdOrName(ctx, userCred, input.NetworkId)
  48. if err != nil {
  49. if err == sql.ErrNoRows {
  50. return httperrors.NewResourceNotFoundError2("network", input.NetworkId)
  51. }
  52. return httperrors.NewGeneralError(err)
  53. }
  54. network := _network.(*models.SNetwork)
  55. input.NetworkId = network.Id
  56. vpc, _ := network.GetVpc()
  57. if vpc == nil {
  58. return httperrors.NewInputParameterError("failed to found vpc for network %s(%s)", network.Name, network.Id)
  59. }
  60. input.ManagerId = vpc.ManagerId
  61. region, err := vpc.GetRegion()
  62. if err != nil {
  63. return err
  64. }
  65. if region.Id != input.CloudregionId {
  66. return httperrors.NewUnsupportOperationError("network %s(%s) does not belong to %s", network.Name, network.Id, self.GetProvider())
  67. }
  68. return nil
  69. }
  70. func (self *SZettaKitRegionDriver) 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. }