cloudsyncelasticcache.go 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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 models
  15. import (
  16. "context"
  17. "fmt"
  18. "github.com/pkg/errors"
  19. "yunion.io/x/cloudmux/pkg/cloudprovider"
  20. "yunion.io/x/log"
  21. "yunion.io/x/pkg/util/compare"
  22. "yunion.io/x/onecloud/pkg/cloudcommon/db"
  23. "yunion.io/x/onecloud/pkg/cloudcommon/db/lockman"
  24. "yunion.io/x/onecloud/pkg/mcclient"
  25. )
  26. func syncElasticcaches(
  27. ctx context.Context,
  28. userCred mcclient.TokenCredential,
  29. syncResults SSyncResultSet,
  30. provider *SCloudprovider,
  31. localRegion *SCloudregion,
  32. remoteRegion cloudprovider.ICloudRegion,
  33. syncRange *SSyncRange,
  34. ) {
  35. extCacheDBs, err := func() ([]cloudprovider.ICloudElasticcache, error) {
  36. defer syncResults.AddRequestCost(ElasticcacheManager)()
  37. return remoteRegion.GetIElasticcaches()
  38. }()
  39. if err != nil {
  40. msg := fmt.Sprintf("GetIElasticcaches for region %s provider %s failed %s", remoteRegion.GetName(), provider.Name, err)
  41. log.Errorf("%s", msg)
  42. return
  43. }
  44. localInstances, remoteInstances, result := func() ([]SElasticcache, []cloudprovider.ICloudElasticcache, compare.SyncResult) {
  45. defer syncResults.AddSqlCost(ElasticcacheManager)()
  46. return localRegion.SyncElasticcaches(ctx, userCred, provider.GetOwnerId(), provider, extCacheDBs, syncRange.Xor)
  47. }()
  48. syncResults.Add(ElasticcacheManager, result)
  49. msg := result.Result()
  50. log.Infof("SyncElasticcaches for region %s provider %s result: %s", localRegion.Name, provider.Name, msg)
  51. if result.IsError() {
  52. return
  53. }
  54. db.OpsLog.LogEvent(provider, db.ACT_SYNC_CLOUD_ELASTIC_CACHE, msg, userCred)
  55. for i := 0; i < len(localInstances); i++ {
  56. func() {
  57. lockman.LockObject(ctx, &localInstances[i])
  58. defer lockman.ReleaseObject(ctx, &localInstances[i])
  59. syncElasticcacheParameters(ctx, userCred, syncResults, &localInstances[i], remoteInstances[i])
  60. syncElasticcacheAccounts(ctx, userCred, syncResults, &localInstances[i], remoteInstances[i])
  61. syncElasticcacheAcls(ctx, userCred, syncResults, &localInstances[i], remoteInstances[i])
  62. syncElasticcacheBackups(ctx, userCred, syncResults, &localInstances[i], remoteInstances[i])
  63. syncElasticcacheSecgroups(ctx, userCred, syncResults, &localInstances[i], remoteInstances[i])
  64. }()
  65. }
  66. }
  67. func syncElasticcacheParameters(ctx context.Context, userCred mcclient.TokenCredential, syncResults SSyncResultSet, localInstance *SElasticcache, remoteInstance cloudprovider.ICloudElasticcache) {
  68. parameters, err := func() ([]cloudprovider.ICloudElasticcacheParameter, error) {
  69. defer syncResults.AddRequestCost(ElasticcacheParameterManager)()
  70. return remoteInstance.GetICloudElasticcacheParameters()
  71. }()
  72. if err != nil {
  73. msg := fmt.Sprintf("GetIElasticcacheParameters for dbinstance %s failed %s", remoteInstance.GetName(), err)
  74. log.Errorf("%s", msg)
  75. return
  76. }
  77. func() {
  78. defer syncResults.AddSqlCost(ElasticcacheParameterManager)()
  79. result := ElasticcacheParameterManager.SyncElasticcacheParameters(ctx, userCred, localInstance, parameters)
  80. syncResults.Add(ElasticcacheParameterManager, result)
  81. msg := fmt.Sprintf("SyncElasticcacheParameters for dbinstance %s result: %s", localInstance.Name, result.Result())
  82. log.Infof("%s", msg)
  83. if result.IsError() {
  84. return
  85. }
  86. }()
  87. }
  88. func syncElasticcacheAccounts(ctx context.Context, userCred mcclient.TokenCredential, syncResults SSyncResultSet, localInstance *SElasticcache, remoteInstance cloudprovider.ICloudElasticcache) {
  89. accounts, err := func() ([]cloudprovider.ICloudElasticcacheAccount, error) {
  90. defer syncResults.AddRequestCost(ElasticcacheAccountManager)()
  91. return remoteInstance.GetICloudElasticcacheAccounts()
  92. }()
  93. if err != nil {
  94. msg := fmt.Sprintf("GetIElasticcacheAccounts for dbinstance %s failed %s", remoteInstance.GetName(), err)
  95. log.Errorf("%s", msg)
  96. return
  97. }
  98. func() {
  99. defer syncResults.AddSqlCost(ElasticcacheAccountManager)()
  100. result := ElasticcacheAccountManager.SyncElasticcacheAccounts(ctx, userCred, localInstance, accounts)
  101. syncResults.Add(ElasticcacheAccountManager, result)
  102. msg := fmt.Sprintf("SyncElasticcacheAccounts for dbinstance %s result: %s", localInstance.Name, result.Result())
  103. log.Infof("%s", msg)
  104. }()
  105. }
  106. func syncElasticcacheAcls(ctx context.Context, userCred mcclient.TokenCredential, syncResults SSyncResultSet, localInstance *SElasticcache, remoteInstance cloudprovider.ICloudElasticcache) {
  107. acls, err := func() ([]cloudprovider.ICloudElasticcacheAcl, error) {
  108. defer syncResults.AddRequestCost(ElasticcacheAclManager)()
  109. return remoteInstance.GetICloudElasticcacheAcls()
  110. }()
  111. if err != nil {
  112. msg := fmt.Sprintf("GetIElasticcacheAcls for dbinstance %s failed %v", remoteInstance.GetName(), err)
  113. if errors.Cause(err) == cloudprovider.ErrNotSupported {
  114. log.Warningf("%s", msg)
  115. } else {
  116. log.Errorf("%s", msg)
  117. }
  118. return
  119. }
  120. func() {
  121. defer syncResults.AddSqlCost(ElasticcacheAclManager)()
  122. result := ElasticcacheAclManager.SyncElasticcacheAcls(ctx, userCred, localInstance, acls)
  123. syncResults.Add(ElasticcacheAclManager, result)
  124. msg := fmt.Sprintf("SyncElasticcacheAcls for dbinstance %s result: %s", localInstance.Name, result.Result())
  125. log.Infof("%s", msg)
  126. }()
  127. }
  128. func syncElasticcacheBackups(ctx context.Context, userCred mcclient.TokenCredential, syncResults SSyncResultSet, localInstance *SElasticcache, remoteInstance cloudprovider.ICloudElasticcache) {
  129. backups, err := func() ([]cloudprovider.ICloudElasticcacheBackup, error) {
  130. defer syncResults.AddRequestCost(ElasticcacheBackupManager)()
  131. return remoteInstance.GetICloudElasticcacheBackups()
  132. }()
  133. if err != nil {
  134. msg := fmt.Sprintf("GetIElasticcacheBackups for dbinstance %s failed %s", remoteInstance.GetName(), err)
  135. log.Errorf("%s", msg)
  136. return
  137. }
  138. func() {
  139. defer syncResults.AddSqlCost(ElasticcacheBackupManager)()
  140. result := ElasticcacheBackupManager.SyncElasticcacheBackups(ctx, userCred, localInstance, backups)
  141. syncResults.Add(ElasticcacheBackupManager, result)
  142. msg := fmt.Sprintf("SyncElasticcacheBackups for dbinstance %s result: %s", localInstance.Name, result.Result())
  143. log.Infof("%s", msg)
  144. }()
  145. }
  146. func syncElasticcacheSecgroups(ctx context.Context, userCred mcclient.TokenCredential, syncResults SSyncResultSet, localInstance *SElasticcache, remoteInstance cloudprovider.ICloudElasticcache) {
  147. secgroupIds, err := func() ([]string, error) {
  148. defer syncResults.AddRequestCost(ElasticcachesecgroupManager)()
  149. return remoteInstance.GetSecurityGroupIds()
  150. }()
  151. if err != nil {
  152. msg := fmt.Sprintf("Elasticcache.GetSecurityGroupIds for dbinstance %s failed %s", remoteInstance.GetName(), err)
  153. if errors.Cause(err) == cloudprovider.ErrNotSupported {
  154. log.Warningf("%s", msg)
  155. } else {
  156. log.Errorf("%s", msg)
  157. }
  158. return
  159. }
  160. func() {
  161. defer syncResults.AddSqlCost(ElasticcachesecgroupManager)()
  162. result := localInstance.SyncElasticcacheSecgroups(ctx, userCred, secgroupIds)
  163. syncResults.Add(ElasticcachesecgroupManager, result)
  164. msg := fmt.Sprintf("SyncElasticcacheSecgroups for dbinstance %s result: %s", localInstance.Name, result.Result())
  165. log.Infof("%s", msg)
  166. }()
  167. }