From 7ae150b9d9207a52205a0665fb42e1d12d0e1eea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Bernacki?= Date: Mon, 18 Nov 2024 11:25:30 +0100 Subject: [PATCH] comments for create and update logic --- gen/definitions/smart_license.yaml | 2 +- internal/provider/resource_fmc_smart_license.go | 7 +++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/gen/definitions/smart_license.yaml b/gen/definitions/smart_license.yaml index e4a4852e..8a7e7636 100644 --- a/gen/definitions/smart_license.yaml +++ b/gen/definitions/smart_license.yaml @@ -15,7 +15,7 @@ attributes: example: REGISTER - model_name: token type: String - description: Registration token. + description: Registration token. Mandatory when registrationType set to REGISTER. example: "X2M3YmJlY..." exclude_test: true - model_name: regStatus diff --git a/internal/provider/resource_fmc_smart_license.go b/internal/provider/resource_fmc_smart_license.go index 3a4c89b9..b6ace35a 100644 --- a/internal/provider/resource_fmc_smart_license.go +++ b/internal/provider/resource_fmc_smart_license.go @@ -130,6 +130,8 @@ func (r *SmartLicenseResource) Create(ctx context.Context, req resource.CreateRe } state.fromBody(ctx, res.Get("items.0")) + // When smart license is already in evaluation mode and user requests evaluation mode - do nothing + // It's not automatically detected by terraform, because two different fields keep status (RegistrationStatus) and requested status (RegistrationType) if state.RegistrationStatus.ValueString() == "EVALUATION" && plan.RegistrationType.ValueString() == "EVALUATION" { plan.RegistrationStatus = state.RegistrationStatus plan.Id = types.StringValue("") @@ -140,6 +142,7 @@ func (r *SmartLicenseResource) Create(ctx context.Context, req resource.CreateRe tflog.Debug(ctx, fmt.Sprintf("%s: Beginning Create", plan.Id.ValueString())) + // Check if token provided when registration is requested if plan.RegistrationType.ValueString() == "REGISTER" && plan.Token.ValueString() == "" { resp.Diagnostics.AddError("Provider Error", "Token required for registration") return @@ -227,6 +230,7 @@ func (r *SmartLicenseResource) Update(ctx context.Context, req resource.UpdateRe reqMods = append(reqMods, fmc.DomainName(plan.Domain.ValueString())) } + // Same logic as in create if state.RegistrationStatus.ValueString() == "EVALUATION" && plan.RegistrationType.ValueString() == "EVALUATION" { plan.RegistrationStatus = state.RegistrationStatus plan.Id = types.StringValue("") @@ -235,11 +239,13 @@ func (r *SmartLicenseResource) Update(ctx context.Context, req resource.UpdateRe return } + // Check if token provided when registration is requested if plan.RegistrationType.ValueString() == "REGISTER" && plan.Token.ValueString() == "" { resp.Diagnostics.AddError("Provider Error", "Token required for registration") return } + // When re-registration is forced, only deregister license if it is not already unregistered if plan.Force.ValueBool() && state.RegistrationStatus.ValueString() != "UNREGISTERED" { res, err := r.deregisterSmartLicense(ctx, reqMods...) if err != nil { @@ -254,6 +260,7 @@ func (r *SmartLicenseResource) Update(ctx context.Context, req resource.UpdateRe state.fromBody(ctx, res.Get("items.0")) } + // When force flag is not set to true, only register license if it is not already registered if state.RegistrationStatus.ValueString() != "REGISTERED" { tflog.Debug(ctx, fmt.Sprintf("%s: Beginning Update", plan.Id.ValueString()))