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

add fmc_smart_license #161

Open
wants to merge 15 commits into
base: main
Choose a base branch
from
45 changes: 45 additions & 0 deletions docs/resources/smart_license.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "fmc_smart_license Resource - terraform-provider-fmc"
subcategory: "License"
description: |-
This resource can manage a Smart License.
---

# fmc_smart_license (Resource)

This resource can manage a Smart License.

## Example Usage

```terraform
// Enable Evaluation Mode
resource "fmc_smart_license" "license" {
registration_type = "EVALUATION"
}

// Force to re-register with the provided token
resource "fmc_smart_license" "license" {
registration_type = "REGISTER"
token = "X2M3YmJlY..."
force = true
}
```

<!-- schema generated by tfplugindocs -->
## Schema

### Required

- `registration_type` (String) Action to be executed on the smart license.
- Choices: `REGISTER`, `EVALUATION`

### Optional

- `force` (Boolean) Set to true to re-register smart license.
- `token` (String) Registration token. Mandatory when registrationType set to REGISTER.

### Read-Only

- `id` (String) The id of the object
- `registration_status` (String) Status of a smart license.
11 changes: 11 additions & 0 deletions examples/resources/fmc_smart_license/resource.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Enable Evaluation Mode
resource "fmc_smart_license" "license" {
registration_type = "EVALUATION"
}

// Force to re-register with the provided token
resource "fmc_smart_license" "license" {
registration_type = "REGISTER"
token = "X2M3YmJlY..."
force = true
}
47 changes: 47 additions & 0 deletions gen/definitions/smart_license.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
---
name: Smart License
rest_endpoint: /api/fmc_platform/v1/license/smartlicenses
doc_category: License
no_update: true
no_delete: true
no_import: true
no_data_source: true
domain_independent: true
resource_custom_example: |-
// Enable Evaluation Mode
resource "fmc_smart_license" "license" {
registration_type = "EVALUATION"
}

// Force to re-register with the provided token
resource "fmc_smart_license" "license" {
registration_type = "REGISTER"
token = "X2M3YmJlY..."
force = true
}
attributes:
- model_name: registrationType
type: String
description: Action to be executed on the smart license.
enum_values: [REGISTER, EVALUATION]
mandatory: true
example: EVALUATION
- model_name: token
type: String
description: Registration token. Mandatory when registrationType set to REGISTER.
example: "X2M3YmJlY..."
exclude_test: true
- model_name: regStatus
igiai marked this conversation as resolved.
Show resolved Hide resolved
tf_name: registration_status
type: String
description: Status of a smart license.
computed: true
example: EVALUATION
exclude_test: true
exclude_example: true
- model_name: force
type: Bool
description: Set to true to re-register smart license.
example: false
exclude_test: true
exclude_example: true
2 changes: 2 additions & 0 deletions gen/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,8 @@ type YamlConfig struct {
TestPrerequisites string `yaml:"test_prerequisites"`
IsBulk bool `yaml:"is_bulk"`
ImportNameQuery bool `yaml:"import_name_query"`
ResourceCustomExample string `yaml:"resource_custom_example"`
DomainIndependent bool `yaml:"domain_independent"`
}

