Skip to content

Commit

Permalink
feat: add media_management datasource
Browse files Browse the repository at this point in the history
  • Loading branch information
Fuochi committed Nov 3, 2022
1 parent fa458ad commit 299ac85
Show file tree
Hide file tree
Showing 5 changed files with 251 additions and 0 deletions.
47 changes: 47 additions & 0 deletions docs/data-sources/media_management.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "radarr_media_management Data Source - terraform-provider-radarr"
subcategory: "Media Management"
description: |-
Media Management ../resources/media_management.
---

# radarr_media_management (Data Source)

[subcategory:Media Management]: #
[Media Management](../resources/media_management).

## Example Usage

```terraform
data "radarr_media_management" "example" {
}
```

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

### Read-Only

- `auto_rename_folders` (Boolean) Auto rename folders.
- `auto_unmonitor_previously_downloaded_movies` (Boolean) Auto unmonitor previously downloaded movies.
- `chmod_folder` (String) Permission in linux format.
- `chown_group` (String) Group used for permission.
- `copy_using_hardlinks` (Boolean) Use hardlinks instead of copy.
- `create_empty_movie_folders` (Boolean) Create empty movies directories.
- `delete_empty_folders` (Boolean) Delete empty movies directories.
- `download_propers_and_repacks` (String) Download proper and repack policy. valid inputs are: 'preferAndUpgrade', 'doNotUpgrade', and 'doNotPrefer'.
- `enable_media_info` (Boolean) Scan files details.
- `extra_file_extensions` (String) Comma separated list of extra files to import (.nfo will be imported as .nfo-orig).
- `file_date` (String) Define the file date modification. valid inputs are: 'none', 'localAirDate, and 'utcAirDate'.
- `id` (Number) Delay Profile ID.
- `import_extra_files` (Boolean) Import extra files. If enabled it will leverage 'extra_file_extensions'.
- `minimum_free_space_when_importing` (Number) Minimum free space in MB to allow import.
- `paths_default_static` (Boolean) Path default static.
- `recycle_bin` (String) Recycle bin absolute path.
- `recycle_bin_cleanup_days` (Number) Recyle bin days of retention.
- `rescan_after_refresh` (String) Rescan after refresh policy. valid inputs are: 'always', 'afterManual' and 'never'.
- `set_permissions_linux` (Boolean) Set permission for imported files.
- `skip_free_space_check_when_importing` (Boolean) Skip free space check before importing.


2 changes: 2 additions & 0 deletions examples/data-sources/radarr_media_management/data-source.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
data "radarr_media_management" "example" {
}
172 changes: 172 additions & 0 deletions internal/provider/media_management_data_source.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
package provider

import (
"context"
"fmt"

"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-log/tflog"
"golift.io/starr/radarr"
)

// Ensure provider defined types fully satisfy framework interfaces.
var _ datasource.DataSource = &MediaManagementDataSource{}

func NewMediaManagementDataSource() datasource.DataSource {
return &MediaManagementDataSource{}
}

// MediaManagementDataSource defines the media management implementation.
type MediaManagementDataSource struct {
client *radarr.Radarr
}

func (d *MediaManagementDataSource) Metadata(ctx context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_media_management"
}

