@@ -30,7 +30,7 @@ func Generate(ctx context.Context, cfg *Config) error {
30
30
return fmt .Errorf ("failed to delete %s: %s" , cfg .OutputDir , err )
31
31
}
32
32
} else if err == nil && ! cfg .Clobber {
33
- return fmt .Errorf ("output dir %q already exists. Use -- clobber to delete it" , cfg .OutputDir )
33
+ return fmt .Errorf ("output dir %q already exists. Use the clobber option to delete it" , cfg .OutputDir )
34
34
}
35
35
36
36
log .Printf ("Generating resources to %s" , cfg .OutputDir )
@@ -62,7 +62,7 @@ func Generate(ctx context.Context, cfg *Config) error {
62
62
}
63
63
64
64
for _ , stack := range stacks {
65
- if err := generateGrafanaResources (ctx , stack .managementKey , stack .url , "stack-" + stack .slug , false , cfg .OutputDir , stack .smURL , stack .smToken ); err != nil {
65
+ if err := generateGrafanaResources (ctx , stack .managementKey , stack .url , "stack-" + stack .slug , false , cfg .OutputDir , stack .smURL , stack .smToken , cfg . IncludeResources ); err != nil {
66
66
return err
67
67
}
68
68
}
@@ -78,7 +78,7 @@ func Generate(ctx context.Context, cfg *Config) error {
78
78
if net .ParseIP (stackName ) != nil {
79
79
stackName = "ip_" + strings .ReplaceAll (stackName , "." , "_" )
80
80
}
81
- if err := generateGrafanaResources (ctx , cfg .Grafana .Auth , cfg .Grafana .URL , stackName , true , cfg .OutputDir , "" , "" ); err != nil {
81
+ if err := generateGrafanaResources (ctx , cfg .Grafana .Auth , cfg .Grafana .URL , stackName , true , cfg .OutputDir , "" , "" , cfg . IncludeResources ); err != nil {
82
82
return err
83
83
}
84
84
}
@@ -93,7 +93,12 @@ func Generate(ctx context.Context, cfg *Config) error {
93
93
return nil
94
94
}
95
95
96
- func generateImportBlocks (ctx context.Context , client * common.Client , listerData any , resources []* common.Resource , outPath , provider string ) error {
96
+ func generateImportBlocks (ctx context.Context , client * common.Client , listerData any , resources []* common.Resource , outPath , provider string , includedResources []string ) error {
97
+ resources , err := filterResources (resources , includedResources )
98
+ if err != nil {
99
+ return err
100
+ }
101
+
97
102
// Generate HCL blocks in parallel with a wait group
98
103
wg := sync.WaitGroup {}
99
104
wg .Add (len (resources ))
@@ -132,20 +137,32 @@ func generateImportBlocks(ctx context.Context, client *common.Client, listerData
132
137
// to = aws_iot_thing.bar
133
138
// id = "foo"
134
139
// }
135
- blocks := make ( []* hclwrite.Block , len ( ids ))
136
- for i , id := range ids {
140
+ var blocks []* hclwrite.Block
141
+ for _ , id := range ids {
137
142
cleanedID := allowedTerraformChars .ReplaceAllString (id , "_" )
138
143
if provider != "cloud" {
139
144
cleanedID = strings .ReplaceAll (provider , "-" , "_" ) + "_" + cleanedID
140
145
}
141
146
147
+ matched , err := filterResourceByName (resource .Name , cleanedID , includedResources )
148
+ if err != nil {
149
+ wg .Done ()
150
+ results <- result {
151
+ resource : resource ,
152
+ err : err ,
153
+ }
154
+ return
155
+ }
156
+ if ! matched {
157
+ continue
158
+ }
159
+
142
160
b := hclwrite .NewBlock ("import" , nil )
143
161
b .Body ().SetAttributeTraversal ("provider" , traversal ("grafana" , provider ))
144
162
b .Body ().SetAttributeTraversal ("to" , traversal (resource .Name , cleanedID ))
145
163
b .Body ().SetAttributeValue ("id" , cty .StringVal (id ))
146
164
147
- blocks [i ] = b
148
- // TODO: Match and update existing import blocks
165
+ blocks = append (blocks , b )
149
166
}
150
167
151
168
wg .Done ()
@@ -189,3 +206,49 @@ func generateImportBlocks(ctx context.Context, client *common.Client, listerData
189
206
190
207
return sortResourcesFile (filepath .Join (outPath , generatedFilename ))
191
208
}
209
+
210
+ func filterResources (resources []* common.Resource , includedResources []string ) ([]* common.Resource , error ) {
211
+ if len (includedResources ) == 0 {
212
+ return resources , nil
213
+ }
214
+
215
+ filteredResources := []* common.Resource {}
216
+ allowedResourceTypes := []string {}
217
+ for _ , included := range includedResources {
218
+ if ! strings .Contains (included , "." ) {
219
+ return nil , fmt .Errorf ("included resource %q is not in the format <type>.<name>" , included )
220
+ }
221
+ allowedResourceTypes = append (allowedResourceTypes , strings .Split (included , "." )[0 ])
222
+ }
223
+
224
+ for _ , resource := range resources {
225
+ for _ , allowedResourceType := range allowedResourceTypes {
226
+ matched , err := filepath .Match (allowedResourceType , resource .Name )
227
+ if err != nil {
228
+ return nil , err
229
+ }
230
+ if matched {
231
+ filteredResources = append (filteredResources , resource )
232
+ break
233
+ }
234
+ }
235
+ }
236
+ return filteredResources , nil
237
+ }
238
+
239
+ func filterResourceByName (resourceType , resourceName string , includedResources []string ) (bool , error ) {
240
+ if len (includedResources ) == 0 {
241
+ return true , nil
242
+ }
243
+
244
+ for _ , included := range includedResources {
245
+ matched , err := filepath .Match (included , resourceType + "." + resourceName )
246
+ if err != nil {
247
+ return false , err
248
+ }
249
+ if matched {
250
+ return true , nil
251
+ }
252
+ }
253
+ return false , nil
254
+ }
0 commit comments