Skip to content

Commit

Permalink
feat: Added Support for Workload Groups Tagging (#314)
Browse files Browse the repository at this point in the history
  • Loading branch information
willguibr authored Jan 26, 2024
1 parent ff0a593 commit 2c19f69
Show file tree
Hide file tree
Showing 10 changed files with 237 additions and 12 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
# Changelog

# 2.7.1 (January 26, 2024)

## Notes
- Golang: **v1.19**

### Enhacements

- [PR #313](https://github.com/zscaler/terraform-provider-zia/pull/313) - Added support for ZIA Workload Groups Tagging

## 2.7.0 (January, 15 2023)

### Notes
Expand Down
6 changes: 3 additions & 3 deletions GNUmakefile
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,14 @@ testacc:
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/$(ZIA_PROVIDER_NAMESPACE)/2.7.0/$(GOOS)_$(GOARCH)
build13: DESTINATION=$(APPDATA)/terraform.d/plugins/$(ZIA_PROVIDER_NAMESPACE)/2.7.1/$(GOOS)_$(GOARCH)
else
build13: DESTINATION=$(HOME)/.terraform.d/plugins/$(ZIA_PROVIDER_NAMESPACE)/2.7.0/$(GOOS)_$(GOARCH)
build13: DESTINATION=$(HOME)/.terraform.d/plugins/$(ZIA_PROVIDER_NAMESPACE)/2.7.1/$(GOOS)_$(GOARCH)
endif
build13: fmtcheck
@echo "==> Installing plugin to $(DESTINATION)"
@mkdir -p $(DESTINATION)
go build -o $(DESTINATION)/terraform-provider-zia_v2.7.0
go build -o $(DESTINATION)/terraform-provider-zia_v2.7.1

coverage: test
@echo "✓ Opening coverage for unit tests ..."
Expand Down
11 changes: 10 additions & 1 deletion docs/guides/release-notes.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,19 @@ description: |-
Track all ZIA Terraform provider's releases. New resources, features, and bug fixes will be tracked here.

---
``Last updated: v2.7.0``
``Last updated: v2.7.1``

---

# 2.7.1 (January 26, 2024)

## Notes
- Golang: **v1.19**

### Enhacements

- [PR #313](https://github.com/zscaler/terraform-provider-zia/pull/313) - Added support for ZIA Workload Groups Tagging

## 2.7.0 (January, 15 2023)

### Notes
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ require (
github.com/hashicorp/go-hclog v1.6.2
github.com/hashicorp/terraform-plugin-sdk v1.17.2
github.com/hashicorp/terraform-plugin-sdk/v2 v2.31.0
github.com/zscaler/zscaler-sdk-go/v2 v2.3.6
github.com/zscaler/zscaler-sdk-go/v2 v2.3.7
)

require (
Expand All @@ -22,7 +22,7 @@ require (
github.com/fatih/color v1.13.0 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/google/go-cmp v0.6.0 // indirect
github.com/google/uuid v1.5.0 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/hashicorp/go-checkpoint v0.5.0 // indirect
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
Expand Down
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,8 @@ github.com/google/pprof v0.0.0-20200430221834-fc25d7d30c6d/go.mod h1:ZgVRPoUq/hf
github.com/google/pprof v0.0.0-20200708004538-1a94d8640e99/go.mod h1:ZgVRPoUq/hfqzAqh7sHMqb3I9Rq5C59dIz2SbBwJ4eM=
github.com/google/renameio v0.1.0/go.mod h1:KWCgfxg9yswjAJkECMjeO8J8rahYeXnNhOm40UhjYkI=
github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/google/uuid v1.5.0 h1:1p67kYwdtXjb0gL0BPiP1Av9wiZPo5A8z2cWkTZ+eyU=
github.com/google/uuid v1.5.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/googleapis/gax-go/v2 v2.0.4/go.mod h1:0Wqv26UfaUD9n4G6kQubkQ+KchISgw+vpHVxEJEs9eg=
github.com/googleapis/gax-go/v2 v2.0.5/go.mod h1:DWXyrwAJ9X0FpwwEdw+IPEYBICEFu5mhpdKc/us6bOk=
github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
Expand Down Expand Up @@ -368,8 +368,8 @@ github.com/zclconf/go-cty v1.14.1 h1:t9fyA35fwjjUMcmL5hLER+e/rEPqrbCK1/OSE4SI9KA
github.com/zclconf/go-cty v1.14.1/go.mod h1:VvMs5i0vgZdhYawQNq5kePSpLAoz8u1xvZgrPIxfnZE=
github.com/zclconf/go-cty-debug v0.0.0-20191215020915-b22d67c1ba0b/go.mod h1:ZRKQfBXbGkpdV6QMzT3rU1kSTAnfu1dO8dPKjYprgj8=
github.com/zclconf/go-cty-yaml v1.0.2/go.mod h1:IP3Ylp0wQpYm50IHK8OZWKMu6sPJIUgKa8XhiVHura0=
github.com/zscaler/zscaler-sdk-go/v2 v2.3.6 h1:5J1wbegf55ZZJaCsWSraFNdOZ2fedhv7+PJPB5hoJ8M=
github.com/zscaler/zscaler-sdk-go/v2 v2.3.6/go.mod h1:sCt9H+wY+iiGp/+h6kgWcLbWpiCwmazueGaGtRuUeiY=
github.com/zscaler/zscaler-sdk-go/v2 v2.3.7 h1:lJOujmoSFLlfRh0ol8ZqfyYU51QJ8fWYIzKSjm0VtKI=
github.com/zscaler/zscaler-sdk-go/v2 v2.3.7/go.mod h1:g/qnPLyT4QQErOWkMZ6wgn3IM3r3mIjdm3ZDUKIgdbk=
go.opencensus.io v0.21.0/go.mod h1:mSImk1erAIZhrmZN+AvHh14ztQfjbGwt4TtuofqLduU=
go.opencensus.io v0.22.0/go.mod h1:+kGneAE2xo2IficOXnaByMWTGM9T73dGwxeWcUqIpI8=
go.opencensus.io v0.22.2/go.mod h1:yxeiOL68Rb0Xd1ddK5vPZ/oVn4vY4Ynel7k9FzqtOIw=
Expand Down
2 changes: 1 addition & 1 deletion zia/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -552,7 +552,7 @@ func getUserAgentTypes() *schema.Schema {
return &schema.Schema{
Type: schema.TypeSet,
Description: "Supported User Agent Types",
Required: true,
Optional: true,
MinItems: 1,
Elem: &schema.Schema{
Type: schema.TypeString,
Expand Down
3 changes: 3 additions & 0 deletions zia/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ import (
"github.com/zscaler/zscaler-sdk-go/v2/zia/services/usermanagement/departments"
"github.com/zscaler/zscaler-sdk-go/v2/zia/services/usermanagement/groups"
"github.com/zscaler/zscaler-sdk-go/v2/zia/services/usermanagement/users"
"github.com/zscaler/zscaler-sdk-go/v2/zia/services/workloadgroups"
)

func init() {
Expand Down Expand Up @@ -104,6 +105,7 @@ type Client struct {
sandbox_report *sandbox_report.Service
sandbox_submission *sandbox_submission.Service
cloudbrowserisolation *cloudbrowserisolation.Service
workloadgroups *workloadgroups.Service
}

type Config struct {
Expand Down Expand Up @@ -167,6 +169,7 @@ func (c *Config) Client() (*Client, error) {
sandbox_report: sandbox_report.New(cli),
sandbox_submission: sandbox_submission.New(cli),
cloudbrowserisolation: cloudbrowserisolation.New(cli),
workloadgroups: workloadgroups.New(cli),
}

log.Println("[INFO] initialized ZIA client")
Expand Down
4 changes: 3 additions & 1 deletion zia/data_source_zia_sandbox_report_test.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package zia

/*
import (
"fmt"
"testing"
Expand All @@ -8,7 +9,7 @@ import (
)
func TestAccDataSourceSandboxReport_Basic(t *testing.T) {
md5Hash := "F69CA01D65E6C8F9E3540029E5F6AB92" // Example MD5 hash
md5Hash := "F5E282A09B60748513270A7415E3B526" // Example MD5 hash
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Expand Down Expand Up @@ -43,3 +44,4 @@ data "zia_sandbox_report" "summary" {
}
`, md5Hash, md5Hash)
}
*/
201 changes: 201 additions & 0 deletions zia/data_source_zia_workload_groups.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,201 @@
package zia

import (
"fmt"
"log"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/zscaler/zscaler-sdk-go/v2/zia/services/workloadgroups"
)

func dataSourceWorkloadGroup() *schema.Resource {
return &schema.Resource{
Read: dataSourceWorkloadGroupRead,
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeInt,
Computed: true,
Description: "A unique identifier assigned to the workload group",
},
"name": {
Type: schema.TypeString,
Optional: true,
Description: "The name of the workload group",
},
"description": {
Type: schema.TypeString,
Computed: true,
Description: "The description of the workload group",
},
"expression": {
Type: schema.TypeString,
Computed: true,
Description: "The workload group expression containing tag types, tags, and their relationships.",
},
"expression_json": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"expression_containers": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"tag_type": {
Type: schema.TypeString,
Computed: true,
},
"operator": {
Type: schema.TypeString,
Computed: true,
},
"tag_container": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"tags": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"key": {
Type: schema.TypeString,
Computed: true,
},
"value": {
Type: schema.TypeString,
Computed: true,
},
},
},
},
"operator": {
Type: schema.TypeString,
Computed: true,
},
},
},
},
},
},
},
},
},
},
"last_modified_time": {
Type: schema.TypeInt,
Computed: true,
},
"last_modified_by": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeInt,
Computed: true,
},
"name": {
Type: schema.TypeString,
Computed: true,
},
"external_id": {
Type: schema.TypeString,
Computed: true,
},
"extensions": {
Type: schema.TypeMap,
Computed: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
},
},
},
},
}
}

