@@ -15,6 +15,10 @@ func resourceComputeUrlMap() *schema.Resource {
15
15
Update : resourceComputeUrlMapUpdate ,
16
16
Delete : resourceComputeUrlMapDelete ,
17
17
18
+ Importer : & schema.ResourceImporter {
19
+ State : resourceComputeUrlMapImportState ,
20
+ },
21
+
18
22
Schema : map [string ]* schema.Schema {
19
23
"default_service" : & schema.Schema {
20
24
Type : schema .TypeString ,
@@ -317,112 +321,63 @@ func resourceComputeUrlMapRead(d *schema.ResourceData, meta interface{}) error {
317
321
d .Set ("self_link" , urlMap .SelfLink )
318
322
d .Set ("map_id" , strconv .FormatUint (urlMap .Id , 10 ))
319
323
d .Set ("fingerprint" , urlMap .Fingerprint )
324
+ d .Set ("default_service" , urlMap .DefaultService )
320
325
321
- hostRuleMap := make (map [string ]* compute.HostRule )
322
- for _ , v := range urlMap .HostRules {
323
- hostRuleMap [v .PathMatcher ] = v
324
- }
325
-
326
- /* Only read host rules into our TF state that we have defined */
327
- _hostRules := d .Get ("host_rule" ).(* schema.Set ).List ()
328
326
_newHostRules := make ([]interface {}, 0 )
329
- for _ , v := range _hostRules {
330
- _hostRule := v .(map [string ]interface {})
331
- _pathMatcher := _hostRule ["path_matcher" ].(string )
332
-
333
- /* Delete local entries that are no longer found on the GCE server */
334
- if hostRule , ok := hostRuleMap [_pathMatcher ]; ok {
335
- _newHostRule := make (map [string ]interface {})
336
- _newHostRule ["path_matcher" ] = _pathMatcher
337
-
338
- hostsSet := make (map [string ]bool )
339
- for _ , host := range hostRule .Hosts {
340
- hostsSet [host ] = true
341
- }
327
+ for _ , hostRule := range urlMap .HostRules {
328
+ _newHostRule := make (map [string ]interface {})
329
+ _newHostRule ["path_matcher" ] = hostRule .PathMatcher
342
330
343
- /* Only store hosts we are keeping track of */
344
- _newHosts := make ([]interface {}, 0 )
345
- for _ , vp := range _hostRule ["hosts" ].([]interface {}) {
346
- if _ , okp := hostsSet [vp .(string )]; okp {
347
- _newHosts = append (_newHosts , vp )
348
- }
349
- }
331
+ _newHosts := make ([]interface {}, 0 )
332
+ for _ , host := range hostRule .Hosts {
333
+ _newHosts = append (_newHosts , host )
334
+ }
350
335
351
- _newHostRule ["hosts" ] = _newHosts
352
- _newHostRule ["description" ] = hostRule .Description
336
+ _newHostRule ["hosts" ] = _newHosts
337
+ _newHostRule ["description" ] = hostRule .Description
353
338
354
- _newHostRules = append (_newHostRules , _newHostRule )
355
- }
339
+ _newHostRules = append (_newHostRules , _newHostRule )
356
340
}
357
341
358
342
d .Set ("host_rule" , _newHostRules )
359
343
360
- pathMatcherMap := make (map [string ]* compute.PathMatcher )
361
- for _ , v := range urlMap .PathMatchers {
362
- pathMatcherMap [v .Name ] = v
363
- }
364
-
365
- /* Only read path matchers into our TF state that we have defined */
366
- _pathMatchers := d .Get ("path_matcher" ).([]interface {})
367
344
_newPathMatchers := make ([]interface {}, 0 )
368
- for _ , v := range _pathMatchers {
369
- _pathMatcher := v .(map [string ]interface {})
370
- _name := _pathMatcher ["name" ].(string )
371
-
372
- if pathMatcher , ok := pathMatcherMap [_name ]; ok {
373
- _newPathMatcher := make (map [string ]interface {})
374
- _newPathMatcher ["name" ] = _name
375
- _newPathMatcher ["default_service" ] = pathMatcher .DefaultService
376
- _newPathMatcher ["description" ] = pathMatcher .Description
377
-
378
- _newPathRules := make ([]interface {}, len (pathMatcher .PathRules ))
379
- for ip , pathRule := range pathMatcher .PathRules {
380
- _newPathRule := make (map [string ]interface {})
381
- _newPathRule ["service" ] = pathRule .Service
382
- _paths := make ([]interface {}, len (pathRule .Paths ))
383
-
384
- for ipp , vpp := range pathRule .Paths {
385
- _paths [ipp ] = vpp
386
- }
387
-
388
- _newPathRule ["paths" ] = _paths
389
-
390
- _newPathRules [ip ] = _newPathRule
345
+ for _ , pathMatcher := range urlMap .PathMatchers {
346
+ _newPathMatcher := make (map [string ]interface {})
347
+ _newPathMatcher ["name" ] = pathMatcher .Name
348
+ _newPathMatcher ["default_service" ] = pathMatcher .DefaultService
349
+ _newPathMatcher ["description" ] = pathMatcher .Description
350
+
351
+ _newPathRules := make ([]interface {}, len (pathMatcher .PathRules ))
352
+ for ip , pathRule := range pathMatcher .PathRules {
353
+ _newPathRule := make (map [string ]interface {})
354
+ _newPathRule ["service" ] = pathRule .Service
355
+ _paths := make ([]interface {}, len (pathRule .Paths ))
356
+
357
+ for ipp , vpp := range pathRule .Paths {
358
+ _paths [ipp ] = vpp
391
359
}
392
360
393
- _newPathMatcher ["path_rule" ] = _newPathRules
394
- _newPathMatchers = append (_newPathMatchers , _newPathMatcher )
361
+ _newPathRule ["paths" ] = _paths
362
+
363
+ _newPathRules [ip ] = _newPathRule
395
364
}
396
- }
397
365
398
- d .Set ("path_matcher" , _newPathMatchers )
366
+ _newPathMatcher ["path_rule" ] = _newPathRules
367
+ _newPathMatchers = append (_newPathMatchers , _newPathMatcher )
399
368
400
- testMap := make (map [string ]* compute.UrlMapTest )
401
- for _ , v := range urlMap .Tests {
402
- testMap [fmt .Sprintf ("%s/%s" , v .Host , v .Path )] = v
403
369
}
404
370
405
- _tests := make ([]interface {}, 0 )
406
- /* Only read tests into our TF state that we have defined */
407
- if v , ok := d .GetOk ("test" ); ok {
408
- _tests = v .([]interface {})
409
- }
371
+ d .Set ("path_matcher" , _newPathMatchers )
372
+
410
373
_newTests := make ([]interface {}, 0 )
411
- for _ , v := range _tests {
412
- _test := v .(map [string ]interface {})
413
- _host := _test ["host" ].(string )
414
- _path := _test ["path" ].(string )
415
-
416
- /* Delete local entries that are no longer found on the GCE server */
417
- if test , ok := testMap [fmt .Sprintf ("%s/%s" , _host , _path )]; ok {
418
- _newTest := make (map [string ]interface {})
419
- _newTest ["host" ] = _host
420
- _newTest ["path" ] = _path
421
- _newTest ["description" ] = test .Description
422
- _newTest ["service" ] = test .Service
423
-
424
- _newTests = append (_newTests , _newTest )
425
- }
374
+ for _ , test := range urlMap .Tests {
375
+ _newTest := make (map [string ]interface {})
376
+ _newTest ["host" ] = test .Host
377
+ _newTest ["path" ] = test .Path
378
+ _newTest ["description" ] = test .Description
379
+ _newTest ["service" ] = test .Service
380
+ _newTests = append (_newTests , _newTest )
426
381
}
427
382
428
383
d .Set ("test" , _newTests )
@@ -676,6 +631,11 @@ func resourceComputeUrlMapDelete(d *schema.ResourceData, meta interface{}) error
676
631
return nil
677
632
}
678
633
634
+ func resourceComputeUrlMapImportState (d * schema.ResourceData , meta interface {}) ([]* schema.ResourceData , error ) {
635
+ d .Set ("name" , d .Id ())
636
+ return []* schema.ResourceData {d }, nil
637
+ }
638
+
679
639
func validateHostRules (v interface {}, k string ) (ws []string , es []error ) {
680
640
pathMatchers := make (map [string ]bool )
681
641
hostRules := v .([]interface {})
0 commit comments