@@ -378,3 +378,81 @@ func remove_quote(v interface{}) interface{} {
378378func bZero (v interface {}) bool {
379379 return reflect .ValueOf (v ).IsZero ()
380380}
381+
382+ func mergeBlock (tf_list , rsp_list []interface {}, tf_mkey , api_mkey string ) []interface {} {
383+ result := []interface {}{}
384+ mkey_index_map := make (map [string ]int )
385+
386+ // create mkey to index map
387+ for i , raw := range rsp_list {
388+ if raw == nil {
389+ continue
390+ }
391+ item := raw .(map [string ]interface {})
392+
393+ keyStr := fmt .Sprintf ("%v" , item [api_mkey ])
394+ mkey_index_map [keyStr ] = i
395+ }
396+
397+ // parse item in terraform configuration
398+ zeroCount := 0
399+ for _ , raw := range tf_list {
400+ if raw == nil {
401+ continue
402+ }
403+ item := raw .(map [string ]interface {})
404+
405+ keyStr := fmt .Sprintf ("%v" , item [tf_mkey ])
406+
407+ if rspIndex , ok := mkey_index_map [keyStr ]; ok {
408+ rspItem := rsp_list [rspIndex ].(map [string ]interface {})
409+ rspItem ["tf_exist" ] = true
410+ result = append (result , rspItem )
411+
412+ delete (mkey_index_map , keyStr )
413+ } else {
414+ if keyStr == "0" {
415+ zeroCount += 1
416+ continue
417+ }
418+ emptyMap := map [string ]interface {}{
419+ "tf_exist" : true ,
420+ }
421+ result = append (result , emptyMap )
422+ }
423+ }
424+
425+ // add items only in response
426+ for _ , idx := range mkey_index_map {
427+ v := rsp_list [idx ].(map [string ]interface {})
428+ if zeroCount > 0 {
429+ v ["tf_exist" ] = true
430+ zeroCount -= 1
431+ } else {
432+ v ["tf_exist" ] = false
433+ }
434+ result = append (result , v )
435+ }
436+
437+ return result
438+ }
439+
440+ func isValidSubnet (i interface {}, k string ) (warnings []string , errors []error ) {
441+ value := i .(string )
442+ cidrValue := convertIP (value )
443+ ip , ipNet , err := net .ParseCIDR (cidrValue )
444+ if err != nil {
445+ errors = append (errors , fmt .Errorf (
446+ "Variable %q is not a valid subnet: %v" , k , value ,
447+ ))
448+ return
449+ }
450+ // Check if IP equals the network base address
451+ if ! ip .Equal (ipNet .IP ) {
452+ warnings = append (warnings , fmt .Sprintf (
453+ "Variable %q is not a valid subnet: %v, do you mean %v?" , k , value , ipNet ,
454+ ))
455+ return
456+ }
457+ return
458+ }
0 commit comments