Skip to content

Commit

Permalink
feat: add indexers datasource
Browse files Browse the repository at this point in the history
  • Loading branch information
Fuochi committed Nov 15, 2022
1 parent 3d260df commit 998f2dd
Show file tree
Hide file tree
Showing 5 changed files with 383 additions and 0 deletions.
66 changes: 66 additions & 0 deletions docs/data-sources/indexers.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "radarr_indexers Data Source - terraform-provider-radarr"
subcategory: "Indexers"
description: |-
List all available Indexers ../resources/indexer.
---

# radarr_indexers (Data Source)

<!-- subcategory:Indexers -->List all available [Indexers](../resources/indexer).

## Example Usage

```terraform
data "radarr_indexers" "example" {
}
```

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

### Read-Only

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

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

Read-Only:

- `additional_parameters` (String) Additional parameters.
- `allow_zero_size` (Boolean) Allow zero size files.
- `api_key` (String) API key.
- `api_path` (String) API path.
- `api_user` (String) API User.
- `base_url` (String) Base URL.
- `captcha_token` (String) Captcha token.
- `categories` (Set of Number) Series list.
- `codecs` (Set of Number) Codecs.
- `config_contract` (String) Indexer configuration template.
- `cookie` (String) Cookie.
- `delay` (Number) Delay before grabbing.
- `download_client_id` (Number) Download client ID.
- `enable_automatic_search` (Boolean) Enable automatic search flag.
- `enable_interactive_search` (Boolean) Enable interactive search flag.
- `enable_rss` (Boolean) Enable RSS flag.
- `id` (Number) Indexer ID.
- `implementation` (String) Indexer implementation name.
- `mediums` (Set of Number) Mediumd.
- `minimum_seeders` (Number) Minimum seeders.
- `multi_languages` (Set of Number) Language list.
- `name` (String) Indexer name.
- `passkey` (String) Passkey.
- `priority` (Number) Priority.
- `protocol` (String) Protocol. Valid values are 'usenet' and 'torrent'.
- `ranked_only` (Boolean) Allow ranked only.
- `remove_year` (Boolean) Remove year.
- `required_flags` (Set of Number) Computed flags.
- `seed_ratio` (Number) Seed ratio.
- `seed_time` (Number) Seed time.
- `tags` (Set of Number) List of associated tags.
- `username` (String) Username.


2 changes: 2 additions & 0 deletions examples/data-sources/radarr_indexers/data-source.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
data "radarr_indexers" "example" {
}
280 changes: 280 additions & 0 deletions internal/provider/indexers_data_source.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,280 @@
package provider

import (
"context"
"fmt"
"strconv"

"github.com/devopsarr/terraform-provider-sonarr/tools"
"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-log/tflog"
"golift.io/starr/radarr"
)

const indexersDataSourceName = "indexers"

// Ensure provider defined types fully satisfy framework interfaces.
var _ datasource.DataSource = &IndexersDataSource{}

func NewIndexersDataSource() datasource.DataSource {
return &IndexersDataSource{}
}

// IndexersDataSource defines the indexers implementation.
type IndexersDataSource struct {
client *radarr.Radarr
}

// Indexers describes the indexers data model.
type Indexers struct {
Indexers types.Set `tfsdk:"indexers"`
ID types.String `tfsdk:"id"`
}

func (d *IndexersDataSource) Metadata(ctx context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_" + indexersDataSourceName
}

