cdn_domain_create_task.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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 cdn
  15. import (
  16. "context"
  17. "time"
  18. "yunion.io/x/cloudmux/pkg/cloudprovider"
  19. "yunion.io/x/jsonutils"
  20. "yunion.io/x/pkg/errors"
  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/cloudcommon/notifyclient"
  26. "yunion.io/x/onecloud/pkg/compute/models"
  27. "yunion.io/x/onecloud/pkg/util/logclient"
  28. )
  29. type CDNDomainCreateTask struct {
  30. taskman.STask
  31. }
  32. func init() {
  33. taskman.RegisterTask(CDNDomainCreateTask{})
  34. }
  35. func (self *CDNDomainCreateTask) taskFailed(ctx context.Context, domain *models.SCDNDomain, err error) {
  36. domain.SetStatus(ctx, self.UserCred, apis.STATUS_CREATE_FAILED, err.Error())
  37. db.OpsLog.LogEvent(domain, db.ACT_DELOCATE_FAIL, err.Error(), self.UserCred)
  38. logclient.AddActionLogWithStartable(self, domain, logclient.ACT_DELETE, err.Error(), self.UserCred, false)
  39. self.SetStageFailed(ctx, jsonutils.NewString(err.Error()))
  40. }
  41. func (self *CDNDomainCreateTask) taskComplete(ctx context.Context, domain *models.SCDNDomain) {
  42. self.SetStageComplete(ctx, nil)
  43. }
  44. func (self *CDNDomainCreateTask) OnInit(ctx context.Context, obj db.IStandaloneModel, body jsonutils.JSONObject) {
  45. domain := obj.(*models.SCDNDomain)
  46. driver, err := domain.GetDriver(ctx)
  47. if err != nil {
  48. self.taskFailed(ctx, domain, errors.Wrapf(err, "GetDriver"))
  49. return
  50. }
  51. opts := cloudprovider.CdnCreateOptions{
  52. Domain: domain.Name,
  53. ServiceType: domain.ServiceType,
  54. Area: domain.Area,
  55. Origins: cloudprovider.SCdnOrigins{},
  56. }
  57. if domain.Origins != nil {
  58. opts.Origins = *domain.Origins
  59. }
  60. iCdn, err := driver.CreateICloudCDNDomain(&opts)
  61. if err != nil {
  62. self.taskFailed(ctx, domain, errors.Wrapf(err, "CreateICloudCDNDomain"))
  63. return
  64. }
  65. err = db.SetExternalId(domain, self.UserCred, iCdn.GetGlobalId())
  66. if err != nil {
  67. self.taskFailed(ctx, domain, errors.Wrapf(err, "unable to update externalId"))
  68. return
  69. }
  70. cloudprovider.WaitMultiStatus(iCdn, []string{
  71. api.CDN_DOMAIN_STATUS_ONLINE,
  72. api.CDN_DOMAIN_STATUS_OFFLINE,
  73. api.CDN_DOMAIN_STATUS_REJECTED,
  74. api.CDN_DOMAIN_STATUS_UNKNOWN,
  75. }, time.Second*5, time.Minute*3)
  76. tags, _ := domain.GetAllUserMetadata()
  77. if len(tags) > 0 {
  78. err = iCdn.SetTags(tags, true)
  79. if err != nil {
  80. logclient.AddActionLogWithStartable(self, domain, logclient.ACT_UPDATE, errors.Wrapf(err, "SetTags"), self.UserCred, false)
  81. }
  82. }
  83. notifyclient.EventNotify(ctx, self.UserCred, notifyclient.SEventNotifyParam{
  84. Obj: domain,
  85. Action: notifyclient.ActionCreate,
  86. })
  87. self.SetStage("OnSyncstatusComplete", nil)
  88. domain.StartSyncstatus(ctx, self.GetUserCred(), self.GetTaskId())
  89. }
  90. func (self *CDNDomainCreateTask) OnSyncstatusComplete(ctx context.Context, domain *models.SCDNDomain, data jsonutils.JSONObject) {
  91. self.taskComplete(ctx, domain)
  92. }
  93. func (self *CDNDomainCreateTask) OnSyncstatusCompleteFailed(ctx context.Context, domain *models.SCDNDomain, data jsonutils.JSONObject) {
  94. self.SetStageFailed(ctx, data)
  95. }