From 2a1d9fd7f866b427fcbff4589b6b19d42c9267a1 Mon Sep 17 00:00:00 2001 From: Koen Smets Date: Tue, 13 Feb 2024 19:24:52 +0100 Subject: [PATCH] feat(file): Add support for /file (#355) * feat(no-release): Add support for /file --- docs/resources/file.md | 42 +++++++++++ examples/resources/routeros_file/import.sh | 3 + examples/resources/routeros_file/resource.tf | 4 ++ routeros/provider.go | 3 + routeros/resource_file.go | 73 ++++++++++++++++++++ routeros/resource_file_test.go | 49 +++++++++++++ 6 files changed, 174 insertions(+) create mode 100644 docs/resources/file.md create mode 100644 examples/resources/routeros_file/import.sh create mode 100644 examples/resources/routeros_file/resource.tf create mode 100644 routeros/resource_file.go create mode 100644 routeros/resource_file_test.go diff --git a/docs/resources/file.md b/docs/resources/file.md new file mode 100644 index 00000000..1ddf8e42 --- /dev/null +++ b/docs/resources/file.md @@ -0,0 +1,42 @@ +# routeros_file (Resource) + + +## Example Usage +```terraform +resource "routeros_file" "test" { + name = "test" + contents = "This is a test" +} +``` + + +## Schema + +### Required + +- `name` (String) Name of the file + +### Optional + +- `___id___` (Number) Resource ID type (.id / name). This is an internal service field, setting a value is not required. +- `___path___` (String) Resource path for CRUD operations. This is an internal service field, setting a value is not required. +- `contents` (String) The actual content of the file + +### Read-Only + +- `creation_time` (String) A time when the file was created +- `id` (String) The ID of this resource. +- `package_architecture` (String) Architecture that package is built for. Applies only to RouterOS ".npk" files +- `package_built_time` (String) A time when the package was built. Applies only to RouterOS ".npk" files +- `package_name` (String) Name of the installable package. Applies only to RouterOS ".npk" files +- `package_version` (String) A version of the installable package. Applies only to RouterOS ".npk" files +- `size` (Number) File size in bytes +- `type` (String) Type of the file. For folders, the file type is the directory + +## Import +Import is supported using the following syntax: +```shell +#The ID can be found via API or the terminal +#The command for the terminal is -> :put [/file get [print show-ids]] +terraform import routeros_file.test "*1" +``` diff --git a/examples/resources/routeros_file/import.sh b/examples/resources/routeros_file/import.sh new file mode 100644 index 00000000..a74e0aed --- /dev/null +++ b/examples/resources/routeros_file/import.sh @@ -0,0 +1,3 @@ +#The ID can be found via API or the terminal +#The command for the terminal is -> :put [/file get [print show-ids]] +terraform import routeros_file.test "*1" \ No newline at end of file diff --git a/examples/resources/routeros_file/resource.tf b/examples/resources/routeros_file/resource.tf new file mode 100644 index 00000000..3c24eb71 --- /dev/null +++ b/examples/resources/routeros_file/resource.tf @@ -0,0 +1,4 @@ +resource "routeros_file" "test" { + name = "test" + contents = "This is a test" +} diff --git a/routeros/provider.go b/routeros/provider.go index e222330d..117a2cfd 100644 --- a/routeros/provider.go +++ b/routeros/provider.go @@ -185,6 +185,9 @@ func Provider() *schema.Provider { "routeros_capsman_rates": ResourceCapsManRates(), "routeros_capsman_security": ResourceCapsManSecurity(), + // File objects + "routeros_file": ResourceFile(), + // Routing "routeros_routing_bgp_connection": ResourceRoutingBGPConnection(), "routeros_routing_bgp_template": ResourceRoutingBGPTemplate(), diff --git a/routeros/resource_file.go b/routeros/resource_file.go new file mode 100644 index 00000000..c1ff9e3c --- /dev/null +++ b/routeros/resource_file.go @@ -0,0 +1,73 @@ +package routeros + +import ( + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" +) + +// https://help.mikrotik.com/docs/pages/viewpage.action?pageId=2555971 +func ResourceFile() *schema.Resource { + resSchema := map[string]*schema.Schema{ + MetaResourcePath: PropResourcePath("/file"), + MetaId: PropId(Id), + + "contents": { + Type: schema.TypeString, + ForceNew: true, + Optional: true, + Description: "The actual content of the file", + }, + "creation_time": { + Type: schema.TypeString, + Computed: true, + Description: "A time when the file was created", + }, + "name": { + Type: schema.TypeString, + Required: true, + Description: "Name of the file", + }, + "package_architecture": { + Type: schema.TypeString, + Computed: true, + Description: "Architecture that package is built for. Applies only to RouterOS \".npk\" files", + }, + "package_built_time": { + Type: schema.TypeString, + Computed: true, + Description: "A time when the package was built. Applies only to RouterOS \".npk\" files", + }, + "package_name": { + Type: schema.TypeString, + Computed: true, + Description: "Name of the installable package. Applies only to RouterOS \".npk\" files", + }, + "package_version": { + Type: schema.TypeString, + Computed: true, + Description: "A version of the installable package. Applies only to RouterOS \".npk\" files", + }, + "size": { + Type: schema.TypeInt, + Computed: true, + Description: "File size in bytes", + }, + "type": { + Type: schema.TypeString, + Computed: true, + Description: "Type of the file. For folders, the file type is the directory", + }, + } + + return &schema.Resource{ + CreateContext: DefaultCreate(resSchema), + ReadContext: DefaultRead(resSchema), + UpdateContext: DefaultUpdate(resSchema), + DeleteContext: DefaultDelete(resSchema), + + Importer: &schema.ResourceImporter{ + StateContext: schema.ImportStatePassthroughContext, + }, + + Schema: resSchema, + } +} diff --git a/routeros/resource_file_test.go b/routeros/resource_file_test.go new file mode 100644 index 00000000..5ed68364 --- /dev/null +++ b/routeros/resource_file_test.go @@ -0,0 +1,49 @@ +package routeros + +import ( + "testing" + + "github.com/hashicorp/terraform-plugin-testing/helper/resource" +) + +const testFileMinVersion = "7.9" +const testFile = "routeros_file.test" + +func TestAccFileTest_basic(t *testing.T) { + if !testCheckMinVersion(t, testFileMinVersion) { + t.Logf("Test skipped, the minimum required version is %v", testFileMinVersion) + return + } + + for _, name := range testNames { + t.Run(name, func(t *testing.T) { + resource.Test(t, resource.TestCase{ + PreCheck: func() { + testAccPreCheck(t) + testSetTransportEnv(t, name) + }, + ProviderFactories: testAccProviderFactories, + CheckDestroy: testCheckResourceDestroy("/file", "routeros_file"), + Steps: []resource.TestStep{ + { + Config: testAccFileConfig(), + Check: resource.ComposeTestCheckFunc( + testResourcePrimaryInstanceId(testFile), + resource.TestCheckResourceAttr(testFile, "name", "test"), + ), + }, + }, + }) + }) + } +} + +func testAccFileConfig() string { + return providerConfig + ` + + resource "routeros_file" "test" { + name = "test" + contents = "This is a test" + } +` +}