func (d *IndexersDataSource) GetSchema(ctx context.Context) (tfsdk.Schema, diag.Diagnostics) {
return tfsdk.Schema{
// This description is used by the documentation generator and the delay server.
MarkdownDescription: "<!-- subcategory:Indexers -->List all available [Indexers](../resources/indexer).",
Attributes: map[string]tfsdk.Attribute{
// TODO: remove ID once framework support tests without ID https://www.terraform.io/plugin/framework/acctests#implement-id-attribute
"id": {
Computed: true,
Type: types.StringType,
},
"indexers": {
MarkdownDescription: "Indexer list.",
Computed: true,
Attributes: tfsdk.SetNestedAttributes(map[string]tfsdk.Attribute{
"enable_automatic_search": {
MarkdownDescription: "Enable automatic search flag.",
Computed: true,
Type: types.BoolType,
},
"enable_interactive_search": {
MarkdownDescription: "Enable interactive search flag.",
Computed: true,
Type: types.BoolType,
},
"enable_rss": {
MarkdownDescription: "Enable RSS flag.",
Computed: true,
Type: types.BoolType,
},
"priority": {
MarkdownDescription: "Priority.",
Computed: true,
Type: types.Int64Type,
},
"download_client_id": {
MarkdownDescription: "Download client ID.",
Computed: true,
Type: types.Int64Type,
},
"config_contract": {
MarkdownDescription: "Indexer configuration template.",
Computed: true,
Type: types.StringType,
},
"implementation": {
MarkdownDescription: "Indexer implementation name.",
Computed: true,
Type: types.StringType,
},
"name": {
MarkdownDescription: "Indexer name.",
Computed: true,
Type: types.StringType,
},
"protocol": {
MarkdownDescription: "Protocol. Valid values are 'usenet' and 'torrent'.",
Computed: true,
Type: types.StringType,
},
"tags": {
MarkdownDescription: "List of associated tags.",
Computed: true,
Type: types.SetType{
ElemType: types.Int64Type,
},
},
"id": {
MarkdownDescription: "Indexer ID.",
Computed: true,
Type: types.Int64Type,
},
// Field values
"allow_zero_size": {
MarkdownDescription: "Allow zero size files.",
Computed: true,
Type: types.BoolType,
},
"remove_year": {
MarkdownDescription: "Remove year.",
Computed: true,
Type: types.BoolType,
},
"ranked_only": {
MarkdownDescription: "Allow ranked only.",
Computed: true,
Type: types.BoolType,
},
"delay": {
MarkdownDescription: "Delay before grabbing.",
Computed: true,
Type: types.Int64Type,
},
"minimum_seeders": {
MarkdownDescription: "Minimum seeders.",
Computed: true,
Type: types.Int64Type,
},
"seed_time": {
MarkdownDescription: "Seed time.",
Computed: true,
Type: types.Int64Type,
},
"seed_ratio": {
MarkdownDescription: "Seed ratio.",
Computed: true,
Type: types.Float64Type,
},
"additional_parameters": {
MarkdownDescription: "Additional parameters.",
Computed: true,
Type: types.StringType,
},
"api_key": {
MarkdownDescription: "API key.",
Computed: true,
Type: types.StringType,
},
"api_user": {
MarkdownDescription: "API User.",
Computed: true,
Type: types.StringType,
},
"api_path": {
MarkdownDescription: "API path.",
Computed: true,
Type: types.StringType,
},
"base_url": {
MarkdownDescription: "Base URL.",
Computed: true,
Type: types.StringType,
},
"captcha_token": {
MarkdownDescription: "Captcha token.",
Computed: true,
Type: types.StringType,
},
"cookie": {
MarkdownDescription: "Cookie.",
Computed: true,
Type: types.StringType,
},
"passkey": {
MarkdownDescription: "Passkey.",
Computed: true,
Type: types.StringType,
},
"username": {
MarkdownDescription: "Username.",
Computed: true,
Type: types.StringType,
},
"categories": {
MarkdownDescription: "Series list.",
Computed: true,
Type: types.SetType{
ElemType: types.Int64Type,
},
},
"multi_languages": {
MarkdownDescription: "Language list.",
Computed: true,
Type: types.SetType{
ElemType: types.Int64Type,
},
},
"required_flags": {
MarkdownDescription: "Computed flags.",
Computed: true,
Type: types.SetType{
ElemType: types.Int64Type,
},
},
"codecs": {
MarkdownDescription: "Codecs.",
Computed: true,
Type: types.SetType{
ElemType: types.Int64Type,
},
},
"mediums": {
MarkdownDescription: "Mediumd.",
Computed: true,
Type: types.SetType{
ElemType: types.Int64Type,
},
},
}),
},
},
}, nil
}

func (d *IndexersDataSource) 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.Radarr)
if !ok {
resp.Diagnostics.AddError(
tools.UnexpectedDataSourceConfigureType,
fmt.Sprintf("Expected *radarr.Radarr, got: %T. Please report this issue to the provider developers.", req.ProviderData),
)

return
}

d.client = client
}

func (d *IndexersDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
var data *Indexers

resp.Diagnostics.Append(req.Config.Get(ctx, &data)...)

if resp.Diagnostics.HasError() {
return
}
// Get indexers current value
response, err := d.client.GetIndexersContext(ctx)
if err != nil {
resp.Diagnostics.AddError(tools.ClientError, fmt.Sprintf("Unable to read %s, got error: %s", indexersDataSourceName, err))

return
}

tflog.Trace(ctx, "read "+indexersDataSourceName)
// Map response body to resource schema attribute
profiles := make([]Indexer, len(response))
for i, p := range response {
profiles[i].write(ctx, p)
}

tfsdk.ValueFrom(ctx, profiles, data.Indexers.Type(context.Background()), &data.Indexers)
// 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)...)
}
34 changes: 34 additions & 0 deletions internal/provider/indexers_data_source_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package provider

import (
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
)

func TestAccIndexersDataSource(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
{
Config: testAccIndexerResourceConfig("datasourceTest", "25"),
},
// Read testing
{
Config: testAccIndexersDataSourceConfig,
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckTypeSetElemNestedAttrs("data.radarr_indexers.test", "indexers.*", map[string]string{"protocol": "usenet"}),
),
},
},
})
}

const testAccIndexersDataSourceConfig = `
data "radarr_indexers" "test" {
}
`
1 change: 1 addition & 0 deletions internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ func (p *RadarrProvider) DataSources(ctx context.Context) []func() datasource.Da
NewDelayProfileDataSource,
NewDelayProfilesDataSource,
NewIndexerConfigDataSource,
NewIndexersDataSource,
NewMediaManagementDataSource,
NewNamingDataSource,
NewNotificationDataSource,
Expand Down

0 comments on commit 998f2dd

Please sign in to comment.