openstack.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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/cloudmux/pkg/cloudprovider"
  19. "yunion.io/x/jsonutils"
  20. "yunion.io/x/sqlchemy"
  21. api "yunion.io/x/onecloud/pkg/apis/compute"
  22. "yunion.io/x/onecloud/pkg/cloudcommon/db/taskman"
  23. "yunion.io/x/onecloud/pkg/compute/models"
  24. "yunion.io/x/onecloud/pkg/httperrors"
  25. "yunion.io/x/onecloud/pkg/mcclient"
  26. )
  27. type SOpenStackRegionDriver struct {
  28. SManagedVirtualizationRegionDriver
  29. }
  30. func init() {
  31. driver := SOpenStackRegionDriver{}
  32. models.RegisterRegionDriver(&driver)
  33. }
  34. func (self *SOpenStackRegionDriver) GetProvider() string {
  35. return api.CLOUD_PROVIDER_OPENSTACK
  36. }
  37. func (self *SOpenStackRegionDriver) IsVpcCreateNeedInputCidr() bool {
  38. return false
  39. }
  40. func (self *SOpenStackRegionDriver) ValidateCreateLoadbalancerData(ctx context.Context, userCred mcclient.TokenCredential, ownerId mcclient.IIdentityProvider, input *api.LoadbalancerCreateInput) (*api.LoadbalancerCreateInput, error) {
  41. // 公网ELB需要指定EIP
  42. if input.AddressType == api.LB_ADDR_TYPE_INTERNET && len(input.EipId) == 0 {
  43. return input, httperrors.NewMissingParameterError("eip_id")
  44. }
  45. return self.SManagedVirtualizationRegionDriver.ValidateCreateLoadbalancerData(ctx, userCred, ownerId, input)
  46. }
  47. func (self *SOpenStackRegionDriver) ValidateCreateEipData(ctx context.Context, userCred mcclient.TokenCredential, input *api.SElasticipCreateInput) error {
  48. if len(input.NetworkId) == 0 {
  49. return httperrors.NewMissingParameterError("network_id")
  50. }
  51. _network, err := models.NetworkManager.FetchByIdOrName(ctx, userCred, input.NetworkId)
  52. if err != nil {
  53. if err == sql.ErrNoRows {
  54. return httperrors.NewResourceNotFoundError2("network", input.NetworkId)
  55. }
  56. return httperrors.NewGeneralError(err)
  57. }
  58. network := _network.(*models.SNetwork)
  59. input.NetworkId = network.Id
  60. vpc, _ := network.GetVpc()
  61. if vpc == nil {
  62. return httperrors.NewInputParameterError("failed to found vpc for network %s(%s)", network.Name, network.Id)
  63. }
  64. input.ManagerId = vpc.ManagerId
  65. region, err := vpc.GetRegion()
  66. if err != nil {
  67. return err
  68. }
  69. if region.Id != input.CloudregionId {
  70. return httperrors.NewUnsupportOperationError("network %s(%s) does not belong to %s", network.Name, network.Id, self.GetProvider())
  71. }
  72. return nil
  73. }
  74. func (self *SOpenStackRegionDriver) ValidateCreateLoadbalancerListenerData(ctx context.Context, userCred mcclient.TokenCredential,
  75. ownerId mcclient.IIdentityProvider, input *api.LoadbalancerListenerCreateInput,
  76. lb *models.SLoadbalancer, lbbg *models.SLoadbalancerBackendGroup) (*api.LoadbalancerListenerCreateInput, error) {
  77. return input, httperrors.NewNotImplementedError("ValidateCreateLoadbalancerListenerData")
  78. }
  79. func (self *SOpenStackRegionDriver) IsSupportLoadbalancerListenerRuleRedirect() bool {
  80. return true
  81. }
  82. func (self *SOpenStackRegionDriver) ValidateUpdateLoadbalancerListenerRuleData(ctx context.Context, userCred mcclient.TokenCredential, input *api.LoadbalancerListenerRuleUpdateInput) (*api.LoadbalancerListenerRuleUpdateInput, error) {
  83. return input, nil
  84. }
  85. func (self *SOpenStackRegionDriver) RequestDeleteLoadbalancerBackendGroup(ctx context.Context, userCred mcclient.TokenCredential, lbbg *models.SLoadbalancerBackendGroup, task taskman.ITask) error {
  86. taskman.LocalTaskRun(task, func() (jsonutils.JSONObject, error) {
  87. return nil, cloudprovider.ErrNotImplemented
  88. })
  89. return nil
  90. }
  91. func (self *SOpenStackRegionDriver) RequestCreateLoadbalancerBackend(ctx context.Context, userCred mcclient.TokenCredential, lbb *models.SLoadbalancerBackend, task taskman.ITask) error {
  92. taskman.LocalTaskRun(task, func() (jsonutils.JSONObject, error) {
  93. return nil, cloudprovider.ErrNotImplemented
  94. })
  95. return nil
  96. }
  97. func (self *SOpenStackRegionDriver) RequestSyncLoadbalancerBackend(ctx context.Context, userCred mcclient.TokenCredential, lbb *models.SLoadbalancerBackend, task taskman.ITask) error {
  98. taskman.LocalTaskRun(task, func() (jsonutils.JSONObject, error) {
  99. return nil, cloudprovider.ErrNotImplemented
  100. })
  101. return nil
  102. }
  103. func (self *SOpenStackRegionDriver) RequestDeleteLoadbalancerBackend(ctx context.Context, userCred mcclient.TokenCredential, lbb *models.SLoadbalancerBackend, task taskman.ITask) error {
  104. taskman.LocalTaskRun(task, func() (jsonutils.JSONObject, error) {
  105. return nil, cloudprovider.ErrNotImplemented
  106. })
  107. return nil
  108. }
  109. func (self *SOpenStackRegionDriver) GetSecurityGroupFilter(vpc *models.SVpc) (func(q *sqlchemy.SQuery) *sqlchemy.SQuery, error) {
  110. return func(q *sqlchemy.SQuery) *sqlchemy.SQuery {
  111. return q.Equals("cloudregion_id", vpc.CloudregionId)
  112. }, nil
  113. }