From a5ea611bc7ce233c1b0f208469bc821ba397cdaf Mon Sep 17 00:00:00 2001 From: danischm Date: Fri, 13 Oct 2023 22:45:17 +0200 Subject: [PATCH] Add cli resource --- CHANGELOG.md | 1 + docs/guides/changelog.md | 1 + docs/resources/cli.md | 33 ++++ examples/resources/iosxe_cli/resource.tf | 6 + gen/doc_category.go | 10 +- gen/templates/provider.go | 1 + internal/provider/provider.go | 1 + internal/provider/resource_iosxe_cli.go | 157 ++++++++++++++++++ internal/provider/resource_iosxe_cli_test.go | 49 ++++++ .../resource_iosxe_save_config_test.go | 46 +++++ templates/guides/changelog.md.tmpl | 1 + 11 files changed, 305 insertions(+), 1 deletion(-) create mode 100644 docs/resources/cli.md create mode 100644 examples/resources/iosxe_cli/resource.tf create mode 100644 internal/provider/resource_iosxe_cli.go create mode 100644 internal/provider/resource_iosxe_cli_test.go create mode 100644 internal/provider/resource_iosxe_save_config_test.go diff --git a/CHANGELOG.md b/CHANGELOG.md index 2dc4e7e0..4b28c626 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ - Add `iosxe_vlan_filter` resource and data source - Add `iosxe_vlan_group` resource and data source - Add `iosxe_save_config` resource +- Add `iosxe_cli` resource ## 0.5.0 diff --git a/docs/guides/changelog.md b/docs/guides/changelog.md index 5f4c8062..ec4aa6d5 100644 --- a/docs/guides/changelog.md +++ b/docs/guides/changelog.md @@ -12,6 +12,7 @@ description: |- - Add `iosxe_vlan_filter` resource and data source - Add `iosxe_vlan_group` resource and data source - Add `iosxe_save_config` resource +- Add `iosxe_cli` resource ## 0.5.0 diff --git a/docs/resources/cli.md b/docs/resources/cli.md new file mode 100644 index 00000000..6b6bf49d --- /dev/null +++ b/docs/resources/cli.md @@ -0,0 +1,33 @@ +--- +# generated by https://github.com/hashicorp/terraform-plugin-docs +page_title: "iosxe_cli Resource - terraform-provider-iosxe" +subcategory: "General" +description: |- + This resources is used to configure arbitrary CLI commands. This should be considered a last resort in case YANG models are not available, as it cannot read the state and therefore cannot reconcile changes. +--- + +# iosxe_cli (Resource) + +This resources is used to configure arbitrary CLI commands. This should be considered a last resort in case YANG models are not available, as it cannot read the state and therefore cannot reconcile changes. + +## Example Usage + +```terraform +resource "iosxe_cli" "example" { + cli = <<-EOT + interface Loopback123 + description configured-via-restconf-cli + EOT +} +``` + + +## Schema + +### Required + +- `cli` (String) This attribute contains the CLI commands. + +### Optional + +- `device` (String) A device name from the provider configuration. diff --git a/examples/resources/iosxe_cli/resource.tf b/examples/resources/iosxe_cli/resource.tf new file mode 100644 index 00000000..af5a5b1f --- /dev/null +++ b/examples/resources/iosxe_cli/resource.tf @@ -0,0 +1,6 @@ +resource "iosxe_cli" "example" { + cli = <<-EOT + interface Loopback123 + description configured-via-restconf-cli + EOT +} diff --git a/gen/doc_category.go b/gen/doc_category.go index bcc98a58..94242b8f 100644 --- a/gen/doc_category.go +++ b/gen/doc_category.go @@ -104,9 +104,17 @@ func main() { if err != nil { log.Fatalf("Error opening documentation: %v", err) } - s := string(content) s = strings.ReplaceAll(s, `subcategory: ""`, `subcategory: "General"`) + os.WriteFile(filename, []byte(s), 0644) + // update iosxe_cli resource + filename = "./docs/resources/cli.md" + content, err = os.ReadFile(filename) + if err != nil { + log.Fatalf("Error opening documentation: %v", err) + } + s = string(content) + s = strings.ReplaceAll(s, `subcategory: ""`, `subcategory: "General"`) os.WriteFile(filename, []byte(s), 0644) } diff --git a/gen/templates/provider.go b/gen/templates/provider.go index 3218ea80..3a7b68e8 100644 --- a/gen/templates/provider.go +++ b/gen/templates/provider.go @@ -278,6 +278,7 @@ func (p *IosxeProvider) Resources(ctx context.Context) []func() resource.Resourc return []func() resource.Resource{ NewRestconfResource, NewSaveConfigResource, + NewCliResource, {{- range .}} New{{camelCase .Name}}Resource, {{- end}} diff --git a/internal/provider/provider.go b/internal/provider/provider.go index f25edf6b..5839bf52 100644 --- a/internal/provider/provider.go +++ b/internal/provider/provider.go @@ -276,6 +276,7 @@ func (p *IosxeProvider) Resources(ctx context.Context) []func() resource.Resourc return []func() resource.Resource{ NewRestconfResource, NewSaveConfigResource, + NewCliResource, NewAAAResource, NewAAAAccountingResource, NewAAAAuthenticationResource, diff --git a/internal/provider/resource_iosxe_cli.go b/internal/provider/resource_iosxe_cli.go new file mode 100644 index 00000000..22b2b409 --- /dev/null +++ b/internal/provider/resource_iosxe_cli.go @@ -0,0 +1,157 @@ +// 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 + +import ( + "context" + "fmt" + "strings" + + "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/types" + "github.com/hashicorp/terraform-plugin-log/tflog" + "github.com/netascode/go-restconf" + "github.com/tidwall/sjson" +) + +// Ensure provider defined types fully satisfy framework interfaces +var _ resource.Resource = &CliResource{} + +func NewCliResource() resource.Resource { + return &CliResource{} +} + +type CliResource struct { + clients map[string]*restconf.Client +} + +func (r *CliResource) Metadata(ctx context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) { + resp.TypeName = req.ProviderTypeName + "_cli" +} + +func (r *CliResource) 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: "This resources is used to configure arbitrary CLI commands. This should be considered a last resort in case YANG models are not available, as it cannot read the state and therefore cannot reconcile changes.", + + Attributes: map[string]schema.Attribute{ + "device": schema.StringAttribute{ + MarkdownDescription: "A device name from the provider configuration.", + Optional: true, + }, + "cli": schema.StringAttribute{ + MarkdownDescription: "This attribute contains the CLI commands.", + Required: true, + }, + }, + } +} + +func (r *CliResource) Configure(_ context.Context, req resource.ConfigureRequest, _ *resource.ConfigureResponse) { + if req.ProviderData == nil { + return + } + + r.clients = req.ProviderData.(map[string]*restconf.Client) +} + +func (r *CliResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) { + var device types.String + var cli types.String + + diags := req.Plan.GetAttribute(ctx, path.Root("device"), &device) + resp.Diagnostics.Append(diags...) + if resp.Diagnostics.HasError() { + return + } + diags = req.Plan.GetAttribute(ctx, path.Root("cli"), &cli) + resp.Diagnostics.Append(diags...) + if resp.Diagnostics.HasError() { + return + } + + if _, ok := r.clients[device.ValueString()]; !ok { + resp.Diagnostics.AddAttributeError(path.Root("device"), "Invalid device", fmt.Sprintf("Device '%s' does not exist in provider configuration.", device.ValueString())) + return + } + + tflog.Debug(ctx, "Beginning to send CLI commands") + + body := "" + body, _ = sjson.Set(body, "Cisco-IOS-XE-cli-rpc:input.config-clis", cli.ValueString()) + request := r.clients[device.ValueString()].NewReq("POST", "/operations/Cisco-IOS-XE-cli-rpc:config-ios-cli-rpc", strings.NewReader(body)) + _, err := r.clients[device.ValueString()].Do(request) + if err != nil { + resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Failed to send CLI commands, got error: %s", err)) + return + } + + tflog.Debug(ctx, "Send CLI commands finished successfully") + + diags = resp.State.SetAttribute(ctx, path.Root("device"), device) + resp.Diagnostics.Append(diags...) + diags = resp.State.SetAttribute(ctx, path.Root("cli"), cli) + resp.Diagnostics.Append(diags...) +} + +func (r *CliResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) { +} + +func (r *CliResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) { + var device types.String + var cli types.String + + diags := req.Plan.GetAttribute(ctx, path.Root("device"), &device) + resp.Diagnostics.Append(diags...) + if resp.Diagnostics.HasError() { + return + } + diags = req.Plan.GetAttribute(ctx, path.Root("cli"), &cli) + resp.Diagnostics.Append(diags...) + if resp.Diagnostics.HasError() { + return + } + + if _, ok := r.clients[device.ValueString()]; !ok { + resp.Diagnostics.AddAttributeError(path.Root("device"), "Invalid device", fmt.Sprintf("Device '%s' does not exist in provider configuration.", device.ValueString())) + return + } + + tflog.Debug(ctx, "Beginning to send CLI commands") + + body := "" + body, _ = sjson.Set(body, "Cisco-IOS-XE-cli-rpc:input.config-clis", cli.ValueString()) + request := r.clients[device.ValueString()].NewReq("POST", "/operations/Cisco-IOS-XE-cli-rpc:config-ios-cli-rpc", strings.NewReader(body)) + _, err := r.clients[device.ValueString()].Do(request) + if err != nil { + resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Failed to send CLI commands, got error: %s", err)) + return + } + + tflog.Debug(ctx, "Send CLI commands finished successfully") + + diags = resp.State.SetAttribute(ctx, path.Root("device"), device) + resp.Diagnostics.Append(diags...) + diags = resp.State.SetAttribute(ctx, path.Root("cli"), cli) + resp.Diagnostics.Append(diags...) +} + +func (r *CliResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) { +} diff --git a/internal/provider/resource_iosxe_cli_test.go b/internal/provider/resource_iosxe_cli_test.go new file mode 100644 index 00000000..1a314724 --- /dev/null +++ b/internal/provider/resource_iosxe_cli_test.go @@ -0,0 +1,49 @@ +// 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 + +import ( + "testing" + + "github.com/hashicorp/terraform-plugin-testing/helper/resource" +) + +func TestAccIosxeCli(t *testing.T) { + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + ProtoV6ProviderFactories: testAccProtoV6ProviderFactories, + Steps: []resource.TestStep{ + { + Config: testAccIosxeCliConfig_loopback(), + }, + }, + }) +} + +func testAccIosxeCliConfig_loopback() string { + return ` + resource "iosxe_cli" "test" { + cli = <<-EOT + interface Loopback123 + description configured-via-restconf-cli + EOT + } + ` +} diff --git a/internal/provider/resource_iosxe_save_config_test.go b/internal/provider/resource_iosxe_save_config_test.go new file mode 100644 index 00000000..15946a97 --- /dev/null +++ b/internal/provider/resource_iosxe_save_config_test.go @@ -0,0 +1,46 @@ +// 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 + +import ( + "testing" + + "github.com/hashicorp/terraform-plugin-testing/helper/resource" +) + +func TestAccIosxeSaveConfig(t *testing.T) { + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + ProtoV6ProviderFactories: testAccProtoV6ProviderFactories, + Steps: []resource.TestStep{ + { + Config: testAccIosxeSaveConfig(), + }, + }, + }) +} + +func testAccIosxeSaveConfig() string { + return ` + resource "iosxe_save_config" "test" { + save = false + } + ` +} diff --git a/templates/guides/changelog.md.tmpl b/templates/guides/changelog.md.tmpl index 5f4c8062..ec4aa6d5 100644 --- a/templates/guides/changelog.md.tmpl +++ b/templates/guides/changelog.md.tmpl @@ -12,6 +12,7 @@ description: |- - Add `iosxe_vlan_filter` resource and data source - Add `iosxe_vlan_group` resource and data source - Add `iosxe_save_config` resource +- Add `iosxe_cli` resource ## 0.5.0