forked from databricks/terraform-provider-databricks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontext.go
818 lines (746 loc) · 26.2 KB
/
context.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
package exporter
import (
"bufio"
"context"
"crypto/md5"
"encoding/json"
"fmt"
"log"
"os"
"os/exec"
"regexp"
"sort"
"strings"
"sync"
"time"
"github.com/databricks/databricks-sdk-go"
"github.com/databricks/databricks-sdk-go/service/catalog"
"github.com/databricks/databricks-sdk-go/service/compute"
"golang.org/x/exp/maps"
"github.com/databricks/terraform-provider-databricks/commands"
"github.com/databricks/terraform-provider-databricks/common"
"github.com/databricks/terraform-provider-databricks/internal/providers/sdkv2"
"github.com/databricks/terraform-provider-databricks/scim"
"github.com/databricks/terraform-provider-databricks/workspace"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
/** High level overview of importer design:
+----------+ Add +--------------------+
| resource +-------> stateApproximation |
+--^-------+ +----|-------------|-+
| | |
+------------------------+ | Emit | |
| "normal provider flow" | +------^-----+ +-----V-----+ +---V---+
+------------^-----------+ | importable +--------> reference | | scope |
| +------^-----+ +--------|--+ +---V---+
+--------------+--------------+ | | |
|terraform-provider-databricks| | | |
+--------------+--------------+ | | |
| | List +--V----------V---+
+----------v---------+ +----^------------+ | |
| | | | | Generated |
| importer command +--------> importContext | | HCL Files |
| | | | | |
+--------------------+ +-----------------+ +-----------------+
*/
type resourceChannel chan *resource
type gitInfoCacheEntry struct {
IsPresent bool
RepoId int64
}
type importContext struct {
// not modified/used only in single thread
Module string
Context context.Context
Client *common.DatabricksClient
Importables map[string]importable
Resources map[string]*schema.Resource
Directory string
nameFixes []regexFix
hclFixes []regexFix
variables map[string]string
variablesLock sync.Mutex
workspaceConfKeys map[string]any
workspaceClient *databricks.WorkspaceClient
accountClient *databricks.AccountClient
channels map[string]resourceChannel
defaultChannel resourceChannel
defaultHanlerChannelSize int
// mutable resources
State *stateApproximation
Scope importedResources
// command-line resources (immutable, or set by the single thread)
includeUserDomains bool
importAllUsers bool
exportDeletedUsersAssets bool
incremental bool
mounts bool
noFormat bool
nativeImportSupported bool
services map[string]struct{}
listing map[string]struct{}
match string
matchRegexStr string
matchRegex *regexp.Regexp
excludeRegexStr string
excludeRegex *regexp.Regexp
filterDirectoriesDuringWorkspaceWalking bool
lastActiveDays int64
lastActiveMs int64
generateDeclaration bool
exportSecrets bool
meAdmin bool
meUserName string
prefix string
accountLevel bool
shImports map[string]bool
notebooksFormat string
updatedSinceStr string
updatedSinceMs int64
waitGroup *sync.WaitGroup
// TODO: protect by mutex?
mountMap map[string]mount
testEmits map[string]bool
testEmitsMutex sync.Mutex
allGroups []scim.Group
groupsMutex sync.Mutex
allUsers map[string]scim.User
usersMutex sync.RWMutex
allUsersMapping map[string]string // maps user_name -> internal ID
allUsersMutex sync.RWMutex
allSps map[string]scim.User
allSpsMapping map[string]string // maps application_id -> internal ID
spsMutex sync.RWMutex
importing map[string]bool
importingMutex sync.RWMutex
sqlDatasources map[string]string
sqlDatasourcesMutex sync.Mutex
// workspace-related objects & corresponding mutex
allDirectories []workspace.ObjectStatus
allWorkspaceObjects []workspace.ObjectStatus
wsObjectsMutex sync.RWMutex
oldWorkspaceObjects []workspace.ObjectStatus
oldWorkspaceObjectMapping map[int64]string
gitInfoCache map[string]gitInfoCacheEntry
gitInfoCacheMutex sync.RWMutex
builtInPolicies map[string]compute.PolicyFamily
builtInPoliciesMutex sync.Mutex
// Workspace-level UC Metastore information
currentMetastore *catalog.GetMetastoreSummaryResponse
// tracking ignored objects
ignoredResourcesMutex sync.Mutex
ignoredResources map[string]struct{}
deletedResources map[string]struct{}
// emitting of users/SPs
emittedUsers map[string]struct{}
emittedUsersMutex sync.RWMutex
userOrSpDirectories map[string]bool
userOrSpDirectoriesMutex sync.RWMutex
tfvarsMutex sync.Mutex
tfvars map[string]string
}
type mount struct {
URL string
InstanceProfile string
ClusterID string
}
var nameFixes = []regexFix{
{regexp.MustCompile(`[0-9a-f]{8}[_-][0-9a-f]{4}[_-][0-9a-f]{4}` +
`[_-][0-9a-f]{4}[_-][0-9a-f]{12}[_-]`), ""},
{regexp.MustCompile(`[-\s\.\|]`), "_"},
{regexp.MustCompile(`\W+`), "_"},
{regexp.MustCompile(`[_]{2,}`), "_"},
}
// less aggressive name normalizations
var simpleNameFixes = []regexFix{
{nameNormalizationRegex, "_"},
}
var workspaceConfKeys = map[string]any{
"enableIpAccessLists": false,
"enableTokensConfig": false,
"maxTokenLifetimeDays": 0,
"maxUserInactiveDays": 0,
"storeInteractiveNotebookResultsInCustomerAccount": false,
"enableDeprecatedClusterNamedInitScripts": false,
"enableDeprecatedGlobalInitScripts": false,
"enforceWorkspaceViewAcls": false,
"enforceUserIsolation": false,
"enableProjectTypeInWorkspace": false,
"enableWorkspaceFilesystem": false,
"enableProjectsAllowList": false,
"projectsAllowList": "",
"reposIpynbResultsExportPermissions": "ALLOW",
"enable-X-Frame-Options": false,
"enable-X-Content-Type-Options": false,
"enable-X-XSS-Protection": false,
"enableResultsDownloading": false,
"enableUploadDataUis": false,
"enableExportNotebook": false,
"enableNotebookTableClipboard": false,
"enableWebTerminal": false,
"enableDbfsFileBrowser": false,
"enableDatabricksAutologgingAdminConf": false,
"mlflowRunArtifactDownloadEnabled": false,
"mlflowModelServingEndpointCreationEnabled": false,
"mlflowModelRegistryEmailNotificationsEnabled": false,
"rStudioUserDefaultHomeBase": false,
"enableVerboseAuditLogs": false,
"enableEnforceImdsV2": false,
"enableLibraryAndInitScriptOnSharedCluster": false,
"enablePipelinesDataSample": false,
"customerApprovedWSLoginExpirationTime": false,
"enableLegacyNotebookVisualizations": "indefinite",
"enableJobViewAcls": false,
"enforceClusterViewAcls": false,
}
const (
defaultChannelSize = 100000
defaultNumRoutines = 2
envVariablePrefix = "EXPORTER_PARALLELISM_"
)
// increased concurrency limits, could be also overridden via environment variables with name: envVariablePrefix + resource type
var goroutinesNumber = map[string]int{
"databricks_notebook": 10,
"databricks_directory": 5,
"databricks_workspace_file": 5,
"databricks_dbfs_file": 3,
"databricks_user": 1,
"databricks_service_principal": 1,
"databricks_dashboard": 4,
"databricks_sql_dashboard": 3,
"databricks_sql_widget": 4,
"databricks_sql_visualization": 4,
"databricks_query": 6,
"databricks_alert": 2,
"databricks_permissions": 11,
}
func makeResourcesChannels() map[string]resourceChannel {
resources := []string{"databricks_user", "databricks_service_principal", "databricks_group"}
if val, exists := os.LookupEnv("EXPORTER_DEDICATED_RESOUSE_CHANNELS"); exists {
resources = strings.Split(val, ",")
}
channels := make(map[string]resourceChannel, len(resources))
for _, r := range resources {
channels[r] = make(resourceChannel, defaultChannelSize)
}
return channels
}
func newImportContext(c *common.DatabricksClient) *importContext {
p := sdkv2.DatabricksProvider()
p.TerraformVersion = "exporter"
p.SetMeta(c)
ctx := context.WithValue(context.Background(), common.Provider, p)
ctx = context.WithValue(ctx, common.ResourceName, "exporter")
c.WithCommandExecutor(func(
ctx context.Context,
c *common.DatabricksClient) common.CommandExecutor {
return commands.NewCommandsAPI(ctx, c)
})
defaultHanlerChannelSize := getEnvAsInt("EXPORTER_DEFAULT_HANDLER_CHANNEL_SIZE", defaultChannelSize*3)
supportedResources := maps.Keys(resourcesMap)
return &importContext{
Client: c,
Context: ctx,
State: newStateApproximation(supportedResources),
Importables: resourcesMap,
Resources: p.ResourcesMap,
Scope: importedResources{},
importing: map[string]bool{},
nameFixes: nameFixes,
hclFixes: []regexFix{}, // Be careful with that! it may break working code
variables: map[string]string{},
allDirectories: []workspace.ObjectStatus{},
allWorkspaceObjects: []workspace.ObjectStatus{},
oldWorkspaceObjects: []workspace.ObjectStatus{},
oldWorkspaceObjectMapping: map[int64]string{},
gitInfoCache: map[string]gitInfoCacheEntry{},
workspaceConfKeys: workspaceConfKeys,
shImports: map[string]bool{},
notebooksFormat: "SOURCE",
allUsers: map[string]scim.User{},
allSps: map[string]scim.User{},
waitGroup: &sync.WaitGroup{},
channels: makeResourcesChannels(),
defaultHanlerChannelSize: defaultHanlerChannelSize,
defaultChannel: make(resourceChannel, defaultHanlerChannelSize),
ignoredResources: map[string]struct{}{},
deletedResources: map[string]struct{}{},
emittedUsers: map[string]struct{}{},
userOrSpDirectories: map[string]bool{},
services: map[string]struct{}{},
listing: map[string]struct{}{},
tfvars: map[string]string{},
}
}
func getLastRunString(fileName string) string {
var updatedSinceStr string
statsData, err := os.ReadFile(fileName)
if err == nil {
var m map[string]any
err = json.Unmarshal(statsData, &m)
s, exists := m["startTime"]
if err == nil && exists {
updatedSinceStr = s.(string)
} else {
log.Printf("[WARN] Can't decode data: err=%v or startTime field doesn't exist. data: '%s'",
err, string(statsData))
}
} else {
log.Printf("[WARN] Can't load data from file %s: %v", fileName, err)
}
return updatedSinceStr
}
func (ic *importContext) Run() error {
startTime := time.Now()
statsFileName := ic.Directory + "/exporter-run-stats.json"
wsObjectsFileName := ic.Directory + "/ws_objects.json"
if len(ic.services) == 0 {
return fmt.Errorf("no services to import")
}
if ic.matchRegexStr != "" {
log.Printf("[DEBUG] Using regex '%s' to filter resources", ic.matchRegexStr)
re, err := regexp.Compile(ic.matchRegexStr)
if err != nil {
log.Printf("[ERROR] can't compile regex '%s': %v", ic.matchRegexStr, err)
return err
}
ic.matchRegex = re
}
if ic.excludeRegexStr != "" {
log.Printf("[DEBUG] Using regex '%s' to filter resources", ic.excludeRegexStr)
re, err := regexp.Compile(ic.excludeRegexStr)
if err != nil {
log.Printf("[ERROR] can't compile regex '%s': %v", ic.excludeRegexStr, err)
return err
}
ic.excludeRegex = re
}
if ic.incremental {
if ic.updatedSinceStr == "" {
ic.updatedSinceStr = getLastRunString(statsFileName)
}
if ic.updatedSinceStr == "" {
return fmt.Errorf("-updated-since is required with -interactive parameter if %s file doesn't exist",
statsFileName)
}
tm, err := time.Parse(time.RFC3339, ic.updatedSinceStr)
if err != nil {
return fmt.Errorf("can't parse value '%s' please specify it in ISO8601 format, i.e. 2023-07-01T00:00:00Z",
ic.updatedSinceStr)
}
ic.updatedSinceStr = tm.UTC().Format(time.RFC3339)
tm, _ = time.Parse(time.RFC3339, ic.updatedSinceStr)
ic.updatedSinceMs = tm.UnixMilli()
ic.loadOldWorkspaceObjects(wsObjectsFileName)
}
log.Printf("[INFO] Importing %s module into %s directory Databricks resources of %s services. Listing %s",
ic.Module, ic.Directory, maps.Keys(ic.services), maps.Keys(ic.listing))
ic.notebooksFormat = strings.ToUpper(ic.notebooksFormat)
_, supportedFormat := fileExtensionFormatMapping[ic.notebooksFormat]
if !supportedFormat && ic.notebooksFormat != "SOURCE" {
return fmt.Errorf("unsupported notebook format: '%s'", ic.notebooksFormat)
}
info, err := os.Stat(ic.Directory)
if os.IsNotExist(err) {
err = os.MkdirAll(ic.Directory, 0755)
if err != nil {
return fmt.Errorf("can't create directory %s", ic.Directory)
}
} else if !info.IsDir() {
return fmt.Errorf("the path %s is not a directory", ic.Directory)
}
ic.accountLevel = ic.Client.Config.IsAccountClient()
if ic.accountLevel {
ic.meAdmin = true
// TODO: check if we can get the current user from the account client
ic.accountClient, err = ic.Client.AccountClient()
if err != nil {
return err
}
} else {
ic.workspaceClient, err = ic.Client.WorkspaceClient()
if err != nil {
return err
}
me, err := ic.workspaceClient.CurrentUser.Me(ic.Context)
if err != nil {
return err
}
ic.meUserName = me.UserName
for _, g := range me.Groups {
if g.Display == "admins" {
ic.meAdmin = true
break
}
}
currentMetastore, err := ic.workspaceClient.Metastores.Summary(ic.Context)
if err == nil {
ic.currentMetastore = currentMetastore
} else {
log.Printf("[WARN] can't get current UC metastore: %v", err)
}
}
// Concurrent execution part
if ic.waitGroup == nil {
ic.waitGroup = &sync.WaitGroup{}
}
// Start goroutines for each resource type
ic.startImportChannels()
// Start listing of objects
listWorkspaceObjectsAlreadyRunning := false
for rnLoop, irLoop := range ic.Importables {
resourceName := rnLoop
ir := irLoop
// TODO: extend this to other services? Like, Git Folders
if !ic.accountLevel && (ir.Service == "notebooks" || ir.Service == "wsfiles" || (ir.Service == "directories" && !ic.incremental)) {
if _, exists := ic.listing[ir.Service]; exists && !listWorkspaceObjectsAlreadyRunning {
ic.waitGroup.Add(1)
log.Printf("[DEBUG] Starting listing of workspace objects")
go func() {
if err := listWorkspaceObjects(ic); err != nil {
log.Printf("[ERROR] listing of workspace objects failed %s", err)
}
log.Print("[DEBUG] Finished listing of workspace objects")
ic.waitGroup.Done()
}()
listWorkspaceObjectsAlreadyRunning = true
}
continue
}
if ir.List == nil {
continue
}
if _, exists := ic.listing[ir.Service]; !exists {
log.Printf("[DEBUG] %s (%s service) is not part of listing", resourceName, ir.Service)
continue
}
if ic.accountLevel && !ir.AccountLevel {
log.Printf("[DEBUG] %s (%s service) is not a account level resource", resourceName, ir.Service)
continue
}
if !ic.accountLevel && !ir.WorkspaceLevel {
log.Printf("[DEBUG] %s (%s service) is not a workspace level resource", resourceName, ir.Service)
continue
}
ic.waitGroup.Add(1)
go func() {
if err := ir.List(ic); err != nil {
log.Printf("[ERROR] %s (%s service) listing failed: %s", resourceName, ir.Service, err)
}
log.Printf("[DEBUG] Finished listing for service %s", resourceName)
ic.waitGroup.Done()
}()
}
ic.waitGroup.Wait()
// close channels
ic.closeImportChannels()
// Generating the code
ic.findDeletedResources()
if ic.Scope.Len() == 0 && len(ic.deletedResources) == 0 {
return fmt.Errorf("no resources to import or delete")
}
shFileName := fmt.Sprintf("%s/import.sh", ic.Directory)
if ic.incremental {
shFile, err := os.Open(shFileName)
if err == nil {
defer shFile.Close()
fileScanner := bufio.NewScanner(shFile)
fileScanner.Split(bufio.ScanLines)
for fileScanner.Scan() {
line := fileScanner.Text()
if strings.HasPrefix(line, "terraform import ") {
ic.shImports[strings.TrimRight(line, "\n")] = true
}
}
} else {
log.Printf("[ERROR] opening %s: %v", shFileName, err)
}
}
sh, err := os.OpenFile(shFileName, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0755)
if err != nil {
return err
}
defer sh.Close()
// nolint
sh.WriteString("#!/bin/sh\n\nset -e\n\n")
if ic.generateDeclaration {
dcfile, err := os.Create(fmt.Sprintf("%s/databricks.tf", ic.Directory))
if err != nil {
return err
}
// nolint
dcfile.WriteString(
`terraform {
required_providers {
databricks = {
source = "databricks/databricks"
version = "` + common.Version() + `"
}
}
}
provider "databricks" {
`)
if ic.accountLevel {
dcfile.WriteString(fmt.Sprintf(` host = "%s"
account_id = "%s"
`, ic.Client.Config.Host, ic.Client.Config.AccountID))
}
dcfile.WriteString(`}`)
dcfile.Close()
}
//
ic.generateAndWriteResources(sh)
err = ic.generateVariables()
if err != nil {
log.Printf("[ERROR] can't write variables file: %s", err.Error())
}
err = ic.generateTfvars()
if err != nil {
log.Printf("[ERROR] can't write terraform.tfvars file: %s", err.Error())
}
// Write stats file
if stats, err := os.Create(statsFileName); err == nil {
defer stats.Close()
statsData := map[string]any{
"startTime": startTime.UTC().Format(time.RFC3339),
"duration": fmt.Sprintf("%f sec", time.Since(startTime).Seconds()),
"exportedObjects": ic.Scope.Len(),
}
statsBytes, _ := json.Marshal(statsData)
if _, err = stats.Write(statsBytes); err != nil {
log.Printf("[ERROR] can't write stats into the %s: %s", statsFileName, err.Error())
}
}
// Write workspace objects file
if len(ic.allWorkspaceObjects) > 0 {
if wsObjects, err := os.Create(wsObjectsFileName); err == nil {
defer wsObjects.Close()
wsObjectsBytes, _ := json.Marshal(ic.allWorkspaceObjects)
if _, err = wsObjects.Write(wsObjectsBytes); err != nil {
log.Printf("[ERROR] can't write workspace objects into the %s: %s", wsObjectsFileName, err.Error())
}
} else {
log.Printf("[ERROR] can't open %s: %s", wsObjectsFileName, err.Error())
}
}
// output ignored resources...
ignoredResourcesFileName := fmt.Sprintf("%s/ignored_resources.txt", ic.Directory)
if ignored, err := os.Create(ignoredResourcesFileName); err == nil {
defer ignored.Close()
ic.ignoredResourcesMutex.Lock()
keys := maps.Keys(ic.ignoredResources)
sort.Strings(keys)
for _, s := range keys {
ignored.WriteString(s + "\n")
}
ic.ignoredResourcesMutex.Unlock()
} else {
log.Printf("[ERROR] can't open %s: %s", ignoredResourcesFileName, err.Error())
}
if !ic.noFormat {
// format generated source code
cmd := exec.CommandContext(context.Background(), "terraform", "fmt")
cmd.Dir = ic.Directory
err = cmd.Run()
if err != nil {
log.Printf("[ERROR] problems when formatting the generated code: %v", err)
return err
}
}
log.Printf("[INFO] Done. Please edit the files and roll out new environment.")
return nil
}
func (ic *importContext) resourceHandler(num int, resourceType string, ch resourceChannel) {
log.Printf("[DEBUG] Starting goroutine %d for resource %s", num, resourceType)
for r := range ch {
log.Printf("[DEBUG] channel for %s, channel size=%d got %v", resourceType, len(ch), r)
if r != nil {
r.ImportResource(ic)
log.Printf("[DEBUG] Finished importing %s, %v", resourceType, r)
}
}
}
func (ic *importContext) startImportChannels() {
for rt, c := range ic.channels {
ch := c
resourceType := rt
numRoutines, exists := goroutinesNumber[resourceType]
if !exists {
numRoutines = defaultNumRoutines
}
numRoutines = getEnvAsInt(envVariablePrefix+resourceType, numRoutines)
for i := 0; i < numRoutines; i++ {
num := i
go func() {
ic.resourceHandler(num, resourceType, ch)
}()
}
}
numRoutines := getEnvAsInt(envVariablePrefix+"default", 15)
for i := 0; i < numRoutines; i++ {
num := i
go func() {
ic.resourceHandler(num, "default", ic.defaultChannel)
}()
}
}
func (ic *importContext) closeImportChannels() {
for rt, ch := range ic.channels {
log.Printf("[DEBUG] Closing channel for resource %s", rt)
close(ch)
}
close(ic.defaultChannel)
}
func (ic *importContext) HasInState(r *resource) bool {
return ic.State.Has(r)
}
func (ic *importContext) Add(r *resource) {
if ic.HasInState(r) { // resource must exist in the state
return
}
rString := r.String()
ic.importingMutex.Lock()
isAdded, ok := ic.importing[rString]
if ok && isAdded {
ic.importingMutex.Unlock()
log.Printf("[DEBUG] %s is already added", rString)
return
}
ic.importing[rString] = true // mark resource as added
ic.importingMutex.Unlock()
state := r.Data.State()
if state == nil {
log.Printf("[ERROR] state is nil for %s", r)
return
}
inst := instanceApproximation{
Attributes: map[string]any{},
}
for k, v := range state.Attributes {
inst.Attributes[k] = v
}
if r.Mode == "" {
r.Mode = "managed"
}
inst.Attributes["id"] = r.ID
ic.State.Append(resourceApproximation{
Mode: r.Mode,
Type: r.Resource,
Name: r.Name,
Instances: []instanceApproximation{inst},
Resource: r,
})
ic.Scope.Append(r)
}
func (ic *importContext) regexFix(s string, fixes []regexFix) string {
for _, x := range fixes {
s = x.Regex.ReplaceAllString(s, x.Replacement)
}
return s
}
func (ic *importContext) ResourceName(r *resource) string {
name := r.Name
if name == "" && ic.Importables[r.Resource].Name != nil {
name = ic.Importables[r.Resource].Name(ic, r.Data)
}
if name == "" {
name = r.ID
}
name = ic.prefix + name
origCaseName := name
name = strings.ToLower(name)
name = ic.regexFix(name, ic.nameFixes)
// this is either numeric id or all-non-ascii
if regexp.MustCompile(`^\d`).MatchString(name) || name == "" {
if name == "" {
origCaseName = r.ID
}
name = fmt.Sprintf("r%x", md5.Sum([]byte(origCaseName)))[0:12]
}
return name
}
func (ic *importContext) EmitIfUpdatedAfterMillis(r *resource, modifiedAt int64, message string) {
updatedSinceMs := ic.getUpdatedSinceMs()
if ic.incremental && modifiedAt < updatedSinceMs {
log.Printf("[DEBUG] skipping %s that was modified at %d (last active=%d)",
message, modifiedAt, updatedSinceMs)
return
}
ic.Emit(r)
}
func (ic *importContext) EmitIfUpdatedAfterMillisAndNameMatches(r *resource, name string, modifiedAt int64, message string) {
if ic.MatchesName(name) {
ic.EmitIfUpdatedAfterMillis(r, modifiedAt, message)
}
}
func (ic *importContext) EmitIfUpdatedAfterIsoString(r *resource, updatedAt, message string) {
updatedSinceStr := ic.getUpdatedSinceStr()
if ic.incremental && updatedAt < updatedSinceStr {
log.Printf("[DEBUG] skipping %s that was modified at %s (updatedSince=%s)", message,
updatedAt, updatedSinceStr)
return
}
ic.Emit(r)
}
func (ic *importContext) Emit(r *resource) {
_, v := r.MatchPair()
if v == "" {
log.Printf("[DEBUG] %s has got empty identifier", r)
return
}
ir, ok := ic.Importables[r.Resource]
if !ok {
log.Printf("[ERROR] %s is not available for import", r)
return
}
if !ic.isServiceEnabled(ir.Service) {
log.Printf("[DEBUG] %s (%s service) is not part of the import", r.Resource, ir.Service)
return
}
rString := r.String()
if ic.testEmits != nil {
log.Printf("[INFO] %s is emitted in test mode", r)
ic.testEmitsMutex.Lock()
ic.testEmits[rString] = true
ic.testEmitsMutex.Unlock()
return
}
// we need to check that we're not importing the same resource twice - this may happen
// under high concurrency for specific resources, for example, directories when they
// aren't part of the listing
ic.importingMutex.Lock()
res, ok := ic.importing[rString]
if ok {
ic.importingMutex.Unlock()
log.Printf("[DEBUG] %s already being imported: %v", rString, res)
return
}
ic.importing[rString] = false // we're starting to add a new resource
ic.importingMutex.Unlock()
_, ok = ic.Resources[r.Resource]
if !ok {
log.Printf("[ERROR] %s is not available in provider", r)
return
}
if ic.accountLevel && !ir.AccountLevel {
log.Printf("[DEBUG] %s (%s service) is not part of the account level export",
r.Resource, ir.Service)
return
}
if !ic.accountLevel && !ir.WorkspaceLevel {
log.Printf("[DEBUG] %s (%s service) is not part of the workspace level export",
r.Resource, ir.Service)
return
}
// from here, it should be done by the goroutine... send resource into the channel
ch, exists := ic.channels[r.Resource]
if exists {
log.Printf("[TRACE] increasing counter & sending to the channel for resource %s", r.Resource)
ic.waitGroup.Add(1)
ch <- r
} else {
log.Print("[TRACE] increasing counter & sending to the default channel")
ic.waitGroup.Add(1)
ic.defaultChannel <- r
}
}