@@ -29,11 +29,7 @@ def bucket-clone [
29
29
}
30
30
if ( $with_indexes ) {
31
31
let indexes = query indexes -- definitions -- disable-context -- clusters $p.src | where bucket == $p.src_bucket
32
- for index in $indexes {
33
- print $" Recreating index ($index.name ) on cluster ($p.dest ) with: "
34
- print $index.definition
35
- query $index.definition -- disable-context -- clusters $p.dest
36
- }
32
+ $indexes | create-indexes $p.dest
37
33
}
38
34
} -- bucket $bucket -- destbucket $destbucket -- source $source -- destination $destination
39
35
}
@@ -128,12 +124,7 @@ def copy-bucket-definition [
128
124
] {
129
125
run_with_default { |p |
130
126
let clonable = buckets get -- clusters $p.src $p.src_bucket | get 0
131
- print $" Create Bucket ($p.dest )_($p.dest_bucket ) with ($clonable.ram_quota ) quota, type ($clonable.type ), ($clonable.replicas ) replicas, ($clonable.min_durability_level ) durability, ($clonable.max_expiry ) expiry"
132
- if ( $clonable.flush_enabled ) {
133
- $clonable | buckets create $p.dest_bucket ( $in.ram_quota / 1MB | into int ) -- clusters $p.dest -- type $in.type -- replicas $in.replicas -- durability $in.min_durability_level -- expiry $in.max_expiry -- flush
134
- } else {
135
- $clonable | buckets create $p.dest_bucket ( $in.ram_quota / 1MB | into int ) -- clusters $p.dest -- type $in.type -- replicas $in.replicas -- durability $in.min_durability_level -- expiry $in.max_expiry
136
- }
127
+ $clonable | _create-bucket-definition $p.dest
137
128
} -- bucket $bucket -- destbucket $destbucket -- source $source -- destination $destination
138
129
}
139
130
@@ -225,3 +216,86 @@ def run_with_default [
225
216
}
226
217
do $operation $params
227
218
}
219
+
220
+ # Exports all buckets, scopes and collections structure
221
+ # for the given cluster
222
+ def export-cluster-struct [
223
+ source : string # The cluster to export
224
+ ] {
225
+ mut export = []
226
+
227
+ let buckets = buckets -- clusters $source
228
+
229
+ for bucket in $buckets {
230
+ mut scope_structs = []
231
+
232
+ let scopes = scopes -- clusters $source -- bucket $bucket.name
233
+
234
+ for scope in $scopes {
235
+ let collections = (collections -- clusters $source -- bucket $bucket.name -- scope $scope.scope | reject - i cluster )
236
+
237
+ # push scope + its collections into scope_structs
238
+ $scope_structs ++= [{
239
+ scope : $scope.scope ,
240
+ collections : $collections
241
+ }]
242
+ }
243
+
244
+ # push bucket + its scopes into export
245
+ let buc = ( $bucket | merge {scopes : $scope_structs } )
246
+ $export ++= [ $buc ]
247
+ }
248
+
249
+ let indexes = query indexes -- definitions -- disable-context -- clusters $source
250
+ let output = {
251
+ buckets : $export ,
252
+ indexes : $indexes
253
+ }
254
+ return $output
255
+ }
256
+
257
+
258
+ # Import all buckets, scopes and collections structure
259
+ # in the given cluster
260
+ def import-cluster-struct [
261
+ destination : string # The cluster to export
262
+ ] {
263
+ let structure = $in
264
+ let buckets = $structure.buckets
265
+ for bucket in $buckets {
266
+ $bucket | _create-bucket-definition $destination
267
+ for scope in ($bucket.scopes | where not ( $it.scope | str starts-with " _" ) ) {
268
+ print $" Create scope ($destination )_($bucket.name )_($scope.scope )"
269
+ scopes create -- clusters $destination -- bucket $bucket.name $scope.scope
270
+ for col in $scope.collections {
271
+ print $" Create collection ($destination )_($bucket.name )_($scope.scope )_($col.collection )"
272
+ collections create -- clusters $destination -- bucket $bucket.name -- scope $scope.scope $col.collection
273
+ }
274
+ }
275
+ }
276
+ let indexes = $structure.indexes
277
+ $indexes | _create-indexes $destination
278
+ }
279
+
280
+ def _create-indexes [
281
+ destination : string # the cluster where to create indexes
282
+ ] {
283
+ let indexes = $in
284
+ for index in $indexes {
285
+ print $" Recreating index ($index.name ) on cluster ($destination ) with: "
286
+ print $index.definition
287
+ query $index.definition -- disable-context -- clusters $destination
288
+ }
289
+ }
290
+
291
+ def _create-bucket-definition [
292
+ destination : string # the cluster where to create indexes
293
+ ] {
294
+ let bucket = $in
295
+ print $" Create Bucket ($destination )_($bucket.name ) with ($bucket.ram_quota / 1024 / 1024 ) quota, type ($bucket.type ), ($bucket.replicas ) replicas, ($bucket.min_durability_level ) durability, ($bucket.max_expiry ) expiry"
296
+ if ( $bucket.flush_enabled ) {
297
+ $bucket | buckets create $in.name ( $in.ram_quota / 1024 / 1024 | into int ) -- clusters $destination -- type $in.type -- replicas $in.replicas -- durability $in.min_durability_level -- expiry $in.max_expiry -- flush
298
+ } else {
299
+ $bucket | buckets create $in.name ( $in.ram_quota / 1024 / 1024 | into int ) -- clusters $destination -- type $in.type -- replicas $in.replicas -- durability $in.min_durability_level -- expiry $in.max_expiry
300
+ }
301
+ }
0 commit comments