@@ -1076,6 +1076,7 @@ func TestTest_TestStep_Taint(t *testing.T) {
1076
1076
}
1077
1077
}
1078
1078
1079
+ //nolint:unparam
1079
1080
func extractResourceAttr (resourceName string , attributeName string , attributeValue * string ) TestCheckFunc {
1080
1081
return func (s * terraform.State ) error {
1081
1082
rs , ok := s .RootModule ().Resources [resourceName ]
@@ -1253,6 +1254,8 @@ func TestTest_TestStep_ProviderFactories_To_ExternalProviders(t *testing.T) {
1253
1254
}
1254
1255
1255
1256
func TestTest_TestStep_ProviderFactories_Import_Inline (t * testing.T ) {
1257
+ id := "none"
1258
+
1256
1259
t .Parallel ()
1257
1260
1258
1261
Test (t , TestCase {
@@ -1316,9 +1319,8 @@ func TestTest_TestStep_ProviderFactories_Import_Inline(t *testing.T) {
1316
1319
ImportStateId : "Z=:cbrJE?Ltg" ,
1317
1320
ImportStatePersist : true ,
1318
1321
ImportStateCheck : composeImportStateCheck (
1319
- testCheckResourceAttrInstanceState ("id" , "none" ),
1320
- testCheckResourceAttrInstanceState ("result" , "Z=:cbrJE?Ltg" ),
1321
- testCheckResourceAttrInstanceState ("length" , "12" ),
1322
+ testCheckResourceAttrInstanceState (& id , "result" , "Z=:cbrJE?Ltg" ),
1323
+ testCheckResourceAttrInstanceState (& id , "length" , "12" ),
1322
1324
),
1323
1325
},
1324
1326
},
@@ -1391,7 +1393,7 @@ func TestTest_TestStep_ProviderFactories_Import_Inline_WithPersistMatch(t *testi
1391
1393
ImportStateId : "Z=:cbrJE?Ltg" ,
1392
1394
ImportStatePersist : true ,
1393
1395
ImportStateCheck : composeImportStateCheck (
1394
- testExtractResourceAttrInstanceState ("result" , & result1 ),
1396
+ testExtractResourceAttrInstanceState ("none" , " result" , & result1 ),
1395
1397
),
1396
1398
},
1397
1399
{
@@ -1484,6 +1486,8 @@ func TestTest_TestStep_ProviderFactories_Import_Inline_WithoutPersist(t *testing
1484
1486
}
1485
1487
1486
1488
func TestTest_TestStep_ProviderFactories_Import_External (t * testing.T ) {
1489
+ id := "none"
1490
+
1487
1491
t .Parallel ()
1488
1492
1489
1493
Test (t , TestCase {
@@ -1500,9 +1504,8 @@ func TestTest_TestStep_ProviderFactories_Import_External(t *testing.T) {
1500
1504
ImportStateId : "Z=:cbrJE?Ltg" ,
1501
1505
ImportStatePersist : true ,
1502
1506
ImportStateCheck : composeImportStateCheck (
1503
- testCheckResourceAttrInstanceState ("id" , "none" ),
1504
- testCheckResourceAttrInstanceState ("result" , "Z=:cbrJE?Ltg" ),
1505
- testCheckResourceAttrInstanceState ("length" , "12" ),
1507
+ testCheckResourceAttrInstanceState (& id , "result" , "Z=:cbrJE?Ltg" ),
1508
+ testCheckResourceAttrInstanceState (& id , "length" , "12" ),
1506
1509
),
1507
1510
},
1508
1511
},
@@ -1528,7 +1531,7 @@ func TestTest_TestStep_ProviderFactories_Import_External_WithPersistMatch(t *tes
1528
1531
ImportStateId : "Z=:cbrJE?Ltg" ,
1529
1532
ImportStatePersist : true ,
1530
1533
ImportStateCheck : composeImportStateCheck (
1531
- testExtractResourceAttrInstanceState ("result" , & result1 ),
1534
+ testExtractResourceAttrInstanceState ("none" , " result" , & result1 ),
1532
1535
),
1533
1536
},
1534
1537
{
@@ -1561,7 +1564,7 @@ func TestTest_TestStep_ProviderFactories_Import_External_WithoutPersistNonMatch(
1561
1564
ImportStateId : "Z=:cbrJE?Ltg" ,
1562
1565
ImportStatePersist : false ,
1563
1566
ImportStateCheck : composeImportStateCheck (
1564
- testExtractResourceAttrInstanceState ("result" , & result1 ),
1567
+ testExtractResourceAttrInstanceState ("none" , " result" , & result1 ),
1565
1568
),
1566
1569
},
1567
1570
{
@@ -1714,6 +1717,188 @@ func TestTest_TestStep_ProviderFactories_RefreshWithPlanModifier_Inline(t *testi
1714
1717
})
1715
1718
}
1716
1719
1720
+ func TestTest_TestStep_ProviderFactories_Import_Inline_With_Data_Source (t * testing.T ) {
1721
+ var id string
1722
+
1723
+ t .Parallel ()
1724
+
1725
+ Test (t , TestCase {
1726
+ ProviderFactories : map [string ]func () (* schema.Provider , error ){
1727
+ "http" : func () (* schema.Provider , error ) { //nolint:unparam // required signature
1728
+ return & schema.Provider {
1729
+ DataSourcesMap : map [string ]* schema.Resource {
1730
+ "http" : {
1731
+ ReadContext : func (ctx context.Context , d * schema.ResourceData , i interface {}) (diags diag.Diagnostics ) {
1732
+ url := d .Get ("url" ).(string )
1733
+
1734
+ responseHeaders := map [string ]string {
1735
+ "headerOne" : "one" ,
1736
+ "headerTwo" : "two" ,
1737
+ "headerThree" : "three" ,
1738
+ "headerFour" : "four" ,
1739
+ }
1740
+ if err := d .Set ("response_headers" , responseHeaders ); err != nil {
1741
+ return append (diags , diag .Errorf ("Error setting HTTP response headers: %s" , err )... )
1742
+ }
1743
+
1744
+ d .SetId (url )
1745
+
1746
+ return diags
1747
+ },
1748
+ Schema : map [string ]* schema.Schema {
1749
+ "url" : {
1750
+ Type : schema .TypeString ,
1751
+ Required : true ,
1752
+ },
1753
+ "response_headers" : {
1754
+ Type : schema .TypeMap ,
1755
+ Computed : true ,
1756
+ Elem : & schema.Schema {
1757
+ Type : schema .TypeString ,
1758
+ },
1759
+ },
1760
+ },
1761
+ },
1762
+ },
1763
+ }, nil
1764
+ },
1765
+ "random" : func () (* schema.Provider , error ) { //nolint:unparam // required signature
1766
+ return & schema.Provider {
1767
+ ResourcesMap : map [string ]* schema.Resource {
1768
+ "random_string" : {
1769
+ CreateContext : func (_ context.Context , d * schema.ResourceData , _ interface {}) diag.Diagnostics {
1770
+ d .SetId ("none" )
1771
+ err := d .Set ("length" , 4 )
1772
+ if err != nil {
1773
+ panic (err )
1774
+ }
1775
+ err = d .Set ("result" , "none" )
1776
+ if err != nil {
1777
+ panic (err )
1778
+ }
1779
+ return nil
1780
+ },
1781
+ DeleteContext : func (_ context.Context , _ * schema.ResourceData , _ interface {}) diag.Diagnostics {
1782
+ return nil
1783
+ },
1784
+ ReadContext : func (_ context.Context , _ * schema.ResourceData , _ interface {}) diag.Diagnostics {
1785
+ return nil
1786
+ },
1787
+ Schema : map [string ]* schema.Schema {
1788
+ "length" : {
1789
+ Required : true ,
1790
+ ForceNew : true ,
1791
+ Type : schema .TypeInt ,
1792
+ },
1793
+ "result" : {
1794
+ Type : schema .TypeString ,
1795
+ Computed : true ,
1796
+ Sensitive : true ,
1797
+ },
1798
+
1799
+ "id" : {
1800
+ Computed : true ,
1801
+ Type : schema .TypeString ,
1802
+ },
1803
+ },
1804
+ Importer : & schema.ResourceImporter {
1805
+ StateContext : func (ctx context.Context , d * schema.ResourceData , meta interface {}) ([]* schema.ResourceData , error ) {
1806
+ val := d .Id ()
1807
+
1808
+ d .SetId (val )
1809
+
1810
+ err := d .Set ("result" , val )
1811
+ if err != nil {
1812
+ panic (err )
1813
+ }
1814
+
1815
+ err = d .Set ("length" , len (val ))
1816
+ if err != nil {
1817
+ panic (err )
1818
+ }
1819
+
1820
+ return []* schema.ResourceData {d }, nil
1821
+ },
1822
+ },
1823
+ },
1824
+ },
1825
+ }, nil
1826
+ },
1827
+ },
1828
+ Steps : []TestStep {
1829
+ {
1830
+ Config : `data "http" "example" {
1831
+ url = "https://checkpoint-api.hashicorp.com/v1/check/terraform"
1832
+ }
1833
+
1834
+ resource "random_string" "example" {
1835
+ length = length(data.http.example.response_headers)
1836
+ }` ,
1837
+ Check : extractResourceAttr ("random_string.example" , "id" , & id ),
1838
+ },
1839
+ {
1840
+ Config : `data "http" "example" {
1841
+ url = "https://checkpoint-api.hashicorp.com/v1/check/terraform"
1842
+ }
1843
+
1844
+ resource "random_string" "example" {
1845
+ length = length(data.http.example.response_headers)
1846
+ }` ,
1847
+ ResourceName : "random_string.example" ,
1848
+ ImportState : true ,
1849
+ ImportStateCheck : composeImportStateCheck (
1850
+ testCheckResourceAttrInstanceState (& id , "length" , "4" ),
1851
+ ),
1852
+ ImportStateVerify : true ,
1853
+ },
1854
+ },
1855
+ })
1856
+ }
1857
+
1858
+ func TestTest_TestStep_ProviderFactories_Import_External_With_Data_Source (t * testing.T ) {
1859
+ var id string
1860
+
1861
+ t .Parallel ()
1862
+
1863
+ Test (t , TestCase {
1864
+ ExternalProviders : map [string ]ExternalProvider {
1865
+ "http" : {
1866
+ Source : "registry.terraform.io/hashicorp/http" ,
1867
+ },
1868
+ "random" : {
1869
+ Source : "registry.terraform.io/hashicorp/random" ,
1870
+ },
1871
+ },
1872
+ Steps : []TestStep {
1873
+ {
1874
+ Config : `data "http" "example" {
1875
+ url = "https://checkpoint-api.hashicorp.com/v1/check/terraform"
1876
+ }
1877
+
1878
+ resource "random_string" "example" {
1879
+ length = length(data.http.example.response_headers)
1880
+ }` ,
1881
+ Check : extractResourceAttr ("random_string.example" , "id" , & id ),
1882
+ },
1883
+ {
1884
+ Config : `data "http" "example" {
1885
+ url = "https://checkpoint-api.hashicorp.com/v1/check/terraform"
1886
+ }
1887
+
1888
+ resource "random_string" "example" {
1889
+ length = length(data.http.example.response_headers)
1890
+ }` ,
1891
+ ResourceName : "random_string.example" ,
1892
+ ImportState : true ,
1893
+ ImportStateCheck : composeImportStateCheck (
1894
+ testCheckResourceAttrInstanceState (& id , "length" , "12" ),
1895
+ ),
1896
+ ImportStateVerify : true ,
1897
+ },
1898
+ },
1899
+ })
1900
+ }
1901
+
1717
1902
func setTimeForTest (t time.Time ) func () {
1718
1903
return func () {
1719
1904
getTimeForTest = func () time.Time {
@@ -1738,43 +1923,41 @@ func composeImportStateCheck(fs ...ImportStateCheckFunc) ImportStateCheckFunc {
1738
1923
}
1739
1924
}
1740
1925
1741
- func testExtractResourceAttrInstanceState (attributeName string , attributeValue * string ) ImportStateCheckFunc {
1926
+ func testExtractResourceAttrInstanceState (id , attributeName string , attributeValue * string ) ImportStateCheckFunc {
1742
1927
return func (is []* terraform.InstanceState ) error {
1743
- if len (is ) != 1 {
1744
- return fmt .Errorf ("unexpected number of instance states: %d" , len (is ))
1745
- }
1928
+ for _ , v := range is {
1929
+ if v .ID != id {
1930
+ continue
1931
+ }
1746
1932
1747
- s := is [0 ]
1933
+ if attrVal , ok := v .Attributes [attributeName ]; ok {
1934
+ * attributeValue = attrVal
1748
1935
1749
- attrValue , ok := s .Attributes [attributeName ]
1750
- if ! ok {
1751
- return fmt .Errorf ("attribute %s not found in instance state" , attributeName )
1936
+ return nil
1937
+ }
1752
1938
}
1753
1939
1754
- * attributeValue = attrValue
1755
-
1756
- return nil
1940
+ return fmt .Errorf ("attribute %s not found in instance state" , attributeName )
1757
1941
}
1758
1942
}
1759
1943
1760
- func testCheckResourceAttrInstanceState (attributeName , attributeValue string ) ImportStateCheckFunc {
1944
+ func testCheckResourceAttrInstanceState (id * string , attributeName , attributeValue string ) ImportStateCheckFunc {
1761
1945
return func (is []* terraform.InstanceState ) error {
1762
- if len (is ) != 1 {
1763
- return fmt .Errorf ("unexpected number of instance states: %d" , len (is ))
1764
- }
1765
-
1766
- s := is [0 ]
1946
+ for _ , v := range is {
1947
+ if v .ID != * id {
1948
+ continue
1949
+ }
1767
1950
1768
- attrVal , ok := s .Attributes [attributeName ]
1769
- if ! ok {
1770
- return fmt .Errorf ("attribute %s found in instance state " , attributeName )
1771
- }
1951
+ if attrVal , ok := v .Attributes [attributeName ]; ok {
1952
+ if attrVal != attributeValue {
1953
+ return fmt .Errorf ("expected: %s got: %s " , attributeValue , attrVal )
1954
+ }
1772
1955
1773
- if attrVal != attributeValue {
1774
- return fmt . Errorf ( "expected: %s got: %s" , attributeValue , attrVal )
1956
+ return nil
1957
+ }
1775
1958
}
1776
1959
1777
- return nil
1960
+ return fmt . Errorf ( "attribute %s not found in instance state" , attributeName )
1778
1961
}
1779
1962
}
1780
1963
0 commit comments