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

Allow Updates to template Attribute in opennebula_service_template Resource Without Forcing New Resource #551

Merged
merged 2 commits into from
Oct 17, 2024
Merged
Changes from all commits
Commits
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
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
Loading