Skip to content

Commit

Permalink
B #468: Improve service_template idempotence
Browse files Browse the repository at this point in the history
  • Loading branch information
vickmp authored and frousselet committed Dec 11, 2023
1 parent f50ce4f commit e2773a7
Showing 1 changed file with 29 additions and 1 deletion.
30 changes: 29 additions & 1 deletion opennebula/resource_opennebula_service_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ func resourceOpennebulaServiceTemplate() *schema.Resource {
return false
}

return reflect.DeepEqual(old_template, new_template)
// Custom deepEqual func to avoid empty fields #468
return deepEqualIgnoreEmpty(old_template, new_template)
},
},
"permissions": {
Expand Down Expand Up @@ -565,3 +566,30 @@ func changeServiceTemplateName(d *schema.ResourceData, meta interface{}, stc *go

return nil
}

func deepEqualIgnoreEmpty(old_template, new_template *srv_tmpl.ServiceTemplate) bool {
old_map := structToMap(old_template)
new_map := structToMap(new_template)

for key, oldValue := range old_map {
// Ignore empty fields since they are not returned by the OCA
if isEmptyValue(reflect.ValueOf(oldValue)) {
continue
}

// If the field is not present or not equal in the new template, return false
newValue, ok := new_map[key]
if !ok || !reflect.DeepEqual(oldValue, newValue) {
return false
}
}

return true
}

func structToMap(obj interface{}) map[string]interface{} {
jsonBytes, _ := json.Marshal(obj)
var mapObj map[string]interface{}
json.Unmarshal(jsonBytes, &mapObj)
return mapObj
}

0 comments on commit e2773a7

Please sign in to comment.