4
4
"bytes"
5
5
"context"
6
6
"database/sql"
7
- "fmt"
8
7
"net/http"
9
8
"runtime"
10
9
"sync/atomic"
@@ -1406,160 +1405,193 @@ func TestWorkspaceTagsTerraform(t *testing.T) {
1406
1405
t .Parallel ()
1407
1406
}
1408
1407
1409
- mainTfTemplate := `
1408
+ providersTf := `
1410
1409
terraform {
1411
1410
required_providers {
1412
1411
coder = {
1413
1412
source = "coder/coder"
1414
1413
}
1415
1414
}
1416
1415
}
1417
- provider "coder" {}
1416
+ provider "coder" {}`
1417
+ dataTf := `
1418
1418
data "coder_workspace" "me" {}
1419
- data "coder_workspace_owner" "me" {}
1420
- data "coder_parameter" "unrelated" {
1421
- name = "unrelated"
1422
- type = "list(string)"
1423
- default = jsonencode(["a", "b"])
1424
- }
1425
- %s
1426
- `
1427
-
1419
+ data "coder_workspace_owner" "me" {}`
1428
1420
for _ , tc := range []struct {
1429
1421
name string
1430
1422
// tags to apply to the external provisioner
1431
1423
provisionerTags map [string ]string
1432
1424
// tags to apply to the create template version request
1433
1425
createTemplateVersionRequestTags map [string ]string
1434
- // the coder_workspace_tags bit of main.tf.
1435
- // you can add more stuff here if you need
1436
- tfWorkspaceTags string
1426
+ // files to add to the template
1427
+ files map [string ]string
1437
1428
}{
1438
1429
{
1439
- name : "no tags" ,
1440
- tfWorkspaceTags : `` ,
1430
+ name : "no tags" ,
1431
+ files : map [string ]string {
1432
+ "main.tf" : `` ,
1433
+ "providers.tf" : providersTf ,
1434
+ "data.tf" : dataTf ,
1435
+ },
1441
1436
},
1442
1437
{
1443
1438
name : "empty tags" ,
1444
- tfWorkspaceTags : `
1445
- data "coder_workspace_tags" "tags" {
1446
- tags = {}
1447
- }
1448
- ` ,
1439
+ files : map [string ]string {
1440
+ "main.tf" : `
1441
+ data "coder_workspace_tags" "tags" {
1442
+ tags = {}
1443
+ }` ,
1444
+ "providers.tf" : providersTf ,
1445
+ "data.tf" : dataTf ,
1446
+ },
1449
1447
},
1450
1448
{
1451
1449
name : "static tag" ,
1452
1450
provisionerTags : map [string ]string {"foo" : "bar" },
1453
- tfWorkspaceTags : `
1454
- data "coder_workspace_tags" "tags" {
1455
- tags = {
1456
- "foo" = "bar"
1457
- }
1458
- }` ,
1451
+ files : map [string ]string {
1452
+ "main.tf" : `
1453
+ data "coder_workspace_tags" "tags" {
1454
+ tags = {
1455
+ "foo" = "bar"
1456
+ }
1457
+ }` ,
1458
+ "providers.tf" : providersTf ,
1459
+ "data.tf" : dataTf ,
1460
+ },
1459
1461
},
1460
1462
{
1461
1463
name : "tag variable" ,
1462
1464
provisionerTags : map [string ]string {"foo" : "bar" },
1463
- tfWorkspaceTags : `
1464
- variable "foo" {
1465
- default = "bar"
1466
- }
1467
- data "coder_workspace_tags" "tags" {
1468
- tags = {
1469
- "foo" = var.foo
1465
+ files : map [string ]string {
1466
+ "main.tf" : `
1467
+ variable "foo" {
1468
+ default = "bar"
1470
1469
}
1471
- }` ,
1470
+ data "coder_workspace_tags" "tags" {
1471
+ tags = {
1472
+ "foo" = var.foo
1473
+ }
1474
+ }` ,
1475
+ "providers.tf" : providersTf ,
1476
+ "data.tf" : dataTf ,
1477
+ },
1472
1478
},
1473
1479
{
1474
1480
name : "tag param" ,
1475
1481
provisionerTags : map [string ]string {"foo" : "bar" },
1476
- tfWorkspaceTags : `
1477
- data "coder_parameter" "foo" {
1478
- name = "foo"
1479
- type = "string"
1480
- default = "bar"
1481
- }
1482
- data "coder_workspace_tags" "tags" {
1483
- tags = {
1484
- "foo" = data.coder_parameter.foo.value
1482
+ files : map [string ]string {
1483
+ "main.tf" : `
1484
+ data "coder_parameter" "foo" {
1485
+ name = "foo"
1486
+ type = "string"
1487
+ default = "bar"
1485
1488
}
1486
- }` ,
1489
+ data "coder_workspace_tags" "tags" {
1490
+ tags = {
1491
+ "foo" = data.coder_parameter.foo.value
1492
+ }
1493
+ }` ,
1494
+ "providers.tf" : providersTf ,
1495
+ "data.tf" : dataTf ,
1496
+ },
1487
1497
},
1488
1498
{
1489
1499
name : "tag param with default from var" ,
1490
1500
provisionerTags : map [string ]string {"foo" : "bar" },
1491
- tfWorkspaceTags : `
1492
- variable "foo" {
1493
- type = string
1494
- default = "bar"
1495
- }
1496
- data "coder_parameter" "foo" {
1497
- name = "foo"
1498
- type = "string"
1499
- default = var.foo
1500
- }
1501
- data "coder_workspace_tags" "tags" {
1502
- tags = {
1503
- "foo" = data.coder_parameter.foo.value
1501
+ files : map [string ]string {
1502
+ "main.tf" : `
1503
+ variable "foo" {
1504
+ type = string
1505
+ default = "bar"
1506
+ }
1507
+ data "coder_parameter" "foo" {
1508
+ name = "foo"
1509
+ type = "string"
1510
+ default = var.foo
1504
1511
}
1505
- }` ,
1512
+ data "coder_workspace_tags" "tags" {
1513
+ tags = {
1514
+ "foo" = data.coder_parameter.foo.value
1515
+ }
1516
+ }` ,
1517
+ "providers.tf" : providersTf ,
1518
+ "data.tf" : dataTf ,
1519
+ },
1506
1520
},
1507
1521
{
1508
1522
name : "override no tags" ,
1509
1523
provisionerTags : map [string ]string {"foo" : "baz" },
1510
1524
createTemplateVersionRequestTags : map [string ]string {"foo" : "baz" },
1511
- tfWorkspaceTags : `` ,
1525
+ files : map [string ]string {
1526
+ "main.tf" : `` ,
1527
+ "providers.tf" : providersTf ,
1528
+ "data.tf" : dataTf ,
1529
+ },
1512
1530
},
1513
1531
{
1514
1532
name : "override empty tags" ,
1515
1533
provisionerTags : map [string ]string {"foo" : "baz" },
1516
1534
createTemplateVersionRequestTags : map [string ]string {"foo" : "baz" },
1517
- tfWorkspaceTags : `
1518
- data "coder_workspace_tags" "tags" {
1519
- tags = {}
1520
- }` ,
1535
+ files : map [string ]string {
1536
+ "main.tf" : `
1537
+ data "coder_workspace_tags" "tags" {
1538
+ tags = {}
1539
+ }` ,
1540
+ "providers.tf" : providersTf ,
1541
+ "data.tf" : dataTf ,
1542
+ },
1521
1543
},
1522
1544
{
1523
1545
name : "does not override static tag" ,
1524
1546
provisionerTags : map [string ]string {"foo" : "bar" },
1525
1547
createTemplateVersionRequestTags : map [string ]string {"foo" : "baz" },
1526
- tfWorkspaceTags : `
1527
- data "coder_workspace_tags" "tags" {
1528
- tags = {
1529
- "foo" = "bar"
1530
- }
1531
- }` ,
1548
+ files : map [string ]string {
1549
+ "main.tf" : `
1550
+ data "coder_workspace_tags" "tags" {
1551
+ tags = {
1552
+ "foo" = "bar"
1553
+ }
1554
+ }` ,
1555
+ "providers.tf" : providersTf ,
1556
+ "data.tf" : dataTf ,
1557
+ },
1532
1558
},
1533
1559
{
1534
1560
name : "locals" ,
1535
1561
provisionerTags : map [string ]string {"foo" : "bar" },
1536
- tfWorkspaceTags : `
1537
- locals {
1538
- tagFoo = "bar"
1539
- }
1540
- data "coder_workspace_tags" "tags" {
1541
- tags = {
1542
- "foo": local.tagFoo
1562
+ files : map [string ]string {
1563
+ "main.tf" : `
1564
+ locals {
1565
+ tagFoo = "bar"
1543
1566
}
1544
- }
1545
- ` ,
1567
+ data "coder_workspace_tags" "tags" {
1568
+ tags = {
1569
+ "foo": local.tagFoo
1570
+ }
1571
+ }` ,
1572
+ "providers.tf" : providersTf ,
1573
+ "data.tf" : dataTf ,
1574
+ },
1546
1575
},
1547
1576
{
1548
1577
name : "var default from local" ,
1549
1578
provisionerTags : map [string ]string {"foo" : "bar" },
1550
- tfWorkspaceTags : `
1551
- locals {
1552
- tagFoo = "bar"
1553
- }
1554
- variable "foo" {
1555
- default = local.tagFoo
1556
- }
1557
- data "coder_workspace_tags" "tags" {
1558
- tags = {
1559
- foo: var.foo
1579
+ files : map [string ]string {
1580
+ "main.tf" : `
1581
+ locals {
1582
+ tagFoo = "bar"
1560
1583
}
1561
- }
1562
- ` ,
1584
+ variable "foo" {
1585
+ default = local.tagFoo
1586
+ }
1587
+ data "coder_workspace_tags" "tags" {
1588
+ tags = {
1589
+ foo: var.foo
1590
+ }
1591
+ }` ,
1592
+ "providers.tf" : providersTf ,
1593
+ "data.tf" : dataTf ,
1594
+ },
1563
1595
},
1564
1596
} {
1565
1597
tc := tc
@@ -1586,8 +1618,7 @@ func TestWorkspaceTagsTerraform(t *testing.T) {
1586
1618
_ = coderdenttest .NewExternalProvisionerDaemonTerraform (t , client , owner .OrganizationID , tc .provisionerTags )
1587
1619
1588
1620
// Creating a template as a template admin must succeed
1589
- templateFiles := map [string ]string {"main.tf" : fmt .Sprintf (mainTfTemplate , tc .tfWorkspaceTags )}
1590
- tarBytes := testutil .CreateTar (t , templateFiles )
1621
+ tarBytes := testutil .CreateTar (t , tc .files )
1591
1622
fi , err := templateAdmin .Upload (ctx , "application/x-tar" , bytes .NewReader (tarBytes ))
1592
1623
require .NoError (t , err , "failed to upload file" )
1593
1624
tv , err := templateAdmin .CreateTemplateVersion (ctx , owner .OrganizationID , codersdk.CreateTemplateVersionRequest {
0 commit comments