Skip to content

Commit

Permalink
feat(GROW-2883): lwgenerate enable adding aws provider default tags (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
Matt Cadorette authored May 10, 2024
1 parent f469da5 commit 5d6ef3d
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 0 deletions.
22 changes: 22 additions & 0 deletions lwgenerate/aws/aws.go
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,9 @@ type GenerateAwsTfConfigurationArgs struct {

// Lacework Organization
LaceworkOrganizationLevel bool

// Default AWS Provider Tags
ProviderDefaultTags map[string]interface{}
}

func (args *GenerateAwsTfConfigurationArgs) IsEmpty() bool {
Expand Down Expand Up @@ -429,6 +432,13 @@ func NewTerraform(
return config
}

// WithProviderDefaultTags adds default_tags to the provider configuration for AWS (if tags are present)
func WithProviderDefaultTags(tags map[string]interface{}) AwsTerraformModifier {
return func(c *GenerateAwsTfConfigurationArgs) {
c.ProviderDefaultTags = tags
}
}

// WithAwsProfile Set the AWS Profile to utilize for the main AWS provider
func WithAwsProfile(name string) AwsTerraformModifier {
return func(c *GenerateAwsTfConfigurationArgs) {
Expand Down Expand Up @@ -806,6 +816,18 @@ func createAwsProvider(args *GenerateAwsTfConfigurationArgs) ([]*hclwrite.Block,
lwgenerate.HclProviderWithAttributes(attributes),
}

if len(args.ProviderDefaultTags) != 0 {
defaultTagsBlock, err := lwgenerate.HclCreateGenericBlock(
"default_tags",
nil,
map[string]interface{}{"tags": args.ProviderDefaultTags},
)
if err != nil {
return nil, err
}
modifiers = append(modifiers, lwgenerate.HclProviderWithGenericBlocks(defaultTagsBlock))
}

if args.AwsAssumeRole != "" {
assumeRoleBlock, err := lwgenerate.HclCreateGenericBlock(
"assume_role",
Expand Down
44 changes: 44 additions & 0 deletions lwgenerate/aws/aws_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,19 @@ func TestGenerationAgentless(t *testing.T) {
assert.NotNil(t, hcl)
assert.Equal(t, moduleImportAgentless, hcl)
}
func TestGenerationWithProviderTags(t *testing.T) {
hcl, err := NewTerraform(
false,
false,
true,
false,
WithAwsRegion("us-east-2"),
WithProviderDefaultTags(map[string]interface{}{"TAG_TEST": "foo", "TAG_TEST1": "bar"}),
).Generate()
assert.Nil(t, err)
assert.NotNil(t, hcl)
assert.Equal(t, moduleImportConfigWithProviderTags, hcl)
}

func TestGenerationAgentlessOrganization(t *testing.T) {
hcl, err := NewTerraform(
Expand Down Expand Up @@ -675,6 +688,37 @@ var moduleImportCloudtrail = `module "main_cloudtrail" {
}
`

var moduleImportConfigWithProviderTags = `terraform {
required_providers {
lacework = {
source = "lacework/lacework"
version = "~> 1.0"
}
}
}
provider "aws" {
alias = "main"
region = "us-east-2"
default_tags {
tags = {
TAG_TEST = "foo"
TAG_TEST1 = "bar"
}
}
}
module "aws_config" {
source = "lacework/config/aws"
version = "~> 0.5"
providers = {
aws = aws.main
}
}
`

var moduleImportConfig = `module "aws_config" {
source = "lacework/config/aws"
version = "~> 0.5"
Expand Down

0 comments on commit 5d6ef3d

Please sign in to comment.