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