Skip to content

Commit

Permalink
comments for create and update logic
Browse files Browse the repository at this point in the history
  • Loading branch information
igiai committed Nov 18, 2024
1 parent f6222bf commit 7ae150b
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
2 changes: 1 addition & 1 deletion gen/definitions/smart_license.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions internal/provider/resource_fmc_smart_license.go
Original file line number Diff line number Diff line change
Expand Up @@ -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("")
Expand All @@ -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
Expand Down Expand Up @@ -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("")
Expand All @@ -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 {
Expand All @@ -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()))

Expand Down

0 comments on commit 7ae150b

Please sign in to comment.