dnszone_syncstatus_task.go 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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/jsonutils"
  18. "yunion.io/x/pkg/errors"
  19. "yunion.io/x/onecloud/pkg/apis"
  20. api "yunion.io/x/onecloud/pkg/apis/compute"
  21. "yunion.io/x/onecloud/pkg/cloudcommon/db"
  22. "yunion.io/x/onecloud/pkg/cloudcommon/db/taskman"
  23. "yunion.io/x/onecloud/pkg/compute/models"
  24. "yunion.io/x/onecloud/pkg/util/logclient"
  25. )
  26. type DnsZoneSyncstatusTask struct {
  27. taskman.STask
  28. }
  29. func init() {
  30. taskman.RegisterTask(DnsZoneSyncstatusTask{})
  31. }
  32. func (self *DnsZoneSyncstatusTask) taskFailed(ctx context.Context, zone *models.SDnsZone, err error) {
  33. zone.SetStatus(ctx, self.GetUserCred(), apis.STATUS_UNKNOWN, err.Error())
  34. self.SetStageComplete(ctx, nil)
  35. }
  36. func (self *DnsZoneSyncstatusTask) OnInit(ctx context.Context, obj db.IStandaloneModel, data jsonutils.JSONObject) {
  37. zone := obj.(*models.SDnsZone)
  38. if len(zone.ManagerId) == 0 {
  39. self.taskComplete(ctx, zone)
  40. return
  41. }
  42. iZone, err := zone.GetICloudDnsZone(ctx)
  43. if err != nil {
  44. self.taskFailed(ctx, zone, errors.Wrapf(err, "GetICloudDnsZone"))
  45. return
  46. }
  47. result := zone.SyncRecords(ctx, self.UserCred, iZone, false)
  48. if result.IsError() {
  49. logclient.AddActionLogWithContext(ctx, zone, logclient.ACT_CLOUD_SYNC, errors.Wrapf(err, "SyncRecords"), self.UserCred, false)
  50. }
  51. zone.SetStatus(ctx, self.UserCred, iZone.GetStatus(), "")
  52. self.SetStageComplete(ctx, nil)
  53. }
  54. func (self *DnsZoneSyncstatusTask) taskComplete(ctx context.Context, dnsZone *models.SDnsZone) {
  55. dnsZone.SetStatus(ctx, self.GetUserCred(), api.DNS_ZONE_STATUS_AVAILABLE, "")
  56. self.SetStageComplete(ctx, nil)
  57. }