Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dlp rules fix #62

Merged
merged 24 commits into from
Apr 13, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
use set instead of list
  • Loading branch information
amazzalel-habib committed Apr 5, 2022
commit 8b8cc245243b71799fede636971478b3f02e5db2
34 changes: 18 additions & 16 deletions gozscaler/dlpdictionaries/dlpdictionaries.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,29 @@ import (
"log"
"net/http"
"strings"

"github.com/willguibr/terraform-provider-zia/gozscaler/common"
)

const (
dlpDictionariesEndpoint = "/dlpDictionaries"
)

type DlpDictionary struct {
ID int `json:"id"`
Name string `json:"name,omitempty"`
Description string `json:"description,omitempty"`
ConfidenceThreshold string `json:"confidenceThreshold,omitempty"`
CustomPhraseMatchType string `json:"customPhraseMatchType,omitempty"`
NameL10nTag bool `json:"nameL10nTag"`
Custom bool `json:"custom"`
ThresholdType string `json:"thresholdType,omitempty"`
DictionaryType string `json:"dictionaryType,omitempty"`
Proximity int `json:"proximity,omitempty"`
Phrases []Phrases `json:"phrases"`
Patterns []Patterns `json:"patterns"`
EDMMatchDetails []EDMMatchDetails `json:"exactDataMatchDetails"`
// IDMProfileMatchAccuracy []IDMProfileMatchAccuracy `json:"idmProfileMatchAccuracy"`
ID int `json:"id"`
Name string `json:"name,omitempty"`
Description string `json:"description,omitempty"`
ConfidenceThreshold string `json:"confidenceThreshold,omitempty"`
CustomPhraseMatchType string `json:"customPhraseMatchType,omitempty"`
NameL10nTag bool `json:"nameL10nTag"`
Custom bool `json:"custom"`
ThresholdType string `json:"thresholdType,omitempty"`
DictionaryType string `json:"dictionaryType,omitempty"`
Proximity int `json:"proximity,omitempty"`
Phrases []Phrases `json:"phrases"`
Patterns []Patterns `json:"patterns"`
EDMMatchDetails []EDMMatchDetails `json:"exactDataMatchDetails"`
IDMProfileMatchAccuracy []IDMProfileMatchAccuracy `json:"idmProfileMatchAccuracy"`
}

