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.
feat: add custom format condition size data source
- Loading branch information
Showing
5 changed files
with
209 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,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. | ||
|
||
|
14 changes: 14 additions & 0 deletions
14
examples/data-sources/radarr_custom_format_condition_size/data-source.tf
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,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
103
internal/provider/custom_format_condition_size_data_source.go
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,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
43
internal/provider/custom_format_condition_size_data_source_test.go
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,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] | ||
}` |
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