Skip to content

Commit

Permalink
fix: Fixed PRA and Inspection Application Segment drifts (#489)
Browse files Browse the repository at this point in the history
* fix: Fixed PRA and Inspection Application Segment drifts
  • Loading branch information
willguibr authored Sep 19, 2024
1 parent c504a6f commit bf55dfe
Show file tree
Hide file tree
Showing 10 changed files with 241 additions and 180 deletions.
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
# Changelog

## 3.33.3 (September, 18 2024)

### Notes

- Release date: **(September, 18 2024)**
- Supported Terraform version: **v1.x**

### Bug Fixes
- [PR #489](https://github.com/zscaler/terraform-provider-zpa/pull/489) - Fixed drift within attribute `common_apps_dto` and `pra_apps` in the `zpa_application_segment_pra` resource.
- [PR #489](https://github.com/zscaler/terraform-provider-zpa/pull/489) - Fixed drift within attribute `common_apps_dto` and `pra_apps` in the `zpa_application_segment_inspection` resource.
~> **NOTE** This fix does not affect existing configurations.

## 3.33.2 (September, 10 2024)

### Notes
Expand Down
6 changes: 3 additions & 3 deletions GNUmakefile
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,14 @@ test\:integration\:zpa:
build13: GOOS=$(shell go env GOOS)
build13: GOARCH=$(shell go env GOARCH)
ifeq ($(OS),Windows_NT) # is Windows_NT on XP, 2000, 7, Vista, 10...
build13: DESTINATION=$(APPDATA)/terraform.d/plugins/$(ZPA_PROVIDER_NAMESPACE)/3.33.2/$(GOOS)_$(GOARCH)
build13: DESTINATION=$(APPDATA)/terraform.d/plugins/$(ZPA_PROVIDER_NAMESPACE)/3.33.3/$(GOOS)_$(GOARCH)
else
build13: DESTINATION=$(HOME)/.terraform.d/plugins/$(ZPA_PROVIDER_NAMESPACE)/3.33.2/$(GOOS)_$(GOARCH)
build13: DESTINATION=$(HOME)/.terraform.d/plugins/$(ZPA_PROVIDER_NAMESPACE)/3.33.3/$(GOOS)_$(GOARCH)
endif
build13: fmtcheck
@echo "==> Installing plugin to $(DESTINATION)"
@mkdir -p $(DESTINATION)
go build -o $(DESTINATION)/terraform-provider-zpa_v3.33.2
go build -o $(DESTINATION)/terraform-provider-zpa_v3.33.3

vet:
@echo "==> Checking source code against go vet and staticcheck"
Expand Down
14 changes: 13 additions & 1 deletion docs/guides/release-notes.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,22 @@ Track all ZPA Terraform provider's releases. New resources, features, and bug fi

---

``Last updated: v3.33.2``
``Last updated: v3.33.3``

---

## 3.33.3 (September, 18 2024)

### Notes

- Release date: **(September, 18 2024)**
- Supported Terraform version: **v1.x**

### Bug Fixes
- [PR #489](https://github.com/zscaler/terraform-provider-zpa/pull/489) - Fixed drift within attribute `common_apps_dto` and `pra_apps` in the `zpa_application_segment_pra` resource.
- [PR #489](https://github.com/zscaler/terraform-provider-zpa/pull/489) - Fixed drift within attribute `common_apps_dto` and `pra_apps` in the `zpa_application_segment_inspection` resource.
~> **NOTE** This fix does not affect existing configurations.

## 3.33.2 (September, 10 2024)

### Notes
Expand Down
4 changes: 2 additions & 2 deletions zpa/data_source_zpa_application_segment_by_type_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ func testAccDataSourceApplicationSegmentByTypeCheck(application_type string) res
func testAccCheckDataSourceApplicationSegmentByTypeConfig_basic(resourceNameSuffix, domainNameSuffix string) string {
return fmt.Sprintf(`
resource "zpa_segment_group" "this" {
name = "tf-acc-test-10"
description = "tf-acc-test-10"
name = "tf-acc-test-100"
description = "tf-acc-test-100"
enabled = true
}
Expand Down
46 changes: 15 additions & 31 deletions zpa/data_source_zpa_application_segment_inspection.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,18 +96,26 @@ func dataSourceApplicationSegmentInspection() *schema.Resource {
Description: "Name of the application.",
},
"inspection_apps": {
Type: schema.TypeList,
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Computed: true,
},
"app_id": {
"name": {
Type: schema.TypeString,
Computed: true,
},
"description": {
Type: schema.TypeString,
Computed: true,
},
"enabled": {
Type: schema.TypeBool,
Computed: true,
},
"application_port": {
Type: schema.TypeString,
Computed: true,
Expand All @@ -124,22 +132,18 @@ func dataSourceApplicationSegmentInspection() *schema.Resource {
Type: schema.TypeString,
Computed: true,
},
"description": {
"domain": {
Type: schema.TypeString,
Computed: true,
},
"domain": {
"app_id": {
Type: schema.TypeString,
Computed: true,
},
"enabled": {
"trusted_untrusted_cert": {
Type: schema.TypeBool,
Computed: true,
},
"name": {
Type: schema.TypeString,
Computed: true,
},
},
},
},
Expand Down Expand Up @@ -224,8 +228,8 @@ func dataSourceApplicationSegmentInspectionRead(d *schema.ResourceData, meta int
_ = d.Set("tcp_port_ranges", resp.TCPPortRanges)
_ = d.Set("udp_port_ranges", resp.UDPPortRanges)

if err := d.Set("inspection_apps", flattenInspectionApps(resp)); err != nil {
return fmt.Errorf("failed to read inspection apps %s", err)
if err := d.Set("inspection_apps", flattenInspectionApps(resp.InspectionAppDto)); err != nil {
return fmt.Errorf("failed to read inspection apps in application segment %s", err)
}

if err := d.Set("server_groups", flattenInspectionAppServerGroups(resp.AppServerGroups)); err != nil {
Expand Down Expand Up @@ -257,23 +261,3 @@ func flattenInspectionAppServerGroups(appServerGroup []applicationsegmentinspect
result[0] = mapIds
return result
}

func flattenInspectionApps(inspectionApp *applicationsegmentinspection.AppSegmentInspection) []interface{} {
inspectionApps := make([]interface{}, len(inspectionApp.InspectionAppDto))
for i, val := range inspectionApp.InspectionAppDto {
inspectionApps[i] = map[string]interface{}{
"id": val.ID,
"app_id": val.AppID,
"application_port": val.ApplicationPort,
"application_protocol": val.ApplicationProtocol,
"certificate_id": val.CertificateID,
"certificate_name": val.CertificateName,
"description": val.Description,
"domain": val.Domain,
"enabled": val.Enabled,
"name": val.Name,
}
}

return inspectionApps
}
144 changes: 83 additions & 61 deletions zpa/resource_zpa_application_segment_inspection.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package zpa
import (
"fmt"
"log"
"strconv"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
Expand All @@ -14,32 +13,11 @@ import (

func resourceApplicationSegmentInspection() *schema.Resource {
return &schema.Resource{
Create: resourceApplicationSegmentInspectionCreate,
Read: resourceApplicationSegmentInspectionRead,
Update: resourceApplicationSegmentInspectionUpdate,
Delete: resourceApplicationSegmentInspectionDelete,
Importer: &schema.ResourceImporter{
State: func(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
client := meta.(*Client)
service := client.ApplicationSegmentInspection

id := d.Id()
_, parseIDErr := strconv.ParseInt(id, 10, 64)
if parseIDErr == nil {
// assume if the passed value is an int
d.Set("id", id)
} else {
resp, _, err := applicationsegmentinspection.GetByName(service, id)
if err == nil {
d.SetId(resp.ID)
d.Set("id", resp.ID)
} else {
return []*schema.ResourceData{d}, err
}
}
return []*schema.ResourceData{d}, nil
},
},
Create: resourceApplicationSegmentInspectionCreate,
Read: resourceApplicationSegmentInspectionRead,
Update: resourceApplicationSegmentInspectionUpdate,
Delete: resourceApplicationSegmentInspectionDelete,
Importer: &schema.ResourceImporter{},

Schema: map[string]*schema.Schema{
"id": {
Expand Down Expand Up @@ -188,6 +166,54 @@ func resourceApplicationSegmentInspection() *schema.Resource {
"0", "1",
}, false),
},
"inspection_apps": {
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Computed: true,
},
"name": {
Type: schema.TypeString,
Computed: true,
},
"enabled": {
Type: schema.TypeBool,
Computed: true,
},
"application_port": {
Type: schema.TypeString,
Computed: true,
},
"application_protocol": {
Type: schema.TypeString,
Computed: true,
},
"certificate_id": {
Type: schema.TypeString,
Computed: true,
},
"certificate_name": {
Type: schema.TypeString,
Computed: true,
},
"domain": {
Type: schema.TypeString,
Computed: true,
},
"app_id": {
Type: schema.TypeString,
Computed: true,
},
"trusted_untrusted_cert": {
Type: schema.TypeBool,
Computed: true,
},
},
},
},
"common_apps_dto": {
Type: schema.TypeSet,
Optional: true,
Expand Down Expand Up @@ -340,8 +366,8 @@ func resourceApplicationSegmentInspectionRead(d *schema.ResourceData, meta inter
_ = d.Set("udp_port_ranges", convertPortsToListString(resp.UDPAppPortRange))
_ = d.Set("server_groups", flattenInspectionAppServerGroupsSimple(resp.AppServerGroups))

if err := d.Set("common_apps_dto", flattenInspectionCommonAppsDto(resp.InspectionAppDto)); err != nil {
return fmt.Errorf("failed to read common application in application segment %s", err)
if err := d.Set("inspection_apps", flattenInspectionApps(resp.InspectionAppDto)); err != nil {
return fmt.Errorf("failed to read inspection apps in application segment %s", err)
}

if err := d.Set("tcp_port_range", flattenNetworkPorts(resp.TCPAppPortRange)); err != nil {
Expand All @@ -354,18 +380,6 @@ func resourceApplicationSegmentInspectionRead(d *schema.ResourceData, meta inter
return nil
}

func flattenInspectionAppServerGroupsSimple(serverGroup []applicationsegmentinspection.AppServerGroups) []interface{} {
result := make([]interface{}, 1)
mapIds := make(map[string]interface{})
ids := make([]string, len(serverGroup))
for i, group := range serverGroup {
ids[i] = group.ID
}
mapIds["id"] = ids
result[0] = mapIds
return result
}

func resourceApplicationSegmentInspectionUpdate(d *schema.ResourceData, meta interface{}) error {
zClient := meta.(*Client)
service := zClient.ApplicationSegmentInspection
Expand Down Expand Up @@ -559,33 +573,41 @@ func expandInspectionAppServerGroups(d *schema.ResourceData) []applicationsegmen
return []applicationsegmentinspection.AppServerGroups{}
}

func flattenInspectionCommonAppsDto(apps []applicationsegmentinspection.InspectionAppDto) []interface{} {
commonAppsDto := make([]interface{}, 1)
func flattenInspectionAppServerGroupsSimple(serverGroup []applicationsegmentinspection.AppServerGroups) []interface{} {
result := make([]interface{}, 1)
mapIds := make(map[string]interface{})
ids := make([]string, len(serverGroup))
for i, group := range serverGroup {
ids[i] = group.ID
}
mapIds["id"] = ids
result[0] = mapIds
return result
}

func flattenInspectionApps(apps []applicationsegmentinspection.InspectionAppDto) []interface{} {
if len(apps) == 0 {
return []interface{}{}
}

appsConfig := make([]interface{}, len(apps))
for i, app := range apps {
appTypes := []string{}
if app.ApplicationProtocol == "HTTP" || app.ApplicationProtocol == "HTTPS" {
appTypes = append(appTypes, "INSPECT")
}
appConfigMap := map[string]interface{}{
"id": app.ID,
"name": app.Name,
"enabled": app.Enabled,
"domain": app.Domain,
"application_port": app.ApplicationPort,
"certificate_id": app.CertificateID,
"application_protocol": app.ApplicationProtocol,
"trust_untrusted_cert": app.TrustUntrustedCert,
"app_types": appTypes,
"id": app.ID,
"name": app.Name,
"enabled": app.Enabled,
"application_port": app.ApplicationPort,
"application_protocol": app.ApplicationProtocol,
"certificate_id": app.CertificateID,
"certificate_name": app.CertificateName,
"domain": app.Domain,
"app_id": app.AppID,
"trusted_untrusted_cert": app.TrustUntrustedCert,
}
appsConfig[i] = appConfigMap
}
commonAppsDto[0] = map[string]interface{}{
"apps_config": appsConfig,
}
return commonAppsDto
return appsConfig
}

func validateProtocolAndCertID(d *schema.ResourceData) error {
commonAppsDto, ok := d.GetOk("common_apps_dto")
if !ok || len(commonAppsDto.(*schema.Set).List()) == 0 {
Expand Down
10 changes: 5 additions & 5 deletions zpa/resource_zpa_application_segment_inspection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,11 @@ func TestAccResourceApplicationSegmentInspection_Basic(t *testing.T) {
),
},
// Import test
{
ResourceName: appSegmentTypeAndName,
ImportState: true,
ImportStateVerify: true,
},
// {
// ResourceName: appSegmentTypeAndName,
// ImportState: true,
// ImportStateVerify: true,
// },
},
})
}
Expand Down
Loading

0 comments on commit bf55dfe

Please sign in to comment.