github.com/GoogleCloudPlatform/terraformer@v0.8.18/terraformutils/connect.go (about) 1 // Copyright 2018 The Terraformer Authors. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package terraformutils 16 17 func ConnectServices(importResources map[string][]Resource, isServicePath bool, resourceConnections map[string]map[string][]string) map[string][]Resource { 18 for resource, connection := range resourceConnections { 19 if _, exist := importResources[resource]; exist { 20 for k, connectionPairs := range connection { 21 if len(connectionPairs)%2 == 1 { 22 continue 23 } 24 if cc, ok := importResources[k]; ok { 25 for i := 0; i < len(connectionPairs)/2; i++ { 26 connectionPair := []string{connectionPairs[i*2], connectionPairs[i*2+1]} 27 for _, ccc := range cc { 28 if !isServicePath { 29 mapResource(importResources, resource, connectionPair, ccc, "local") 30 } else { 31 mapResource(importResources, resource, connectionPair, ccc, k) 32 } 33 } 34 } 35 } 36 } 37 } 38 } 39 return importResources 40 } 41 42 func mapResource(importResources map[string][]Resource, resource string, connectionPair []string, resourceToMap Resource, k string) { 43 for i := range importResources[resource] { 44 key := connectionPair[1] 45 if connectionPair[1] == "self_link" || connectionPair[1] == "id" { 46 key = resourceToMap.GetIDKey() 47 } 48 mappingResourceAttr := WalkAndGet(key, resourceToMap.InstanceState.Attributes) 49 keyValue := resourceToMap.InstanceInfo.Type + "_" + resourceToMap.ResourceName + "_" + key 50 linkValue := "${data.terraform_remote_state." + k + ".outputs." + keyValue + "}" 51 52 if len(mappingResourceAttr) == 1 { 53 resourceIdentifier := mappingResourceAttr[0].(string) 54 WalkAndOverride(connectionPair[0], resourceIdentifier, linkValue, importResources[resource][i].Item) 55 } 56 } 57 }