ai_gateway_create_task.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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 ai_gateway
  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/onecloud/pkg/apis"
  21. "yunion.io/x/onecloud/pkg/cloudcommon/db"
  22. "yunion.io/x/onecloud/pkg/cloudcommon/db/taskman"
  23. "yunion.io/x/onecloud/pkg/cloudcommon/notifyclient"
  24. "yunion.io/x/onecloud/pkg/compute/models"
  25. "yunion.io/x/onecloud/pkg/util/logclient"
  26. )
  27. type AiGatewayCreateTask struct {
  28. taskman.STask
  29. }
  30. func init() {
  31. taskman.RegisterTask(AiGatewayCreateTask{})
  32. }
  33. func (self *AiGatewayCreateTask) taskFailed(ctx context.Context, gateway *models.SAiGateway, err error) {
  34. gateway.SetStatus(ctx, self.UserCred, apis.STATUS_CREATE_FAILED, err.Error())
  35. db.OpsLog.LogEvent(gateway, db.ACT_CREATE, err.Error(), self.UserCred)
  36. logclient.AddActionLogWithStartable(self, gateway, logclient.ACT_CREATE, err.Error(), self.UserCred, false)
  37. self.SetStageFailed(ctx, jsonutils.NewString(err.Error()))
  38. }
  39. func (self *AiGatewayCreateTask) taskComplete(ctx context.Context, gateway *models.SAiGateway) {
  40. gateway.SetStatus(ctx, self.UserCred, apis.STATUS_AVAILABLE, "")
  41. self.SetStageComplete(ctx, nil)
  42. }
  43. func (self *AiGatewayCreateTask) OnInit(ctx context.Context, obj db.IStandaloneModel, body jsonutils.JSONObject) {
  44. gateway := obj.(*models.SAiGateway)
  45. driver, err := gateway.GetDriver(ctx)
  46. if err != nil {
  47. self.taskFailed(ctx, gateway, errors.Wrapf(err, "GetDriver"))
  48. return
  49. }
  50. opts := cloudprovider.AiGatewayCreateOptions{
  51. Name: gateway.Name,
  52. Authentication: gateway.Authentication,
  53. CacheInvalidateOnUpdate: gateway.CacheInvalidateOnUpdate,
  54. CacheTTL: gateway.CacheTTL,
  55. CollectLogs: gateway.CollectLogs,
  56. RateLimitingInterval: gateway.RateLimitingInterval,
  57. RateLimitingLimit: gateway.RateLimitingLimit,
  58. RateLimitingTechnique: gateway.RateLimitingTechnique,
  59. }
  60. iGateway, err := driver.CreateIAiGateway(&opts)
  61. if err != nil {
  62. self.taskFailed(ctx, gateway, errors.Wrapf(err, "CreateIAiGateway"))
  63. return
  64. }
  65. err = gateway.SyncWithCloudAiGateway(ctx, self.UserCred, iGateway)
  66. if err != nil {
  67. self.taskFailed(ctx, gateway, errors.Wrapf(err, "SyncWithCloudAiGateway"))
  68. return
  69. }
  70. notifyclient.EventNotify(ctx, self.UserCred, notifyclient.SEventNotifyParam{
  71. Obj: gateway,
  72. Action: notifyclient.ActionCreate,
  73. })
  74. self.taskComplete(ctx, gateway)
  75. }