diff --git a/docs/data-sources/sp_profile.md b/docs/data-sources/sp_profile.md new file mode 100644 index 00000000..826590eb --- /dev/null +++ b/docs/data-sources/sp_profile.md @@ -0,0 +1,32 @@ +--- +# generated by https://github.com/hashicorp/terraform-plugin-docs +page_title: "catalystcenter_sp_profile Data Source - terraform-provider-catalystcenter" +subcategory: "Network Settings" +description: |- + This data source can read the SP Profile. +--- + +# catalystcenter_sp_profile (Data Source) + +This data source can read the SP Profile. + +## Example Usage + +```terraform +data "catalystcenter_sp_profile" "example" { + id = "Profile1" +} +``` + + +## Schema + +### Required + +- `id` (String) The id of the object + +### Read-Only + +- `model` (String) The model +- `name` (String) The name of the SP profile +- `wan_provider` (String) The WAN provider diff --git a/docs/resources/network.md b/docs/resources/network.md index 8bba78d2..b336fcf6 100644 --- a/docs/resources/network.md +++ b/docs/resources/network.md @@ -50,6 +50,7 @@ resource "catalystcenter_network" "example" { - `endpoint_aaa_server_secondary_ip` (String) In case of `ISE` server type, this is the PSN IP address, in case of `AAA` this is a secondary IP address - `endpoint_aaa_server_shared_secret` (String) Only relevant for type `ISE`, shared secret - `endpoint_aaa_server_type` (String) Type of network AAA server + - Choices: `AAA`, `ISE` - `netflow_collector` (String) Netflow collector IP - `netflow_collector_port` (Number) Netflow collector port - `network_aaa_server_primary_ip` (String) In case of `ISE` server type, this is the PAN IP address, in case of `AAA` this is the primary IP address @@ -58,6 +59,7 @@ resource "catalystcenter_network" "example" { - `network_aaa_server_secondary_ip` (String) In case of `ISE` server type, this is the PSN IP address, in case of `AAA` this is a secondary IP address - `network_aaa_server_shared_secret` (String) Only relevant for type `ISE`, shared secret - `network_aaa_server_type` (String) Type of network AAA server + - Choices: `AAA`, `ISE` - `ntp_servers` (List of String) List of NTP server IPs - `primary_dns_server` (String) Primary DNS server IP - `secondary_dns_server` (String) Secondary DNS server IP diff --git a/docs/resources/sp_profile.md b/docs/resources/sp_profile.md new file mode 100644 index 00000000..11c38dce --- /dev/null +++ b/docs/resources/sp_profile.md @@ -0,0 +1,43 @@ +--- +# generated by https://github.com/hashicorp/terraform-plugin-docs +page_title: "catalystcenter_sp_profile Resource - terraform-provider-catalystcenter" +subcategory: "Network Settings" +description: |- + This resource can manage a SP Profile. +--- + +# catalystcenter_sp_profile (Resource) + +This resource can manage a SP Profile. + +## Example Usage + +```terraform +resource "catalystcenter_sp_profile" "example" { + name = "Profile1" + model = "4-class-model" + wan_provider = "Provider1" +} +``` + + +## Schema + +### Required + +- `model` (String) The model + - Choices: `4-class-model`, `5-class-model`, `6-class-model`, `8-class-model` +- `name` (String) The name of the SP profile +- `wan_provider` (String) The WAN provider + +### Read-Only + +- `id` (String) The id of the object + +## Import + +Import is supported using the following syntax: + +```shell +terraform import catalystcenter_sp_profile.example "Profile1" +``` diff --git a/examples/data-sources/catalystcenter_sp_profile/data-source.tf b/examples/data-sources/catalystcenter_sp_profile/data-source.tf new file mode 100644 index 00000000..356758cc --- /dev/null +++ b/examples/data-sources/catalystcenter_sp_profile/data-source.tf @@ -0,0 +1,3 @@ +data "catalystcenter_sp_profile" "example" { + id = "Profile1" +} diff --git a/examples/resources/catalystcenter_sp_profile/import.sh b/examples/resources/catalystcenter_sp_profile/import.sh new file mode 100644 index 00000000..24bab492 --- /dev/null +++ b/examples/resources/catalystcenter_sp_profile/import.sh @@ -0,0 +1 @@ +terraform import catalystcenter_sp_profile.example "Profile1" diff --git a/examples/resources/catalystcenter_sp_profile/resource.tf b/examples/resources/catalystcenter_sp_profile/resource.tf new file mode 100644 index 00000000..246afce8 --- /dev/null +++ b/examples/resources/catalystcenter_sp_profile/resource.tf @@ -0,0 +1,5 @@ +resource "catalystcenter_sp_profile" "example" { + name = "Profile1" + model = "4-class-model" + wan_provider = "Provider1" +} diff --git a/gen/definitions/network.yaml b/gen/definitions/network.yaml index 2ecb5e6e..198ae29c 100644 --- a/gen/definitions/network.yaml +++ b/gen/definitions/network.yaml @@ -101,7 +101,7 @@ attributes: tf_name: network_aaa_server_type type: String write_only: true - enum_value: [AAA, ISE] + enum_values: [AAA, ISE] description: Type of network AAA server example: AAA exclude_test: true @@ -143,7 +143,7 @@ attributes: tf_name: endpoint_aaa_server_type type: String write_only: true - enum_value: [AAA, ISE] + enum_values: [AAA, ISE] description: Type of network AAA server example: AAA exclude_test: true diff --git a/gen/definitions/sp_profile.yaml b/gen/definitions/sp_profile.yaml new file mode 100644 index 00000000..29042713 --- /dev/null +++ b/gen/definitions/sp_profile.yaml @@ -0,0 +1,33 @@ +--- +name: SP Profile +rest_endpoint: /dna/intent/api/v2/service-provider +delete_rest_endpoint: /dna/intent/api/v2/sp-profile +get_from_all: true +id_from_query_path: response.0.value +id_from_attribute: true +doc_category: Network Settings +attributes: + - model_name: profileName + response_model_name: spProfileName + tf_name: name + data_path: settings.qos.0 + response_data_path: spProfileName + type: String + id: true + description: The name of the SP profile + example: Profile1 + - model_name: model + data_path: settings.qos.0 + response_data_path: slaProfileName + type: String + enum_values: [4-class-model, 5-class-model, 6-class-model, 8-class-model] + mandatory: true + description: The model + example: 4-class-model + - model_name: wanProvider + data_path: settings.qos.0 + response_data_path: wanProvider + type: String + mandatory: true + description: The WAN provider + example: Provider1 diff --git a/gen/generator.go b/gen/generator.go index 25a0b1e9..b6384547 100644 --- a/gen/generator.go +++ b/gen/generator.go @@ -97,6 +97,7 @@ type YamlConfig struct { Name string `yaml:"name"` RestEndpoint string `yaml:"rest_endpoint"` GetRestEndpoint string `yaml:"get_rest_endpoint"` + DeleteRestEndpoint string `yaml:"delete_rest_endpoint"` GetNoId bool `yaml:"get_no_id"` GetFromAll bool `yaml:"get_from_all"` GetRequiresId bool `yaml:"get_requires_id"` @@ -107,6 +108,7 @@ type YamlConfig struct { IdPath string `yaml:"id_path"` IdFromQueryPath string `yaml:"id_from_query_path"` IdQueryParam string `yaml:"id_query_param"` + IdFromAttribute string `yaml:"id_from_attribute"` PutIdIncludePath string `yaml:"put_id_include_path"` PutIdQueryParam string `yaml:"put_id_query_param"` DataSourceNameQuery bool `yaml:"data_source_name_query"` diff --git a/gen/schema/schema.yaml b/gen/schema/schema.yaml index bf0c4699..a9953000 100644 --- a/gen/schema/schema.yaml +++ b/gen/schema/schema.yaml @@ -2,6 +2,7 @@ name: str() # Name of the resource rest_endpoint: str() # REST endpoint path get_rest_endpoint: str(required=False) # Override GET REST endpoint path +delete_rest_endpoint: str(required=False) # Override DELETE REST endpoint path get_no_id: bool(required=False) # Set to true if the GET request does not require an ID get_from_all: bool(required=False) # Set to true if GET does not support querying individual objects get_requires_id: bool(required=False) # Set to true if the GET request requires an ID in the URL path @@ -12,6 +13,7 @@ no_read_prefix: bool(required=False) # Set to true if it is an Open API endpoint id_path: str(required=False) # Path to the ID in the response (use "." to access nested elements) id_from_query_path: str(required=False) # Specify path to ID container if ID is not included in POST response and needs to be resolved via separate GET request id_query_param: str(required=False) # Query parameter used to specify the ID +id_from_attribute: bool(required=False) # Set to true if the ID is derived from an attribute put_id_include_path: str(required=False) # If PUT needs to have specific JSON path where ID should be inserted put_id_query_param: str(required=False) # If PUT needs to have specific query parameter where ID should be inserted data_source_name_query: bool(required=False) # Set to true if the data source supports name queries diff --git a/gen/templates/data_source.go b/gen/templates/data_source.go index 32bf547b..13418090 100644 --- a/gen/templates/data_source.go +++ b/gen/templates/data_source.go @@ -220,7 +220,12 @@ func (d *{{camelCase .Name}}DataSource) Read(ctx context.Context, req datasource } {{- if .GetFromAll}} + {{- if .IdFromAttribute}} + {{- $id := getId .Attributes}} + res = res.Get("{{.IdFromQueryPath}}.#({{if $id.ResponseModelName}}{{$id.ResponseModelName}}{{else}}{{$id.ModelName}}{{end}}==\"" + config.Id.ValueString() + "\")") + {{- else}} res = res.Get("{{.IdFromQueryPath}}.#(id==\"" + config.Id.ValueString() + "\")") + {{- end}} {{- end}} config.fromBody(ctx, res) diff --git a/gen/templates/resource.go b/gen/templates/resource.go index 1f8cac7e..7a17f41d 100644 --- a/gen/templates/resource.go +++ b/gen/templates/resource.go @@ -408,9 +408,11 @@ func (r *{{camelCase .Name}}Resource) Create(ctx context.Context, req resource.C resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Failed to configure object (POST), got error: %s, %s", err, res.String())) return } + {{- /* Check if id can be resolved directly from response */}} {{- if .IdPath}} plan.Id = types.StringValue(res.Get("{{.IdPath}}").String()) - {{- else if .IdFromQueryPath}} + {{- /* Check if we need an extra query to resolve the id */}} + {{- else if and .IdFromQueryPath (not .IdFromAttribute)}} {{- $id := getId .Attributes}} params = "" {{- if hasQueryParam .Attributes}} @@ -423,6 +425,7 @@ func (r *{{camelCase .Name}}Resource) Create(ctx context.Context, req resource.C return } plan.Id = types.StringValue(res.Get("{{.IdFromQueryPath}}.#({{if $id.ResponseModelName}}{{$id.ResponseModelName}}{{else}}{{$id.ModelName}}{{end}}==\""+ plan.{{toGoName $id.TfName}}.Value{{$id.Type}}() +"\").id").String()) + {{- /* If we have an id attribute we will use that as id */}} {{- else if hasId .Attributes}} {{- $id := getId .Attributes}} plan.Id = types.StringValue(fmt.Sprint(plan.{{toGoName $id.TfName}}.Value{{$id.Type}}())) @@ -467,7 +470,12 @@ func (r *{{camelCase .Name}}Resource) Read(ctx context.Context, req resource.Rea } {{- if .GetFromAll}} + {{- if .IdFromAttribute}} + {{- $id := getId .Attributes}} + res = res.Get("{{.IdFromQueryPath}}.#({{if $id.ResponseModelName}}{{$id.ResponseModelName}}{{else}}{{$id.ModelName}}{{end}}==\"" + state.{{toGoName $id.TfName}}.Value{{$id.Type}}() + "\")") + {{- else}} res = res.Get("{{.IdFromQueryPath}}.#(id==\"" + state.Id.ValueString() + "\")") + {{- end}} {{- end}} state.updateFromBody(ctx, res) @@ -540,7 +548,7 @@ func (r *{{camelCase .Name}}Resource) Delete(ctx context.Context, req resource.D tflog.Debug(ctx, fmt.Sprintf("%s: Beginning Delete", state.Id.ValueString())) {{- if not .NoDelete}} - res, err := r.client.Delete(state.getPath() + "/" + state.Id.ValueString()) + res, err := r.client.Delete({{if .DeleteRestEndpoint}}"{{.DeleteRestEndpoint}}"{{else}}state.getPath(){{end}} + "/" + state.Id.ValueString()) if err != nil { resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Failed to delete object (DELETE), got error: %s, %s", err, res.String())) return diff --git a/internal/provider/data_source_catalystcenter_sp_profile.go b/internal/provider/data_source_catalystcenter_sp_profile.go new file mode 100644 index 00000000..dd96f505 --- /dev/null +++ b/internal/provider/data_source_catalystcenter_sp_profile.go @@ -0,0 +1,120 @@ +// 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 + +// Code generated by "gen/generator.go"; DO NOT EDIT. + +package provider + +//template:begin imports +import ( + "context" + "fmt" + + "github.com/hashicorp/terraform-plugin-framework/datasource" + "github.com/hashicorp/terraform-plugin-framework/datasource/schema" + "github.com/hashicorp/terraform-plugin-log/tflog" + cc "github.com/netascode/go-catalystcenter" +) + +//template:end imports + +//template:begin model + +// Ensure the implementation satisfies the expected interfaces. +var ( + _ datasource.DataSource = &SPProfileDataSource{} + _ datasource.DataSourceWithConfigure = &SPProfileDataSource{} +) + +func NewSPProfileDataSource() datasource.DataSource { + return &SPProfileDataSource{} +} + +type SPProfileDataSource struct { + client *cc.Client +} + +func (d *SPProfileDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) { + resp.TypeName = req.ProviderTypeName + "_sp_profile" +} + +func (d *SPProfileDataSource) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) { + resp.Schema = schema.Schema{ + // This description is used by the documentation generator and the language server. + MarkdownDescription: "This data source can read the SP Profile.", + + Attributes: map[string]schema.Attribute{ + "id": schema.StringAttribute{ + MarkdownDescription: "The id of the object", + Required: true, + }, + "name": schema.StringAttribute{ + MarkdownDescription: "The name of the SP profile", + Computed: true, + }, + "model": schema.StringAttribute{ + MarkdownDescription: "The model", + Computed: true, + }, + "wan_provider": schema.StringAttribute{ + MarkdownDescription: "The WAN provider", + Computed: true, + }, + }, + } +} + +func (d *SPProfileDataSource) Configure(_ context.Context, req datasource.ConfigureRequest, _ *datasource.ConfigureResponse) { + if req.ProviderData == nil { + return + } + + d.client = req.ProviderData.(*CcProviderData).Client +} + +//template:end model + +//template:begin read +func (d *SPProfileDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) { + var config SPProfile + + // Read config + diags := req.Config.Get(ctx, &config) + resp.Diagnostics.Append(diags...) + if resp.Diagnostics.HasError() { + return + } + + tflog.Debug(ctx, fmt.Sprintf("%s: Beginning Read", config.Id.String())) + + params := "" + res, err := d.client.Get(config.getPath() + params) + if err != nil { + resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Failed to retrieve object, got error: %s", err)) + return + } + res = res.Get("response.0.value.#(spProfileName==\"" + config.Id.ValueString() + "\")") + + config.fromBody(ctx, res) + + tflog.Debug(ctx, fmt.Sprintf("%s: Read finished successfully", config.Id.ValueString())) + + diags = resp.State.Set(ctx, &config) + resp.Diagnostics.Append(diags...) +} + +//template:end read diff --git a/internal/provider/data_source_catalystcenter_sp_profile_test.go b/internal/provider/data_source_catalystcenter_sp_profile_test.go new file mode 100644 index 00000000..4d9058c0 --- /dev/null +++ b/internal/provider/data_source_catalystcenter_sp_profile_test.go @@ -0,0 +1,70 @@ +// 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 + +// Code generated by "gen/generator.go"; DO NOT EDIT. + +package provider + +//template:begin imports +import ( + "testing" + + "github.com/hashicorp/terraform-plugin-testing/helper/resource" +) + +//template:end imports + +//template:begin testAccDataSource +func TestAccDataSourceCcSPProfile(t *testing.T) { + var checks []resource.TestCheckFunc + checks = append(checks, resource.TestCheckResourceAttr("data.catalystcenter_sp_profile.test", "name", "Profile1")) + checks = append(checks, resource.TestCheckResourceAttr("data.catalystcenter_sp_profile.test", "model", "4-class-model")) + checks = append(checks, resource.TestCheckResourceAttr("data.catalystcenter_sp_profile.test", "wan_provider", "Provider1")) + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + ProtoV6ProviderFactories: testAccProtoV6ProviderFactories, + Steps: []resource.TestStep{ + { + Config: testAccDataSourceCcSPProfileConfig(), + Check: resource.ComposeTestCheckFunc(checks...), + }, + }, + }) +} + +//template:end testAccDataSource + +//template:begin testPrerequisites +//template:end testPrerequisites + +//template:begin testAccDataSourceConfig +func testAccDataSourceCcSPProfileConfig() string { + config := `resource "catalystcenter_sp_profile" "test" {` + "\n" + config += ` name = "Profile1"` + "\n" + config += ` model = "4-class-model"` + "\n" + config += ` wan_provider = "Provider1"` + "\n" + config += `}` + "\n" + + config += ` + data "catalystcenter_sp_profile" "test" { + id = catalystcenter_sp_profile.test.id + } + ` + return config +} + +//template:end testAccDataSourceConfig diff --git a/internal/provider/model_catalystcenter_sp_profile.go b/internal/provider/model_catalystcenter_sp_profile.go new file mode 100644 index 00000000..1bf75a39 --- /dev/null +++ b/internal/provider/model_catalystcenter_sp_profile.go @@ -0,0 +1,107 @@ +// 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 + +// Code generated by "gen/generator.go"; DO NOT EDIT. + +package provider + +//template:begin imports +import ( + "context" + + "github.com/hashicorp/terraform-plugin-framework/types" + "github.com/tidwall/gjson" + "github.com/tidwall/sjson" +) + +//template:end imports + +//template:begin types +type SPProfile struct { + Id types.String `tfsdk:"id"` + Name types.String `tfsdk:"name"` + Model types.String `tfsdk:"model"` + WanProvider types.String `tfsdk:"wan_provider"` +} + +//template:end types + +//template:begin getPath +func (data SPProfile) getPath() string { + return "/dna/intent/api/v2/service-provider" +} + +//template:end getPath + +//template:begin toBody +func (data SPProfile) toBody(ctx context.Context, state SPProfile) string { + body := "" + if !data.Name.IsNull() { + body, _ = sjson.Set(body, "settings.qos.0.profileName", data.Name.ValueString()) + } + if !data.Model.IsNull() { + body, _ = sjson.Set(body, "settings.qos.0.model", data.Model.ValueString()) + } + if !data.WanProvider.IsNull() { + body, _ = sjson.Set(body, "settings.qos.0.wanProvider", data.WanProvider.ValueString()) + } + return body +} + +//template:end toBody + +//template:begin fromBody +func (data *SPProfile) fromBody(ctx context.Context, res gjson.Result) { + if value := res.Get("spProfileName"); value.Exists() { + data.Name = types.StringValue(value.String()) + } else { + data.Name = types.StringNull() + } + if value := res.Get("slaProfileName"); value.Exists() { + data.Model = types.StringValue(value.String()) + } else { + data.Model = types.StringNull() + } + if value := res.Get("wanProvider"); value.Exists() { + data.WanProvider = types.StringValue(value.String()) + } else { + data.WanProvider = types.StringNull() + } +} + +//template:end fromBody + +//template:begin updateFromBody +func (data *SPProfile) updateFromBody(ctx context.Context, res gjson.Result) { + if value := res.Get("spProfileName"); value.Exists() && !data.Name.IsNull() { + data.Name = types.StringValue(value.String()) + } else { + data.Name = types.StringNull() + } + if value := res.Get("slaProfileName"); value.Exists() && !data.Model.IsNull() { + data.Model = types.StringValue(value.String()) + } else { + data.Model = types.StringNull() + } + if value := res.Get("wanProvider"); value.Exists() && !data.WanProvider.IsNull() { + data.WanProvider = types.StringValue(value.String()) + } else { + data.WanProvider = types.StringNull() + } +} + +//template:end updateFromBody diff --git a/internal/provider/provider.go b/internal/provider/provider.go index ec61b46b..c9a24a91 100644 --- a/internal/provider/provider.go +++ b/internal/provider/provider.go @@ -253,6 +253,7 @@ func (p *CcProvider) Resources(ctx context.Context) []func() resource.Resource { NewIPPoolResource, NewIPPoolReservationResource, NewNetworkResource, + NewSPProfileResource, } } @@ -271,6 +272,7 @@ func (p *CcProvider) DataSources(ctx context.Context) []func() datasource.DataSo NewIPPoolDataSource, NewIPPoolReservationDataSource, NewNetworkDataSource, + NewSPProfileDataSource, } } diff --git a/internal/provider/resource_catalystcenter_network.go b/internal/provider/resource_catalystcenter_network.go index 95912362..f15190fe 100644 --- a/internal/provider/resource_catalystcenter_network.go +++ b/internal/provider/resource_catalystcenter_network.go @@ -131,8 +131,11 @@ func (r *NetworkResource) Schema(ctx context.Context, req resource.SchemaRequest Required: true, }, "network_aaa_server_type": schema.StringAttribute{ - MarkdownDescription: helpers.NewAttributeDescription("Type of network AAA server").String, + MarkdownDescription: helpers.NewAttributeDescription("Type of network AAA server").AddStringEnumDescription("AAA", "ISE").String, Optional: true, + Validators: []validator.String{ + stringvalidator.OneOf("AAA", "ISE"), + }, }, "network_aaa_server_primary_ip": schema.StringAttribute{ MarkdownDescription: helpers.NewAttributeDescription("In case of `ISE` server type, this is the PAN IP address, in case of `AAA` this is the primary IP address").String, @@ -154,8 +157,11 @@ func (r *NetworkResource) Schema(ctx context.Context, req resource.SchemaRequest Optional: true, }, "endpoint_aaa_server_type": schema.StringAttribute{ - MarkdownDescription: helpers.NewAttributeDescription("Type of network AAA server").String, + MarkdownDescription: helpers.NewAttributeDescription("Type of network AAA server").AddStringEnumDescription("AAA", "ISE").String, Optional: true, + Validators: []validator.String{ + stringvalidator.OneOf("AAA", "ISE"), + }, }, "endpoint_aaa_server_primary_ip": schema.StringAttribute{ MarkdownDescription: helpers.NewAttributeDescription("In case of `ISE` server type, this is the PAN IP address, in case of `AAA` this is the primary IP address").String, diff --git a/internal/provider/resource_catalystcenter_sp_profile.go b/internal/provider/resource_catalystcenter_sp_profile.go new file mode 100644 index 00000000..e644fe60 --- /dev/null +++ b/internal/provider/resource_catalystcenter_sp_profile.go @@ -0,0 +1,238 @@ +// 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 + +// Code generated by "gen/generator.go"; DO NOT EDIT. + +package provider + +//template:begin imports +import ( + "context" + "fmt" + "strings" + + "github.com/CiscoDevNet/terraform-provider-catalystcenter/internal/provider/helpers" + "github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator" + "github.com/hashicorp/terraform-plugin-framework/path" + "github.com/hashicorp/terraform-plugin-framework/resource" + "github.com/hashicorp/terraform-plugin-framework/resource/schema" + "github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier" + "github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier" + "github.com/hashicorp/terraform-plugin-framework/schema/validator" + "github.com/hashicorp/terraform-plugin-framework/types" + "github.com/hashicorp/terraform-plugin-log/tflog" + cc "github.com/netascode/go-catalystcenter" +) + +//template:end imports + +//template:begin model + +// Ensure provider defined types fully satisfy framework interfaces +var _ resource.Resource = &SPProfileResource{} +var _ resource.ResourceWithImportState = &SPProfileResource{} + +func NewSPProfileResource() resource.Resource { + return &SPProfileResource{} +} + +type SPProfileResource struct { + client *cc.Client +} + +func (r *SPProfileResource) Metadata(ctx context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) { + resp.TypeName = req.ProviderTypeName + "_sp_profile" +} + +func (r *SPProfileResource) Schema(ctx context.Context, req resource.SchemaRequest, resp *resource.SchemaResponse) { + resp.Schema = schema.Schema{ + // This description is used by the documentation generator and the language server. + MarkdownDescription: helpers.NewAttributeDescription("This resource can manage a SP Profile.").String, + + Attributes: map[string]schema.Attribute{ + "id": schema.StringAttribute{ + MarkdownDescription: "The id of the object", + Computed: true, + PlanModifiers: []planmodifier.String{ + stringplanmodifier.UseStateForUnknown(), + }, + }, + "name": schema.StringAttribute{ + MarkdownDescription: helpers.NewAttributeDescription("The name of the SP profile").String, + Required: true, + PlanModifiers: []planmodifier.String{ + stringplanmodifier.RequiresReplace(), + }, + }, + "model": schema.StringAttribute{ + MarkdownDescription: helpers.NewAttributeDescription("The model").AddStringEnumDescription("4-class-model", "5-class-model", "6-class-model", "8-class-model").String, + Required: true, + Validators: []validator.String{ + stringvalidator.OneOf("4-class-model", "5-class-model", "6-class-model", "8-class-model"), + }, + }, + "wan_provider": schema.StringAttribute{ + MarkdownDescription: helpers.NewAttributeDescription("The WAN provider").String, + Required: true, + }, + }, + } +} + +func (r *SPProfileResource) Configure(_ context.Context, req resource.ConfigureRequest, _ *resource.ConfigureResponse) { + if req.ProviderData == nil { + return + } + + r.client = req.ProviderData.(*CcProviderData).Client +} + +//template:end model + +//template:begin create +func (r *SPProfileResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) { + var plan SPProfile + + // Read plan + diags := req.Plan.Get(ctx, &plan) + resp.Diagnostics.Append(diags...) + if resp.Diagnostics.HasError() { + return + } + + tflog.Debug(ctx, fmt.Sprintf("%s: Beginning Create", plan.Id.ValueString())) + + // Create object + body := plan.toBody(ctx, SPProfile{}) + + params := "" + res, err := r.client.Post(plan.getPath()+params, body) + if err != nil { + resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Failed to configure object (POST), got error: %s, %s", err, res.String())) + return + } + plan.Id = types.StringValue(fmt.Sprint(plan.Name.ValueString())) + + tflog.Debug(ctx, fmt.Sprintf("%s: Create finished successfully", plan.Id.ValueString())) + + diags = resp.State.Set(ctx, &plan) + resp.Diagnostics.Append(diags...) +} + +//template:end create + +//template:begin read +func (r *SPProfileResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) { + var state SPProfile + + // Read state + diags := req.State.Get(ctx, &state) + resp.Diagnostics.Append(diags...) + if resp.Diagnostics.HasError() { + return + } + + tflog.Debug(ctx, fmt.Sprintf("%s: Beginning Read", state.Id.String())) + + params := "" + res, err := r.client.Get(state.getPath() + params) + if err != nil && strings.Contains(err.Error(), "StatusCode 404") { + resp.State.RemoveResource(ctx) + return + } else if err != nil { + resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Failed to retrieve object (GET), got error: %s, %s", err, res.String())) + return + } + res = res.Get("response.0.value.#(spProfileName==\"" + state.Name.ValueString() + "\")") + + state.updateFromBody(ctx, res) + + tflog.Debug(ctx, fmt.Sprintf("%s: Read finished successfully", state.Id.ValueString())) + + diags = resp.State.Set(ctx, &state) + resp.Diagnostics.Append(diags...) +} + +//template:end read + +//template:begin update +func (r *SPProfileResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) { + var plan, state SPProfile + + // Read plan + diags := req.Plan.Get(ctx, &plan) + resp.Diagnostics.Append(diags...) + if resp.Diagnostics.HasError() { + return + } + // Read state + diags = req.State.Get(ctx, &state) + resp.Diagnostics.Append(diags...) + if resp.Diagnostics.HasError() { + return + } + + tflog.Debug(ctx, fmt.Sprintf("%s: Beginning Update", plan.Id.ValueString())) + + body := plan.toBody(ctx, state) + params := "" + + res, err := r.client.Put(plan.getPath()+"/"+plan.Id.ValueString()+params, body) + if err != nil { + resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Failed to configure object (PUT), got error: %s, %s", err, res.String())) + return + } + + tflog.Debug(ctx, fmt.Sprintf("%s: Update finished successfully", plan.Id.ValueString())) + + diags = resp.State.Set(ctx, &plan) + resp.Diagnostics.Append(diags...) +} + +//template:end update + +//template:begin delete +func (r *SPProfileResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) { + var state SPProfile + + // Read state + diags := req.State.Get(ctx, &state) + resp.Diagnostics.Append(diags...) + if resp.Diagnostics.HasError() { + return + } + + tflog.Debug(ctx, fmt.Sprintf("%s: Beginning Delete", state.Id.ValueString())) + res, err := r.client.Delete("/dna/intent/api/v2/sp-profile" + "/" + state.Id.ValueString()) + if err != nil { + resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Failed to delete object (DELETE), got error: %s, %s", err, res.String())) + return + } + + tflog.Debug(ctx, fmt.Sprintf("%s: Delete finished successfully", state.Id.ValueString())) + + resp.State.RemoveResource(ctx) +} + +//template:end delete + +//template:begin import +func (r *SPProfileResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) { + resource.ImportStatePassthroughID(ctx, path.Root("id"), req, resp) +} + +//template:end import diff --git a/internal/provider/resource_catalystcenter_sp_profile_test.go b/internal/provider/resource_catalystcenter_sp_profile_test.go new file mode 100644 index 00000000..bc61d010 --- /dev/null +++ b/internal/provider/resource_catalystcenter_sp_profile_test.go @@ -0,0 +1,88 @@ +// 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 + +// Code generated by "gen/generator.go"; DO NOT EDIT. + +package provider + +//template:begin imports +import ( + "os" + "testing" + + "github.com/hashicorp/terraform-plugin-testing/helper/resource" +) + +//template:end imports + +//template:begin testAcc +func TestAccCcSPProfile(t *testing.T) { + var checks []resource.TestCheckFunc + checks = append(checks, resource.TestCheckResourceAttr("catalystcenter_sp_profile.test", "name", "Profile1")) + checks = append(checks, resource.TestCheckResourceAttr("catalystcenter_sp_profile.test", "model", "4-class-model")) + checks = append(checks, resource.TestCheckResourceAttr("catalystcenter_sp_profile.test", "wan_provider", "Provider1")) + + var steps []resource.TestStep + if os.Getenv("SKIP_MINIMUM_TEST") == "" { + steps = append(steps, resource.TestStep{ + Config: testAccCcSPProfileConfig_minimum(), + }) + } + steps = append(steps, resource.TestStep{ + Config: testAccCcSPProfileConfig_all(), + Check: resource.ComposeTestCheckFunc(checks...), + }) + steps = append(steps, resource.TestStep{ + ResourceName: "catalystcenter_sp_profile.test", + ImportState: true, + }) + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + ProtoV6ProviderFactories: testAccProtoV6ProviderFactories, + Steps: steps, + }) +} + +//template:end testAcc + +//template:begin testPrerequisites +//template:end testPrerequisites + +//template:begin testAccConfigMinimal +func testAccCcSPProfileConfig_minimum() string { + config := `resource "catalystcenter_sp_profile" "test" {` + "\n" + config += ` name = "Profile1"` + "\n" + config += ` model = "4-class-model"` + "\n" + config += ` wan_provider = "Provider1"` + "\n" + config += `}` + "\n" + return config +} + +//template:end testAccConfigMinimal + +//template:begin testAccConfigAll +func testAccCcSPProfileConfig_all() string { + config := `resource "catalystcenter_sp_profile" "test" {` + "\n" + config += ` name = "Profile1"` + "\n" + config += ` model = "4-class-model"` + "\n" + config += ` wan_provider = "Provider1"` + "\n" + config += `}` + "\n" + return config +} + +//template:end testAccConfigAll