From 4176130e004fedb1e4b1ab1b733c0ecf43cc797f Mon Sep 17 00:00:00 2001 From: Sean Conroy <141843633+seconroy@users.noreply.github.com> Date: Thu, 12 Sep 2024 09:59:25 +0100 Subject: [PATCH] Fixes url list issue (#332) --- CHANGELOG.md | 3 +- ... policy_object_security_url_allow_list.md} | 10 +- .../policy_object_security_url_block_list.md | 42 +++ docs/guides/changelog.md | 3 +- ... policy_object_security_url_allow_list.md} | 12 +- .../policy_object_security_url_block_list.md | 61 +++++ .../data-source.tf | 4 + .../data-source.tf | 4 + .../import.sh | 1 + .../resource.tf | 10 + .../import.sh | 1 + .../resource.tf | 10 + ...olicy_object_security_url_allow_list.yaml} | 3 +- ...policy_object_security_url_block_list.yaml | 29 ++ ...olicy_object_security_url_allow_list.json} | 0 ...policy_object_security_url_block_list.json | 114 ++++++++ gen/schema/schema.yaml | 2 +- gen/templates/profile_parcels/model.go | 3 + ...n_policy_object_security_url_allow_list.go | 133 +++++++++ ...icy_object_security_url_allow_list_test.go | 81 ++++++ ...n_policy_object_security_url_block_list.go | 133 +++++++++ ...icy_object_security_url_block_list_test.go | 81 ++++++ ...n_policy_object_security_url_allow_list.go | 179 ++++++++++++ ...n_policy_object_security_url_block_list.go | 179 ++++++++++++ internal/provider/provider.go | 6 +- ...n_policy_object_security_url_allow_list.go | 259 ++++++++++++++++++ ...icy_object_security_url_allow_list_test.go | 79 ++++++ ...n_policy_object_security_url_block_list.go | 259 ++++++++++++++++++ ...icy_object_security_url_block_list_test.go | 79 ++++++ templates/guides/changelog.md.tmpl | 3 +- 30 files changed, 1765 insertions(+), 18 deletions(-) rename docs/data-sources/{policy_object_security_url_list.md => policy_object_security_url_allow_list.md} (68%) create mode 100644 docs/data-sources/policy_object_security_url_block_list.md rename docs/resources/{policy_object_security_url_list.md => policy_object_security_url_allow_list.md} (68%) create mode 100644 docs/resources/policy_object_security_url_block_list.md create mode 100644 examples/data-sources/sdwan_policy_object_security_url_allow_list/data-source.tf create mode 100644 examples/data-sources/sdwan_policy_object_security_url_block_list/data-source.tf create mode 100644 examples/resources/sdwan_policy_object_security_url_allow_list/import.sh create mode 100644 examples/resources/sdwan_policy_object_security_url_allow_list/resource.tf create mode 100644 examples/resources/sdwan_policy_object_security_url_block_list/import.sh create mode 100644 examples/resources/sdwan_policy_object_security_url_block_list/resource.tf rename gen/definitions/profile_parcels/{policy_object_security_url_list.yaml => policy_object_security_url_allow_list.yaml} (91%) create mode 100644 gen/definitions/profile_parcels/policy_object_security_url_block_list.yaml rename gen/models/profile_parcels/{policy_object_security_url_list.json => policy_object_security_url_allow_list.json} (100%) create mode 100644 gen/models/profile_parcels/policy_object_security_url_block_list.json create mode 100644 internal/provider/data_source_sdwan_policy_object_security_url_allow_list.go create mode 100644 internal/provider/data_source_sdwan_policy_object_security_url_allow_list_test.go create mode 100644 internal/provider/data_source_sdwan_policy_object_security_url_block_list.go create mode 100644 internal/provider/data_source_sdwan_policy_object_security_url_block_list_test.go create mode 100644 internal/provider/model_sdwan_policy_object_security_url_allow_list.go create mode 100644 internal/provider/model_sdwan_policy_object_security_url_block_list.go create mode 100644 internal/provider/resource_sdwan_policy_object_security_url_allow_list.go create mode 100644 internal/provider/resource_sdwan_policy_object_security_url_allow_list_test.go create mode 100644 internal/provider/resource_sdwan_policy_object_security_url_block_list.go create mode 100644 internal/provider/resource_sdwan_policy_object_security_url_block_list_test.go diff --git a/CHANGELOG.md b/CHANGELOG.md index f11922fd..504caa5a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,7 +12,8 @@ - Add `sdwan_policy_object_security_local_domain_list` resource and data source - Add `sdwan_policy_object_security_port_list` resource and data source - Add `sdwan_policy_object_security_scalable_group_tag_list` resource and data source -- Add `sdwan_policy_object_security_url_list` resource and data source +- Add `sdwan_policy_object_security_url_allow_list` resource and data source +- Add `sdwan_policy_object_security_url_block_list` resource and data source - Add `sdwan_policy_object_preferred_color_group` resource and data source - Add `sdwan_policy_object_security_identity_list` resource and data source - Add `sdwan_policy_object_security_local_application_list` resource and data source diff --git a/docs/data-sources/policy_object_security_url_list.md b/docs/data-sources/policy_object_security_url_allow_list.md similarity index 68% rename from docs/data-sources/policy_object_security_url_list.md rename to docs/data-sources/policy_object_security_url_allow_list.md index f5365f1e..154e848c 100644 --- a/docs/data-sources/policy_object_security_url_list.md +++ b/docs/data-sources/policy_object_security_url_allow_list.md @@ -1,19 +1,19 @@ --- # generated by https://github.com/hashicorp/terraform-plugin-docs -page_title: "sdwan_policy_object_security_url_list Data Source - terraform-provider-sdwan" +page_title: "sdwan_policy_object_security_url_allow_list Data Source - terraform-provider-sdwan" subcategory: "Policy Objects" description: |- - This data source can read the Policy Object Security URL List Policy_object. + This data source can read the Policy Object Security URL Allow List Policy_object. --- -# sdwan_policy_object_security_url_list (Data Source) +# sdwan_policy_object_security_url_allow_list (Data Source) -This data source can read the Policy Object Security URL List Policy_object. +This data source can read the Policy Object Security URL Allow List Policy_object. ## Example Usage ```terraform -data "sdwan_policy_object_security_url_list" "example" { +data "sdwan_policy_object_security_url_allow_list" "example" { id = "f6b2c44c-693c-4763-b010-895aa3d236bd" feature_profile_id = "f6dd22c8-0b4f-496c-9a0b-6813d1f8b8ac" } diff --git a/docs/data-sources/policy_object_security_url_block_list.md b/docs/data-sources/policy_object_security_url_block_list.md new file mode 100644 index 00000000..50e0c637 --- /dev/null +++ b/docs/data-sources/policy_object_security_url_block_list.md @@ -0,0 +1,42 @@ +--- +# generated by https://github.com/hashicorp/terraform-plugin-docs +page_title: "sdwan_policy_object_security_url_block_list Data Source - terraform-provider-sdwan" +subcategory: "Policy Objects" +description: |- + This data source can read the Policy Object Security URL Block List Policy_object. +--- + +# sdwan_policy_object_security_url_block_list (Data Source) + +This data source can read the Policy Object Security URL Block List Policy_object. + +## Example Usage + +```terraform +data "sdwan_policy_object_security_url_block_list" "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 Policy_object + +### Read-Only + +- `description` (String) The description of the Policy_object +- `entries` (Attributes List) URL List (see [below for nested schema](#nestedatt--entries)) +- `name` (String) The name of the Policy_object +- `version` (Number) The version of the Policy_object + + +### Nested Schema for `entries` + +Read-Only: + +- `pattern` (String) valid url pattern diff --git a/docs/guides/changelog.md b/docs/guides/changelog.md index cd91fbee..db826a0f 100644 --- a/docs/guides/changelog.md +++ b/docs/guides/changelog.md @@ -21,7 +21,8 @@ description: |- - Add `sdwan_policy_object_security_local_domain_list` resource and data source - Add `sdwan_policy_object_security_port_list` resource and data source - Add `sdwan_policy_object_security_scalable_group_tag_list` resource and data source -- Add `sdwan_policy_object_security_url_list` resource and data source +- Add `sdwan_policy_object_security_url_allow_list` resource and data source +- Add `sdwan_policy_object_security_url_block_list` resource and data source - Add `sdwan_policy_object_preferred_color_group` resource and data source - Add `sdwan_policy_object_security_identity_list` resource and data source - Add `sdwan_policy_object_security_local_application_list` resource and data source diff --git a/docs/resources/policy_object_security_url_list.md b/docs/resources/policy_object_security_url_allow_list.md similarity index 68% rename from docs/resources/policy_object_security_url_list.md rename to docs/resources/policy_object_security_url_allow_list.md index 53162bec..77c34564 100644 --- a/docs/resources/policy_object_security_url_list.md +++ b/docs/resources/policy_object_security_url_allow_list.md @@ -1,21 +1,21 @@ --- # generated by https://github.com/hashicorp/terraform-plugin-docs -page_title: "sdwan_policy_object_security_url_list Resource - terraform-provider-sdwan" +page_title: "sdwan_policy_object_security_url_allow_list Resource - terraform-provider-sdwan" subcategory: "Policy Objects" description: |- - This resource can manage a Policy Object Security URL List Policy_object. + This resource can manage a Policy Object Security URL Allow List Policy_object. Minimum SD-WAN Manager version: 20.12.0 --- -# sdwan_policy_object_security_url_list (Resource) +# sdwan_policy_object_security_url_allow_list (Resource) -This resource can manage a Policy Object Security URL List Policy_object. +This resource can manage a Policy Object Security URL Allow List Policy_object. - Minimum SD-WAN Manager version: `20.12.0` ## Example Usage ```terraform -resource "sdwan_policy_object_security_url_list" "example" { +resource "sdwan_policy_object_security_url_allow_list" "example" { name = "Example" description = "My Example" feature_profile_id = "f6dd22c8-0b4f-496c-9a0b-6813d1f8b8ac" @@ -57,5 +57,5 @@ Optional: Import is supported using the following syntax: ```shell -terraform import sdwan_policy_object_security_url_list.example "f6b2c44c-693c-4763-b010-895aa3d236bd" +terraform import sdwan_policy_object_security_url_allow_list.example "f6b2c44c-693c-4763-b010-895aa3d236bd" ``` diff --git a/docs/resources/policy_object_security_url_block_list.md b/docs/resources/policy_object_security_url_block_list.md new file mode 100644 index 00000000..7e3dd53b --- /dev/null +++ b/docs/resources/policy_object_security_url_block_list.md @@ -0,0 +1,61 @@ +--- +# generated by https://github.com/hashicorp/terraform-plugin-docs +page_title: "sdwan_policy_object_security_url_block_list Resource - terraform-provider-sdwan" +subcategory: "Policy Objects" +description: |- + This resource can manage a Policy Object Security URL Block List Policy_object. + Minimum SD-WAN Manager version: 20.12.0 +--- + +# sdwan_policy_object_security_url_block_list (Resource) + +This resource can manage a Policy Object Security URL Block List Policy_object. + - Minimum SD-WAN Manager version: `20.12.0` + +## Example Usage + +```terraform +resource "sdwan_policy_object_security_url_block_list" "example" { + name = "Example" + description = "My Example" + feature_profile_id = "f6dd22c8-0b4f-496c-9a0b-6813d1f8b8ac" + entries = [ + { + pattern = "www.cisco.com" + } + ] +} +``` + + +## Schema + +### Required + +- `entries` (Attributes List) URL List (see [below for nested schema](#nestedatt--entries)) +- `feature_profile_id` (String) Feature Profile ID +- `name` (String) The name of the Policy_object + +### Optional + +- `description` (String) The description of the Policy_object + +### Read-Only + +- `id` (String) The id of the Policy_object +- `version` (Number) The version of the Policy_object + + +### Nested Schema for `entries` + +Optional: + +- `pattern` (String) valid url pattern + +## Import + +Import is supported using the following syntax: + +```shell +terraform import sdwan_policy_object_security_url_block_list.example "f6b2c44c-693c-4763-b010-895aa3d236bd" +``` diff --git a/examples/data-sources/sdwan_policy_object_security_url_allow_list/data-source.tf b/examples/data-sources/sdwan_policy_object_security_url_allow_list/data-source.tf new file mode 100644 index 00000000..ebcdc7e3 --- /dev/null +++ b/examples/data-sources/sdwan_policy_object_security_url_allow_list/data-source.tf @@ -0,0 +1,4 @@ +data "sdwan_policy_object_security_url_allow_list" "example" { + id = "f6b2c44c-693c-4763-b010-895aa3d236bd" + feature_profile_id = "f6dd22c8-0b4f-496c-9a0b-6813d1f8b8ac" +} diff --git a/examples/data-sources/sdwan_policy_object_security_url_block_list/data-source.tf b/examples/data-sources/sdwan_policy_object_security_url_block_list/data-source.tf new file mode 100644 index 00000000..22cc252a --- /dev/null +++ b/examples/data-sources/sdwan_policy_object_security_url_block_list/data-source.tf @@ -0,0 +1,4 @@ +data "sdwan_policy_object_security_url_block_list" "example" { + id = "f6b2c44c-693c-4763-b010-895aa3d236bd" + feature_profile_id = "f6dd22c8-0b4f-496c-9a0b-6813d1f8b8ac" +} diff --git a/examples/resources/sdwan_policy_object_security_url_allow_list/import.sh b/examples/resources/sdwan_policy_object_security_url_allow_list/import.sh new file mode 100644 index 00000000..9e7b7436 --- /dev/null +++ b/examples/resources/sdwan_policy_object_security_url_allow_list/import.sh @@ -0,0 +1 @@ +terraform import sdwan_policy_object_security_url_allow_list.example "f6b2c44c-693c-4763-b010-895aa3d236bd" diff --git a/examples/resources/sdwan_policy_object_security_url_allow_list/resource.tf b/examples/resources/sdwan_policy_object_security_url_allow_list/resource.tf new file mode 100644 index 00000000..397d1fd7 --- /dev/null +++ b/examples/resources/sdwan_policy_object_security_url_allow_list/resource.tf @@ -0,0 +1,10 @@ +resource "sdwan_policy_object_security_url_allow_list" "example" { + name = "Example" + description = "My Example" + feature_profile_id = "f6dd22c8-0b4f-496c-9a0b-6813d1f8b8ac" + entries = [ + { + pattern = "www.cisco.com" + } + ] +} diff --git a/examples/resources/sdwan_policy_object_security_url_block_list/import.sh b/examples/resources/sdwan_policy_object_security_url_block_list/import.sh new file mode 100644 index 00000000..8ac2eb4c --- /dev/null +++ b/examples/resources/sdwan_policy_object_security_url_block_list/import.sh @@ -0,0 +1 @@ +terraform import sdwan_policy_object_security_url_block_list.example "f6b2c44c-693c-4763-b010-895aa3d236bd" diff --git a/examples/resources/sdwan_policy_object_security_url_block_list/resource.tf b/examples/resources/sdwan_policy_object_security_url_block_list/resource.tf new file mode 100644 index 00000000..e7e84343 --- /dev/null +++ b/examples/resources/sdwan_policy_object_security_url_block_list/resource.tf @@ -0,0 +1,10 @@ +resource "sdwan_policy_object_security_url_block_list" "example" { + name = "Example" + description = "My Example" + feature_profile_id = "f6dd22c8-0b4f-496c-9a0b-6813d1f8b8ac" + entries = [ + { + pattern = "www.cisco.com" + } + ] +} diff --git a/gen/definitions/profile_parcels/policy_object_security_url_list.yaml b/gen/definitions/profile_parcels/policy_object_security_url_allow_list.yaml similarity index 91% rename from gen/definitions/profile_parcels/policy_object_security_url_list.yaml rename to gen/definitions/profile_parcels/policy_object_security_url_allow_list.yaml index 7ca29746..a74e2060 100644 --- a/gen/definitions/profile_parcels/policy_object_security_url_list.yaml +++ b/gen/definitions/profile_parcels/policy_object_security_url_allow_list.yaml @@ -1,10 +1,11 @@ --- -name: Policy Object Security URL List +name: Policy Object Security URL Allow List rest_endpoint: /v1/feature-profile/sdwan/policy-object/%v/security-urllist minimum_version: 20.12.0 test_tags: [SDWAN_2012] skip_minimum_test: true parcel_type: policy_object +type_value: urlallowed attributes: - tf_name: feature_profile_id reference: true diff --git a/gen/definitions/profile_parcels/policy_object_security_url_block_list.yaml b/gen/definitions/profile_parcels/policy_object_security_url_block_list.yaml new file mode 100644 index 00000000..0f744647 --- /dev/null +++ b/gen/definitions/profile_parcels/policy_object_security_url_block_list.yaml @@ -0,0 +1,29 @@ +--- +name: Policy Object Security URL Block List +rest_endpoint: /v1/feature-profile/sdwan/policy-object/%v/security-urllist +minimum_version: 20.12.0 +test_tags: [SDWAN_2012] +skip_minimum_test: true +parcel_type: policy_object +type_value: urlblocked +attributes: + - tf_name: feature_profile_id + reference: true + type: String + mandatory: true + description: Feature Profile ID + example: f6dd22c8-0b4f-496c-9a0b-6813d1f8b8ac + test_value: sdwan_policy_object_feature_profile.test.id + - model_name: entries + mandatory: true + attributes: + - model_name: pattern + id: true + mandatory: true + example: www.cisco.com + +test_prerequisites: | + resource "sdwan_policy_object_feature_profile" "test" { + name = "POLICY_OBJECT_FP_1" + description = "My policy object feature profile 1" + } \ No newline at end of file diff --git a/gen/models/profile_parcels/policy_object_security_url_list.json b/gen/models/profile_parcels/policy_object_security_url_allow_list.json similarity index 100% rename from gen/models/profile_parcels/policy_object_security_url_list.json rename to gen/models/profile_parcels/policy_object_security_url_allow_list.json diff --git a/gen/models/profile_parcels/policy_object_security_url_block_list.json b/gen/models/profile_parcels/policy_object_security_url_block_list.json new file mode 100644 index 00000000..f1ef9da4 --- /dev/null +++ b/gen/models/profile_parcels/policy_object_security_url_block_list.json @@ -0,0 +1,114 @@ +{ + "request": { + "$schema": "http://json-schema.org/draft/2019-09/schema", + "$id": "https://cisco.com/schema/profileparcel/sdwan/policy-object/security-urllist/post/request_schema.json", + "title": "URL List Parcel Schema", + "description": "URL List profile parcel schema for POST request", + "type": "object", + "properties": { + "name": { + "type": "string", + "pattern": "^[^&<>! \"]+$", + "minLength": 1, + "maxLength": 32 + }, + "description": { + "type": "string" + }, + "type": { + "type": "string", + "enum": [ + "urlallowed", + "urlblocked" + ] + }, + "data": { + "type": "object", + "properties": { + "entries": { + "description": "URL List", + "type": "array", + "maxItems": 64, + "minItems": 1, + "uniqueItems": true, + "items": { + "type": "object", + "properties": { + "pattern": { + "description": "valid url pattern", + "type": "object", + "properties": { + "optionType": { + "type": "string", + "enum": [ + "global" + ] + }, + "value": { + "type": "string", + "minLength": 1, + "maxLength": 240, + "pattern": "^[^*+].*" + } + }, + "required": [ + "optionType", + "value" + ], + "additionalProperties": false + } + }, + "required": [ + "pattern" + ], + "additionalProperties": false + } + } + }, + "required": [ + "entries" + ], + "additionalProperties": false + }, + "documentation": { + "description": "This is the documentation for POST request schema for URL List profile parcel", + "details-1": "variable name should be present with given format as specified in schema if optionType value is variable", + "details-2": "variable name should not be present if optionType value is NOT variable", + "details-3": "when option Type is global, value should be present with given format/restrictions as specified in schema", + "details-4": "when option Type is default, value should be present with given default value as specified in schema", + "examples": [ + { + "data": { + "entries": [ + { + "pattern": { + "optionType": "global", + "value": "www.google.com" + } + }, + { + "pattern": { + "optionType": "global", + "value": "outlook.com" + } + } + ] + }, + "name": "UrlList" + } + ] + } + }, + "required": [ + "name", + "data", + "type" + ], + "not": { + "required": [ + "documentation" + ] + }, + "additionalProperties": false + } +} \ No newline at end of file diff --git a/gen/schema/schema.yaml b/gen/schema/schema.yaml index b28e406c..7b7f9be9 100644 --- a/gen/schema/schema.yaml +++ b/gen/schema/schema.yaml @@ -19,7 +19,7 @@ attributes: list(include('attribute'), required=False) # List of attributes test_tags: list(str(), required=False) # List of test tags, tests are only executed if an environment variable with one of these tags is configured test_prerequisites: str(required=False) # HCL code that is included in the acceptance tests to define prerequisites remove_id: bool(required=False) # Set to true if default ID attribute should be removed -type_value: str(required=False) # Set to a string which is being provided as a read-only "type" resource and data source attribute +type_value: str(required=False) # Set to a string which is being provided as a read-only "type" resource and data source attribute (Generic) or add to the body of a request (Profile parcels) no_import: bool(required=False) # Set to true if the resource does not support importing no_data_source: bool(required=False) # Set to true if no data source should be created no_resource: bool(required=False) # Set to true if no resource should be created diff --git a/gen/templates/profile_parcels/model.go b/gen/templates/profile_parcels/model.go index 7a67328b..146d4019 100644 --- a/gen/templates/profile_parcels/model.go +++ b/gen/templates/profile_parcels/model.go @@ -158,6 +158,9 @@ func (data {{camelCase .Name}}) toBody(ctx context.Context) string { body := "" body, _ = sjson.Set(body, "name", data.Name.ValueString()) body, _ = sjson.Set(body, "description", data.Description.ValueString()) + {{- if .TypeValue}} + body, _ = sjson.Set(body, "type", "{{.TypeValue}}") + {{- end}} path := "data." {{- range .Attributes}} {{- if .Value}} diff --git a/internal/provider/data_source_sdwan_policy_object_security_url_allow_list.go b/internal/provider/data_source_sdwan_policy_object_security_url_allow_list.go new file mode 100644 index 00000000..3efba69f --- /dev/null +++ b/internal/provider/data_source_sdwan_policy_object_security_url_allow_list.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 = &PolicyObjectSecurityURLAllowListProfileParcelDataSource{} + _ datasource.DataSourceWithConfigure = &PolicyObjectSecurityURLAllowListProfileParcelDataSource{} +) + +func NewPolicyObjectSecurityURLAllowListProfileParcelDataSource() datasource.DataSource { + return &PolicyObjectSecurityURLAllowListProfileParcelDataSource{} +} + +type PolicyObjectSecurityURLAllowListProfileParcelDataSource struct { + client *sdwan.Client +} + +func (d *PolicyObjectSecurityURLAllowListProfileParcelDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) { + resp.TypeName = req.ProviderTypeName + "_policy_object_security_url_allow_list" +} + +func (d *PolicyObjectSecurityURLAllowListProfileParcelDataSource) 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 URL Allow List Policy_object.", + + Attributes: map[string]schema.Attribute{ + "id": schema.StringAttribute{ + MarkdownDescription: "The id of the Policy_object", + Required: true, + }, + "version": schema.Int64Attribute{ + MarkdownDescription: "The version of the Policy_object", + Computed: true, + }, + "name": schema.StringAttribute{ + MarkdownDescription: "The name of the Policy_object", + Computed: true, + }, + "description": schema.StringAttribute{ + MarkdownDescription: "The description of the Policy_object", + Computed: true, + }, + "feature_profile_id": schema.StringAttribute{ + MarkdownDescription: "Feature Profile ID", + Required: true, + }, + "entries": schema.ListNestedAttribute{ + MarkdownDescription: "URL List", + Computed: true, + NestedObject: schema.NestedAttributeObject{ + Attributes: map[string]schema.Attribute{ + "pattern": schema.StringAttribute{ + MarkdownDescription: "valid url pattern", + Computed: true, + }, + }, + }, + }, + }, + } +} + +func (d *PolicyObjectSecurityURLAllowListProfileParcelDataSource) 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 *PolicyObjectSecurityURLAllowListProfileParcelDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) { + var config PolicyObjectSecurityURLAllowList + + // 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_url_allow_list_test.go b/internal/provider/data_source_sdwan_policy_object_security_url_allow_list_test.go new file mode 100644 index 00000000..5925036c --- /dev/null +++ b/internal/provider/data_source_sdwan_policy_object_security_url_allow_list_test.go @@ -0,0 +1,81 @@ +// 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 TestAccDataSourceSdwanPolicyObjectSecurityURLAllowListProfileParcel(t *testing.T) { + if os.Getenv("SDWAN_2012") == "" { + t.Skip("skipping test, set environment variable SDWAN_2012") + } + var checks []resource.TestCheckFunc + checks = append(checks, resource.TestCheckResourceAttr("data.sdwan_policy_object_security_url_allow_list.test", "entries.0.pattern", "www.cisco.com")) + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + ProtoV6ProviderFactories: testAccProtoV6ProviderFactories, + Steps: []resource.TestStep{ + { + Config: testAccDataSourceSdwanPolicyObjectSecurityURLAllowListPrerequisitesProfileParcelConfig + testAccDataSourceSdwanPolicyObjectSecurityURLAllowListProfileParcelConfig(), + Check: resource.ComposeTestCheckFunc(checks...), + }, + }, + }) +} + +// End of section. //template:end testAccDataSource + +// Section below is generated&owned by "gen/generator.go". //template:begin testPrerequisites +const testAccDataSourceSdwanPolicyObjectSecurityURLAllowListPrerequisitesProfileParcelConfig = ` +resource "sdwan_policy_object_feature_profile" "test" { + name = "POLICY_OBJECT_FP_1" + description = "My policy object feature profile 1" +} +` + +// End of section. //template:end testPrerequisites + +// Section below is generated&owned by "gen/generator.go". //template:begin testAccDataSourceConfig +func testAccDataSourceSdwanPolicyObjectSecurityURLAllowListProfileParcelConfig() string { + config := `resource "sdwan_policy_object_security_url_allow_list" "test" {` + "\n" + config += ` name = "TF_TEST"` + "\n" + config += ` description = "Terraform integration test"` + "\n" + config += ` feature_profile_id = sdwan_policy_object_feature_profile.test.id` + "\n" + config += ` entries = [{` + "\n" + config += ` pattern = "www.cisco.com"` + "\n" + config += ` }]` + "\n" + config += `}` + "\n" + + config += ` + data "sdwan_policy_object_security_url_allow_list" "test" { + id = sdwan_policy_object_security_url_allow_list.test.id + feature_profile_id = sdwan_policy_object_feature_profile.test.id + } + ` + return config +} + +// End of section. //template:end testAccDataSourceConfig diff --git a/internal/provider/data_source_sdwan_policy_object_security_url_block_list.go b/internal/provider/data_source_sdwan_policy_object_security_url_block_list.go new file mode 100644 index 00000000..c8dad032 --- /dev/null +++ b/internal/provider/data_source_sdwan_policy_object_security_url_block_list.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 = &PolicyObjectSecurityURLBlockListProfileParcelDataSource{} + _ datasource.DataSourceWithConfigure = &PolicyObjectSecurityURLBlockListProfileParcelDataSource{} +) + +func NewPolicyObjectSecurityURLBlockListProfileParcelDataSource() datasource.DataSource { + return &PolicyObjectSecurityURLBlockListProfileParcelDataSource{} +} + +type PolicyObjectSecurityURLBlockListProfileParcelDataSource struct { + client *sdwan.Client +} + +func (d *PolicyObjectSecurityURLBlockListProfileParcelDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) { + resp.TypeName = req.ProviderTypeName + "_policy_object_security_url_block_list" +} + +func (d *PolicyObjectSecurityURLBlockListProfileParcelDataSource) 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 URL Block List Policy_object.", + + Attributes: map[string]schema.Attribute{ + "id": schema.StringAttribute{ + MarkdownDescription: "The id of the Policy_object", + Required: true, + }, + "version": schema.Int64Attribute{ + MarkdownDescription: "The version of the Policy_object", + Computed: true, + }, + "name": schema.StringAttribute{ + MarkdownDescription: "The name of the Policy_object", + Computed: true, + }, + "description": schema.StringAttribute{ + MarkdownDescription: "The description of the Policy_object", + Computed: true, + }, + "feature_profile_id": schema.StringAttribute{ + MarkdownDescription: "Feature Profile ID", + Required: true, + }, + "entries": schema.ListNestedAttribute{ + MarkdownDescription: "URL List", + Computed: true, + NestedObject: schema.NestedAttributeObject{ + Attributes: map[string]schema.Attribute{ + "pattern": schema.StringAttribute{ + MarkdownDescription: "valid url pattern", + Computed: true, + }, + }, + }, + }, + }, + } +} + +func (d *PolicyObjectSecurityURLBlockListProfileParcelDataSource) 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 *PolicyObjectSecurityURLBlockListProfileParcelDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) { + var config PolicyObjectSecurityURLBlockList + + // 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_url_block_list_test.go b/internal/provider/data_source_sdwan_policy_object_security_url_block_list_test.go new file mode 100644 index 00000000..e89786a7 --- /dev/null +++ b/internal/provider/data_source_sdwan_policy_object_security_url_block_list_test.go @@ -0,0 +1,81 @@ +// 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 TestAccDataSourceSdwanPolicyObjectSecurityURLBlockListProfileParcel(t *testing.T) { + if os.Getenv("SDWAN_2012") == "" { + t.Skip("skipping test, set environment variable SDWAN_2012") + } + var checks []resource.TestCheckFunc + checks = append(checks, resource.TestCheckResourceAttr("data.sdwan_policy_object_security_url_block_list.test", "entries.0.pattern", "www.cisco.com")) + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + ProtoV6ProviderFactories: testAccProtoV6ProviderFactories, + Steps: []resource.TestStep{ + { + Config: testAccDataSourceSdwanPolicyObjectSecurityURLBlockListPrerequisitesProfileParcelConfig + testAccDataSourceSdwanPolicyObjectSecurityURLBlockListProfileParcelConfig(), + Check: resource.ComposeTestCheckFunc(checks...), + }, + }, + }) +} + +// End of section. //template:end testAccDataSource + +// Section below is generated&owned by "gen/generator.go". //template:begin testPrerequisites +const testAccDataSourceSdwanPolicyObjectSecurityURLBlockListPrerequisitesProfileParcelConfig = ` +resource "sdwan_policy_object_feature_profile" "test" { + name = "POLICY_OBJECT_FP_1" + description = "My policy object feature profile 1" +} +` + +// End of section. //template:end testPrerequisites + +// Section below is generated&owned by "gen/generator.go". //template:begin testAccDataSourceConfig +func testAccDataSourceSdwanPolicyObjectSecurityURLBlockListProfileParcelConfig() string { + config := `resource "sdwan_policy_object_security_url_block_list" "test" {` + "\n" + config += ` name = "TF_TEST"` + "\n" + config += ` description = "Terraform integration test"` + "\n" + config += ` feature_profile_id = sdwan_policy_object_feature_profile.test.id` + "\n" + config += ` entries = [{` + "\n" + config += ` pattern = "www.cisco.com"` + "\n" + config += ` }]` + "\n" + config += `}` + "\n" + + config += ` + data "sdwan_policy_object_security_url_block_list" "test" { + id = sdwan_policy_object_security_url_block_list.test.id + feature_profile_id = sdwan_policy_object_feature_profile.test.id + } + ` + return config +} + +// End of section. //template:end testAccDataSourceConfig diff --git a/internal/provider/model_sdwan_policy_object_security_url_allow_list.go b/internal/provider/model_sdwan_policy_object_security_url_allow_list.go new file mode 100644 index 00000000..02e10503 --- /dev/null +++ b/internal/provider/model_sdwan_policy_object_security_url_allow_list.go @@ -0,0 +1,179 @@ +// 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 PolicyObjectSecurityURLAllowList 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 []PolicyObjectSecurityURLAllowListEntries `tfsdk:"entries"` +} + +type PolicyObjectSecurityURLAllowListEntries struct { + Pattern types.String `tfsdk:"pattern"` +} + +// End of section. //template:end types + +// Section below is generated&owned by "gen/generator.go". //template:begin getModel +func (data PolicyObjectSecurityURLAllowList) getModel() string { + return "policy_object_security_url_allow_list" +} + +// End of section. //template:end getModel + +// Section below is generated&owned by "gen/generator.go". //template:begin getPath +func (data PolicyObjectSecurityURLAllowList) getPath() string { + return fmt.Sprintf("/v1/feature-profile/sdwan/policy-object/%v/security-urllist", 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 PolicyObjectSecurityURLAllowList) toBody(ctx context.Context) string { + body := "" + body, _ = sjson.Set(body, "name", data.Name.ValueString()) + body, _ = sjson.Set(body, "description", data.Description.ValueString()) + body, _ = sjson.Set(body, "type", "urlallowed") + path := "data." + if true { + + for _, item := range data.Entries { + itemBody := "" + if !item.Pattern.IsNull() { + if true { + itemBody, _ = sjson.Set(itemBody, "pattern.optionType", "global") + itemBody, _ = sjson.Set(itemBody, "pattern.value", item.Pattern.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 *PolicyObjectSecurityURLAllowList) 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([]PolicyObjectSecurityURLAllowListEntries, 0) + value.ForEach(func(k, v gjson.Result) bool { + item := PolicyObjectSecurityURLAllowListEntries{} + item.Pattern = types.StringNull() + + if t := v.Get("pattern.optionType"); t.Exists() { + va := v.Get("pattern.value") + if t.String() == "global" { + item.Pattern = 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 *PolicyObjectSecurityURLAllowList) 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{"pattern"} + keyValues := [...]string{data.Entries[i].Pattern.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") + vv := v.Get(keys[ik] + ".value") + if tt.Exists() && vv.Exists() { + if (tt.String() == "variable" && vv.String() == keyValuesVariables[ik]) || (tt.String() == "global" && vv.String() == keyValues[ik]) { + found = true + continue + } + found = false + break + } + continue + } + if found { + r = v + return false + } + return true + }, + ) + data.Entries[i].Pattern = types.StringNull() + + if t := r.Get("pattern.optionType"); t.Exists() { + va := r.Get("pattern.value") + if t.String() == "global" { + data.Entries[i].Pattern = types.StringValue(va.String()) + } + } + } +} + +// End of section. //template:end updateFromBody + +// Section below is generated&owned by "gen/generator.go". //template:begin isNull +func (data *PolicyObjectSecurityURLAllowList) 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/model_sdwan_policy_object_security_url_block_list.go b/internal/provider/model_sdwan_policy_object_security_url_block_list.go new file mode 100644 index 00000000..1a9183e7 --- /dev/null +++ b/internal/provider/model_sdwan_policy_object_security_url_block_list.go @@ -0,0 +1,179 @@ +// 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 PolicyObjectSecurityURLBlockList 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 []PolicyObjectSecurityURLBlockListEntries `tfsdk:"entries"` +} + +type PolicyObjectSecurityURLBlockListEntries struct { + Pattern types.String `tfsdk:"pattern"` +} + +// End of section. //template:end types + +// Section below is generated&owned by "gen/generator.go". //template:begin getModel +func (data PolicyObjectSecurityURLBlockList) getModel() string { + return "policy_object_security_url_block_list" +} + +// End of section. //template:end getModel + +// Section below is generated&owned by "gen/generator.go". //template:begin getPath +func (data PolicyObjectSecurityURLBlockList) getPath() string { + return fmt.Sprintf("/v1/feature-profile/sdwan/policy-object/%v/security-urllist", 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 PolicyObjectSecurityURLBlockList) toBody(ctx context.Context) string { + body := "" + body, _ = sjson.Set(body, "name", data.Name.ValueString()) + body, _ = sjson.Set(body, "description", data.Description.ValueString()) + body, _ = sjson.Set(body, "type", "urlblocked") + path := "data." + if true { + + for _, item := range data.Entries { + itemBody := "" + if !item.Pattern.IsNull() { + if true { + itemBody, _ = sjson.Set(itemBody, "pattern.optionType", "global") + itemBody, _ = sjson.Set(itemBody, "pattern.value", item.Pattern.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 *PolicyObjectSecurityURLBlockList) 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([]PolicyObjectSecurityURLBlockListEntries, 0) + value.ForEach(func(k, v gjson.Result) bool { + item := PolicyObjectSecurityURLBlockListEntries{} + item.Pattern = types.StringNull() + + if t := v.Get("pattern.optionType"); t.Exists() { + va := v.Get("pattern.value") + if t.String() == "global" { + item.Pattern = 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 *PolicyObjectSecurityURLBlockList) 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{"pattern"} + keyValues := [...]string{data.Entries[i].Pattern.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") + vv := v.Get(keys[ik] + ".value") + if tt.Exists() && vv.Exists() { + if (tt.String() == "variable" && vv.String() == keyValuesVariables[ik]) || (tt.String() == "global" && vv.String() == keyValues[ik]) { + found = true + continue + } + found = false + break + } + continue + } + if found { + r = v + return false + } + return true + }, + ) + data.Entries[i].Pattern = types.StringNull() + + if t := r.Get("pattern.optionType"); t.Exists() { + va := r.Get("pattern.value") + if t.String() == "global" { + data.Entries[i].Pattern = types.StringValue(va.String()) + } + } + } +} + +// End of section. //template:end updateFromBody + +// Section below is generated&owned by "gen/generator.go". //template:begin isNull +func (data *PolicyObjectSecurityURLBlockList) 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 6568d09d..84887aa8 100644 --- a/internal/provider/provider.go +++ b/internal/provider/provider.go @@ -308,7 +308,8 @@ func (p *SdwanProvider) Resources(ctx context.Context) []func() resource.Resourc NewPolicyObjectSecurityLocalDomainListProfileParcelResource, NewPolicyObjectSecurityPortListProfileParcelResource, NewPolicyObjectSecurityScalableGroupTagListProfileParcelResource, - NewPolicyObjectSecurityURLListProfileParcelResource, + NewPolicyObjectSecurityURLAllowListProfileParcelResource, + NewPolicyObjectSecurityURLBlockListProfileParcelResource, NewPolicyObjectSLAClassListProfileParcelResource, NewPolicyObjectStandardCommunityListProfileParcelResource, NewPolicyObjectTLOCListProfileParcelResource, @@ -511,7 +512,8 @@ func (p *SdwanProvider) DataSources(ctx context.Context) []func() datasource.Dat NewPolicyObjectSecurityLocalDomainListProfileParcelDataSource, NewPolicyObjectSecurityPortListProfileParcelDataSource, NewPolicyObjectSecurityScalableGroupTagListProfileParcelDataSource, - NewPolicyObjectSecurityURLListProfileParcelDataSource, + NewPolicyObjectSecurityURLAllowListProfileParcelDataSource, + NewPolicyObjectSecurityURLBlockListProfileParcelDataSource, NewPolicyObjectSLAClassListProfileParcelDataSource, NewPolicyObjectStandardCommunityListProfileParcelDataSource, NewPolicyObjectTLOCListProfileParcelDataSource, diff --git a/internal/provider/resource_sdwan_policy_object_security_url_allow_list.go b/internal/provider/resource_sdwan_policy_object_security_url_allow_list.go new file mode 100644 index 00000000..d5cc5181 --- /dev/null +++ b/internal/provider/resource_sdwan_policy_object_security_url_allow_list.go @@ -0,0 +1,259 @@ +// 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" + "regexp" + "sync" + + "github.com/CiscoDevNet/terraform-provider-sdwan/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" + "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 = &PolicyObjectSecurityURLAllowListProfileParcelResource{} +var _ resource.ResourceWithImportState = &PolicyObjectSecurityURLAllowListProfileParcelResource{} + +func NewPolicyObjectSecurityURLAllowListProfileParcelResource() resource.Resource { + return &PolicyObjectSecurityURLAllowListProfileParcelResource{} +} + +type PolicyObjectSecurityURLAllowListProfileParcelResource struct { + client *sdwan.Client + updateMutex *sync.Mutex +} + +func (r *PolicyObjectSecurityURLAllowListProfileParcelResource) Metadata(ctx context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) { + resp.TypeName = req.ProviderTypeName + "_policy_object_security_url_allow_list" +} + +func (r *PolicyObjectSecurityURLAllowListProfileParcelResource) 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 URL Allow List Policy_object.").AddMinimumVersionDescription("20.12.0").String, + + Attributes: map[string]schema.Attribute{ + "id": schema.StringAttribute{ + MarkdownDescription: "The id of the Policy_object", + Computed: true, + PlanModifiers: []planmodifier.String{ + stringplanmodifier.UseStateForUnknown(), + }, + }, + "version": schema.Int64Attribute{ + MarkdownDescription: "The version of the Policy_object", + Computed: true, + }, + "name": schema.StringAttribute{ + MarkdownDescription: "The name of the Policy_object", + Required: true, + }, + "description": schema.StringAttribute{ + MarkdownDescription: "The description of the Policy_object", + Optional: true, + }, + "feature_profile_id": schema.StringAttribute{ + MarkdownDescription: helpers.NewAttributeDescription("Feature Profile ID").String, + Required: true, + }, + "entries": schema.ListNestedAttribute{ + MarkdownDescription: helpers.NewAttributeDescription("URL List").String, + Required: true, + NestedObject: schema.NestedAttributeObject{ + Attributes: map[string]schema.Attribute{ + "pattern": schema.StringAttribute{ + MarkdownDescription: helpers.NewAttributeDescription("valid url pattern").String, + Optional: true, + Validators: []validator.String{ + stringvalidator.LengthBetween(1, 240), + stringvalidator.RegexMatches(regexp.MustCompile(`^[^*+].*`), ""), + }, + }, + }, + }, + }, + }, + } +} + +func (r *PolicyObjectSecurityURLAllowListProfileParcelResource) 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 *PolicyObjectSecurityURLAllowListProfileParcelResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) { + var plan PolicyObjectSecurityURLAllowList + + // 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 *PolicyObjectSecurityURLAllowListProfileParcelResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) { + var state PolicyObjectSecurityURLAllowList + + // 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 *PolicyObjectSecurityURLAllowListProfileParcelResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) { + var plan, state PolicyObjectSecurityURLAllowList + + // 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 *PolicyObjectSecurityURLAllowListProfileParcelResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) { + var state PolicyObjectSecurityURLAllowList + + // 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 *PolicyObjectSecurityURLAllowListProfileParcelResource) 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_url_allow_list_test.go b/internal/provider/resource_sdwan_policy_object_security_url_allow_list_test.go new file mode 100644 index 00000000..8eee8ee1 --- /dev/null +++ b/internal/provider/resource_sdwan_policy_object_security_url_allow_list_test.go @@ -0,0 +1,79 @@ +// 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 TestAccSdwanPolicyObjectSecurityURLAllowListProfileParcel(t *testing.T) { + if os.Getenv("SDWAN_2012") == "" { + t.Skip("skipping test, set environment variable SDWAN_2012") + } + var checks []resource.TestCheckFunc + checks = append(checks, resource.TestCheckResourceAttr("sdwan_policy_object_security_url_allow_list.test", "entries.0.pattern", "www.cisco.com")) + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + ProtoV6ProviderFactories: testAccProtoV6ProviderFactories, + Steps: []resource.TestStep{ + + { + Config: testAccSdwanPolicyObjectSecurityURLAllowListPrerequisitesProfileParcelConfig + testAccSdwanPolicyObjectSecurityURLAllowListProfileParcelConfig_all(), + Check: resource.ComposeTestCheckFunc(checks...), + }, + }, + }) +} + +// End of section. //template:end testAcc + +// Section below is generated&owned by "gen/generator.go". //template:begin testPrerequisites +const testAccSdwanPolicyObjectSecurityURLAllowListPrerequisitesProfileParcelConfig = ` +resource "sdwan_policy_object_feature_profile" "test" { + name = "POLICY_OBJECT_FP_1" + description = "My policy object feature profile 1" +} +` + +// 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 testAccSdwanPolicyObjectSecurityURLAllowListProfileParcelConfig_all() string { + config := `resource "sdwan_policy_object_security_url_allow_list" "test" {` + "\n" + config += ` name = "TF_TEST_ALL"` + "\n" + config += ` description = "Terraform integration test"` + "\n" + config += ` feature_profile_id = sdwan_policy_object_feature_profile.test.id` + "\n" + config += ` entries = [{` + "\n" + config += ` pattern = "www.cisco.com"` + "\n" + config += ` }]` + "\n" + config += `}` + "\n" + return config +} + +// End of section. //template:end testAccConfigAll diff --git a/internal/provider/resource_sdwan_policy_object_security_url_block_list.go b/internal/provider/resource_sdwan_policy_object_security_url_block_list.go new file mode 100644 index 00000000..ca946fc2 --- /dev/null +++ b/internal/provider/resource_sdwan_policy_object_security_url_block_list.go @@ -0,0 +1,259 @@ +// 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" + "regexp" + "sync" + + "github.com/CiscoDevNet/terraform-provider-sdwan/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" + "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 = &PolicyObjectSecurityURLBlockListProfileParcelResource{} +var _ resource.ResourceWithImportState = &PolicyObjectSecurityURLBlockListProfileParcelResource{} + +func NewPolicyObjectSecurityURLBlockListProfileParcelResource() resource.Resource { + return &PolicyObjectSecurityURLBlockListProfileParcelResource{} +} + +type PolicyObjectSecurityURLBlockListProfileParcelResource struct { + client *sdwan.Client + updateMutex *sync.Mutex +} + +func (r *PolicyObjectSecurityURLBlockListProfileParcelResource) Metadata(ctx context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) { + resp.TypeName = req.ProviderTypeName + "_policy_object_security_url_block_list" +} + +func (r *PolicyObjectSecurityURLBlockListProfileParcelResource) 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 URL Block List Policy_object.").AddMinimumVersionDescription("20.12.0").String, + + Attributes: map[string]schema.Attribute{ + "id": schema.StringAttribute{ + MarkdownDescription: "The id of the Policy_object", + Computed: true, + PlanModifiers: []planmodifier.String{ + stringplanmodifier.UseStateForUnknown(), + }, + }, + "version": schema.Int64Attribute{ + MarkdownDescription: "The version of the Policy_object", + Computed: true, + }, + "name": schema.StringAttribute{ + MarkdownDescription: "The name of the Policy_object", + Required: true, + }, + "description": schema.StringAttribute{ + MarkdownDescription: "The description of the Policy_object", + Optional: true, + }, + "feature_profile_id": schema.StringAttribute{ + MarkdownDescription: helpers.NewAttributeDescription("Feature Profile ID").String, + Required: true, + }, + "entries": schema.ListNestedAttribute{ + MarkdownDescription: helpers.NewAttributeDescription("URL List").String, + Required: true, + NestedObject: schema.NestedAttributeObject{ + Attributes: map[string]schema.Attribute{ + "pattern": schema.StringAttribute{ + MarkdownDescription: helpers.NewAttributeDescription("valid url pattern").String, + Optional: true, + Validators: []validator.String{ + stringvalidator.LengthBetween(1, 240), + stringvalidator.RegexMatches(regexp.MustCompile(`^[^*+].*`), ""), + }, + }, + }, + }, + }, + }, + } +} + +func (r *PolicyObjectSecurityURLBlockListProfileParcelResource) 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 *PolicyObjectSecurityURLBlockListProfileParcelResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) { + var plan PolicyObjectSecurityURLBlockList + + // 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 *PolicyObjectSecurityURLBlockListProfileParcelResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) { + var state PolicyObjectSecurityURLBlockList + + // 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 *PolicyObjectSecurityURLBlockListProfileParcelResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) { + var plan, state PolicyObjectSecurityURLBlockList + + // 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 *PolicyObjectSecurityURLBlockListProfileParcelResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) { + var state PolicyObjectSecurityURLBlockList + + // 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 *PolicyObjectSecurityURLBlockListProfileParcelResource) 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_url_block_list_test.go b/internal/provider/resource_sdwan_policy_object_security_url_block_list_test.go new file mode 100644 index 00000000..eb72f6bd --- /dev/null +++ b/internal/provider/resource_sdwan_policy_object_security_url_block_list_test.go @@ -0,0 +1,79 @@ +// 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 TestAccSdwanPolicyObjectSecurityURLBlockListProfileParcel(t *testing.T) { + if os.Getenv("SDWAN_2012") == "" { + t.Skip("skipping test, set environment variable SDWAN_2012") + } + var checks []resource.TestCheckFunc + checks = append(checks, resource.TestCheckResourceAttr("sdwan_policy_object_security_url_block_list.test", "entries.0.pattern", "www.cisco.com")) + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + ProtoV6ProviderFactories: testAccProtoV6ProviderFactories, + Steps: []resource.TestStep{ + + { + Config: testAccSdwanPolicyObjectSecurityURLBlockListPrerequisitesProfileParcelConfig + testAccSdwanPolicyObjectSecurityURLBlockListProfileParcelConfig_all(), + Check: resource.ComposeTestCheckFunc(checks...), + }, + }, + }) +} + +// End of section. //template:end testAcc + +// Section below is generated&owned by "gen/generator.go". //template:begin testPrerequisites +const testAccSdwanPolicyObjectSecurityURLBlockListPrerequisitesProfileParcelConfig = ` +resource "sdwan_policy_object_feature_profile" "test" { + name = "POLICY_OBJECT_FP_1" + description = "My policy object feature profile 1" +} +` + +// 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 testAccSdwanPolicyObjectSecurityURLBlockListProfileParcelConfig_all() string { + config := `resource "sdwan_policy_object_security_url_block_list" "test" {` + "\n" + config += ` name = "TF_TEST_ALL"` + "\n" + config += ` description = "Terraform integration test"` + "\n" + config += ` feature_profile_id = sdwan_policy_object_feature_profile.test.id` + "\n" + config += ` entries = [{` + "\n" + config += ` pattern = "www.cisco.com"` + "\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 cd91fbee..db826a0f 100644 --- a/templates/guides/changelog.md.tmpl +++ b/templates/guides/changelog.md.tmpl @@ -21,7 +21,8 @@ description: |- - Add `sdwan_policy_object_security_local_domain_list` resource and data source - Add `sdwan_policy_object_security_port_list` resource and data source - Add `sdwan_policy_object_security_scalable_group_tag_list` resource and data source -- Add `sdwan_policy_object_security_url_list` resource and data source +- Add `sdwan_policy_object_security_url_allow_list` resource and data source +- Add `sdwan_policy_object_security_url_block_list` resource and data source - Add `sdwan_policy_object_preferred_color_group` resource and data source - Add `sdwan_policy_object_security_identity_list` resource and data source - Add `sdwan_policy_object_security_local_application_list` resource and data source