type YamlConfigAttribute struct {
Expand Down
3 changes: 2 additions & 1 deletion gen/schema/schema.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ test_tags: list(str(), required=False) # List of test tags, tests are only execu
test_prerequisites: str(required=False) # HCL code that is included in the acceptance tests to define prerequisites
is_bulk: bool(required=False) # Treat this as a bulk resource
import_name_query: bool(required=False) # Set to true if import should be done using object name
resource_custom_example: str(required=False) # If provided, example for resource will not be generated. This custom example will be used instead for documentation
domain_independent: bool(required=False) # If set to true if the resource/data source is not domain dependent
---
attribute:
model_name: str(required=False) # Name of the attribute in the model (payload)
Expand Down Expand Up @@ -56,4 +58,3 @@ attribute:
minimum_test_value: str(required=False) # Value used for "minimum" resource acceptance test
test_tags: list(str(), required=False) # List of test tags, attribute is only included in acceptance tests if an environment variable with one of these tags is configured
attributes: list(include('attribute'), required=False) # List of attributes, only relevant if type is "List" or "Set"

2 changes: 2 additions & 0 deletions gen/templates/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ import (

type {{camelCase .Name}} struct {
Id types.String `tfsdk:"id"`
{{- if not .DomainIndependent}}
Domain types.String `tfsdk:"domain"`
{{- end}}
{{- range .Attributes}}
{{- if not .Value}}
{{- if isNestedListSet .}}
Expand Down
2 changes: 2 additions & 0 deletions gen/templates/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,13 +88,15 @@ func (r *{{camelCase .Name}}Resource) Schema(ctx context.Context, req resource.S
stringplanmodifier.UseStateForUnknown(),
},
},
{{- if not .DomainIndependent}}
"domain": schema.StringAttribute{
MarkdownDescription: "The name of the FMC domain",
Optional: true,
PlanModifiers: []planmodifier.String{
stringplanmodifier.RequiresReplace(),
},
},
{{- end}}
{{- range .Attributes}}
{{- if not .Value}}
"{{.TfName}}": schema.{{if isNestedListMapSet .}}{{.Type}}Nested{{else if isList .}}List{{else if isSet .}}Set{{else if eq .Type "Versions"}}List{{else if eq .Type "Version"}}Int64{{else}}{{.Type}}{{end}}Attribute{
Expand Down
4 changes: 3 additions & 1 deletion gen/templates/resource.tf
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
resource "fmc_{{snakeCase .Name}}" "example" {
{{- if not .ResourceCustomExample}} resource "fmc_{{snakeCase .Name}}" "example" {
{{- range .Attributes}}
{{- if and (not .ExcludeExample) (not .Value) (not .ResourceId) (not .Computed)}}
{{- if isNestedListMapSet .}}
Expand Down Expand Up @@ -51,3 +51,5 @@ resource "fmc_{{snakeCase .Name}}" "example" {
{{- end}}
{{- end}}
}
{{- else}} {{.ResourceCustomExample}}
{{- end}}
118 changes: 118 additions & 0 deletions internal/provider/model_fmc_smart_license.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
// Copyright © 2023 Cisco Systems, Inc. and its affiliates.
// All rights reserved.
//
// Licensed under the Mozilla Public License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://mozilla.org/MPL/2.0/
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// SPDX-License-Identifier: MPL-2.0

package provider

// Section below is generated&owned by "gen/generator.go". //template:begin imports
import (
"context"

"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/tidwall/gjson"
"github.com/tidwall/sjson"
)

// End of section. //template:end imports

// Section below is generated&owned by "gen/generator.go". //template:begin types

type SmartLicense struct {
Id types.String `tfsdk:"id"`
RegistrationType types.String `tfsdk:"registration_type"`
Token types.String `tfsdk:"token"`
RegistrationStatus types.String `tfsdk:"registration_status"`
Force types.Bool `tfsdk:"force"`
}

// End of section. //template:end types

// Section below is generated&owned by "gen/generator.go". //template:begin getPath

func (data SmartLicense) getPath() string {
return "/api/fmc_platform/v1/license/smartlicenses"
}

// End of section. //template:end getPath

func (data SmartLicense) toBody(ctx context.Context, state SmartLicense) string {
body := ""
if data.Id.ValueString() != "" {
body, _ = sjson.Set(body, "id", data.Id.ValueString())
}
if !data.RegistrationType.IsNull() {
body, _ = sjson.Set(body, "registrationType", data.RegistrationType.ValueString())
}
if !data.Token.IsNull() {
body, _ = sjson.Set(body, "token", data.Token.ValueString())
}
return body
}

func (data *SmartLicense) fromBody(ctx context.Context, res gjson.Result) {
if value := res.Get("regStatus"); value.Exists() {
data.RegistrationStatus = types.StringValue(value.String())
} else {
data.RegistrationStatus = types.StringNull()
}
}

// Section below is generated&owned by "gen/generator.go". //template:begin fromBodyPartial

// fromBodyPartial reads values from a gjson.Result into a tfstate model. It ignores null attributes in order to
// uncouple the provider from the exact values that the backend API might summon to replace nulls. (Such behavior might
// easily change across versions of the backend API.) For List/Set/Map attributes, the func only updates the
// "managed" elements, instead of all elements.
func (data *SmartLicense) fromBodyPartial(ctx context.Context, res gjson.Result) {
if value := res.Get("registrationType"); value.Exists() && !data.RegistrationType.IsNull() {
data.RegistrationType = types.StringValue(value.String())
} else {
data.RegistrationType = types.StringNull()
}
if value := res.Get("token"); value.Exists() && !data.Token.IsNull() {
data.Token = types.StringValue(value.String())
} else {
data.Token = types.StringNull()
}
if value := res.Get("regStatus"); value.Exists() && !data.RegistrationStatus.IsNull() {
data.RegistrationStatus = types.StringValue(value.String())
} else {
data.RegistrationStatus = types.StringNull()
}
if value := res.Get("force"); value.Exists() && !data.Force.IsNull() {
data.Force = types.BoolValue(value.Bool())
} else {
data.Force = types.BoolNull()
}
}

// End of section. //template:end fromBodyPartial

// Section below is generated&owned by "gen/generator.go". //template:begin fromBodyUnknowns

// fromBodyUnknowns updates the Unknown Computed tfstate values from a JSON.
// Known values are not changed (usual for Computed attributes with UseStateForUnknown or with Default).
func (data *SmartLicense) fromBodyUnknowns(ctx context.Context, res gjson.Result) {
if data.RegistrationStatus.IsUnknown() {
if value := res.Get("regStatus"); value.Exists() {
data.RegistrationStatus = types.StringValue(value.String())
} else {
data.RegistrationStatus = types.StringNull()
}
}
}

// End of section. //template:end fromBodyUnknowns
1 change: 1 addition & 0 deletions internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,7 @@ func (p *FmcProvider) Resources(ctx context.Context) []func() resource.Resource
NewSecurityZoneResource,
NewSecurityZonesResource,
NewSGTResource,
NewSmartLicenseResource,
NewStandardACLResource,
NewURLResource,
NewURLGroupResource,
Expand Down
Loading
Loading