Skip to content

Commit

Permalink
Allow updates to service template w/o forcing resource replacement (#551
Browse files Browse the repository at this point in the history
)
  • Loading branch information
shurkys authored Oct 17, 2024
1 parent 5d936fe commit 5d906d1
Showing 1 changed file with 33 additions and 1 deletion.
34 changes: 33 additions & 1 deletion opennebula/resource_opennebula_service_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func resourceOpennebulaServiceTemplate() *schema.Resource {
"template": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ForceNew: false,
Description: "Service Template body in json format",
// Check JSON structure diffs, not binary diffs
DiffSuppressFunc: func(k, old, new string, d *schema.ResourceData) bool {
Expand Down Expand Up @@ -363,6 +363,38 @@ func resourceOpennebulaServiceTemplateUpdate(ctx context.Context, d *schema.Reso
return diags
}

if d.HasChange("template") {
newTemplate := d.Get("template").(string)

// Unmarshal the new template
newSTemplate := &srv_tmpl.ServiceTemplate{}
err := json.Unmarshal([]byte(newTemplate), newSTemplate)
if err != nil {
diags = append(diags, diag.Diagnostic{
Severity: diag.Error,
Summary: "Failed to parse the new template",
Detail: err.Error(),
})
return diags
}

// Update the template
stemplate.Template = newSTemplate.Template

// Apply the changes
err = stc.Update(stemplate, true)
if err != nil {
diags = append(diags, diag.Diagnostic{
Severity: diag.Error,
Summary: "Failed to update the service template",
Detail: err.Error(),
})
return diags
}

log.Printf("[INFO] Successfully updated the template for service template ID %x\n", stemplate.ID)
}

if d.HasChange("name") {
err := stc.Rename(d.Get("name").(string))
if err != nil {
Expand Down

0 comments on commit 5d906d1

Please sign in to comment.