-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Added Support for Workload Groups Tagging (#314)
- Loading branch information
Showing
10 changed files
with
237 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters