github.com/GoogleCloudPlatform/terraformer@v0.8.18/terraformutils/connect_test.go (about)

     1  // Copyright 2019 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  import (
    18  	"log"
    19  	"reflect"
    20  	"testing"
    21  
    22  	"github.com/zclconf/go-cty/cty"
    23  )
    24  
    25  func TestSimpleReference(t *testing.T) {
    26  	importResources := map[string][]Resource{
    27  		"type1": {prepare("ID1", "type1", map[string]string{
    28  			"type2_ref": "ID2",
    29  		}, map[string]interface{}{
    30  			"type2_ref": "ID2",
    31  		})},
    32  		"type2": {prepareNoAttrs("ID2", "type2")},
    33  	}
    34  
    35  	resourceConnections := map[string]map[string][]string{
    36  		"type1": {
    37  			"type2": {"type2_ref", "id"},
    38  		},
    39  	}
    40  	resources := ConnectServices(importResources, true, resourceConnections)
    41  
    42  	if !reflect.DeepEqual(resources["type1"][0].Item, map[string]interface{}{
    43  		"type2_ref": "${data.terraform_remote_state.type2.outputs.type2_tfer--name-type2_id}",
    44  	}) {
    45  		t.Errorf("failed to connect %v", resources["type1"][0].Item)
    46  	}
    47  }
    48  
    49  func TestManyReferences(t *testing.T) {
    50  	importResources := map[string][]Resource{
    51  		"type1": {prepare("ID1", "type1", map[string]string{
    52  			"type2_ref1": "ID2",
    53  			"type2_ref2": "ID2",
    54  		}, map[string]interface{}{
    55  			"type2_ref1": "ID2",
    56  			"type2_ref2": "ID2",
    57  		})},
    58  		"type2": {prepareNoAttrs("ID2", "type2")},
    59  	}
    60  
    61  	resourceConnections := map[string]map[string][]string{
    62  		"type1": {
    63  			"type2": {
    64  				"type2_ref1", "id",
    65  				"type2_ref2", "id",
    66  			},
    67  		},
    68  	}
    69  	resources := ConnectServices(importResources, true, resourceConnections)
    70  
    71  	if !reflect.DeepEqual(resources["type1"][0].Item, map[string]interface{}{
    72  		"type2_ref1": "${data.terraform_remote_state.type2.outputs.type2_tfer--name-type2_id}",
    73  		"type2_ref2": "${data.terraform_remote_state.type2.outputs.type2_tfer--name-type2_id}",
    74  	}) {
    75  		t.Errorf("failed to connect %v", resources["type1"][0].Item)
    76  	}
    77  }
    78  
    79  func TestResourceGroups(t *testing.T) {
    80  	importResources := map[string][]Resource{
    81  		"group1": {prepare("ID1", "type1", map[string]string{
    82  			"type2_ref1": "ID2",
    83  			"type2_ref2": "ID2",
    84  		}, map[string]interface{}{
    85  			"type2_ref1": "ID2",
    86  			"type2_ref2": "ID2",
    87  		}),
    88  			prepareNoAttrs("ID3", "type3")},
    89  		"group2": {
    90  			prepare("ID2", "type2", map[string]string{
    91  				"uid": "ID2",
    92  			}, map[string]interface{}{
    93  				"uid": "ID2",
    94  			}),
    95  			prepareNoAttrs("ID4", "type4")},
    96  	}
    97  
    98  	resourceConnections := map[string]map[string][]string{
    99  		"group1": {
   100  			"group2": {
   101  				"type2_ref1", "uid",
   102  				"type2_ref2", "uid",
   103  			},
   104  		},
   105  	}
   106  	resources := ConnectServices(importResources, true, resourceConnections)
   107  
   108  	if !reflect.DeepEqual(resources["group1"][0].Item, map[string]interface{}{
   109  		"type2_ref1": "${data.terraform_remote_state.group2.outputs.type2_tfer--name-type2_uid}",
   110  		"type2_ref2": "${data.terraform_remote_state.group2.outputs.type2_tfer--name-type2_uid}",
   111  	}) {
   112  		t.Errorf("failed to connect %v", resources["group1"][0].Item)
   113  	}
   114  }
   115  
   116  func TestNestedReference(t *testing.T) {
   117  	importResources := map[string][]Resource{
   118  		"type1": {prepare("ID1", "type1", map[string]string{
   119  			"nested.type2_ref": "ID2",
   120  		}, mapI("nested", mapI("type2_ref", "ID2")))},
   121  		"type2": {prepareNoAttrs("ID2", "type2")},
   122  	}
   123  
   124  	resourceConnections := map[string]map[string][]string{
   125  		"type1": {
   126  			"type2": {"nested.type2_ref", "id"},
   127  		},
   128  	}
   129  	resources := ConnectServices(importResources, true, resourceConnections)
   130  
   131  	if !reflect.DeepEqual(resources["type1"][0].Item, mapI("nested", mapI("type2_ref", "${data.terraform_remote_state.type2.outputs.type2_tfer--name-type2_id}"))) {
   132  		t.Errorf("failed to connect %v", resources)
   133  	}
   134  }
   135  
   136  func prepareNoAttrs(id, resourceType string) Resource {
   137  	return prepare(id, resourceType, map[string]string{}, map[string]interface{}{})
   138  }
   139  
   140  func prepare(id, resourceType string, attributes map[string]string, attributesParsed map[string]interface{}) Resource {
   141  	r := NewResource(id, "name-"+resourceType, resourceType, "provider", attributes, []string{}, map[string]interface{}{})
   142  	r.InstanceState.Attributes["id"] = r.InstanceState.ID
   143  	err := r.ParseTFstate(&MockedFlatmapParser{
   144  		attributesParsed: attributesParsed,
   145  	}, cty.NilType)
   146  	if err != nil {
   147  		log.Println(err)
   148  	}
   149  	return r
   150  }
   151  
   152  func mapI(key string, value interface{}) map[string]interface{} {
   153  	return map[string]interface{}{key: value}
   154  }
   155  
   156  type MockedFlatmapParser struct {
   157  	FlatmapParser
   158  	attributesParsed map[string]interface{}
   159  }
   160  
   161  func (p *MockedFlatmapParser) Parse(ty cty.Type) (map[string]interface{}, error) {
   162  	return p.attributesParsed, nil
   163  }