|
| 1 | +package provider |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + |
| 6 | + "github.com/PaloAltoNetworks/pango/panorama/template" |
| 7 | + "github.com/hashicorp/terraform-plugin-framework/resource" |
| 8 | + "github.com/hashicorp/terraform-plugin-log/tflog" |
| 9 | +) |
| 10 | + |
| 11 | +// TemplateCustom stores state shared between PreCreate and PostCreate hooks. |
| 12 | +type TemplateCustom struct { |
| 13 | + savedDefaultVsys *string |
| 14 | +} |
| 15 | + |
| 16 | +func NewTemplateCustom(data *ProviderData) (*TemplateCustom, error) { |
| 17 | + return &TemplateCustom{}, nil |
| 18 | +} |
| 19 | + |
| 20 | +// PreCreate saves and strips default_vsys from the SDK object before Create, |
| 21 | +// because PAN-OS cannot set this field during initial creation. |
| 22 | +func (o *TemplateResource) PreCreate( |
| 23 | + ctx context.Context, |
| 24 | + req resource.CreateRequest, |
| 25 | + resp *resource.CreateResponse, |
| 26 | + state *TemplateResourceModel, |
| 27 | + location template.Location, |
| 28 | + obj *template.Entry, |
| 29 | + ev *EncryptedValuesManager, |
| 30 | +) { |
| 31 | + o.custom.savedDefaultVsys = obj.DefaultVsys |
| 32 | + obj.DefaultVsys = nil |
| 33 | +} |
| 34 | + |
| 35 | +// PostCreate creates the vsys referenced by default_vsys inside the template, |
| 36 | +// then sets default_vsys via an Update call. The vsys is added directly to the |
| 37 | +// SDK entry's Config struct so the Update's edit action includes it. |
| 38 | +func (o *TemplateResource) PostCreate( |
| 39 | + ctx context.Context, |
| 40 | + req resource.CreateRequest, |
| 41 | + resp *resource.CreateResponse, |
| 42 | + state *TemplateResourceModel, |
| 43 | + location template.Location, |
| 44 | + obj *template.Entry, |
| 45 | + ev *EncryptedValuesManager, |
| 46 | +) { |
| 47 | + if o.custom.savedDefaultVsys == nil { |
| 48 | + return |
| 49 | + } |
| 50 | + |
| 51 | + defaultVsys := *o.custom.savedDefaultVsys |
| 52 | + templateName := state.Name.ValueString() |
| 53 | + |
| 54 | + tflog.Info(ctx, "performing post-create update to set default_vsys", map[string]any{ |
| 55 | + "resource_name": "panos_template", |
| 56 | + "name": templateName, |
| 57 | + "default_vsys": defaultVsys, |
| 58 | + }) |
| 59 | + |
| 60 | + // Populate the Config struct with the vsys entry so the SDK's edit |
| 61 | + // action includes it. PAN-OS requires the vsys to exist before it |
| 62 | + // accepts it as a valid default_vsys reference. |
| 63 | + obj.Config = &template.Config{ |
| 64 | + Devices: []template.ConfigDevices{ |
| 65 | + { |
| 66 | + Name: "localhost.localdomain", |
| 67 | + Vsys: []template.ConfigDevicesVsys{ |
| 68 | + {Name: defaultVsys}, |
| 69 | + }, |
| 70 | + }, |
| 71 | + }, |
| 72 | + } |
| 73 | + obj.DefaultVsys = o.custom.savedDefaultVsys |
| 74 | + |
| 75 | + components, err := state.resourceXpathParentComponents() |
| 76 | + if err != nil { |
| 77 | + resp.Diagnostics.AddError("Error creating resource xpath for post-create update", err.Error()) |
| 78 | + return |
| 79 | + } |
| 80 | + |
| 81 | + updated, err := o.manager.Update(ctx, location, components, obj, "") |
| 82 | + if err != nil { |
| 83 | + resp.Diagnostics.AddError( |
| 84 | + "Error setting default_vsys after create", |
| 85 | + "Template created successfully but setting default_vsys failed. "+ |
| 86 | + "Run terraform apply again to retry. Error: "+err.Error(), |
| 87 | + ) |
| 88 | + return |
| 89 | + } |
| 90 | + |
| 91 | + resp.Diagnostics.Append(state.CopyFromPango(ctx, o.client, nil, updated, ev)...) |
| 92 | +} |
0 commit comments