diff --git a/docs/data-sources/import_list.md b/docs/data-sources/import_list.md new file mode 100644 index 00000000..56dfd638 --- /dev/null +++ b/docs/data-sources/import_list.md @@ -0,0 +1,86 @@ +--- +# generated by https://github.com/hashicorp/terraform-plugin-docs +page_title: "radarr_import_list Data Source - terraform-provider-radarr" +subcategory: "Import Lists" +description: |- + Single Import List ../resources/import_list. +--- + +# radarr_import_list (Data Source) + +Single [Import List](../resources/import_list). + +## Example Usage + +```terraform +data "radarr_import_list" "example" { + name = "Example" +} +``` + + +## Schema + +### Required + +- `name` (String) Import List name. + +### 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. +- `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_list/data-source.tf b/examples/data-sources/radarr_import_list/data-source.tf new file mode 100644 index 00000000..efe0c227 --- /dev/null +++ b/examples/data-sources/radarr_import_list/data-source.tf @@ -0,0 +1,3 @@ +data "radarr_import_list" "example" { + name = "Example" +} diff --git a/internal/provider/import_list_data_source.go b/internal/provider/import_list_data_source.go new file mode 100644 index 00000000..1ff6d03f --- /dev/null +++ b/internal/provider/import_list_data_source.go @@ -0,0 +1,315 @@ +package provider + +import ( + "context" + "fmt" + + "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/types" + "github.com/hashicorp/terraform-plugin-log/tflog" +) + +const importListDataSourceName = "import_list" + +// Ensure provider defined types fully satisfy framework interfaces. +var _ datasource.DataSource = &ImportListDataSource{} + +func NewImportListDataSource() datasource.DataSource { + return &ImportListDataSource{} +} + +// ImportListDataSource defines the import_list implementation. +type ImportListDataSource struct { + client *radarr.APIClient +} + +func (d *ImportListDataSource) Metadata(ctx context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) { + resp.TypeName = req.ProviderTypeName + "_" + importListDataSourceName +} + +func (d *ImportListDataSource) 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: "Single [Import List](../resources/import_list).", + 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.", + Required: 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 *ImportListDataSource) Configure(ctx context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) { + if client := helpers.DataSourceConfigure(ctx, req, resp); client != nil { + d.client = client + } +} + +func (d *ImportListDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) { + var data *ImportList + + resp.Diagnostics.Append(resp.State.Get(ctx, &data)...) + + if resp.Diagnostics.HasError() { + return + } + // Get importList current value + response, _, err := d.client.ImportListApi.ListImportList(ctx).Execute() + if err != nil { + resp.Diagnostics.AddError(helpers.ClientError, helpers.ParseClientError(helpers.Read, importListDataSourceName, err)) + + return + } + + importList, err := findImportList(data.Name.ValueString(), response) + if err != nil { + resp.Diagnostics.AddError(helpers.DataSourceError, fmt.Sprintf("Unable to find %s, got error: %s", importListDataSourceName, err)) + + return + } + + tflog.Trace(ctx, "read "+importListDataSourceName) + data.write(ctx, importList) + resp.Diagnostics.Append(resp.State.Set(ctx, &data)...) +} + +func findImportList(name string, importLists []*radarr.ImportListResource) (*radarr.ImportListResource, error) { + for _, i := range importLists { + if i.GetName() == name { + return i, nil + } + } + + return nil, helpers.ErrDataNotFoundError(importListDataSourceName, "name", name) +} diff --git a/internal/provider/import_list_data_source_test.go b/internal/provider/import_list_data_source_test.go new file mode 100644 index 00000000..4ea3f3fc --- /dev/null +++ b/internal/provider/import_list_data_source_test.go @@ -0,0 +1,48 @@ +package provider + +import ( + "testing" + + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" +) + +func TestAccImportListDataSource(t *testing.T) { + t.Parallel() + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + ProtoV6ProviderFactories: testAccProtoV6ProviderFactories, + Steps: []resource.TestStep{ + // Read testing + { + PreConfig: rootFolderDSInit, + Config: testAccImportListDataSourceConfig, + Check: resource.ComposeAggregateTestCheckFunc( + resource.TestCheckResourceAttrSet("data.radarr_import_list.test", "id"), + resource.TestCheckResourceAttr("data.radarr_import_list.test", "monitor", "movieOnly")), + }, + }, + }) +} + +const testAccImportListDataSourceConfig = ` +resource "radarr_import_list" "test" { + enabled = false + enable_auto = false + search_on_add = false + list_type = "program" + root_folder_path = "/config" + monitor = "movieOnly" + minimum_availability = "tba" + quality_profile_id = 1 + name = "importListDataTest" + implementation = "RadarrImport" + config_contract = "RadarrSettings" + base_url = "http://127.0.0.1:7878" + api_key = "testAPIKey" +} + +data "radarr_import_list" "test" { + name = radarr_import_list.test.name +} +` diff --git a/internal/provider/provider.go b/internal/provider/provider.go index acd924c6..eafb85c9 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 + NewImportListDataSource, NewImportListsDataSource, NewNotificationDataSource, NewNotificationsDataSource,