ksyun.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. "fmt"
  18. "time"
  19. "yunion.io/x/pkg/errors"
  20. "yunion.io/x/onecloud/pkg/apis"
  21. api "yunion.io/x/onecloud/pkg/apis/compute"
  22. "yunion.io/x/onecloud/pkg/compute/models"
  23. "yunion.io/x/onecloud/pkg/mcclient"
  24. )
  25. type SKsyunRegionDriver struct {
  26. SManagedVirtualizationRegionDriver
  27. }
  28. func init() {
  29. driver := SKsyunRegionDriver{}
  30. models.RegisterRegionDriver(&driver)
  31. }
  32. func (self *SKsyunRegionDriver) GetProvider() string {
  33. return api.CLOUD_PROVIDER_KSYUN
  34. }
  35. func (self *SKsyunRegionDriver) ValidateCreateSnapshotData(ctx context.Context, userCred mcclient.TokenCredential, disk *models.SDisk, storage *models.SStorage, input *api.SnapshotCreateInput) error {
  36. return nil
  37. }
  38. func (self *SKsyunRegionDriver) CreateDefaultSecurityGroup(
  39. ctx context.Context,
  40. userCred mcclient.TokenCredential,
  41. ownerId mcclient.IIdentityProvider,
  42. vpc *models.SVpc,
  43. ) (*models.SSecurityGroup, error) {
  44. region, err := vpc.GetRegion()
  45. if err != nil {
  46. return nil, errors.Wrapf(err, "GetRegion")
  47. }
  48. driver := region.GetDriver()
  49. newGroup := &models.SSecurityGroup{}
  50. newGroup.SetModelManager(models.SecurityGroupManager, newGroup)
  51. newGroup.Name = fmt.Sprintf("%s-%d", driver.GetDefaultSecurityGroupNamePrefix(), time.Now().Unix())
  52. // 部分云可能不需要vpcId, 创建完安全组后会自动置空
  53. newGroup.VpcId = vpc.Id
  54. newGroup.ManagerId = vpc.ManagerId
  55. newGroup.CloudregionId = vpc.CloudregionId
  56. newGroup.DomainId = ownerId.GetProjectDomainId()
  57. newGroup.ProjectId = ownerId.GetProjectId()
  58. newGroup.ProjectSrc = string(apis.OWNER_SOURCE_LOCAL)
  59. err = models.SecurityGroupManager.TableSpec().Insert(ctx, newGroup)
  60. if err != nil {
  61. return nil, errors.Wrapf(err, "insert")
  62. }
  63. err = driver.RequestCreateSecurityGroup(ctx, userCred, newGroup, api.SSecgroupRuleResourceSet{})
  64. if err != nil {
  65. return nil, errors.Wrapf(err, "RequestCreateSecurityGroup")
  66. }
  67. return newGroup, nil
  68. }