-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #35 from env0/fix-support-gcp-container-cluster-re…
…source-type Fix: support gcp container cluster resource type
- Loading branch information
Showing
14 changed files
with
513 additions
and
72 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package tagging | ||
|
||
import ( | ||
"github.com/env0/terratag/convert" | ||
) | ||
|
||
func tagAutoscalingGroup(args TagBlockArgs) Result { | ||
// for now, we count on it that if there's a single "tag" in the schema (unlike "tags" block), | ||
// then no "tags" interpolation is used, but rather multiple instances of a "tag" block | ||
// https://www.terraform.io/docs/providers/aws/r/autoscaling_group.html | ||
convert.AppendTagBlocks(args.Block, args.Tags) | ||
|
||
return Result{} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package tagging | ||
|
||
func tagNodeConfigBlocks(args TagBlockArgs) []string { | ||
var swappedTagsStrings []string | ||
|
||
for _, block := range args.Block.Body().Blocks() { | ||
blockArgs := args | ||
blockArgs.Block = block | ||
|
||
if block.Type() == "node_config" { | ||
swappedTagsStrings = append(swappedTagsStrings, TagBlock(blockArgs)) | ||
} else { | ||
swappedTagsStrings = append(swappedTagsStrings, tagNodeConfigBlocks(blockArgs)...) | ||
} | ||
} | ||
|
||
return swappedTagsStrings | ||
} | ||
|
||
func tagContainerCluster(args TagBlockArgs) Result { | ||
rootBlockArgs := args | ||
rootBlockArgs.TagId = "resource_labels" | ||
rootBlockSwappedTagsStrings := []string{TagBlock(rootBlockArgs)} | ||
|
||
return Result{SwappedTagsStrings: append(rootBlockSwappedTagsStrings, tagNodeConfigBlocks(args)...)} | ||
} | ||
|
||
func tagContainerNodePool(args TagBlockArgs) Result { | ||
return Result{SwappedTagsStrings: tagNodeConfigBlocks(args)} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package tagging | ||
|
||
import ( | ||
"github.com/env0/terratag/convert" | ||
"github.com/env0/terratag/tag_keys" | ||
"github.com/env0/terratag/terraform" | ||
"github.com/hashicorp/hcl/v2/hclwrite" | ||
"github.com/zclconf/go-cty/cty" | ||
) | ||
|
||
func defaultTaggingFn(args TagBlockArgs) Result { | ||
return Result{ | ||
SwappedTagsStrings: []string{TagBlock(args)}, | ||
} | ||
} | ||
|
||
func TagBlock(args TagBlockArgs) string { | ||
hasExistingTags := convert.MoveExistingTags(args.Filename, args.Terratag, args.Block, args.TagId) | ||
|
||
tagsValue := "" | ||
if hasExistingTags { | ||
tagsValue = "merge( " + convert.GetExistingTagsExpression(args.Terratag.Found[tag_keys.GetResourceExistingTagsKey(args.Filename, args.Block)]) + ", local." + tag_keys.GetTerratagAddedKey(args.Filename) + ")" | ||
} else { | ||
tagsValue = "local." + tag_keys.GetTerratagAddedKey(args.Filename) | ||
} | ||
|
||
if args.TfVersion == 11 { | ||
tagsValue = "${" + tagsValue + "}" | ||
} | ||
|
||
args.Block.Body().SetAttributeValue(args.TagId, cty.StringVal(tagsValue)) | ||
|
||
return tagsValue | ||
} | ||
|
||
func HasResourceTagFn(resourceType string) bool { | ||
return resourceTypeToFnMap[resourceType] != nil | ||
} | ||
|
||
func TagResource(args TagBlockArgs) Result { | ||
var result Result | ||
resourceType := terraform.GetResourceType(*args.Block) | ||
|
||
customTaggingFn := resourceTypeToFnMap[resourceType] | ||
|
||
if customTaggingFn != nil { | ||
result = customTaggingFn(args) | ||
} else { | ||
result = defaultTaggingFn(args) | ||
} | ||
|
||
return result | ||
} | ||
|
||
var resourceTypeToFnMap = map[string]TagResourceFn{ | ||
"aws_autoscaling_group": tagAutoscalingGroup, | ||
"google_container_cluster": tagContainerCluster, | ||
"google_container_node_pool": tagContainerNodePool, | ||
} | ||
|
||
type TagBlockArgs struct { | ||
Filename string | ||
Block *hclwrite.Block | ||
Tags string | ||
Terratag convert.TerratagLocal | ||
TagId string | ||
TfVersion int | ||
} | ||
|
||
type TagResourceFn func(args TagBlockArgs) Result | ||
|
||
type Result struct { | ||
SwappedTagsStrings []string | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
test/fixture/terraform_11/google_container_cluster/expected/main.terratag.tf
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
resource "google_container_cluster" "no-labels-cluster" { | ||
name = "cluster" | ||
location = "us-central1" | ||
|
||
node_config { | ||
machine_type = "n1-standard-1" | ||
labels = "${local.terratag_added_main}" | ||
} | ||
|
||
node_pool { | ||
node_config { | ||
machine_type = "n1-standard-1" | ||
labels = "${local.terratag_added_main}" | ||
} | ||
} | ||
resource_labels = "${local.terratag_added_main}" | ||
} | ||
|
||
resource "google_container_node_pool" "no-labels-pool" { | ||
cluster = "${google_container_cluster.no-labels-cluster.name}" | ||
|
||
node_config { | ||
machine_type = "n1-standard-1" | ||
labels = "${local.terratag_added_main}" | ||
} | ||
} | ||
|
||
resource "google_container_cluster" "existing-labels-cluster" { | ||
name = "cluster2" | ||
location = "us-central1" | ||
|
||
resource_labels = "${merge( map("foo" , "bar"), local.terratag_added_main)}" | ||
|
||
node_config { | ||
machine_type = "n1-standard-1" | ||
labels = "${merge( map("foo" , "bar"), local.terratag_added_main)}" | ||
} | ||
|
||
node_pool { | ||
node_config { | ||
machine_type = "n1-standard-1" | ||
labels = "${merge( map("foo" , "bar"), local.terratag_added_main)}" | ||
} | ||
} | ||
} | ||
|
||
resource "google_container_node_pool" "existing-labels-pool" { | ||
cluster = "${google_container_cluster.existing-labels-cluster.name}" | ||
|
||
node_config { | ||
machine_type = "n1-standard-1" | ||
labels = "${merge( map("foo" , "bar"), local.terratag_added_main)}" | ||
} | ||
} | ||
locals { | ||
terratag_added_main = {"env0_environment_id"="40907eff-cf7c-419a-8694-e1c6bf1d1168","env0_project_id"="43fd4ff1-8d37-4d9d-ac97-295bd850bf94"} | ||
} | ||
|
Oops, something went wrong.