func (d *MediaManagementDataSource) GetSchema(ctx context.Context) (tfsdk.Schema, diag.Diagnostics) {
return tfsdk.Schema{
// This description is used by the documentation generator and the delay server.
MarkdownDescription: "[subcategory:Media Management]: #\n[Media Management](../resources/media_management).",
Attributes: map[string]tfsdk.Attribute{
"id": {
MarkdownDescription: "Delay Profile ID.",
Computed: true,
Type: types.Int64Type,
},
"auto_rename_folders": {
MarkdownDescription: "Auto rename folders.",
Computed: true,
Type: types.BoolType,
},
"auto_unmonitor_previously_downloaded_movies": {
MarkdownDescription: "Auto unmonitor previously downloaded movies.",
Computed: true,
Type: types.BoolType,
},
"copy_using_hardlinks": {
MarkdownDescription: "Use hardlinks instead of copy.",
Computed: true,
Type: types.BoolType,
},
"create_empty_movie_folders": {
MarkdownDescription: "Create empty movies directories.",
Computed: true,
Type: types.BoolType,
},
"delete_empty_folders": {
MarkdownDescription: "Delete empty movies directories.",
Computed: true,
Type: types.BoolType,
},
"enable_media_info": {
MarkdownDescription: "Scan files details.",
Computed: true,
Type: types.BoolType,
},
"import_extra_files": {
MarkdownDescription: "Import extra files. If enabled it will leverage 'extra_file_extensions'.",
Computed: true,
Type: types.BoolType,
},
"paths_default_static": {
MarkdownDescription: "Path default static.",
Computed: true,
Type: types.BoolType,
},
"set_permissions_linux": {
MarkdownDescription: "Set permission for imported files.",
Computed: true,
Type: types.BoolType,
},
"skip_free_space_check_when_importing": {
MarkdownDescription: "Skip free space check before importing.",
Computed: true,
Type: types.BoolType,
},
"minimum_free_space_when_importing": {
MarkdownDescription: "Minimum free space in MB to allow import.",
Computed: true,
Type: types.Int64Type,
},
"recycle_bin_cleanup_days": {
MarkdownDescription: "Recyle bin days of retention.",
Computed: true,
Type: types.Int64Type,
},
"chmod_folder": {
MarkdownDescription: "Permission in linux format.",
Computed: true,
Type: types.StringType,
},
"chown_group": {
MarkdownDescription: "Group used for permission.",
Computed: true,
Type: types.StringType,
},
"download_propers_and_repacks": {
MarkdownDescription: "Download proper and repack policy. valid inputs are: 'preferAndUpgrade', 'doNotUpgrade', and 'doNotPrefer'.",
Computed: true,
Type: types.StringType,
},
"extra_file_extensions": {
MarkdownDescription: "Comma separated list of extra files to import (.nfo will be imported as .nfo-orig).",
Computed: true,
Type: types.StringType,
},
"file_date": {
MarkdownDescription: "Define the file date modification. valid inputs are: 'none', 'localAirDate, and 'utcAirDate'.",
Computed: true,
Type: types.StringType,
},
"recycle_bin": {
MarkdownDescription: "Recycle bin absolute path.",
Computed: true,
Type: types.StringType,
},
"rescan_after_refresh": {
MarkdownDescription: "Rescan after refresh policy. valid inputs are: 'always', 'afterManual' and 'never'.",
Computed: true,
Type: types.StringType,
},
},
}, nil
}

func (d *MediaManagementDataSource) Configure(ctx context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) {
// Prevent panic if the provider has not been configured.
if req.ProviderData == nil {
return
}

client, ok := req.ProviderData.(*radarr.Radarr)
if !ok {
resp.Diagnostics.AddError(
UnexpectedDataSourceConfigureType,
fmt.Sprintf("Expected *radarr.Radarr, got: %T. Please report this issue to the provider developers.", req.ProviderData),
)

return
}

d.client = client
}

func (d *MediaManagementDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
// Get indexer config current value
response, err := d.client.GetMediaManagementContext(ctx)
if err != nil {
resp.Diagnostics.AddError(ClientError, fmt.Sprintf("Unable to read indexer cofig, got error: %s", err))

return
}

tflog.Trace(ctx, "read madia_management")

result := writeMediaManagement(response)
resp.Diagnostics.Append(resp.State.Set(ctx, &result)...)
}
29 changes: 29 additions & 0 deletions internal/provider/media_management_data_source_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package provider

import (
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
)

func TestAccMediaManagementDataSource(t *testing.T) {
t.Parallel()

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
Steps: []resource.TestStep{
// Read testing
{
Config: testAccMediaManagementDataSourceConfig,
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttrSet("data.radarr_media_management.test", "id")),
},
},
})
}

const testAccMediaManagementDataSourceConfig = `
data "radarr_media_management" "test" {
}
`
1 change: 1 addition & 0 deletions internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ func (p *RadarrProvider) Resources(ctx context.Context) []func() resource.Resour

func (p *RadarrProvider) DataSources(ctx context.Context) []func() datasource.DataSource {
return []func() datasource.DataSource{
NewMediaManagementDataSource,
NewRootFolderDataSource,
NewRootFoldersDataSource,
NewSystemStatusDataSource,
Expand Down

0 comments on commit 299ac85

Please sign in to comment.