dnszone_remove_vpcs_task.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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/pkg/errors"
  20. "yunion.io/x/pkg/utils"
  21. "yunion.io/x/onecloud/pkg/apis"
  22. api "yunion.io/x/onecloud/pkg/apis/compute"
  23. "yunion.io/x/onecloud/pkg/cloudcommon/db"
  24. "yunion.io/x/onecloud/pkg/cloudcommon/db/taskman"
  25. "yunion.io/x/onecloud/pkg/compute/models"
  26. "yunion.io/x/onecloud/pkg/util/logclient"
  27. )
  28. type DnsZoneRemoveVpcsTask struct {
  29. taskman.STask
  30. }
  31. func init() {
  32. taskman.RegisterTask(DnsZoneRemoveVpcsTask{})
  33. }
  34. func (self *DnsZoneRemoveVpcsTask) taskFailed(ctx context.Context, zone *models.SDnsZone, err error) {
  35. zone.SetStatus(ctx, self.GetUserCred(), apis.STATUS_UNKNOWN, err.Error())
  36. logclient.AddActionLogWithContext(ctx, zone, logclient.ACT_REMOVE_VPCS, err, self.UserCred, false)
  37. self.SetStageFailed(ctx, jsonutils.NewString(err.Error()))
  38. }
  39. func (self *DnsZoneRemoveVpcsTask) OnInit(ctx context.Context, obj db.IStandaloneModel, data jsonutils.JSONObject) {
  40. zone := obj.(*models.SDnsZone)
  41. if len(zone.ManagerId) == 0 {
  42. self.taskComplete(ctx, zone)
  43. return
  44. }
  45. iZone, err := zone.GetICloudDnsZone(ctx)
  46. if err != nil {
  47. self.taskFailed(ctx, zone, errors.Wrapf(err, "GetICloudDnsZone"))
  48. return
  49. }
  50. iVpcIds, err := iZone.GetICloudVpcIds()
  51. if err != nil {
  52. self.taskFailed(ctx, zone, errors.Wrapf(err, "GetICloudVpcIds"))
  53. return
  54. }
  55. vpcIds := []string{}
  56. self.GetParams().Unmarshal(&vpcIds, "vpc_ids")
  57. for i := range vpcIds {
  58. vpcObj, err := models.VpcManager.FetchById(vpcIds[i])
  59. if err != nil {
  60. self.taskFailed(ctx, zone, errors.Wrapf(err, "Vpc.FetchById(%s)", vpcIds[i]))
  61. return
  62. }
  63. vpc := vpcObj.(*models.SVpc)
  64. region, err := vpc.GetRegion()
  65. if err != nil {
  66. self.taskFailed(ctx, zone, errors.Wrapf(err, "GetRegion(%s)", vpc.CloudregionId))
  67. return
  68. }
  69. opts := &cloudprovider.SPrivateZoneVpc{
  70. Id: vpc.ExternalId,
  71. RegionId: region.GetRegionExtId(),
  72. }
  73. if utils.IsInStringArray(opts.Id, iVpcIds) {
  74. err = iZone.RemoveVpc(opts)
  75. if err != nil {
  76. self.taskFailed(ctx, zone, errors.Wrapf(err, "RemoveVpc(%s)", vpc.Name))
  77. return
  78. }
  79. }
  80. zone.RemoveVpc(ctx, vpcIds[i])
  81. }
  82. self.taskComplete(ctx, zone)
  83. }
  84. func (self *DnsZoneRemoveVpcsTask) taskComplete(ctx context.Context, zone *models.SDnsZone) {
  85. zone.SetStatus(ctx, self.GetUserCred(), api.DNS_ZONE_STATUS_AVAILABLE, "")
  86. self.SetStageComplete(ctx, nil)
  87. }