diff --git a/docs/data-sources/languages.md b/docs/data-sources/languages.md new file mode 100644 index 00000000..f61080ba --- /dev/null +++ b/docs/data-sources/languages.md @@ -0,0 +1,37 @@ +--- +# generated by https://github.com/hashicorp/terraform-plugin-docs +page_title: "radarr_languages Data Source - terraform-provider-radarr" +subcategory: "Profiles" +description: |- + List all available Languages ../data-sources/language. +--- + +# radarr_languages (Data Source) + +List all available [Languages](../data-sources/language). + +## Example Usage + +```terraform +data "radarr_languages" "example" { +} +``` + + +## Schema + +### Read-Only + +- `id` (String) The ID of this resource. +- `languages` (Attributes Set) Language list. (see [below for nested schema](#nestedatt--languages)) + + +### Nested Schema for `languages` + +Read-Only: + +- `id` (Number) Language ID. +- `name` (String) Language. +- `name_lower` (String) Language in lowercase. + + diff --git a/examples/data-sources/radarr_languages/data-source.tf b/examples/data-sources/radarr_languages/data-source.tf new file mode 100644 index 00000000..558fac9d --- /dev/null +++ b/examples/data-sources/radarr_languages/data-source.tf @@ -0,0 +1,2 @@ +data "radarr_languages" "example" { +} diff --git a/internal/provider/languages_data_source.go b/internal/provider/languages_data_source.go new file mode 100644 index 00000000..260206ad --- /dev/null +++ b/internal/provider/languages_data_source.go @@ -0,0 +1,120 @@ +package provider + +import ( + "context" + "fmt" + "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 languagesDataSourceName = "languages" + +// Ensure provider defined types fully satisfy framework interfaces. +var _ datasource.DataSource = &LanguagesDataSource{} + +func NewLanguagesDataSource() datasource.DataSource { + return &LanguagesDataSource{} +} + +// LanguagesDataSource defines the languages implementation. +type LanguagesDataSource struct { + client *radarr.APIClient +} + +// Languages describes the languages data model. +type Languages struct { + Languages types.Set `tfsdk:"languages"` + ID types.String `tfsdk:"id"` +} + +func (d *LanguagesDataSource) Metadata(ctx context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) { + resp.TypeName = req.ProviderTypeName + "_" + languagesDataSourceName +} + +func (d *LanguagesDataSource) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) { + resp.Schema = schema.Schema{ + MarkdownDescription: "List all available [Languages](../data-sources/language).", + 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, + }, + "languages": schema.SetNestedAttribute{ + MarkdownDescription: "Language list.", + Computed: true, + NestedObject: schema.NestedAttributeObject{ + Attributes: map[string]schema.Attribute{ + "id": schema.Int64Attribute{ + MarkdownDescription: "Language ID.", + Computed: true, + }, + "name": schema.StringAttribute{ + MarkdownDescription: "Language.", + Computed: true, + }, + "name_lower": schema.StringAttribute{ + MarkdownDescription: "Language in lowercase.", + Computed: true, + }, + }, + }, + }, + }, + } +} + +func (d *LanguagesDataSource) 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.APIClient) + if !ok { + resp.Diagnostics.AddError( + helpers.UnexpectedDataSourceConfigureType, + fmt.Sprintf("Expected *radarr.APIClient, got: %T. Please report this issue to the provider developers.", req.ProviderData), + ) + + return + } + + d.client = client +} + +func (d *LanguagesDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) { + var data *Languages + + resp.Diagnostics.Append(req.Config.Get(ctx, &data)...) + + if resp.Diagnostics.HasError() { + return + } + + // Get languages current value + response, _, err := d.client.LanguageApi.ListLanguage(ctx).Execute() + if err != nil { + resp.Diagnostics.AddError(helpers.ClientError, helpers.ParseClientError(helpers.Read, languagesDataSourceName, err)) + + return + } + + tflog.Trace(ctx, "read "+languagesDataSourceName) + // Map response body to resource schema attribute + languages := make([]Language, len(response)) + for i, t := range response { + languages[i].write(t) + } + + tfsdk.ValueFrom(ctx, languages, data.Languages.Type(ctx), &data.Languages) + // 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/languages_data_source_test.go b/internal/provider/languages_data_source_test.go new file mode 100644 index 00000000..57a6ede5 --- /dev/null +++ b/internal/provider/languages_data_source_test.go @@ -0,0 +1,30 @@ +package provider + +import ( + "testing" + + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" +) + +func TestAccLanguagesDataSource(t *testing.T) { + t.Parallel() + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + ProtoV6ProviderFactories: testAccProtoV6ProviderFactories, + Steps: []resource.TestStep{ + // Read testing + { + Config: testAccLanguagesDataSourceConfig, + Check: resource.ComposeAggregateTestCheckFunc( + resource.TestCheckTypeSetElemNestedAttrs("data.radarr_languages.test", "languages.*", map[string]string{"name": "English"}), + ), + }, + }, + }) +} + +const testAccLanguagesDataSourceConfig = ` +data "radarr_languages" "test" { +} +` diff --git a/internal/provider/provider.go b/internal/provider/provider.go index ff7b52fd..8af39214 100644 --- a/internal/provider/provider.go +++ b/internal/provider/provider.go @@ -243,6 +243,7 @@ func (p *RadarrProvider) DataSources(ctx context.Context) []func() datasource.Da NewDelayProfileDataSource, NewDelayProfilesDataSource, NewLanguageDataSource, + NewLanguagesDataSource, NewQualityProfileDataSource, NewQualityProfilesDataSource,