dnszone_create_task.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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 dnszone
  15. import (
  16. "context"
  17. "yunion.io/x/cloudmux/pkg/cloudprovider"
  18. "yunion.io/x/jsonutils"
  19. "yunion.io/x/log"
  20. "yunion.io/x/pkg/errors"
  21. api "yunion.io/x/onecloud/pkg/apis/compute"
  22. "yunion.io/x/onecloud/pkg/cloudcommon/db"
  23. "yunion.io/x/onecloud/pkg/cloudcommon/db/taskman"
  24. "yunion.io/x/onecloud/pkg/compute/models"
  25. "yunion.io/x/onecloud/pkg/util/logclient"
  26. )
  27. type DnsZoneCreateTask struct {
  28. taskman.STask
  29. }
  30. func init() {
  31. taskman.RegisterTask(DnsZoneCreateTask{})
  32. }
  33. func (self *DnsZoneCreateTask) taskFailed(ctx context.Context, zone *models.SDnsZone, err error) {
  34. zone.SetStatus(ctx, self.GetUserCred(), api.DNS_ZONE_STATUS_CREATE_FAILE, err.Error())
  35. db.OpsLog.LogEvent(zone, db.ACT_CREATE, zone.GetShortDesc(ctx), self.GetUserCred())
  36. logclient.AddActionLogWithContext(ctx, zone, logclient.ACT_CREATE, err, self.UserCred, false)
  37. self.SetStageFailed(ctx, jsonutils.NewString(err.Error()))
  38. }
  39. func (self *DnsZoneCreateTask) OnInit(ctx context.Context, obj db.IStandaloneModel, data jsonutils.JSONObject) {
  40. zone := obj.(*models.SDnsZone)
  41. if len(zone.ManagerId) == 0 {
  42. db.Update(zone, func() error {
  43. zone.Status = api.DNS_ZONE_STATUS_AVAILABLE
  44. return nil
  45. })
  46. self.taskComplete(ctx, zone)
  47. return
  48. }
  49. provider, err := zone.GetProvider(ctx)
  50. if err != nil {
  51. self.taskFailed(ctx, zone, errors.Wrapf(err, "GetProviderIdsQuery"))
  52. return
  53. }
  54. opts := &cloudprovider.SDnsZoneCreateOptions{
  55. Name: zone.Name,
  56. Desc: zone.Description,
  57. ZoneType: cloudprovider.TDnsZoneType(zone.ZoneType),
  58. Vpcs: []cloudprovider.SPrivateZoneVpc{},
  59. }
  60. if zone.ZoneType == string(cloudprovider.PrivateZone) {
  61. vpcs, err := zone.GetVpcs()
  62. if err != nil {
  63. self.taskFailed(ctx, zone, errors.Wrapf(err, "GetVpcs"))
  64. return
  65. }
  66. for i := range vpcs {
  67. region, err := vpcs[i].GetRegion()
  68. if err != nil {
  69. self.taskFailed(ctx, zone, errors.Wrapf(err, "GetRegion"))
  70. return
  71. }
  72. opts.Vpcs = append(opts.Vpcs, cloudprovider.SPrivateZoneVpc{
  73. Id: vpcs[i].ExternalId,
  74. RegionId: region.GetRegionExtId(),
  75. })
  76. }
  77. }
  78. iZone, err := provider.CreateICloudDnsZone(opts)
  79. if err != nil {
  80. self.taskFailed(ctx, zone, errors.Wrapf(err, "CreateICloudDnsZone"))
  81. return
  82. }
  83. err = zone.SyncWithDnsZone(ctx, self.UserCred, iZone)
  84. if err != nil {
  85. self.taskFailed(ctx, zone, errors.Wrapf(err, "SyncWithDnsZone"))
  86. return
  87. }
  88. result := zone.SyncRecords(ctx, self.UserCred, iZone, false)
  89. log.Infof("sync records for zone %s result: %s", zone.Name, result.Result())
  90. self.taskComplete(ctx, zone)
  91. }
  92. func (self *DnsZoneCreateTask) taskComplete(ctx context.Context, zone *models.SDnsZone) {
  93. self.SetStageComplete(ctx, nil)
  94. }