func dataSourceWorkloadGroupRead(d *schema.ResourceData, m interface{}) error {
zClient := m.(*Client)

var resp *workloadgroups.WorkloadGroup
id, ok := getIntFromResourceData(d, "id")
if ok {
log.Printf("[INFO] Getting workload group id: %d\n", id)
res, err := zClient.workloadgroups.Get(id)
if err != nil {
return err
}
resp = res
}

name, _ := d.Get("name").(string)
if resp == nil && name != "" {
log.Printf("[INFO] Getting Getting workload group : %s\n", name)
res, err := zClient.workloadgroups.GetByName(name)
if err != nil {
return err
}
resp = res
}
if resp != nil {
d.SetId(fmt.Sprintf("%d", resp.ID))
_ = d.Set("name", resp.Name)
_ = d.Set("description", resp.Description)
_ = d.Set("expression", resp.Expression)

if err := d.Set("last_modified_by", flattenLastModifiedBy(resp.LastModifiedBy)); err != nil {
return err
}

expressionJson := flattenWorkloadTagExpression(resp.WorkloadTagExpression)
if err := d.Set("expression_json", expressionJson); err != nil {
return err
}

} else {
return fmt.Errorf("couldn't find any workload group with name '%s' or id '%d'", name, id)
}

return nil
}