type Phrases struct {
Expand All @@ -48,8 +50,8 @@ type EDMMatchDetails struct {
}

type IDMProfileMatchAccuracy struct {
AdpIdmProfile string `json:"adpIdmProfile,omitempty"`
MatchAccuracy string `json:"matchAccuracy,omitempty"`
AdpIdmProfile *common.IDNameExtensions `json:"adpIdmProfile,omitempty"`
MatchAccuracy string `json:"matchAccuracy,omitempty"`
}

func (service *Service) Get(dlpDictionariesID int) (*DlpDictionary, error) {
Expand Down
25 changes: 20 additions & 5 deletions zia/data_source_zia_dlp_dictionaries.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func dataSourceDLPDictionaries() *schema.Resource {
Computed: true,
},
"phrases": {
Type: schema.TypeList,
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
Expand All @@ -51,7 +51,7 @@ func dataSourceDLPDictionaries() *schema.Resource {
Computed: true,
},
"patterns": {
Type: schema.TypeList,
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
Expand Down Expand Up @@ -79,7 +79,7 @@ func dataSourceDLPDictionaries() *schema.Resource {
Computed: true,
},
"exact_data_match_details": {
Type: schema.TypeList,
Type: schema.TypeSet,
Computed: true,
Description: "Exact Data Match (EDM) related information for custom DLP dictionaries.",
Elem: &schema.Resource{
Expand Down Expand Up @@ -113,13 +113,13 @@ func dataSourceDLPDictionaries() *schema.Resource {
},
},
"idm_profile_match_accuracy": {
Type: schema.TypeList,
Type: schema.TypeSet,
Computed: true,
Description: "List of Indexed Document Match (IDM) profiles and their corresponding match accuracy for custom DLP dictionaries.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"adp_idm_profile": {
Type: schema.TypeList,
Type: schema.TypeSet,
Computed: true,
Description: "The action applied to a DLP dictionary using patterns",
Elem: &schema.Resource{
Expand Down Expand Up @@ -198,6 +198,9 @@ func dataSourceDLPDictionariesRead(d *schema.ResourceData, m interface{}) error
if err := d.Set("exact_data_match_details", flattenEDMDetails(resp)); err != nil {
return err
}
if err := d.Set("idm_profile_match_accuracy", flattenIDMProfileMatchAccuracy(resp)); err != nil {
return err
}

} else {
return fmt.Errorf("couldn't find any dlp dictionary with name '%s' or id '%d'", name, id)
Expand Down Expand Up @@ -244,3 +247,15 @@ func flattenEDMDetails(edm *dlpdictionaries.DlpDictionary) []interface{} {

return edmDetails
}

func flattenIDMProfileMatchAccuracy(edm *dlpdictionaries.DlpDictionary) []interface{} {
idmProfileMatchAccuracies := make([]interface{}, len(edm.IDMProfileMatchAccuracy))
for i, val := range edm.IDMProfileMatchAccuracy {
idmProfileMatchAccuracies[i] = map[string]interface{}{
"match_accuracy": val.MatchAccuracy,
"adp_idm_profile": val.AdpIdmProfile,
}
}

return idmProfileMatchAccuracies
}
177 changes: 125 additions & 52 deletions zia/resource_zia_dlp_dictionaries.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
"github.com/willguibr/terraform-provider-zia/gozscaler/client"
"github.com/willguibr/terraform-provider-zia/gozscaler/common"
"github.com/willguibr/terraform-provider-zia/gozscaler/dlpdictionaries"
)

Expand Down Expand Up @@ -65,7 +66,7 @@ func resourceDLPDictionaries() *schema.Resource {
}, false),
},
"phrases": {
Type: schema.TypeList,
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
Expand All @@ -89,7 +90,7 @@ func resourceDLPDictionaries() *schema.Resource {
}, false),
},
"patterns": {
Type: schema.TypeList,
Type: schema.TypeSet,
Optional: true,
Description: "List containing the patterns used within a custom DLP dictionary. This attribute is not applicable to predefined DLP dictionaries",
Elem: &schema.Resource{
Expand Down Expand Up @@ -128,7 +129,7 @@ func resourceDLPDictionaries() *schema.Resource {
}, false),
},
"exact_data_match_details": {
Type: schema.TypeList,
Type: schema.TypeSet,
Optional: true,
Description: "Exact Data Match (EDM) related information for custom DLP dictionaries.",
Elem: &schema.Resource{
Expand Down Expand Up @@ -170,25 +171,27 @@ func resourceDLPDictionaries() *schema.Resource {
},
},
"idm_profile_match_accuracy": {
Type: schema.TypeList,
Type: schema.TypeSet,
Optional: true,
Computed: true,
Description: "List of Indexed Document Match (IDM) profiles and their corresponding match accuracy for custom DLP dictionaries.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"adp_idm_profile": {
Type: schema.TypeList,
Type: schema.TypeSet,
Optional: true,
MaxItems: 1,
Description: "The action applied to a DLP dictionary using patterns",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeInt,
Computed: true,
Description: "Identifier that uniquely identifies an entity",
Type: schema.TypeInt,
Computed: true,
Optional: true,
},
"extensions": {
Type: schema.TypeMap,
Computed: true,
Optional: true,
Elem: &schema.Schema{
Type: schema.TypeString,
Expand All @@ -197,17 +200,17 @@ func resourceDLPDictionaries() *schema.Resource {
},
},
},
"match_accuracy": {
Type: schema.TypeString,
Optional: true,
Description: "The IDM template match accuracy.",
ValidateFunc: validation.StringInSlice([]string{
"LOW", "MEDIUM", "HEAVY",
}, false),
},
},
},
},
"match_accuracy": {
Type: schema.TypeString,
Optional: true,
Description: "The IDM template match accuracy.",
ValidateFunc: validation.StringInSlice([]string{
"LOW", "MEDIUM", "HEAVY",
}, false),
},
"proximity": {
Type: schema.TypeInt,
Optional: true,
Expand Down Expand Up @@ -278,9 +281,9 @@ func resourceDLPDictionariesRead(d *schema.ResourceData, m interface{}) error {
}

// Need to fully flatten and expand this menu
// if err := d.Set("idm_profile_match_accuracy", flattenIDMProfileMatch(resp)); err != nil {
// return err
// }
if err := d.Set("idm_profile_match_accuracy", flattenIDMProfileMatchAccuracy(resp)); err != nil {
return err
}

return nil
}
Expand Down Expand Up @@ -346,59 +349,129 @@ func expandDLPDictionaries(d *schema.ResourceData) dlpdictionaries.DlpDictionary
if edmDetails != nil {
result.EDMMatchDetails = edmDetails
}

idmProfileMarch := expandIDMProfileMatchAccuracy(d)
if idmProfileMarch != nil {
result.IDMProfileMatchAccuracy = idmProfileMarch
}
return result
}

