cnware.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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 regiondrivers
  15. import (
  16. "context"
  17. "yunion.io/x/jsonutils"
  18. "yunion.io/x/pkg/errors"
  19. api "yunion.io/x/onecloud/pkg/apis/compute"
  20. "yunion.io/x/onecloud/pkg/cloudcommon/db"
  21. "yunion.io/x/onecloud/pkg/cloudcommon/db/taskman"
  22. "yunion.io/x/onecloud/pkg/compute/models"
  23. "yunion.io/x/onecloud/pkg/mcclient"
  24. )
  25. type SCNwareRegionDriver struct {
  26. SManagedVirtualizationRegionDriver
  27. }
  28. func init() {
  29. driver := SCNwareRegionDriver{}
  30. models.RegisterRegionDriver(&driver)
  31. }
  32. func (self *SCNwareRegionDriver) GetProvider() string {
  33. return api.CLOUD_PROVIDER_CNWARE
  34. }
  35. func (self *SCNwareRegionDriver) RequestCreateNetwork(ctx context.Context, userCred mcclient.TokenCredential, net *models.SNetwork, task taskman.ITask) error {
  36. return net.SetStatus(ctx, userCred, api.NETWORK_STATUS_AVAILABLE, "")
  37. }
  38. func (self *SCNwareRegionDriver) RequestCreateInstanceSnapshot(ctx context.Context, guest *models.SGuest, isp *models.SInstanceSnapshot, task taskman.ITask, params *jsonutils.JSONDict) error {
  39. taskman.LocalTaskRun(task, func() (jsonutils.JSONObject, error) {
  40. ivm, err := guest.GetIVM(ctx)
  41. if err != nil {
  42. return nil, errors.Wrap(err, "unable to GetIVM")
  43. }
  44. _, err = ivm.CreateInstanceSnapshot(ctx, isp.GetName(), isp.Description)
  45. if err != nil {
  46. return nil, errors.Wrap(err, "unable to CreateInstanceSnapshot")
  47. }
  48. _, err = db.Update(isp, func() error {
  49. isp.SetExternalId(isp.Name)
  50. return nil
  51. })
  52. return nil, err
  53. })
  54. return nil
  55. }