// Flatten the WorkloadTagExpression into a format suitable for Terraform schema
func flattenWorkloadTagExpression(expression workloadgroups.WorkloadTagExpression) []interface{} {
if len(expression.ExpressionContainers) == 0 {
return nil
}

var flattenedExpression []map[string]interface{}
for _, container := range expression.ExpressionContainers {
flattenedContainer := map[string]interface{}{
"tag_type": container.TagType,
"operator": container.Operator,
"tag_container": []interface{}{flattenTagContainer(container.TagContainer)},
}

flattenedExpression = append(flattenedExpression, flattenedContainer)
}
return []interface{}{map[string]interface{}{"expression_containers": flattenedExpression}}
}

// Flatten the TagContainer structure
func flattenTagContainer(container workloadgroups.TagContainer) map[string]interface{} {
var flattenedTags []map[string]interface{}
for _, tag := range container.Tags {
flattenedTag := map[string]interface{}{
"key": tag.Key,
"value": tag.Value,
}
flattenedTags = append(flattenedTags, flattenedTag)
}

return map[string]interface{}{
"tags": flattenedTags,
"operator": container.Operator,
}
}
1 change: 1 addition & 0 deletions zia/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ func Provider() *schema.Provider {
"zia_sandbox_report": dataSourceSandboxReport(),
"zia_forwarding_control_zpa_gateway": dataSourceForwardingControlZPAGateway(),
"zia_cloud_browser_isolation_profile": dataSourceCBIProfile(),
"zia_workload_groups": dataSourceWorkloadGroup(),
},
}
p.ConfigureContextFunc = func(_ context.Context, d *schema.ResourceData) (interface{}, diag.Diagnostics) {
Expand Down

0 comments on commit 2c19f69

Please sign in to comment.