Skip to content

Commit

Permalink
feat: add custom format condition size data source
Browse files Browse the repository at this point in the history
  • Loading branch information
Fuochi committed Jan 31, 2023
1 parent d15dfad commit 7672dfd
Show file tree
Hide file tree
Showing 5 changed files with 209 additions and 0 deletions.
48 changes: 48 additions & 0 deletions docs/data-sources/custom_format_condition_size.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "radarr_custom_format_condition_size Data Source - terraform-provider-radarr"
subcategory: "Profiles"
description: |-
Custom format condition size data source.
---

# radarr_custom_format_condition_size (Data Source)

<!-- subcategory:Profiles --> Custom format condition size data source.

## Example Usage

```terraform
data "radarr_custom_format_condition_size" "example" {
name = "Example"
negate = false
required = false
min = 5
max = 50
}
resource "radarr_custom_format" "example" {
include_custom_format_when_renaming = false
name = "Example"
specifications = [data.radarr_custom_format_condition_size.example]
}
```

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

### Required

- `max` (Number) Max size in GB.
- `min` (Number) Min size in GB.
- `name` (String) Specification name.
- `negate` (Boolean) Negate flag.
- `required` (Boolean) Computed flag.

### Read-Only

- `id` (Number) Custom format condition size ID.
- `implementation` (String) Implementation.


Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
data "radarr_custom_format_condition_size" "example" {
name = "Example"
negate = false
required = false
min = 5
max = 50
}

resource "radarr_custom_format" "example" {
include_custom_format_when_renaming = false
name = "Example"

specifications = [data.radarr_custom_format_condition_size.example]
}
103 changes: 103 additions & 0 deletions internal/provider/custom_format_condition_size_data_source.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
package provider

import (
"context"

"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/path"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-log/tflog"
"github.com/mitchellh/hashstructure/v2"
)

const (
customFormatConditionSizeDataSourceName = "custom_format_condition_size"
customFormatConditionSizeImplementation = "SizeSpecification"
)

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

func NewCustomFormatConditionSizeDataSource() datasource.DataSource {
return &CustomFormatConditionSizeDataSource{}
}

// CustomFormatConditionSizeDataSource defines the custom_format_condition_size implementation.
type CustomFormatConditionSizeDataSource struct {
client *radarr.APIClient
}
type CustomFormatConditionSize struct {
Name types.String `tfsdk:"name"`
Min types.Int64 `tfsdk:"min"`
Max types.Int64 `tfsdk:"max"`
Negate types.Bool `tfsdk:"negate"`
Required types.Bool `tfsdk:"required"`
}

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

func (d *CustomFormatConditionSizeDataSource) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) {
resp.Schema = schema.Schema{
// This description is used by the documentation generator and the delay server.
MarkdownDescription: "<!-- subcategory:Profiles --> Custom format condition size data source.",
Attributes: map[string]schema.Attribute{
"negate": schema.BoolAttribute{
MarkdownDescription: "Negate flag.",
Required: true,
},
"required": schema.BoolAttribute{
MarkdownDescription: "Computed flag.",
Required: true,
},
"implementation": schema.StringAttribute{
MarkdownDescription: "Implementation.",
Computed: true,
},
"name": schema.StringAttribute{
MarkdownDescription: "Specification name.",
Required: true,
},
// TODO: remove ID once framework support tests without ID https://www.terraform.io/plugin/framework/acctests#implement-id-attribute
"id": schema.Int64Attribute{
MarkdownDescription: "Custom format condition size ID.",
Computed: true,
},
// Field values
"min": schema.Int64Attribute{
MarkdownDescription: "Min size in GB.",
Required: true,
},
"max": schema.Int64Attribute{
MarkdownDescription: "Max size in GB.",
Required: true,
},
},
}
}

func (d *CustomFormatConditionSizeDataSource) Configure(ctx context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) {
if client := helpers.DataSourceConfigure(ctx, req, resp); client != nil {
d.client = client
}
}

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

hash, err := hashstructure.Hash(&data, hashstructure.FormatV2, nil)
if err != nil {
resp.Diagnostics.AddError(helpers.DataSourceError, helpers.ParseClientError(helpers.Create, customFormatConditionSizeDataSourceName, err))

return
}

tflog.Trace(ctx, "read "+customFormatConditionSizeDataSourceName)
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("implementation"), customFormatConditionSizeImplementation)...)
// TODO: remove ID once framework support tests without ID https://www.terraform.io/plugin/framework/acctests#implement-id-attribute
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("id"), int64(hash))...)
}
43 changes: 43 additions & 0 deletions internal/provider/custom_format_condition_size_data_source_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package provider

import (
"testing"

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

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

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
Steps: []resource.TestStep{
// Read testing
{
Config: testAccCustomFormatConditionSizeDataSourceConfig,
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttrSet("data.radarr_custom_format_condition_size.test", "id"),
resource.TestCheckResourceAttr("data.radarr_custom_format_condition_size.test", "name", "Test"),
resource.TestCheckResourceAttr("radarr_custom_format.test", "specifications.0.min", "5"),
resource.TestCheckResourceAttr("radarr_custom_format.test", "specifications.0.max", "50")),
},
},
})
}

const testAccCustomFormatConditionSizeDataSourceConfig = `
data "radarr_custom_format_condition_size" "test" {
name = "Test"
negate = false
required = false
min = 5
max = 50
}
resource "radarr_custom_format" "test" {
include_custom_format_when_renaming = false
name = "TestWithDSSize"
specifications = [data.radarr_custom_format_condition_size.test]
}`
1 change: 1 addition & 0 deletions internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,7 @@ func (p *RadarrProvider) DataSources(ctx context.Context) []func() datasource.Da
NewCustomFormatConditionEditionDataSource,
NewCustomFormatConditionLanguageDataSource,
NewCustomFormatConditionResolutionDataSource,
NewCustomFormatConditionSizeDataSource,
NewCustomFormatConditionSourceDataSource,

// System Status
Expand Down

0 comments on commit 7672dfd

Please sign in to comment.