diff --git a/CHANGELOG.md b/CHANGELOG.md index a12a0e3a..6ad3570f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -42,6 +42,7 @@ - Add `sdwan_policy_object_security_geolocation_profile_parcel` resource and data source - Add `sdwan_policy_object_security_ips_signature_profile_parcel` resource and data source - Add `sdwan_policy_object_security_localdomain_profile_parcel` resource and data source +- Add `sdwan_policy_object_security_port_profile_parcel` resource and data source ## 0.3.13 diff --git a/docs/data-sources/policy_object_security_port_profile_parcel.md b/docs/data-sources/policy_object_security_port_profile_parcel.md new file mode 100644 index 00000000..6ed68170 --- /dev/null +++ b/docs/data-sources/policy_object_security_port_profile_parcel.md @@ -0,0 +1,42 @@ +--- +# generated by https://github.com/hashicorp/terraform-plugin-docs +page_title: "sdwan_policy_object_security_port_profile_parcel Data Source - terraform-provider-sdwan" +subcategory: "Profile Parcels" +description: |- + This data source can read the Policy Object Security Port profile parcel. +--- + +# sdwan_policy_object_security_port_profile_parcel (Data Source) + +This data source can read the Policy Object Security Port profile parcel. + +## Example Usage + +```terraform +data "sdwan_policy_object_security_port_profile_parcel" "example" { + id = "f6b2c44c-693c-4763-b010-895aa3d236bd" + feature_profile_id = "f6dd22c8-0b4f-496c-9a0b-6813d1f8b8ac" +} +``` + + +## Schema + +### Required + +- `feature_profile_id` (String) Feature Profile ID +- `id` (String) The id of the profile parcel + +### Read-Only + +- `description` (String) The description of the profile parcel +- `entries` (Attributes List) Port List (see [below for nested schema](#nestedatt--entries)) +- `name` (String) The name of the profile parcel +- `version` (Number) The version of the profile parcel + + +### Nested Schema for `entries` + +Read-Only: + +- `port` (String) can be single port or port range diff --git a/docs/guides/changelog.md b/docs/guides/changelog.md index 2f045b7d..308940a4 100644 --- a/docs/guides/changelog.md +++ b/docs/guides/changelog.md @@ -51,6 +51,7 @@ description: |- - Add `sdwan_policy_object_security_geolocation_profile_parcel` resource and data source - Add `sdwan_policy_object_security_ips_signature_profile_parcel` resource and data source - Add `sdwan_policy_object_security_localdomain_profile_parcel` resource and data source +- Add `sdwan_policy_object_security_port_profile_parcel` resource and data source ## 0.3.13 diff --git a/docs/resources/policy_object_security_port_profile_parcel.md b/docs/resources/policy_object_security_port_profile_parcel.md new file mode 100644 index 00000000..c1bee5a9 --- /dev/null +++ b/docs/resources/policy_object_security_port_profile_parcel.md @@ -0,0 +1,61 @@ +--- +# generated by https://github.com/hashicorp/terraform-plugin-docs +page_title: "sdwan_policy_object_security_port_profile_parcel Resource - terraform-provider-sdwan" +subcategory: "Profile Parcels" +description: |- + This resource can manage a Policy Object Security Port profile parcel. + Minimum SD-WAN Manager version: 20.12.0 +--- + +# sdwan_policy_object_security_port_profile_parcel (Resource) + +This resource can manage a Policy Object Security Port profile parcel. + - Minimum SD-WAN Manager version: `20.12.0` + +## Example Usage + +```terraform +resource "sdwan_policy_object_security_port_profile_parcel" "example" { + name = "Example" + description = "My Example" + feature_profile_id = "f6dd22c8-0b4f-496c-9a0b-6813d1f8b8ac" + entries = [ + { + port = "100" + } + ] +} +``` + + +## Schema + +### Required + +- `entries` (Attributes List) Port List (see [below for nested schema](#nestedatt--entries)) +- `feature_profile_id` (String) Feature Profile ID +- `name` (String) The name of the profile parcel + +### Optional + +- `description` (String) The description of the profile parcel + +### Read-Only + +- `id` (String) The id of the profile parcel +- `version` (Number) The version of the profile parcel + + +### Nested Schema for `entries` + +Optional: + +- `port` (String) can be single port or port range + +## Import + +Import is supported using the following syntax: + +```shell +terraform import sdwan_policy_object_security_port_profile_parcel.example "f6b2c44c-693c-4763-b010-895aa3d236bd" +``` diff --git a/examples/data-sources/sdwan_policy_object_security_port_profile_parcel/data-source.tf b/examples/data-sources/sdwan_policy_object_security_port_profile_parcel/data-source.tf new file mode 100644 index 00000000..3216e905 --- /dev/null +++ b/examples/data-sources/sdwan_policy_object_security_port_profile_parcel/data-source.tf @@ -0,0 +1,4 @@ +data "sdwan_policy_object_security_port_profile_parcel" "example" { + id = "f6b2c44c-693c-4763-b010-895aa3d236bd" + feature_profile_id = "f6dd22c8-0b4f-496c-9a0b-6813d1f8b8ac" +} diff --git a/examples/resources/sdwan_policy_object_security_port_profile_parcel/import.sh b/examples/resources/sdwan_policy_object_security_port_profile_parcel/import.sh new file mode 100644 index 00000000..57f4b818 --- /dev/null +++ b/examples/resources/sdwan_policy_object_security_port_profile_parcel/import.sh @@ -0,0 +1 @@ +terraform import sdwan_policy_object_security_port_profile_parcel.example "f6b2c44c-693c-4763-b010-895aa3d236bd" diff --git a/examples/resources/sdwan_policy_object_security_port_profile_parcel/resource.tf b/examples/resources/sdwan_policy_object_security_port_profile_parcel/resource.tf new file mode 100644 index 00000000..7a014511 --- /dev/null +++ b/examples/resources/sdwan_policy_object_security_port_profile_parcel/resource.tf @@ -0,0 +1,10 @@ +resource "sdwan_policy_object_security_port_profile_parcel" "example" { + name = "Example" + description = "My Example" + feature_profile_id = "f6dd22c8-0b4f-496c-9a0b-6813d1f8b8ac" + entries = [ + { + port = "100" + } + ] +} diff --git a/gen/definitions/profile_parcels/policy_object_security_port.yaml b/gen/definitions/profile_parcels/policy_object_security_port.yaml new file mode 100644 index 00000000..71185049 --- /dev/null +++ b/gen/definitions/profile_parcels/policy_object_security_port.yaml @@ -0,0 +1,24 @@ +--- +name: Policy Object Security Port +rest_endpoint: /v1/feature-profile/sdwan/policy-object/%v/security-port +minimum_version: 20.12.0 +test_tags: [SDWAN_2012, TF_VAR_policy_object_feature_template_id] +skip_minimum_test: true +attributes: + - tf_name: feature_profile_id + reference: true + type: String + mandatory: true + description: Feature Profile ID + example: f6dd22c8-0b4f-496c-9a0b-6813d1f8b8ac + test_value: var.policy_object_feature_template_id + - model_name: entries + mandatory: true + attributes: + - model_name: port + id: true + mandatory: true + example: 100 + +test_prerequisites: | + variable "policy_object_feature_template_id" {} \ No newline at end of file diff --git a/internal/provider/data_source_sdwan_policy_object_security_port_profile_parcel.go b/internal/provider/data_source_sdwan_policy_object_security_port_profile_parcel.go new file mode 100644 index 00000000..838040c7 --- /dev/null +++ b/internal/provider/data_source_sdwan_policy_object_security_port_profile_parcel.go @@ -0,0 +1,133 @@ +// 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" + "fmt" + "net/url" + + "github.com/hashicorp/terraform-plugin-framework/datasource" + "github.com/hashicorp/terraform-plugin-framework/datasource/schema" + "github.com/hashicorp/terraform-plugin-log/tflog" + "github.com/netascode/go-sdwan" +) + +// End of section. //template:end imports + +// Section below is generated&owned by "gen/generator.go". //template:begin model + +// Ensure the implementation satisfies the expected interfaces. +var ( + _ datasource.DataSource = &PolicyObjectSecurityPortProfileParcelDataSource{} + _ datasource.DataSourceWithConfigure = &PolicyObjectSecurityPortProfileParcelDataSource{} +) + +func NewPolicyObjectSecurityPortProfileParcelDataSource() datasource.DataSource { + return &PolicyObjectSecurityPortProfileParcelDataSource{} +} + +type PolicyObjectSecurityPortProfileParcelDataSource struct { + client *sdwan.Client +} + +func (d *PolicyObjectSecurityPortProfileParcelDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) { + resp.TypeName = req.ProviderTypeName + "_policy_object_security_port_profile_parcel" +} + +func (d *PolicyObjectSecurityPortProfileParcelDataSource) 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 Policy Object Security Port profile parcel.", + + Attributes: map[string]schema.Attribute{ + "id": schema.StringAttribute{ + MarkdownDescription: "The id of the profile parcel", + Required: true, + }, + "version": schema.Int64Attribute{ + MarkdownDescription: "The version of the profile parcel", + Computed: true, + }, + "name": schema.StringAttribute{ + MarkdownDescription: "The name of the profile parcel", + Computed: true, + }, + "description": schema.StringAttribute{ + MarkdownDescription: "The description of the profile parcel", + Computed: true, + }, + "feature_profile_id": schema.StringAttribute{ + MarkdownDescription: "Feature Profile ID", + Required: true, + }, + "entries": schema.ListNestedAttribute{ + MarkdownDescription: "Port List", + Computed: true, + NestedObject: schema.NestedAttributeObject{ + Attributes: map[string]schema.Attribute{ + "port": schema.StringAttribute{ + MarkdownDescription: "can be single port or port range", + Computed: true, + }, + }, + }, + }, + }, + } +} + +func (d *PolicyObjectSecurityPortProfileParcelDataSource) Configure(_ context.Context, req datasource.ConfigureRequest, _ *datasource.ConfigureResponse) { + if req.ProviderData == nil { + return + } + + d.client = req.ProviderData.(*SdwanProviderData).Client +} + +// End of section. //template:end model + +// Section below is generated&owned by "gen/generator.go". //template:begin read +func (d *PolicyObjectSecurityPortProfileParcelDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) { + var config PolicyObjectSecurityPort + + // 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())) + + res, err := d.client.Get(config.getPath() + "/" + url.QueryEscape(config.Id.ValueString())) + if err != nil { + resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Failed to retrieve object, got error: %s", err)) + return + } + + config.fromBody(ctx, res) + + tflog.Debug(ctx, fmt.Sprintf("%s: Read finished successfully", config.Name.ValueString())) + + diags = resp.State.Set(ctx, &config) + resp.Diagnostics.Append(diags...) +} + +// End of section. //template:end read diff --git a/internal/provider/data_source_sdwan_policy_object_security_port_profile_parcel_test.go b/internal/provider/data_source_sdwan_policy_object_security_port_profile_parcel_test.go new file mode 100644 index 00000000..723eafa0 --- /dev/null +++ b/internal/provider/data_source_sdwan_policy_object_security_port_profile_parcel_test.go @@ -0,0 +1,78 @@ +// 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 ( + "os" + "testing" + + "github.com/hashicorp/terraform-plugin-testing/helper/resource" +) + +// End of section. //template:end imports + +// Section below is generated&owned by "gen/generator.go". //template:begin testAccDataSource +func TestAccDataSourceSdwanPolicyObjectSecurityPortProfileParcel(t *testing.T) { + if os.Getenv("SDWAN_2012") == "" && os.Getenv("TF_VAR_policy_object_feature_template_id") == "" { + t.Skip("skipping test, set environment variable SDWAN_2012 or TF_VAR_policy_object_feature_template_id") + } + var checks []resource.TestCheckFunc + checks = append(checks, resource.TestCheckResourceAttr("data.sdwan_policy_object_security_port_profile_parcel.test", "entries.0.port", "100")) + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + ProtoV6ProviderFactories: testAccProtoV6ProviderFactories, + Steps: []resource.TestStep{ + { + Config: testAccDataSourceSdwanPolicyObjectSecurityPortPrerequisitesProfileParcelConfig + testAccDataSourceSdwanPolicyObjectSecurityPortProfileParcelConfig(), + Check: resource.ComposeTestCheckFunc(checks...), + }, + }, + }) +} + +// End of section. //template:end testAccDataSource + +// Section below is generated&owned by "gen/generator.go". //template:begin testPrerequisites +const testAccDataSourceSdwanPolicyObjectSecurityPortPrerequisitesProfileParcelConfig = ` +variable "policy_object_feature_template_id" {} +` + +// End of section. //template:end testPrerequisites + +// Section below is generated&owned by "gen/generator.go". //template:begin testAccDataSourceConfig +func testAccDataSourceSdwanPolicyObjectSecurityPortProfileParcelConfig() string { + config := `resource "sdwan_policy_object_security_port_profile_parcel" "test" {` + "\n" + config += ` name = "TF_TEST"` + "\n" + config += ` description = "Terraform integration test"` + "\n" + config += ` feature_profile_id = var.policy_object_feature_template_id` + "\n" + config += ` entries = [{` + "\n" + config += ` port = "100"` + "\n" + config += ` }]` + "\n" + config += `}` + "\n" + + config += ` + data "sdwan_policy_object_security_port_profile_parcel" "test" { + id = sdwan_policy_object_security_port_profile_parcel.test.id + feature_profile_id = var.policy_object_feature_template_id + } + ` + return config +} + +// End of section. //template:end testAccDataSourceConfig diff --git a/internal/provider/model_sdwan_policy_object_security_port_profile_parcel.go b/internal/provider/model_sdwan_policy_object_security_port_profile_parcel.go new file mode 100644 index 00000000..948b580b --- /dev/null +++ b/internal/provider/model_sdwan_policy_object_security_port_profile_parcel.go @@ -0,0 +1,175 @@ +// 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" + "fmt" + "net/url" + + "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 PolicyObjectSecurityPort struct { + Id types.String `tfsdk:"id"` + Version types.Int64 `tfsdk:"version"` + Name types.String `tfsdk:"name"` + Description types.String `tfsdk:"description"` + FeatureProfileId types.String `tfsdk:"feature_profile_id"` + Entries []PolicyObjectSecurityPortEntries `tfsdk:"entries"` +} + +type PolicyObjectSecurityPortEntries struct { + Port types.String `tfsdk:"port"` +} + +// End of section. //template:end types + +// Section below is generated&owned by "gen/generator.go". //template:begin getModel +func (data PolicyObjectSecurityPort) getModel() string { + return "policy_object_security_port" +} + +// End of section. //template:end getModel + +// Section below is generated&owned by "gen/generator.go". //template:begin getPath +func (data PolicyObjectSecurityPort) getPath() string { + return fmt.Sprintf("/v1/feature-profile/sdwan/policy-object/%v/security-port", url.QueryEscape(data.FeatureProfileId.ValueString())) +} + +// End of section. //template:end getPath + +// Section below is generated&owned by "gen/generator.go". //template:begin toBody +func (data PolicyObjectSecurityPort) toBody(ctx context.Context) string { + body := "" + body, _ = sjson.Set(body, "name", data.Name.ValueString()) + body, _ = sjson.Set(body, "description", data.Description.ValueString()) + path := "data." + if true { + + for _, item := range data.Entries { + itemBody := "" + if !item.Port.IsNull() { + if true { + itemBody, _ = sjson.Set(itemBody, "port.optionType", "global") + itemBody, _ = sjson.Set(itemBody, "port.value", item.Port.ValueString()) + } + } + body, _ = sjson.SetRaw(body, path+"entries.-1", itemBody) + } + } + return body +} + +// End of section. //template:end toBody + +// Section below is generated&owned by "gen/generator.go". //template:begin fromBody +func (data *PolicyObjectSecurityPort) fromBody(ctx context.Context, res gjson.Result) { + data.Name = types.StringValue(res.Get("payload.name").String()) + if value := res.Get("payload.description"); value.Exists() && value.String() != "" { + data.Description = types.StringValue(value.String()) + } else { + data.Description = types.StringNull() + } + path := "payload.data." + if value := res.Get(path + "entries"); value.Exists() { + data.Entries = make([]PolicyObjectSecurityPortEntries, 0) + value.ForEach(func(k, v gjson.Result) bool { + item := PolicyObjectSecurityPortEntries{} + item.Port = types.StringNull() + + if t := v.Get("port.optionType"); t.Exists() { + va := v.Get("port.value") + if t.String() == "global" { + item.Port = types.StringValue(va.String()) + } + } + data.Entries = append(data.Entries, item) + return true + }) + } +} + +// End of section. //template:end fromBody + +// Section below is generated&owned by "gen/generator.go". //template:begin updateFromBody +func (data *PolicyObjectSecurityPort) updateFromBody(ctx context.Context, res gjson.Result) { + data.Name = types.StringValue(res.Get("payload.name").String()) + if value := res.Get("payload.description"); value.Exists() && value.String() != "" { + data.Description = types.StringValue(value.String()) + } else { + data.Description = types.StringNull() + } + path := "payload.data." + for i := range data.Entries { + keys := [...]string{"port"} + keyValues := [...]string{data.Entries[i].Port.ValueString()} + keyValuesVariables := [...]string{""} + + var r gjson.Result + res.Get(path + "entries").ForEach( + func(_, v gjson.Result) bool { + found := false + for ik := range keys { + tt := v.Get(keys[ik] + ".optionType").String() + vv := v.Get(keys[ik] + ".value").String() + if (tt == "variable" && vv == keyValuesVariables[ik]) || (tt == "global" && vv == keyValues[ik]) { + found = true + continue + } + found = false + break + } + if found { + r = v + return false + } + return true + }, + ) + data.Entries[i].Port = types.StringNull() + + if t := r.Get("port.optionType"); t.Exists() { + va := r.Get("port.value") + if t.String() == "global" { + data.Entries[i].Port = types.StringValue(va.String()) + } + } + } +} + +// End of section. //template:end updateFromBody + +// Section below is generated&owned by "gen/generator.go". //template:begin isNull +func (data *PolicyObjectSecurityPort) isNull(ctx context.Context, res gjson.Result) bool { + if !data.FeatureProfileId.IsNull() { + return false + } + if len(data.Entries) > 0 { + return false + } + return true +} + +// End of section. //template:end isNull diff --git a/internal/provider/provider.go b/internal/provider/provider.go index cb3ed863..435e8d58 100644 --- a/internal/provider/provider.go +++ b/internal/provider/provider.go @@ -302,6 +302,7 @@ func (p *SdwanProvider) Resources(ctx context.Context) []func() resource.Resourc NewPolicyObjectSecurityGeolocationProfileParcelResource, NewPolicyObjectSecurityIPsSignatureProfileParcelResource, NewPolicyObjectSecurityLocaldomainProfileParcelResource, + NewPolicyObjectSecurityPortProfileParcelResource, NewPolicyObjectTLOCListProfileParcelResource, NewPolicyObjectVPNGroupProfileParcelResource, NewServiceLANVPNProfileParcelResource, @@ -486,6 +487,7 @@ func (p *SdwanProvider) DataSources(ctx context.Context) []func() datasource.Dat NewPolicyObjectSecurityGeolocationProfileParcelDataSource, NewPolicyObjectSecurityIPsSignatureProfileParcelDataSource, NewPolicyObjectSecurityLocaldomainProfileParcelDataSource, + NewPolicyObjectSecurityPortProfileParcelDataSource, NewPolicyObjectTLOCListProfileParcelDataSource, NewPolicyObjectVPNGroupProfileParcelDataSource, NewServiceLANVPNProfileParcelDataSource, diff --git a/internal/provider/resource_sdwan_policy_object_security_port_profile_parcel.go b/internal/provider/resource_sdwan_policy_object_security_port_profile_parcel.go new file mode 100644 index 00000000..f0dc673e --- /dev/null +++ b/internal/provider/resource_sdwan_policy_object_security_port_profile_parcel.go @@ -0,0 +1,252 @@ +// 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" + "fmt" + "net/url" + "sync" + + "github.com/CiscoDevNet/terraform-provider-sdwan/internal/provider/helpers" + "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/types" + "github.com/hashicorp/terraform-plugin-log/tflog" + "github.com/netascode/go-sdwan" +) + +// End of section. //template:end imports + +// Section below is generated&owned by "gen/generator.go". //template:begin model + +// Ensure provider defined types fully satisfy framework interfaces +var _ resource.Resource = &PolicyObjectSecurityPortProfileParcelResource{} +var _ resource.ResourceWithImportState = &PolicyObjectSecurityPortProfileParcelResource{} + +func NewPolicyObjectSecurityPortProfileParcelResource() resource.Resource { + return &PolicyObjectSecurityPortProfileParcelResource{} +} + +type PolicyObjectSecurityPortProfileParcelResource struct { + client *sdwan.Client + updateMutex *sync.Mutex +} + +func (r *PolicyObjectSecurityPortProfileParcelResource) Metadata(ctx context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) { + resp.TypeName = req.ProviderTypeName + "_policy_object_security_port_profile_parcel" +} + +func (r *PolicyObjectSecurityPortProfileParcelResource) 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 Policy Object Security Port profile parcel.").AddMinimumVersionDescription("20.12.0").String, + + Attributes: map[string]schema.Attribute{ + "id": schema.StringAttribute{ + MarkdownDescription: "The id of the profile parcel", + Computed: true, + PlanModifiers: []planmodifier.String{ + stringplanmodifier.UseStateForUnknown(), + }, + }, + "version": schema.Int64Attribute{ + MarkdownDescription: "The version of the profile parcel", + Computed: true, + }, + "name": schema.StringAttribute{ + MarkdownDescription: "The name of the profile parcel", + Required: true, + }, + "description": schema.StringAttribute{ + MarkdownDescription: "The description of the profile parcel", + Optional: true, + }, + "feature_profile_id": schema.StringAttribute{ + MarkdownDescription: helpers.NewAttributeDescription("Feature Profile ID").String, + Required: true, + }, + "entries": schema.ListNestedAttribute{ + MarkdownDescription: helpers.NewAttributeDescription("Port List").String, + Required: true, + NestedObject: schema.NestedAttributeObject{ + Attributes: map[string]schema.Attribute{ + "port": schema.StringAttribute{ + MarkdownDescription: helpers.NewAttributeDescription("can be single port or port range").String, + Optional: true, + }, + }, + }, + }, + }, + } +} + +func (r *PolicyObjectSecurityPortProfileParcelResource) Configure(_ context.Context, req resource.ConfigureRequest, _ *resource.ConfigureResponse) { + if req.ProviderData == nil { + return + } + + r.client = req.ProviderData.(*SdwanProviderData).Client + r.updateMutex = req.ProviderData.(*SdwanProviderData).UpdateMutex +} + +// End of section. //template:end model + +// Section below is generated&owned by "gen/generator.go". //template:begin create +func (r *PolicyObjectSecurityPortProfileParcelResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) { + var plan PolicyObjectSecurityPort + + // 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.Name.ValueString())) + + // Create object + body := plan.toBody(ctx) + + res, err := r.client.Post(plan.getPath(), 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(res.Get("parcelId").String()) + plan.Version = types.Int64Value(0) + + tflog.Debug(ctx, fmt.Sprintf("%s: Create finished successfully", plan.Name.ValueString())) + + diags = resp.State.Set(ctx, &plan) + resp.Diagnostics.Append(diags...) +} + +// End of section. //template:end create + +// Section below is generated&owned by "gen/generator.go". //template:begin read +func (r *PolicyObjectSecurityPortProfileParcelResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) { + var state PolicyObjectSecurityPort + + // 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.Name.String())) + + res, err := r.client.Get(state.getPath() + "/" + url.QueryEscape(state.Id.ValueString())) + if res.Get("error.message").String() == "Invalid feature Id" { + 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 + } + + // If every attribute is set to null we are dealing with an import operation and therefore reading all attributes + if state.isNull(ctx, res) { + state.fromBody(ctx, res) + } else { + state.updateFromBody(ctx, res) + } + + tflog.Debug(ctx, fmt.Sprintf("%s: Read finished successfully", state.Name.ValueString())) + + diags = resp.State.Set(ctx, &state) + resp.Diagnostics.Append(diags...) +} + +// End of section. //template:end read + +// Section below is generated&owned by "gen/generator.go". //template:begin update +func (r *PolicyObjectSecurityPortProfileParcelResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) { + var plan, state PolicyObjectSecurityPort + + // 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.Name.ValueString())) + + body := plan.toBody(ctx) + res, err := r.client.Put(plan.getPath()+"/"+url.QueryEscape(plan.Id.ValueString()), body) + if err != nil { + resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Failed to configure object (PUT), got error: %s, %s", err, res.String())) + return + } + + plan.Version = types.Int64Value(state.Version.ValueInt64() + 1) + + tflog.Debug(ctx, fmt.Sprintf("%s: Update finished successfully", plan.Name.ValueString())) + + diags = resp.State.Set(ctx, &plan) + resp.Diagnostics.Append(diags...) +} + +// End of section. //template:end update + +// Section below is generated&owned by "gen/generator.go". //template:begin delete +func (r *PolicyObjectSecurityPortProfileParcelResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) { + var state PolicyObjectSecurityPort + + // 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.Name.ValueString())) + + res, err := r.client.Delete(state.getPath() + "/" + url.QueryEscape(state.Id.ValueString())) + if err != nil && res.Get("error.message").String() != "Invalid Template Id" { + 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.Name.ValueString())) + + resp.State.RemoveResource(ctx) +} + +// End of section. //template:end delete + +// Section below is generated&owned by "gen/generator.go". //template:begin import +func (r *PolicyObjectSecurityPortProfileParcelResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) { + resource.ImportStatePassthroughID(ctx, path.Root("id"), req, resp) +} + +// End of section. //template:end import diff --git a/internal/provider/resource_sdwan_policy_object_security_port_profile_parcel_test.go b/internal/provider/resource_sdwan_policy_object_security_port_profile_parcel_test.go new file mode 100644 index 00000000..667f786f --- /dev/null +++ b/internal/provider/resource_sdwan_policy_object_security_port_profile_parcel_test.go @@ -0,0 +1,76 @@ +// 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 ( + "os" + "testing" + + "github.com/hashicorp/terraform-plugin-testing/helper/resource" +) + +// End of section. //template:end imports + +// Section below is generated&owned by "gen/generator.go". //template:begin testAcc +func TestAccSdwanPolicyObjectSecurityPortProfileParcel(t *testing.T) { + if os.Getenv("SDWAN_2012") == "" && os.Getenv("TF_VAR_policy_object_feature_template_id") == "" { + t.Skip("skipping test, set environment variable SDWAN_2012 or TF_VAR_policy_object_feature_template_id") + } + var checks []resource.TestCheckFunc + checks = append(checks, resource.TestCheckResourceAttr("sdwan_policy_object_security_port_profile_parcel.test", "entries.0.port", "100")) + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + ProtoV6ProviderFactories: testAccProtoV6ProviderFactories, + Steps: []resource.TestStep{ + + { + Config: testAccSdwanPolicyObjectSecurityPortPrerequisitesProfileParcelConfig + testAccSdwanPolicyObjectSecurityPortProfileParcelConfig_all(), + Check: resource.ComposeTestCheckFunc(checks...), + }, + }, + }) +} + +// End of section. //template:end testAcc + +// Section below is generated&owned by "gen/generator.go". //template:begin testPrerequisites +const testAccSdwanPolicyObjectSecurityPortPrerequisitesProfileParcelConfig = ` +variable "policy_object_feature_template_id" {} +` + +// End of section. //template:end testPrerequisites + +// Section below is generated&owned by "gen/generator.go". //template:begin testAccConfigMinimum + +// End of section. //template:end testAccConfigMinimum + +// Section below is generated&owned by "gen/generator.go". //template:begin testAccConfigAll +func testAccSdwanPolicyObjectSecurityPortProfileParcelConfig_all() string { + config := `resource "sdwan_policy_object_security_port_profile_parcel" "test" {` + "\n" + config += ` name = "TF_TEST_ALL"` + "\n" + config += ` description = "Terraform integration test"` + "\n" + config += ` feature_profile_id = var.policy_object_feature_template_id` + "\n" + config += ` entries = [{` + "\n" + config += ` port = "100"` + "\n" + config += ` }]` + "\n" + config += `}` + "\n" + return config +} + +// End of section. //template:end testAccConfigAll diff --git a/templates/guides/changelog.md.tmpl b/templates/guides/changelog.md.tmpl index 2f045b7d..308940a4 100644 --- a/templates/guides/changelog.md.tmpl +++ b/templates/guides/changelog.md.tmpl @@ -51,6 +51,7 @@ description: |- - Add `sdwan_policy_object_security_geolocation_profile_parcel` resource and data source - Add `sdwan_policy_object_security_ips_signature_profile_parcel` resource and data source - Add `sdwan_policy_object_security_localdomain_profile_parcel` resource and data source +- Add `sdwan_policy_object_security_port_profile_parcel` resource and data source ## 0.3.13