Skip to content

Commit

Permalink
feat(GROW-2931): Add support for GCP default labels (#1633)
Browse files Browse the repository at this point in the history
Signed-off-by: Lei Jin <[email protected]>
  • Loading branch information
leijin-lw authored Jun 3, 2024
1 parent 6ae460a commit 6d76586
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 3 deletions.
32 changes: 29 additions & 3 deletions lwgenerate/gcp/gcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,9 @@ type GenerateGcpTfConfigurationArgs struct {

Projects []string

// Default GCP Provider labels
ProviderDefaultLabels map[string]interface{}

// Add custom blocks to the root `terraform{}` block. Can be used for advanced configuration. Things like backend, etc
ExtraBlocksRootTerraform []*hclwrite.Block

Expand Down Expand Up @@ -235,6 +238,13 @@ func WithGcpServiceAccountCredentials(path string) GcpTerraformModifier {
}
}

// WithProviderDefaultLabels adds default_labels to the provider configuration for GCP (if labels are present)
func WithProviderDefaultLabels(labels map[string]interface{}) GcpTerraformModifier {
return func(c *GenerateGcpTfConfigurationArgs) {
c.ProviderDefaultLabels = labels
}
}

// WithConfigOutputs Set Custom Terraform Outputs
func WithCustomOutputs(outputs []lwgenerate.HclOutput) GcpTerraformModifier {
return func(c *GenerateGcpTfConfigurationArgs) {
Expand Down Expand Up @@ -465,7 +475,7 @@ func (args *GenerateGcpTfConfigurationArgs) Generate() (string, error) {
}

gcpProvider, err := createGcpProvider(args.ExtraProviderArguments,
args.ServiceAccountCredentials, args.GcpProjectId, args.Regions, "")
args.ServiceAccountCredentials, args.GcpProjectId, args.Regions, "", args.ProviderDefaultLabels)
if err != nil {
return "", errors.Wrap(err, "failed to generate gcp provider")
}
Expand Down Expand Up @@ -546,6 +556,7 @@ func createGcpProvider(
projectId string,
regionsArg []string,
alias string,
providerDefaultLabels map[string]interface{},
) ([]*hclwrite.Block, error) {
blocks := []*hclwrite.Block{}

Expand Down Expand Up @@ -578,10 +589,25 @@ func createGcpProvider(
attrs["region"] = region
}

modifiers := []lwgenerate.HclProviderModifier{
lwgenerate.HclProviderWithAttributes(attrs),
}

if len(providerDefaultLabels) != 0 {
defaultLabelsBlock, err := lwgenerate.HclCreateGenericBlock(
"default_labels",
nil,
providerDefaultLabels,
)
if err != nil {
return nil, err
}
modifiers = append(modifiers, lwgenerate.HclProviderWithGenericBlocks(defaultLabelsBlock))
}

provider, err := lwgenerate.NewProvider(
"google",
lwgenerate.HclProviderWithAttributes(attrs),
).ToBlock()
modifiers...).ToBlock()
if err != nil {
return nil, err
}
Expand Down
26 changes: 26 additions & 0 deletions lwgenerate/gcp/gcp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -862,6 +862,19 @@ func TestGenerationConfigWithOutputs(t *testing.T) {
assert.Equal(t, RequiredProviders+"\n"+gcpProvider+"\n"+moduleImportProjectLevelAuditLogWithoutConfiguration+"\n"+customOutput, hcl)
}

func TestGenerationConfigWithDefaultProviderLabels(t *testing.T) {
hcl, err := gcp.NewTerraform(
false, false, true, false,
gcp.WithGcpServiceAccountCredentials("/path/to/credentials"),
gcp.WithProjectId(projectName),
gcp.WithRegions([]string{"us-east1"}),
gcp.WithProviderDefaultLabels(map[string]interface{}{"LABEL_TEST": "foo", "LABEL_TEST1": "bar"})).Generate()
assert.Nil(t, err)
assert.NotNil(t, hcl)
assert.Equal(t, RequiredProviders+"\n"+gcpProviderWithDefaultLabels+"\n"+moduleImportProjectLevelAuditLogWithoutConfiguration, hcl)

}

func ProviderWithCredentials(projectName string) string {
return fmt.Sprintf(`provider "google" {
credentials = "/path/to/credentials"
Expand Down Expand Up @@ -905,6 +918,19 @@ var gcpProviderWithExtraArguments = `provider "google" {
}
`

var gcpProviderWithDefaultLabels = `provider "google" {
alias = "us-east1"
credentials = "/path/to/credentials"
project = "project1"
region = "us-east1"
default_labels {
LABEL_TEST = "foo"
LABEL_TEST1 = "bar"
}
}
`

var testVariable = `variable "var_name" {
}
`
Expand Down
3 changes: 3 additions & 0 deletions lwgenerate/gcp/gke.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ type GenerateGkeTfConfigurationArgs struct {
PubSubTopicLabels map[string]string
ServiceAccountCredentials string
WaitTime string
// Default GCP Provider labels
ProviderDefaultLabels map[string]interface{}
// Add custom blocks to the root `terraform{}` block. Can be used for advanced configuration. Things like backend, etc
ExtraBlocksRootTerraform []*hclwrite.Block
// ExtraProviderArguments allows adding more arguments to the provider block as needed (custom use cases)
Expand All @@ -48,6 +50,7 @@ func (args *GenerateGkeTfConfigurationArgs) Generate() (string, error) {
args.ProjectId,
[]string{},
args.GcpProviderAlias,
args.ProviderDefaultLabels,
)
if err != nil {
return "", errors.Wrap(err, "failed to generate gcp provider")
Expand Down

0 comments on commit 6d76586

Please sign in to comment.