generated from hashicorp/terraform-provider-scaffolding-framework
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
190 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
data "radarr_languages" "example" { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)...) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" { | ||
} | ||
` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters