Skip to content

Commit

Permalink
feat(file): Add support for /file (#355)
Browse files Browse the repository at this point in the history
* feat(no-release): Add support for /file
  • Loading branch information
ksmets authored Feb 13, 2024
1 parent eb4bef2 commit 2a1d9fd
Show file tree
Hide file tree
Showing 6 changed files with 174 additions and 0 deletions.
42 changes: 42 additions & 0 deletions docs/resources/file.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# routeros_file (Resource)


## Example Usage
```terraform
resource "routeros_file" "test" {
name = "test"
contents = "This is a test"
}
```

<!-- schema generated by tfplugindocs -->
## Schema

### Required

- `name` (String) Name of the file

### Optional

- `___id___` (Number) <em>Resource ID type (.id / name). This is an internal service field, setting a value is not required.</em>
- `___path___` (String) <em>Resource path for CRUD operations. This is an internal service field, setting a value is not required.</em>
- `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"
```
3 changes: 3 additions & 0 deletions examples/resources/routeros_file/import.sh
Original file line number Diff line number Diff line change
@@ -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"
4 changes: 4 additions & 0 deletions examples/resources/routeros_file/resource.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
resource "routeros_file" "test" {
name = "test"
contents = "This is a test"
}
3 changes: 3 additions & 0 deletions routeros/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
73 changes: 73 additions & 0 deletions routeros/resource_file.go
Original file line number Diff line number Diff line change
@@ -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,
}
}
49 changes: 49 additions & 0 deletions routeros/resource_file_test.go
Original file line number Diff line number Diff line change
@@ -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"
}
`
}

0 comments on commit 2a1d9fd

Please sign in to comment.