From 3e83758cd7cf82b406eb60abacfa1e4c451481e2 Mon Sep 17 00:00:00 2001 From: William Guilherme Date: Fri, 31 Jan 2025 12:09:13 -0800 Subject: [PATCH] fix: Fixed ZIA Issues during Import --- CHANGELOG.md | 13 +++++++++ cmd/generate.go | 48 +++++++++++++++---------------- cmd/import.go | 5 ++-- docs/guides/release-notes.md | 14 ++++++++- terraformutils/helpers/helpers.go | 4 ++- terraformutils/nesting/nesting.go | 3 ++ 6 files changed, 58 insertions(+), 29 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index dd4685b..0eb6146 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,18 @@ # Changelog +## 2.0.1 (January, 31 2025) + +### Notes + +- Release date: **(January, 31 2025)** +- Supported Terraform version: **v1.x.x** + +### Bug Fixes +- [PR #257](https://github.com/zscaler/zscaler-terraformer/pull/257). Fixed ZIA import resource for `zia_dlp_notification_templates` due to heredoc missformatting. - [Issue #253](https://github.com/zscaler/zscaler-terraformer/issues/253) +- [PR #257](https://github.com/zscaler/zscaler-terraformer/pull/257). Fixed ZIA import resource for `zia_end_user_notification` due to heredoc missformatting and attribute validation issue. - [Issue #254](https://github.com/zscaler/zscaler-terraformer/issues/254) +- [PR #257](https://github.com/zscaler/zscaler-terraformer/pull/257). Fixed ZIA import resources for: `zia_forwarding_control_rule` due to missing attribute `id` within the `zpa_gateway` block. - [Issue #255](https://github.com/zscaler/zscaler-terraformer/issues/255) +- [PR #257](https://github.com/zscaler/zscaler-terraformer/pull/257). Fixed ZIA import resources for: `zia_forwarding_control_zpa_gateway` due to missing attribute `type`. - [Issue #256](https://github.com/zscaler/zscaler-terraformer/issues/256) + ## 2.0.0 (January, 29 2025) - BREAKING CHANGES ### Notes diff --git a/cmd/generate.go b/cmd/generate.go index 1c99991..257724f 100644 --- a/cmd/generate.go +++ b/cmd/generate.go @@ -1360,7 +1360,6 @@ func generate(ctx context.Context, cmd *cobra.Command, writer io.Writer, resourc if api.ZIAService == nil { log.Fatal("ZIA service is not initialized") } - // EXACTLY like the TF pattern: service := api.ZIAService gws, err := zpa_gateways.GetAll(ctx, service) if err != nil { @@ -1371,6 +1370,8 @@ func generate(ctx context.Context, cmd *cobra.Command, writer io.Writer, resourc if helpers.IsInList(gw.Name, []string{"Auto ZPA Gateway"}) { continue } + // Ensure type is always "ZPA" + gw.Type = "ZPA" gwsFiltered = append(gwsFiltered, gw) } resourceCount = len(gwsFiltered) @@ -1471,21 +1472,6 @@ func generate(ctx context.Context, cmd *cobra.Command, writer io.Writer, resourc m, _ := json.Marshal(rulesFiltered) _ = json.Unmarshal(m, &jsonStructData) - // case "zia_advanced_settings": - // if api.ZIAService == nil { - // log.Fatal("ZIA service is not initialized") - // } - // // EXACTLY like the TF pattern: - // service := api.ZIAService - // advSettings, err := advanced_settings.GetAdvancedSettings(ctx, service) - // if err != nil { - // log.Fatal(err) - // } - // jsonPayload := []*advanced_settings.AdvancedSettings{advSettings} - // resourceCount = len(jsonPayload) - // m, _ := json.Marshal(jsonPayload) - // _ = json.Unmarshal(m, &jsonStructData) - case "zia_advanced_settings": if api.ZIAService == nil { log.Fatal("ZIA service is not initialized") @@ -1722,6 +1708,27 @@ func generate(ctx context.Context, cmd *cobra.Command, writer io.Writer, resourc continue } + // Ensure proper Heredoc formatting for multi-line string attributes + if attrName == "quarantine_custom_notification_text" { + value := structData[apiAttrName] + if value != nil { + valueStr := strings.TrimSpace(value.(string)) + formattedValue := helpers.FormatHeredoc(valueStr) + output += fmt.Sprintf(" %s = <<-EOT\n%s\nEOT\n\n", attrName, formattedValue) + continue + } + } + + // Ensure proper Heredoc formatting for multi-line string attributes + if attrName == "plain_text_message" || attrName == "html_message" || attrName == "subject" { + value := structData[apiAttrName] + if value != nil { + valueStr := strings.TrimSpace(value.(string)) + formattedValue := helpers.FormatHeredoc(valueStr) // Use the updated helper function + output += fmt.Sprintf(" %s = <<-EOT\n%s\nEOT\n\n", attrName, formattedValue) + continue + } + } ty := r.Block.Attributes[attrName].AttributeType // (A) ADD THIS BLOCK: @@ -1812,15 +1819,6 @@ func generate(ctx context.Context, cmd *cobra.Command, writer io.Writer, resourc if strValue, ok := value.(string); ok { value = strValue } - } else if resourceType == "zia_dlp_notification_templates" && helpers.IsInList(attrName, []string{"subject", "plain_text_message", "html_message"}) { - valueStr := strings.ReplaceAll(value.(string), "$", "$$") - formattedValue := helpers.FormatHeredoc(valueStr) - switch attrName { - case "html_message", "plain_text_message": - output += fmt.Sprintf(" %s = <<-EOT\n%sEOT\n\n", attrName, formattedValue) - case "subject": - output += fmt.Sprintf(" %s = <<-EOT\n%sEOT\n", attrName, formattedValue) - } } output += nesting.WriteAttrLine(attrName, value, false) diff --git a/cmd/import.go b/cmd/import.go index a1a66a7..22fa874 100644 --- a/cmd/import.go +++ b/cmd/import.go @@ -1126,7 +1126,6 @@ func importResource(ctx context.Context, cmd *cobra.Command, writer io.Writer, r if api.ZIAService == nil { log.Fatal("ZIA service is not initialized") } - // EXACTLY like the TF pattern: service := api.ZIAService gws, err := zpa_gateways.GetAll(ctx, service) if err != nil { @@ -1137,10 +1136,12 @@ func importResource(ctx context.Context, cmd *cobra.Command, writer io.Writer, r if helpers.IsInList(gw.Name, []string{"Auto ZPA Gateway"}) { continue } + // Ensure type is always "ZPA" + gw.Type = "ZPA" gwsFiltered = append(gwsFiltered, gw) } - m, _ := json.Marshal(gwsFiltered) resourceCount = len(gwsFiltered) + m, _ := json.Marshal(gwsFiltered) _ = json.Unmarshal(m, &jsonStructData) case "zia_sandbox_rules": if api.ZIAService == nil { diff --git a/docs/guides/release-notes.md b/docs/guides/release-notes.md index c41507d..e9085a3 100644 --- a/docs/guides/release-notes.md +++ b/docs/guides/release-notes.md @@ -12,10 +12,22 @@ Track all Zscaler Terraformer Tool releases. New resources, features, and bug fi --- -``Last updated: v2.0.0`` +``Last updated: v2.0.1`` --- +## 2.0.1 (January, 31 2025) + +### Notes + +- Release date: **(January, 31 2025)** +- Supported Terraform version: **v1.x.x** + +### Bug Fixes +- [PR #257](https://github.com/zscaler/zscaler-terraformer/pull/257). Fixed ZIA import resource for `zia_dlp_notification_templates` due to heredoc missformatting. - [Issue #253](https://github.com/zscaler/zscaler-terraformer/issues/253) +- [PR #257](https://github.com/zscaler/zscaler-terraformer/pull/257). Fixed ZIA import resource for `zia_end_user_notification` due to heredoc missformatting and attribute validation issue. - [Issue #254](https://github.com/zscaler/zscaler-terraformer/issues/254) +- [PR #257](https://github.com/zscaler/zscaler-terraformer/pull/257). Fixed ZIA import resources for: `zia_forwarding_control_rule` due to missing attribute `id` within the `zpa_gateway` block. - [Issue #255](https://github.com/zscaler/zscaler-terraformer/issues/255) +- [PR #257](https://github.com/zscaler/zscaler-terraformer/pull/257). Fixed ZIA import resources for: `zia_forwarding_control_zpa_gateway` due to missing attribute `type`. - [Issue #256](https://github.com/zscaler/zscaler-terraformer/issues/256) ## 2.0.0 (January, 29 2025) - BREAKING CHANGES diff --git a/terraformutils/helpers/helpers.go b/terraformutils/helpers/helpers.go index ba43fda..ce1d689 100644 --- a/terraformutils/helpers/helpers.go +++ b/terraformutils/helpers/helpers.go @@ -419,7 +419,9 @@ func FormatHeredoc(value string) string { for i, line := range lines { trimmedLine := strings.TrimSpace(line) if trimmedLine != "" { - formatted += fmt.Sprintf("%s\n", trimmedLine) + // Escape `$` to `$$` to prevent Terraform interpretation issues + escapedLine := strings.ReplaceAll(trimmedLine, "$", "$$") + formatted += fmt.Sprintf("%s\n", escapedLine) } else if i != len(lines)-1 { formatted += "\n" } diff --git a/terraformutils/nesting/nesting.go b/terraformutils/nesting/nesting.go index c73f78e..7348ea1 100644 --- a/terraformutils/nesting/nesting.go +++ b/terraformutils/nesting/nesting.go @@ -39,6 +39,9 @@ import ( var log = logrus.New() var noSkipIDBlocks = map[string]map[string]bool{ + "zia_forwarding_control_rule": { + "zpa_gateway": true, + }, "zia_firewall_dns_rule": { "dns_gateway": true, },