Skip to content

Commit

Permalink
feat: add quality profiles datasource
Browse files Browse the repository at this point in the history
  • Loading branch information
Fuochi committed Nov 21, 2022
1 parent 8a7e8bb commit c176978
Show file tree
Hide file tree
Showing 5 changed files with 351 additions and 0 deletions.
84 changes: 84 additions & 0 deletions docs/data-sources/quality_profiles.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "radarr_quality_profiles Data Source - terraform-provider-radarr"
subcategory: "Profiles"
description: |-
List all available Quality Profiles ../resources/quality_profile.
---

# radarr_quality_profiles (Data Source)

<!-- subcategory:Profiles -->List all available [Quality Profiles](../resources/quality_profile).

## Example Usage

```terraform
data "radarr_quality_profiles" "example" {
}
```

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

### Read-Only

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

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

Read-Only:

- `cutoff` (Number) Quality ID to which cutoff.
- `cutoff_format_score` (Number) Cutoff format score.
- `format_items` (Attributes Set) Format items. (see [below for nested schema](#nestedatt--quality_profiles--format_items))
- `id` (Number) Quality Profile ID.
- `language` (Attributes) Language. (see [below for nested schema](#nestedatt--quality_profiles--language))
- `min_format_score` (Number) Min format score.
- `name` (String) Quality Profile Name.
- `quality_groups` (Attributes Set) Quality groups. (see [below for nested schema](#nestedatt--quality_profiles--quality_groups))
- `upgrade_allowed` (Boolean) Upgrade allowed flag.

<a id="nestedatt--quality_profiles--format_items"></a>
### Nested Schema for `quality_profiles.format_items`

Read-Only:

- `format` (Number) Format.
- `name` (String) Name.
- `score` (Number) Score.


<a id="nestedatt--quality_profiles--language"></a>
### Nested Schema for `quality_profiles.language`

Read-Only:

- `id` (Number) ID.
- `name` (String) Name.


<a id="nestedatt--quality_profiles--quality_groups"></a>
### Nested Schema for `quality_profiles.quality_groups`

Required:

- `qualities` (Attributes Set) Qualities in group. (see [below for nested schema](#nestedatt--quality_profiles--quality_groups--qualities))

Read-Only:

- `id` (Number) Quality group ID.
- `name` (String) Quality group name.

<a id="nestedatt--quality_profiles--quality_groups--qualities"></a>
### Nested Schema for `quality_profiles.quality_groups.qualities`

Read-Only:

- `id` (Number) Quality ID.
- `name` (String) Quality name.
- `resolution` (Number) Resolution.
- `source` (String) Source.


2 changes: 2 additions & 0 deletions examples/data-sources/radarr_quality_profiles/data-source.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
data "radarr_quality_profiles" "example" {
}
1 change: 1 addition & 0 deletions internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ func (p *RadarrProvider) DataSources(ctx context.Context) []func() datasource.Da
NewNamingDataSource,
NewNotificationDataSource,
NewNotificationsDataSource,
NewQualityProfilesDataSource,
NewRemotePathMappingDataSource,
NewRemotePathMappingsDataSource,
NewRestrictionDataSource,
Expand Down
222 changes: 222 additions & 0 deletions internal/provider/quality_profiles_data_source.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,222 @@
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 qualityProfilesDataSourceName = "quality_profiles"

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

func NewQualityProfilesDataSource() datasource.DataSource {
return &QualityProfilesDataSource{}
}

// QualityProfilesDataSource defines the qyality profiles implementation.
type QualityProfilesDataSource struct {
client *radarr.Radarr
}

// QualityProfiles describes the qyality profiles data model.
type QualityProfiles struct {
QualityProfiles types.Set `tfsdk:"quality_profiles"`
ID types.String `tfsdk:"id"`
}

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

func (d *QualityProfilesDataSource) GetSchema(ctx context.Context) (tfsdk.Schema, diag.Diagnostics) {
return tfsdk.Schema{
// This description is used by the documentation generator and the quality server.
MarkdownDescription: "<!-- subcategory:Profiles -->List all available [Quality Profiles](../resources/quality_profile).",
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,
},
"quality_profiles": {
MarkdownDescription: "Quality Profile list.",
Computed: true,
Attributes: tfsdk.SetNestedAttributes(map[string]tfsdk.Attribute{
"id": {
MarkdownDescription: "Quality Profile ID.",
Computed: true,
Type: types.Int64Type,
},
"name": {
MarkdownDescription: "Quality Profile Name.",
Computed: true,
Type: types.StringType,
},
"upgrade_allowed": {
MarkdownDescription: "Upgrade allowed flag.",
Computed: true,
Type: types.BoolType,
},
"cutoff": {
MarkdownDescription: "Quality ID to which cutoff.",
Computed: true,
Type: types.Int64Type,
},
"cutoff_format_score": {
MarkdownDescription: "Cutoff format score.",
Computed: true,
Type: types.Int64Type,
},
"min_format_score": {
MarkdownDescription: "Min format score.",
Computed: true,
Type: types.Int64Type,
},
"language": {
MarkdownDescription: "Language.",
Computed: true,
Attributes: tfsdk.SingleNestedAttributes(map[string]tfsdk.Attribute{
"id": {
MarkdownDescription: "ID.",
Computed: true,
Type: types.Int64Type,
},
"name": {
MarkdownDescription: "Name.",
Computed: true,
Type: types.StringType,
},
}),
},
"quality_groups": {
MarkdownDescription: "Quality groups.",
Computed: true,
Attributes: tfsdk.SetNestedAttributes(map[string]tfsdk.Attribute{
"id": {
MarkdownDescription: "Quality group ID.",
Computed: true,
Type: types.Int64Type,
},
"name": {
MarkdownDescription: "Quality group name.",
Computed: true,
Type: types.StringType,
},
"qualities": {
MarkdownDescription: "Qualities in group.",
Required: true,
Attributes: tfsdk.SetNestedAttributes(map[string]tfsdk.Attribute{
"id": {
MarkdownDescription: "Quality ID.",
Computed: true,
Type: types.Int64Type,
},
"resolution": {
MarkdownDescription: "Resolution.",
Computed: true,
Type: types.Int64Type,
},
"name": {
MarkdownDescription: "Quality name.",
Computed: true,
Type: types.StringType,
},
"source": {
MarkdownDescription: "Source.",
Computed: true,
Type: types.StringType,
},
}),
},
}),
},
"format_items": {
MarkdownDescription: "Format items.",
Computed: true,
Attributes: tfsdk.SetNestedAttributes(map[string]tfsdk.Attribute{
"format": {
MarkdownDescription: "Format.",
Computed: true,
Type: types.Int64Type,
},
"score": {
MarkdownDescription: "Score.",
Computed: true,
Type: types.Int64Type,
},
"name": {
MarkdownDescription: "Name.",
Computed: true,
Type: types.StringType,
},
}),
},
}),
},
},
}, nil
}

func (d *QualityProfilesDataSource) 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 *QualityProfilesDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
var data *QualityProfiles

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

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

return
}

tflog.Trace(ctx, "read "+qualityProfilesDataSourceName)
// Map response body to resource schema attribute
profiles := *writeQualitiyprofiles(ctx, response)
tfsdk.ValueFrom(ctx, profiles, data.QualityProfiles.Type(context.Background()), &data.QualityProfiles)

// 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)...)
}

func writeQualitiyprofiles(ctx context.Context, qualities []*radarr.QualityProfile) *[]QualityProfile {
output := make([]QualityProfile, len(qualities))
for i, p := range qualities {
output[i].write(ctx, p)
}

return &output
}
42 changes: 42 additions & 0 deletions internal/provider/quality_profiles_data_source_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package provider

import (
"os"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"golift.io/starr"
"golift.io/starr/radarr"
)

func TestAccQualityProfilesDataSource(t *testing.T) {
t.Parallel()

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
Steps: []resource.TestStep{
// Read testing
{
PreConfig: qualityprofilesDSInit,
Config: testAccQualityProfilesDataSourceConfig,
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckTypeSetElemNestedAttrs("data.radarr_quality_profiles.test", "quality_profiles.*", map[string]string{"name": "Any"}),
),
},
},
})
}

const testAccQualityProfilesDataSourceConfig = `
data "radarr_quality_profiles" "test" {
}
`

func qualityprofilesDSInit() {
// keep only first two profiles to avoid longer tests
client := *radarr.New(starr.New(os.Getenv("RADARR_API_KEY"), os.Getenv("RADARR_URL"), 0))
for i := 3; i < 7; i++ {
_ = client.DeleteQualityProfile(int64(i))
}
}

0 comments on commit c176978

Please sign in to comment.