func expandDLPDictionariesPhrases(d *schema.ResourceData) []dlpdictionaries.Phrases {
var dlpPhraseItems []dlpdictionaries.Phrases
if dlpPhraseInterface, ok := d.GetOk("phrases"); ok {
dlpPhrase := dlpPhraseInterface.([]interface{})
dlpPhraseItems = make([]dlpdictionaries.Phrases, len(dlpPhrase))
for i, phrase := range dlpPhrase {
dlpItem := phrase.(map[string]interface{})
dlpPhraseItems[i] = dlpdictionaries.Phrases{
Action: dlpItem["action"].(string),
Phrase: dlpItem["phrase"].(string),
}
dlpPhraseInterface, ok := d.GetOk("phrases")
if !ok {
return dlpPhraseItems
}
dlpPhrases, ok := dlpPhraseInterface.(*schema.Set)
if !ok {
return dlpPhraseItems
}
for _, dlpItemObj := range dlpPhrases.List() {
dlpItem, ok := dlpItemObj.(map[string]interface{})
if !ok {
return dlpPhraseItems
}
dlpPhraseItems = append(dlpPhraseItems, dlpdictionaries.Phrases{
Action: dlpItem["action"].(string),
Phrase: dlpItem["phrase"].(string),
})
}

return dlpPhraseItems
}

func expandDLPDictionariesPatterns(d *schema.ResourceData) []dlpdictionaries.Patterns {
var dlpPatternsItems []dlpdictionaries.Patterns
if dlpPatternsInterface, ok := d.GetOk("patterns"); ok {
dlpPattern := dlpPatternsInterface.([]interface{})
dlpPatternsItems = make([]dlpdictionaries.Patterns, len(dlpPattern))
for i, pattern := range dlpPattern {
dlpItem := pattern.(map[string]interface{})
dlpPatternsItems[i] = dlpdictionaries.Patterns{
Action: dlpItem["action"].(string),
Pattern: dlpItem["pattern"].(string),
}
dlpPatternsInterface, ok := d.GetOk("patterns")
if !ok {
return dlpPatternsItems
}
dlpPatterns, ok := dlpPatternsInterface.(*schema.Set)
if !ok {
return dlpPatternsItems
}
for _, patternObj := range dlpPatterns.List() {
dlpItem, ok := patternObj.(map[string]interface{})
if !ok {
return dlpPatternsItems
}
dlpPatternsItems = append(dlpPatternsItems, dlpdictionaries.Patterns{
Action: dlpItem["action"].(string),
Pattern: dlpItem["pattern"].(string),
})
}

return dlpPatternsItems
}

func expandEDMDetails(d *schema.ResourceData) []dlpdictionaries.EDMMatchDetails {
var dlpEdmDetails []dlpdictionaries.EDMMatchDetails
if dlpEdmInterface, ok := d.GetOk("exact_data_match_details"); ok {
dlpEdmDetail := dlpEdmInterface.([]interface{})
dlpEdmDetails = make([]dlpdictionaries.EDMMatchDetails, len(dlpEdmDetail))
for i, pattern := range dlpEdmDetail {
dlpEdmItem := pattern.(map[string]interface{})
dlpEdmDetails[i] = dlpdictionaries.EDMMatchDetails{
DictionaryEdmMappingID: dlpEdmItem["dictionaryEdmMappingId"].(int),
SchemaID: dlpEdmItem["schema_id"].(int),
PrimaryField: dlpEdmItem["primary_field"].(int),
SecondaryFields: dlpEdmItem["secondary_fields"].([]int),
SecondaryFieldMatchOn: dlpEdmItem["secondary_field_match_on"].(string),
}
dlpEdmInterface, ok := d.GetOk("exact_data_match_details")
if !ok {
return dlpEdmDetails
}
dlpEdmDetailSet, ok := dlpEdmInterface.(*schema.Set)
if !ok {
return dlpEdmDetails
}
for _, dlpEdmDetailObj := range dlpEdmDetailSet.List() {
dlpEdmItem, ok := dlpEdmDetailObj.(map[string]interface{})
if !ok {
return dlpEdmDetails
}
dlpEdmDetails = append(dlpEdmDetails, dlpdictionaries.EDMMatchDetails{
DictionaryEdmMappingID: dlpEdmItem["dictionaryEdmMappingId"].(int),
SchemaID: dlpEdmItem["schema_id"].(int),
PrimaryField: dlpEdmItem["primary_field"].(int),
SecondaryFields: dlpEdmItem["secondary_fields"].([]int),
SecondaryFieldMatchOn: dlpEdmItem["secondary_field_match_on"].(string),
})
}

return dlpEdmDetails
}

func expandIDMProfileMatchAccuracy(d *schema.ResourceData) []dlpdictionaries.IDMProfileMatchAccuracy {
var idmProfileMatchAccuracies []dlpdictionaries.IDMProfileMatchAccuracy
dlpEdmInterface, ok := d.GetOk("idm_profile_match_accuracy")
if !ok {
return idmProfileMatchAccuracies
}
dlpEdmDetailSet, ok := dlpEdmInterface.(*schema.Set)
if !ok {
return idmProfileMatchAccuracies
}
for _, dlpEdmDetailObj := range dlpEdmDetailSet.List() {
dlpEdmItem, ok := dlpEdmDetailObj.(map[string]interface{})
if !ok {
return idmProfileMatchAccuracies
}
var profile *common.IDNameExtensions
profiles := expandIDMProfile(dlpEdmItem, "adp_idm_profile")
if len(profiles) > 0 {
profile = &profiles[0]
}
idmProfileMatchAccuracies = append(idmProfileMatchAccuracies, dlpdictionaries.IDMProfileMatchAccuracy{
MatchAccuracy: dlpEdmItem["match_accuracy"].(string),
AdpIdmProfile: profile,
})
}
return idmProfileMatchAccuracies
}

func expandIDMProfile(m map[string]interface{}, key string) []common.IDNameExtensions {
setInterface, ok := m[key]
if ok {
set := setInterface.(*schema.Set)
var result []common.IDNameExtensions
for _, item := range set.List() {
itemMap, _ := item.(map[string]interface{})
if itemMap != nil {
result = append(result, common.IDNameExtensions{
ID: itemMap["id"].(int),
Extensions: itemMap["extensions"].(map[string]interface{}),
})
}
}
return result
}
return []common.IDNameExtensions{}
}