Skip to content

Commit

Permalink
feat: add languages data source
Browse files Browse the repository at this point in the history
  • Loading branch information
Fuochi committed Jan 19, 2023
1 parent a9b75d6 commit 94da3d4
Show file tree
Hide file tree
Showing 5 changed files with 190 additions and 0 deletions.
37 changes: 37 additions & 0 deletions docs/data-sources/languages.md
Original file line number Diff line number Diff line change
@@ -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)

<!-- subcategory:Profiles -->List all available [Languages](../data-sources/language).

## Example Usage

```terraform
data "radarr_languages" "example" {
}
```

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

### Read-Only

- `id` (String) The ID of this resource.
- `languages` (Attributes Set) Language list. (see [below for nested schema](#nestedatt--languages))

<a id="nestedatt--languages"></a>
### Nested Schema for `languages`

Read-Only:

- `id` (Number) Language ID.
- `name` (String) Language.
- `name_lower` (String) Language in lowercase.


2 changes: 2 additions & 0 deletions examples/data-sources/radarr_languages/data-source.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
data "radarr_languages" "example" {
}
120 changes: 120 additions & 0 deletions internal/provider/languages_data_source.go
Original file line number Diff line number Diff line change
@@ -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: "<!-- subcategory:Profiles -->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)...)
}
30 changes: 30 additions & 0 deletions internal/provider/languages_data_source_test.go
Original file line number Diff line number Diff line change
@@ -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" {
}
`
1 change: 1 addition & 0 deletions internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,7 @@ func (p *RadarrProvider) DataSources(ctx context.Context) []func() datasource.Da
NewDelayProfileDataSource,
NewDelayProfilesDataSource,
NewLanguageDataSource,
NewLanguagesDataSource,
NewQualityProfileDataSource,
NewQualityProfilesDataSource,

Expand Down

0 comments on commit 94da3d4

Please sign in to comment.