diff --git a/docs/data-sources/import_lists.md b/docs/data-sources/import_lists.md new file mode 100644 index 00000000..43693837 --- /dev/null +++ b/docs/data-sources/import_lists.md @@ -0,0 +1,90 @@ +--- +# generated by https://github.com/hashicorp/terraform-plugin-docs +page_title: "radarr_import_lists Data Source - terraform-provider-radarr" +subcategory: "Import Lists" +description: |- + List all available Import Lists ../resources/import_list. +--- + +# radarr_import_lists (Data Source) + +List all available [Import Lists](../resources/import_list). + +## Example Usage + +```terraform +data "radarr_import_lists" "example" { +} +``` + + +## Schema + +### Read-Only + +- `id` (String) The ID of this resource. +- `import_lists` (Attributes Set) Import List list. (see [below for nested schema](#nestedatt--import_lists)) + + +### Nested Schema for `import_lists` + +Read-Only: + +- `access_token` (String, Sensitive) Access token. +- `account_id` (String) Account ID. +- `api_key` (String, Sensitive) API key. +- `auth_user` (String) Auth user. +- `base_url` (String) Base URL. +- `cast` (Boolean) Include cast. +- `cast_director` (Boolean) Include cast director. +- `cast_producer` (Boolean) Include cast producer. +- `cast_sound` (Boolean) Include cast sound. +- `cast_writing` (Boolean) Include cast writing. +- `certification` (String) Certification. +- `company_id` (String) Company ID. +- `config_contract` (String) ImportList configuration template. +- `enable_auto` (Boolean) Enable automatic add flag. +- `enabled` (Boolean) Enabled flag. +- `exclude_genre_ids` (String) Exclude genre IDs. +- `expires` (String) Expires. +- `genres` (String) Genres. +- `id` (Number) Import List ID. +- `implementation` (String) ImportList implementation name. +- `include_genre_ids` (String) Include genre IDs. +- `keyword_id` (String) Keyword ID. +- `language_code` (Number) Language code. +- `limit` (Number) limit. +- `link` (String) Link. +- `list_id` (String) List ID. +- `list_order` (Number) List order. +- `list_type` (String) List type. +- `listname` (String) List name. +- `min_score` (Number) Min score. +- `min_vote_average` (String) Min vote average. +- `min_votes` (String) Min votes. +- `minimum_availability` (String) Minimum availability. +- `monitor` (String) Should monitor. +- `name` (String) Import List name. +- `only_active` (Boolean) Only active. +- `person_id` (String) Person ID. +- `port` (Number) Port. +- `profile_ids` (Set of Number) Profile IDs. +- `quality_profile_id` (Number) Quality profile ID. +- `rating` (String) Rating. +- `refresh_token` (String, Sensitive) Refresh token. +- `root_folder_path` (String) Root folder path. +- `search_on_add` (Boolean) Search on add flag. +- `source` (Number) Source. +- `tag_ids` (Set of Number) Tag IDs. +- `tags` (Set of Number) List of associated tags. +- `tmdb_certification` (String) Certification. +- `tmdb_list_type` (Number) TMDB list type. +- `trakt_additional_parameters` (String) Trakt additional parameters. +- `trakt_list_type` (Number) Trakt list type. +- `url` (String) URL. +- `url_base` (String) Base URL. +- `user_list_type` (Number) User list type. +- `username` (String) Username. +- `years` (String) Years. + + diff --git a/examples/data-sources/radarr_import_lists/data-source.tf b/examples/data-sources/radarr_import_lists/data-source.tf new file mode 100644 index 00000000..9abe1560 --- /dev/null +++ b/examples/data-sources/radarr_import_lists/data-source.tf @@ -0,0 +1,2 @@ +data "radarr_import_lists" "example" { +} diff --git a/internal/provider/import_lists_data_source.go b/internal/provider/import_lists_data_source.go new file mode 100644 index 00000000..ba490a4f --- /dev/null +++ b/internal/provider/import_lists_data_source.go @@ -0,0 +1,326 @@ +package provider + +import ( + "context" + "strconv" + + "github.com/devopsarr/radarr-go/radarr" + "github.com/devopsarr/terraform-provider-radarr/internal/helpers" + "github.com/hashicorp/terraform-plugin-framework/datasource" + "github.com/hashicorp/terraform-plugin-framework/datasource/schema" + "github.com/hashicorp/terraform-plugin-framework/tfsdk" + "github.com/hashicorp/terraform-plugin-framework/types" + "github.com/hashicorp/terraform-plugin-log/tflog" +) + +const importListsDataSourceName = "import_lists" + +// Ensure provider defined types fully satisfy framework interfaces. +var _ datasource.DataSource = &ImportListsDataSource{} + +func NewImportListsDataSource() datasource.DataSource { + return &ImportListsDataSource{} +} + +// ImportListsDataSource defines the import lists implementation. +type ImportListsDataSource struct { + client *radarr.APIClient +} + +// ImportLists describes the import lists data model. +type ImportLists struct { + ImportLists types.Set `tfsdk:"import_lists"` + ID types.String `tfsdk:"id"` +} + +func (d *ImportListsDataSource) Metadata(ctx context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) { + resp.TypeName = req.ProviderTypeName + "_" + importListsDataSourceName +} + +func (d *ImportListsDataSource) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) { + resp.Schema = schema.Schema{ + // This description is used by the documentation generator and the delay server. + MarkdownDescription: "List all available [Import Lists](../resources/import_list).", + Attributes: map[string]schema.Attribute{ + // TODO: remove ID once framework support tests without ID https://www.terraform.io/plugin/framework/acctests#implement-id-attribute + "id": schema.StringAttribute{ + Computed: true, + }, + "import_lists": schema.SetNestedAttribute{ + MarkdownDescription: "Import List list.", + Computed: true, + NestedObject: schema.NestedAttributeObject{ + Attributes: map[string]schema.Attribute{ + "enable_auto": schema.BoolAttribute{ + MarkdownDescription: "Enable automatic add flag.", + Computed: true, + }, + "enabled": schema.BoolAttribute{ + MarkdownDescription: "Enabled flag.", + Computed: true, + }, + "search_on_add": schema.BoolAttribute{ + MarkdownDescription: "Search on add flag.", + Computed: true, + }, + "quality_profile_id": schema.Int64Attribute{ + MarkdownDescription: "Quality profile ID.", + Computed: true, + }, + "list_order": schema.Int64Attribute{ + MarkdownDescription: "List order.", + Computed: true, + }, + "root_folder_path": schema.StringAttribute{ + MarkdownDescription: "Root folder path.", + Computed: true, + }, + "monitor": schema.StringAttribute{ + MarkdownDescription: "Should monitor.", + Computed: true, + }, + "minimum_availability": schema.StringAttribute{ + MarkdownDescription: "Minimum availability.", + Computed: true, + }, + "implementation": schema.StringAttribute{ + MarkdownDescription: "ImportList implementation name.", + Computed: true, + }, + "config_contract": schema.StringAttribute{ + MarkdownDescription: "ImportList configuration template.", + Computed: true, + }, + "list_type": schema.StringAttribute{ + MarkdownDescription: "List type.", + Computed: true, + }, + "name": schema.StringAttribute{ + MarkdownDescription: "Import List name.", + Computed: true, + }, + "tags": schema.SetAttribute{ + MarkdownDescription: "List of associated tags.", + Computed: true, + ElementType: types.Int64Type, + }, + "id": schema.Int64Attribute{ + MarkdownDescription: "Import List ID.", + Computed: true, + }, + // Field values + "only_active": schema.BoolAttribute{ + MarkdownDescription: "Only active.", + Computed: true, + }, + "cast": schema.BoolAttribute{ + MarkdownDescription: "Include cast.", + Computed: true, + }, + "cast_director": schema.BoolAttribute{ + MarkdownDescription: "Include cast director.", + Computed: true, + }, + "cast_producer": schema.BoolAttribute{ + MarkdownDescription: "Include cast producer.", + Computed: true, + }, + "cast_sound": schema.BoolAttribute{ + MarkdownDescription: "Include cast sound.", + Computed: true, + }, + "cast_writing": schema.BoolAttribute{ + MarkdownDescription: "Include cast writing.", + Computed: true, + }, + "port": schema.Int64Attribute{ + MarkdownDescription: "Port.", + Computed: true, + }, + "source": schema.Int64Attribute{ + MarkdownDescription: "Source.", + Computed: true, + }, + "min_score": schema.Int64Attribute{ + MarkdownDescription: "Min score.", + Computed: true, + }, + "tmdb_list_type": schema.Int64Attribute{ + MarkdownDescription: "TMDB list type.", + Computed: true, + }, + "user_list_type": schema.Int64Attribute{ + MarkdownDescription: "User list type.", + Computed: true, + }, + "limit": schema.Int64Attribute{ + MarkdownDescription: "limit.", + Computed: true, + }, + "trakt_list_type": schema.Int64Attribute{ + MarkdownDescription: "Trakt list type.", + Computed: true, + }, + "language_code": schema.Int64Attribute{ + MarkdownDescription: "Language code.", + Computed: true, + }, + "listname": schema.StringAttribute{ + MarkdownDescription: "List name.", + Computed: true, + }, + "username": schema.StringAttribute{ + MarkdownDescription: "Username.", + Computed: true, + }, + "auth_user": schema.StringAttribute{ + MarkdownDescription: "Auth user.", + Computed: true, + }, + "access_token": schema.StringAttribute{ + MarkdownDescription: "Access token.", + Computed: true, + Sensitive: true, + }, + "refresh_token": schema.StringAttribute{ + MarkdownDescription: "Refresh token.", + Computed: true, + Sensitive: true, + }, + "api_key": schema.StringAttribute{ + MarkdownDescription: "API key.", + Computed: true, + Sensitive: true, + }, + "company_id": schema.StringAttribute{ + MarkdownDescription: "Company ID.", + Computed: true, + }, + "keyword_id": schema.StringAttribute{ + MarkdownDescription: "Keyword ID.", + Computed: true, + }, + "list_id": schema.StringAttribute{ + MarkdownDescription: "List ID.", + Computed: true, + }, + "person_id": schema.StringAttribute{ + MarkdownDescription: "Person ID.", + Computed: true, + }, + "account_id": schema.StringAttribute{ + MarkdownDescription: "Account ID.", + Computed: true, + }, + "base_url": schema.StringAttribute{ + MarkdownDescription: "Base URL.", + Computed: true, + }, + "url_base": schema.StringAttribute{ + MarkdownDescription: "Base URL.", + Computed: true, + }, + "url": schema.StringAttribute{ + MarkdownDescription: "URL.", + Computed: true, + }, + "link": schema.StringAttribute{ + MarkdownDescription: "Link.", + Computed: true, + }, + "expires": schema.StringAttribute{ + MarkdownDescription: "Expires.", + Computed: true, + }, + "trakt_additional_parameters": schema.StringAttribute{ + MarkdownDescription: "Trakt additional parameters.", + Computed: true, + }, + "certification": schema.StringAttribute{ + MarkdownDescription: "Certification.", + Computed: true, + }, + "genres": schema.StringAttribute{ + MarkdownDescription: "Genres.", + Computed: true, + }, + "years": schema.StringAttribute{ + MarkdownDescription: "Years.", + Computed: true, + }, + "rating": schema.StringAttribute{ + MarkdownDescription: "Rating.", + Computed: true, + }, + "min_vote_average": schema.StringAttribute{ + MarkdownDescription: "Min vote average.", + Computed: true, + }, + "min_votes": schema.StringAttribute{ + MarkdownDescription: "Min votes.", + Computed: true, + }, + "tmdb_certification": schema.StringAttribute{ + MarkdownDescription: "Certification.", + Computed: true, + }, + "include_genre_ids": schema.StringAttribute{ + MarkdownDescription: "Include genre IDs.", + Computed: true, + }, + "exclude_genre_ids": schema.StringAttribute{ + MarkdownDescription: "Exclude genre IDs.", + Computed: true, + }, + "profile_ids": schema.SetAttribute{ + MarkdownDescription: "Profile IDs.", + Computed: true, + ElementType: types.Int64Type, + }, + "tag_ids": schema.SetAttribute{ + MarkdownDescription: "Tag IDs.", + Computed: true, + ElementType: types.Int64Type, + }, + }, + }, + }, + }, + } +} + +func (d *ImportListsDataSource) Configure(ctx context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) { + if client := helpers.DataSourceConfigure(ctx, req, resp); client != nil { + d.client = client + } +} + +func (d *ImportListsDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) { + var data *ImportLists + + resp.Diagnostics.Append(resp.State.Get(ctx, &data)...) + + if resp.Diagnostics.HasError() { + return + } + // Get import lists current value + response, _, err := d.client.ImportListApi.ListImportList(ctx).Execute() + if err != nil { + resp.Diagnostics.AddError(helpers.ClientError, helpers.ParseClientError(helpers.Read, importListsDataSourceName, err)) + + return + } + + tflog.Trace(ctx, "read "+importListsDataSourceName) + // Map response body to resource schema attribute + importLists := make([]ImportList, len(response)) + for i, d := range response { + importLists[i].Tags = types.SetNull(types.Int64Type) + importLists[i].write(ctx, d) + } + + tfsdk.ValueFrom(ctx, importLists, data.ImportLists.Type(ctx), &data.ImportLists) + // TODO: remove ID once framework support tests without ID https://www.terraform.io/plugin/framework/acctests#implement-id-attribute + data.ID = types.StringValue(strconv.Itoa(len(response))) + resp.Diagnostics.Append(resp.State.Set(ctx, &data)...) +} diff --git a/internal/provider/import_lists_data_source_test.go b/internal/provider/import_lists_data_source_test.go new file mode 100644 index 00000000..3228760b --- /dev/null +++ b/internal/provider/import_lists_data_source_test.go @@ -0,0 +1,35 @@ +package provider + +import ( + "testing" + + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" +) + +func TestAccImportListsDataSource(t *testing.T) { + t.Parallel() + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + ProtoV6ProviderFactories: testAccProtoV6ProviderFactories, + Steps: []resource.TestStep{ + // Create a delay profile to have a value to check + { + PreConfig: rootFolderDSInit, + Config: testAccImportListResourceConfig("importListsDataTest", "false"), + }, + // Read testing + { + Config: testAccImportListsDataSourceConfig, + Check: resource.ComposeAggregateTestCheckFunc( + resource.TestCheckTypeSetElemNestedAttrs("data.radarr_import_lists.test", "import_lists.*", map[string]string{"base_url": "http://127.0.0.1:7878"}), + ), + }, + }, + }) +} + +const testAccImportListsDataSourceConfig = ` +data "radarr_import_lists" "test" { +} +` diff --git a/internal/provider/provider.go b/internal/provider/provider.go index 9603be7f..acd924c6 100644 --- a/internal/provider/provider.go +++ b/internal/provider/provider.go @@ -251,6 +251,7 @@ func (p *RadarrProvider) DataSources(ctx context.Context) []func() datasource.Da NewMoviesDataSource, // Notifications + NewImportListsDataSource, NewNotificationDataSource, NewNotificationsDataSource,