diff --git a/CHANGELOG.md b/CHANGELOG.md index 780ade6..3fddc52 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +## 0.1.6 (unreleased) + +- Add `ise_downloadable_acl` resource and data source + ## 0.1.5 - Make `sgacls` attribute of `ise_trustsec_egress_matrix_cell` resource optional diff --git a/docs/data-sources/downloadable_acl.md b/docs/data-sources/downloadable_acl.md new file mode 100644 index 0000000..7c6a6cb --- /dev/null +++ b/docs/data-sources/downloadable_acl.md @@ -0,0 +1,33 @@ +--- +# generated by https://github.com/hashicorp/terraform-plugin-docs +page_title: "ise_downloadable_acl Data Source - terraform-provider-ise" +subcategory: "Policy" +description: |- + This data source can read the Downloadable ACL. +--- + +# ise_downloadable_acl (Data Source) + +This data source can read the Downloadable ACL. + +## Example Usage + +```terraform +data "ise_downloadable_acl" "example" { + id = "76d24097-41c4-4558-a4d0-a8c07ac08470" +} +``` + + +## Schema + +### Optional + +- `id` (String) The id of the object +- `name` (String) The name of the downloadable ACL + +### Read-Only + +- `dacl` (String) The DACL content +- `dacl_type` (String) The type of ACL +- `description` (String) Description diff --git a/docs/guides/changelog.md b/docs/guides/changelog.md index a1c8c41..67a0d39 100644 --- a/docs/guides/changelog.md +++ b/docs/guides/changelog.md @@ -7,6 +7,10 @@ description: |- # Changelog +## 0.1.6 (unreleased) + +- Add `ise_downloadable_acl` resource and data source + ## 0.1.5 - Make `sgacls` attribute of `ise_trustsec_egress_matrix_cell` resource optional diff --git a/docs/resources/downloadable_acl.md b/docs/resources/downloadable_acl.md new file mode 100644 index 0000000..7542915 --- /dev/null +++ b/docs/resources/downloadable_acl.md @@ -0,0 +1,49 @@ +--- +# generated by https://github.com/hashicorp/terraform-plugin-docs +page_title: "ise_downloadable_acl Resource - terraform-provider-ise" +subcategory: "Policy" +description: |- + This resource can manage a Downloadable ACL. +--- + +# ise_downloadable_acl (Resource) + +This resource can manage a Downloadable ACL. + +## Example Usage + +```terraform +resource "ise_downloadable_acl" "example" { + name = "MyACL" + description = "My first downloadable ACL" + dacl = "permit ip any any" + dacl_type = "IPV4" +} +``` + + +## Schema + +### Required + +- `dacl` (String) The DACL content +- `name` (String) The name of the downloadable ACL + +### Optional + +- `dacl_type` (String) The type of ACL + - Choices: `IPV4`, `IPV6`, `IP_AGNOSTIC` + - Default value: `IPV4` +- `description` (String) Description + +### Read-Only + +- `id` (String) The id of the object + +## Import + +Import is supported using the following syntax: + +```shell +terraform import ise_downloadable_acl.example "76d24097-41c4-4558-a4d0-a8c07ac08470" +``` diff --git a/examples/data-sources/ise_downloadable_acl/data-source.tf b/examples/data-sources/ise_downloadable_acl/data-source.tf new file mode 100644 index 0000000..2fc72cd --- /dev/null +++ b/examples/data-sources/ise_downloadable_acl/data-source.tf @@ -0,0 +1,3 @@ +data "ise_downloadable_acl" "example" { + id = "76d24097-41c4-4558-a4d0-a8c07ac08470" +} diff --git a/examples/resources/ise_downloadable_acl/import.sh b/examples/resources/ise_downloadable_acl/import.sh new file mode 100644 index 0000000..a4be36e --- /dev/null +++ b/examples/resources/ise_downloadable_acl/import.sh @@ -0,0 +1 @@ +terraform import ise_downloadable_acl.example "76d24097-41c4-4558-a4d0-a8c07ac08470" diff --git a/examples/resources/ise_downloadable_acl/resource.tf b/examples/resources/ise_downloadable_acl/resource.tf new file mode 100644 index 0000000..97ba750 --- /dev/null +++ b/examples/resources/ise_downloadable_acl/resource.tf @@ -0,0 +1,6 @@ +resource "ise_downloadable_acl" "example" { + name = "MyACL" + description = "My first downloadable ACL" + dacl = "permit ip any any" + dacl_type = "IPV4" +} diff --git a/gen/definitions/downloadable_acl.yaml b/gen/definitions/downloadable_acl.yaml new file mode 100644 index 0000000..afd4f8a --- /dev/null +++ b/gen/definitions/downloadable_acl.yaml @@ -0,0 +1,30 @@ +--- +name: Downloadable ACL +rest_endpoint: /ers/config/downloadableacl +data_source_name_query: true +doc_category: Policy +attributes: + - model_name: name + data_path: [DownloadableAcl] + type: String + mandatory: true + description: The name of the downloadable ACL + example: MyACL + - model_name: description + data_path: [DownloadableAcl] + type: String + description: Description + example: My first downloadable ACL + - model_name: dacl + data_path: [DownloadableAcl] + type: String + mandatory: true + description: The DACL content + example: permit ip any any + - model_name: daclType + data_path: [DownloadableAcl] + type: String + enum_values: [IPV4, IPV6, IP_AGNOSTIC] + description: The type of ACL + default_value: IPV4 + example: IPV4 diff --git a/internal/provider/data_source_ise_downloadable_acl.go b/internal/provider/data_source_ise_downloadable_acl.go new file mode 100644 index 0000000..a1db15c --- /dev/null +++ b/internal/provider/data_source_ise_downloadable_acl.go @@ -0,0 +1,163 @@ +// Copyright © 2023 Cisco Systems, Inc. and its affiliates. +// All rights reserved. +// +// Licensed under the Mozilla Public License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://mozilla.org/MPL/2.0/ +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// SPDX-License-Identifier: MPL-2.0 + +// Code generated by "gen/generator.go"; DO NOT EDIT. + +package provider + +//template:begin imports +import ( + "context" + "fmt" + + "github.com/hashicorp/terraform-plugin-framework-validators/datasourcevalidator" + "github.com/hashicorp/terraform-plugin-framework/datasource" + "github.com/hashicorp/terraform-plugin-framework/datasource/schema" + "github.com/hashicorp/terraform-plugin-framework/path" + "github.com/hashicorp/terraform-plugin-framework/types" + "github.com/hashicorp/terraform-plugin-log/tflog" + "github.com/netascode/go-ise" + "github.com/tidwall/gjson" +) + +//template:end imports + +//template:begin model + +// Ensure the implementation satisfies the expected interfaces. +var ( + _ datasource.DataSource = &DownloadableACLDataSource{} + _ datasource.DataSourceWithConfigure = &DownloadableACLDataSource{} +) + +func NewDownloadableACLDataSource() datasource.DataSource { + return &DownloadableACLDataSource{} +} + +type DownloadableACLDataSource struct { + client *ise.Client +} + +func (d *DownloadableACLDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) { + resp.TypeName = req.ProviderTypeName + "_downloadable_acl" +} + +func (d *DownloadableACLDataSource) 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 Downloadable ACL.", + + Attributes: map[string]schema.Attribute{ + "id": schema.StringAttribute{ + MarkdownDescription: "The id of the object", + Optional: true, + Computed: true, + }, + "name": schema.StringAttribute{ + MarkdownDescription: "The name of the downloadable ACL", + Optional: true, + Computed: true, + }, + "description": schema.StringAttribute{ + MarkdownDescription: "Description", + Computed: true, + }, + "dacl": schema.StringAttribute{ + MarkdownDescription: "The DACL content", + Computed: true, + }, + "dacl_type": schema.StringAttribute{ + MarkdownDescription: "The type of ACL", + Computed: true, + }, + }, + } +} +func (d *DownloadableACLDataSource) ConfigValidators(ctx context.Context) []datasource.ConfigValidator { + return []datasource.ConfigValidator{ + datasourcevalidator.ExactlyOneOf( + path.MatchRoot("id"), + path.MatchRoot("name"), + ), + } +} + +func (d *DownloadableACLDataSource) Configure(_ context.Context, req datasource.ConfigureRequest, _ *datasource.ConfigureResponse) { + if req.ProviderData == nil { + return + } + + d.client = req.ProviderData.(*IseProviderData).Client +} + +//template:end model + +//template:begin read +func (d *DownloadableACLDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) { + var config DownloadableACL + + // 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())) + if config.Id.IsNull() && !config.Name.IsNull() { + for page := 1; ; page++ { + res, err := d.client.Get(fmt.Sprintf("%s?size=100&page=%v", config.getPath(), page)) + if err != nil { + resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Failed to retrieve objects, got error: %s", err)) + return + } + if value := res.Get("SearchResult.resources"); len(value.Array()) > 0 { + value.ForEach(func(k, v gjson.Result) bool { + if config.Name.ValueString() == v.Get("name").String() { + config.Id = types.StringValue(v.Get("id").String()) + tflog.Debug(ctx, fmt.Sprintf("%s: Found object with name '%v', id: %v", config.Id.String(), config.Name.ValueString(), config.Id.String())) + return false + } + return true + }) + } + if !config.Id.IsNull() || !res.Get("SearchResult.nextPage").Exists() { + break + } + } + + if config.Id.IsNull() { + resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Failed to find object with name: %s", config.Name.ValueString())) + return + } + } + + res, err := d.client.Get(config.getPath() + "/" + 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.Id.ValueString())) + + diags = resp.State.Set(ctx, &config) + resp.Diagnostics.Append(diags...) +} + +//template:end read diff --git a/internal/provider/data_source_ise_downloadable_acl_test.go b/internal/provider/data_source_ise_downloadable_acl_test.go new file mode 100644 index 0000000..3c89d5e --- /dev/null +++ b/internal/provider/data_source_ise_downloadable_acl_test.go @@ -0,0 +1,72 @@ +// Copyright © 2023 Cisco Systems, Inc. and its affiliates. +// All rights reserved. +// +// Licensed under the Mozilla Public License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://mozilla.org/MPL/2.0/ +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// SPDX-License-Identifier: MPL-2.0 + +// Code generated by "gen/generator.go"; DO NOT EDIT. + +package provider + +//template:begin imports +import ( + "testing" + + "github.com/hashicorp/terraform-plugin-testing/helper/resource" +) + +//template:end imports + +//template:begin testAccDataSource +func TestAccDataSourceIseDownloadableACL(t *testing.T) { + var checks []resource.TestCheckFunc + checks = append(checks, resource.TestCheckResourceAttr("data.ise_downloadable_acl.test", "name", "MyACL")) + checks = append(checks, resource.TestCheckResourceAttr("data.ise_downloadable_acl.test", "description", "My first downloadable ACL")) + checks = append(checks, resource.TestCheckResourceAttr("data.ise_downloadable_acl.test", "dacl", "permit ip any any")) + checks = append(checks, resource.TestCheckResourceAttr("data.ise_downloadable_acl.test", "dacl_type", "IPV4")) + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + ProtoV6ProviderFactories: testAccProtoV6ProviderFactories, + Steps: []resource.TestStep{ + { + Config: testAccDataSourceIseDownloadableACLConfig(), + Check: resource.ComposeTestCheckFunc(checks...), + }, + }, + }) +} + +//template:end testAccDataSource + +//template:begin testPrerequisites +//template:end testPrerequisites + +//template:begin testAccDataSourceConfig +func testAccDataSourceIseDownloadableACLConfig() string { + config := `resource "ise_downloadable_acl" "test" {` + "\n" + config += ` name = "MyACL"` + "\n" + config += ` description = "My first downloadable ACL"` + "\n" + config += ` dacl = "permit ip any any"` + "\n" + config += ` dacl_type = "IPV4"` + "\n" + config += `}` + "\n" + + config += ` + data "ise_downloadable_acl" "test" { + id = ise_downloadable_acl.test.id + } + ` + return config +} + +//template:end testAccDataSourceConfig diff --git a/internal/provider/model_ise_downloadable_acl.go b/internal/provider/model_ise_downloadable_acl.go new file mode 100644 index 0000000..935d3ae --- /dev/null +++ b/internal/provider/model_ise_downloadable_acl.go @@ -0,0 +1,121 @@ +// Copyright © 2023 Cisco Systems, Inc. and its affiliates. +// All rights reserved. +// +// Licensed under the Mozilla Public License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://mozilla.org/MPL/2.0/ +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// SPDX-License-Identifier: MPL-2.0 + +// Code generated by "gen/generator.go"; DO NOT EDIT. + +package provider + +//template:begin imports +import ( + "context" + + "github.com/hashicorp/terraform-plugin-framework/types" + "github.com/tidwall/gjson" + "github.com/tidwall/sjson" +) + +//template:end imports + +//template:begin types +type DownloadableACL struct { + Id types.String `tfsdk:"id"` + Name types.String `tfsdk:"name"` + Description types.String `tfsdk:"description"` + Dacl types.String `tfsdk:"dacl"` + DaclType types.String `tfsdk:"dacl_type"` +} + +//template:end types + +//template:begin getPath +func (data DownloadableACL) getPath() string { + return "/ers/config/downloadableacl" +} + +//template:end getPath + +//template:begin toBody +func (data DownloadableACL) toBody(ctx context.Context, state DownloadableACL) string { + body := "" + if !data.Name.IsNull() { + body, _ = sjson.Set(body, "DownloadableAcl.name", data.Name.ValueString()) + } + if !data.Description.IsNull() { + body, _ = sjson.Set(body, "DownloadableAcl.description", data.Description.ValueString()) + } + if !data.Dacl.IsNull() { + body, _ = sjson.Set(body, "DownloadableAcl.dacl", data.Dacl.ValueString()) + } + if !data.DaclType.IsNull() { + body, _ = sjson.Set(body, "DownloadableAcl.daclType", data.DaclType.ValueString()) + } + return body +} + +//template:end toBody + +//template:begin fromBody +func (data *DownloadableACL) fromBody(ctx context.Context, res gjson.Result) { + if value := res.Get("DownloadableAcl.name"); value.Exists() { + data.Name = types.StringValue(value.String()) + } else { + data.Name = types.StringNull() + } + if value := res.Get("DownloadableAcl.description"); value.Exists() { + data.Description = types.StringValue(value.String()) + } else { + data.Description = types.StringNull() + } + if value := res.Get("DownloadableAcl.dacl"); value.Exists() { + data.Dacl = types.StringValue(value.String()) + } else { + data.Dacl = types.StringNull() + } + if value := res.Get("DownloadableAcl.daclType"); value.Exists() { + data.DaclType = types.StringValue(value.String()) + } else { + data.DaclType = types.StringValue("IPV4") + } +} + +//template:end fromBody + +//template:begin updateFromBody +func (data *DownloadableACL) updateFromBody(ctx context.Context, res gjson.Result) { + if value := res.Get("DownloadableAcl.name"); value.Exists() && !data.Name.IsNull() { + data.Name = types.StringValue(value.String()) + } else { + data.Name = types.StringNull() + } + if value := res.Get("DownloadableAcl.description"); value.Exists() && !data.Description.IsNull() { + data.Description = types.StringValue(value.String()) + } else { + data.Description = types.StringNull() + } + if value := res.Get("DownloadableAcl.dacl"); value.Exists() && !data.Dacl.IsNull() { + data.Dacl = types.StringValue(value.String()) + } else { + data.Dacl = types.StringNull() + } + if value := res.Get("DownloadableAcl.daclType"); value.Exists() && !data.DaclType.IsNull() { + data.DaclType = types.StringValue(value.String()) + } else if data.DaclType.ValueString() != "IPV4" { + data.DaclType = types.StringNull() + } +} + +//template:end updateFromBody diff --git a/internal/provider/provider.go b/internal/provider/provider.go index 02a9cff..9772e99 100644 --- a/internal/provider/provider.go +++ b/internal/provider/provider.go @@ -245,6 +245,7 @@ func (p *IseProvider) Resources(ctx context.Context) []func() resource.Resource NewAllowedProtocolsResource, NewAuthorizationProfileResource, NewCertificateAuthenticationProfileResource, + NewDownloadableACLResource, NewEndpointIdentityGroupResource, NewInternalUserResource, NewLicenseTierStateResource, @@ -271,6 +272,7 @@ func (p *IseProvider) DataSources(ctx context.Context) []func() datasource.DataS NewAllowedProtocolsDataSource, NewAuthorizationProfileDataSource, NewCertificateAuthenticationProfileDataSource, + NewDownloadableACLDataSource, NewEndpointIdentityGroupDataSource, NewInternalUserDataSource, NewLicenseTierStateDataSource, diff --git a/internal/provider/resource_ise_downloadable_acl.go b/internal/provider/resource_ise_downloadable_acl.go new file mode 100644 index 0000000..bd98b1f --- /dev/null +++ b/internal/provider/resource_ise_downloadable_acl.go @@ -0,0 +1,238 @@ +// Copyright © 2023 Cisco Systems, Inc. and its affiliates. +// All rights reserved. +// +// Licensed under the Mozilla Public License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://mozilla.org/MPL/2.0/ +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// SPDX-License-Identifier: MPL-2.0 + +// Code generated by "gen/generator.go"; DO NOT EDIT. + +package provider + +//template:begin imports +import ( + "context" + "fmt" + "strings" + + "github.com/CiscoDevNet/terraform-provider-ise/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/stringdefault" + "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-ise" +) + +//template:end imports + +//template:begin model + +// Ensure provider defined types fully satisfy framework interfaces +var _ resource.Resource = &DownloadableACLResource{} +var _ resource.ResourceWithImportState = &DownloadableACLResource{} + +func NewDownloadableACLResource() resource.Resource { + return &DownloadableACLResource{} +} + +type DownloadableACLResource struct { + client *ise.Client +} + +func (r *DownloadableACLResource) Metadata(ctx context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) { + resp.TypeName = req.ProviderTypeName + "_downloadable_acl" +} + +func (r *DownloadableACLResource) 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 Downloadable ACL.").String, + + Attributes: map[string]schema.Attribute{ + "id": schema.StringAttribute{ + MarkdownDescription: "The id of the object", + Computed: true, + PlanModifiers: []planmodifier.String{ + stringplanmodifier.UseStateForUnknown(), + }, + }, + "name": schema.StringAttribute{ + MarkdownDescription: helpers.NewAttributeDescription("The name of the downloadable ACL").String, + Required: true, + }, + "description": schema.StringAttribute{ + MarkdownDescription: helpers.NewAttributeDescription("Description").String, + Optional: true, + }, + "dacl": schema.StringAttribute{ + MarkdownDescription: helpers.NewAttributeDescription("The DACL content").String, + Required: true, + }, + "dacl_type": schema.StringAttribute{ + MarkdownDescription: helpers.NewAttributeDescription("The type of ACL").AddStringEnumDescription("IPV4", "IPV6", "IP_AGNOSTIC").AddDefaultValueDescription("IPV4").String, + Optional: true, + Computed: true, + Validators: []validator.String{ + stringvalidator.OneOf("IPV4", "IPV6", "IP_AGNOSTIC"), + }, + Default: stringdefault.StaticString("IPV4"), + }, + }, + } +} + +func (r *DownloadableACLResource) Configure(_ context.Context, req resource.ConfigureRequest, _ *resource.ConfigureResponse) { + if req.ProviderData == nil { + return + } + + r.client = req.ProviderData.(*IseProviderData).Client +} + +//template:end model + +//template:begin create +func (r *DownloadableACLResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) { + var plan DownloadableACL + + // Read plan + diags := req.Plan.Get(ctx, &plan) + resp.Diagnostics.Append(diags...) + if resp.Diagnostics.HasError() { + return + } + + tflog.Debug(ctx, fmt.Sprintf("%s: Beginning Create", plan.Id.ValueString())) + + // Create object + body := plan.toBody(ctx, DownloadableACL{}) + res, location, 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 + } + locationElements := strings.Split(location, "/") + plan.Id = types.StringValue(locationElements[len(locationElements)-1]) + + tflog.Debug(ctx, fmt.Sprintf("%s: Create finished successfully", plan.Id.ValueString())) + + diags = resp.State.Set(ctx, &plan) + resp.Diagnostics.Append(diags...) +} + +//template:end create + +//template:begin read +func (r *DownloadableACLResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) { + var state DownloadableACL + + // Read state + diags := req.State.Get(ctx, &state) + resp.Diagnostics.Append(diags...) + if resp.Diagnostics.HasError() { + return + } + + tflog.Debug(ctx, fmt.Sprintf("%s: Beginning Read", state.Id.String())) + + res, err := r.client.Get(state.getPath() + "/" + state.Id.ValueString()) + if err != nil && strings.Contains(err.Error(), "StatusCode 404") { + resp.State.RemoveResource(ctx) + return + } else if err != nil { + resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Failed to retrieve object (GET), got error: %s, %s", err, res.String())) + return + } + + state.updateFromBody(ctx, res) + + tflog.Debug(ctx, fmt.Sprintf("%s: Read finished successfully", state.Id.ValueString())) + + diags = resp.State.Set(ctx, &state) + resp.Diagnostics.Append(diags...) +} + +//template:end read + +//template:begin update +func (r *DownloadableACLResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) { + var plan, state DownloadableACL + + // Read plan + diags := req.Plan.Get(ctx, &plan) + resp.Diagnostics.Append(diags...) + if resp.Diagnostics.HasError() { + return + } + // Read state + diags = req.State.Get(ctx, &state) + resp.Diagnostics.Append(diags...) + if resp.Diagnostics.HasError() { + return + } + + tflog.Debug(ctx, fmt.Sprintf("%s: Beginning Update", plan.Id.ValueString())) + + body := plan.toBody(ctx, state) + + res, err := r.client.Put(plan.getPath()+"/"+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 + } + + tflog.Debug(ctx, fmt.Sprintf("%s: Update finished successfully", plan.Id.ValueString())) + + diags = resp.State.Set(ctx, &plan) + resp.Diagnostics.Append(diags...) +} + +//template:end update + +//template:begin delete +func (r *DownloadableACLResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) { + var state DownloadableACL + + // Read state + diags := req.State.Get(ctx, &state) + resp.Diagnostics.Append(diags...) + if resp.Diagnostics.HasError() { + return + } + + tflog.Debug(ctx, fmt.Sprintf("%s: Beginning Delete", state.Id.ValueString())) + res, err := r.client.Delete(state.getPath() + "/" + state.Id.ValueString()) + if err != nil { + resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Failed to delete object (DELETE), got error: %s, %s", err, res.String())) + return + } + + tflog.Debug(ctx, fmt.Sprintf("%s: Delete finished successfully", state.Id.ValueString())) + + resp.State.RemoveResource(ctx) +} + +//template:end delete + +//template:begin import +func (r *DownloadableACLResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) { + resource.ImportStatePassthroughID(ctx, path.Root("id"), req, resp) +} + +//template:end import diff --git a/internal/provider/resource_ise_downloadable_acl_test.go b/internal/provider/resource_ise_downloadable_acl_test.go new file mode 100644 index 0000000..c5d6396 --- /dev/null +++ b/internal/provider/resource_ise_downloadable_acl_test.go @@ -0,0 +1,89 @@ +// Copyright © 2023 Cisco Systems, Inc. and its affiliates. +// All rights reserved. +// +// Licensed under the Mozilla Public License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://mozilla.org/MPL/2.0/ +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// SPDX-License-Identifier: MPL-2.0 + +// Code generated by "gen/generator.go"; DO NOT EDIT. + +package provider + +//template:begin imports +import ( + "os" + "testing" + + "github.com/hashicorp/terraform-plugin-testing/helper/resource" +) + +//template:end imports + +//template:begin testAcc +func TestAccIseDownloadableACL(t *testing.T) { + var checks []resource.TestCheckFunc + checks = append(checks, resource.TestCheckResourceAttr("ise_downloadable_acl.test", "name", "MyACL")) + checks = append(checks, resource.TestCheckResourceAttr("ise_downloadable_acl.test", "description", "My first downloadable ACL")) + checks = append(checks, resource.TestCheckResourceAttr("ise_downloadable_acl.test", "dacl", "permit ip any any")) + checks = append(checks, resource.TestCheckResourceAttr("ise_downloadable_acl.test", "dacl_type", "IPV4")) + + var steps []resource.TestStep + if os.Getenv("SKIP_MINIMUM_TEST") == "" { + steps = append(steps, resource.TestStep{ + Config: testAccIseDownloadableACLConfig_minimum(), + }) + } + steps = append(steps, resource.TestStep{ + Config: testAccIseDownloadableACLConfig_all(), + Check: resource.ComposeTestCheckFunc(checks...), + }) + steps = append(steps, resource.TestStep{ + ResourceName: "ise_downloadable_acl.test", + ImportState: true, + }) + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + ProtoV6ProviderFactories: testAccProtoV6ProviderFactories, + Steps: steps, + }) +} + +//template:end testAcc + +//template:begin testPrerequisites +//template:end testPrerequisites + +//template:begin testAccConfigMinimal +func testAccIseDownloadableACLConfig_minimum() string { + config := `resource "ise_downloadable_acl" "test" {` + "\n" + config += ` name = "MyACL"` + "\n" + config += ` dacl = "permit ip any any"` + "\n" + config += `}` + "\n" + return config +} + +//template:end testAccConfigMinimal + +//template:begin testAccConfigAll +func testAccIseDownloadableACLConfig_all() string { + config := `resource "ise_downloadable_acl" "test" {` + "\n" + config += ` name = "MyACL"` + "\n" + config += ` description = "My first downloadable ACL"` + "\n" + config += ` dacl = "permit ip any any"` + "\n" + config += ` dacl_type = "IPV4"` + "\n" + config += `}` + "\n" + return config +} + +//template:end testAccConfigAll diff --git a/templates/guides/changelog.md.tmpl b/templates/guides/changelog.md.tmpl index a1c8c41..67a0d39 100644 --- a/templates/guides/changelog.md.tmpl +++ b/templates/guides/changelog.md.tmpl @@ -7,6 +7,10 @@ description: |- # Changelog +## 0.1.6 (unreleased) + +- Add `ise_downloadable_acl` resource and data source + ## 0.1.5 - Make `sgacls` attribute of `ise_trustsec_egress_matrix_cell` resource optional