Skip to content

Commit

Permalink
Avoid recreating datamap due to API inconsistency
Browse files Browse the repository at this point in the history
  • Loading branch information
wcmjunior committed Nov 2, 2023
1 parent 66aed7c commit 0d59e5c
Showing 1 changed file with 32 additions and 1 deletion.
33 changes: 32 additions & 1 deletion cyral/internal/repository/datamap/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@ type DataMapMapping struct {
}

func (dm *DataMap) WriteToSchema(d *schema.ResourceData) error {
// TODO: If in the future the order of the list of attributes the API
// returns becomes deterministic, this check can be removed. -aholmquist 2022-08-04
currentDataMap := getDatamapFromResource(d)
if currentDataMap.equal(dm) {
return nil
}

var mappings []interface{}
for label, mapping := range dm.Labels {
mappingContents := make(map[string]interface{})
Expand Down Expand Up @@ -61,7 +68,7 @@ func (dm *DataMap) ReadFromSchema(d *schema.ResourceData) error {
return nil
}

func (dm *DataMap) equal(other DataMap) bool {
func (dm *DataMap) equal(other *DataMap) bool {
for label, thisMapping := range dm.Labels {
if otherMapping, ok := other.Labels[label]; ok {
if !utils.ElementsMatch(thisMapping.Attributes, otherMapping.Attributes) {
Expand All @@ -73,3 +80,27 @@ func (dm *DataMap) equal(other DataMap) bool {
}
return true
}

func getDatamapFromResource(d *schema.ResourceData) DataMap {
mappings := d.Get("mapping").(*schema.Set).List()

dataMap := DataMap{
Labels: make(map[string]*DataMapMapping),
}
for _, mappingIface := range mappings {
mapping := mappingIface.(map[string]interface{})

label := mapping["label"].(string)
var attributes []string
if mappingAtts, ok := mapping["attributes"]; ok {
for _, attributeIface := range mappingAtts.([]interface{}) {
attributes = append(attributes, attributeIface.(string))
}
}
dataMap.Labels[label] = &DataMapMapping{
Attributes: attributes,
}
}

return dataMap
}

0 comments on commit 0d59e5c

Please sign in to comment.