Skip to content

Commit

Permalink
feat: improve diagnostics part 2
Browse files Browse the repository at this point in the history
  • Loading branch information
Fuochi committed Jun 23, 2023
1 parent 32c87b8 commit 0e97afe
Show file tree
Hide file tree
Showing 47 changed files with 703 additions and 481 deletions.
2 changes: 1 addition & 1 deletion docs/resources/notification.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ resource "radarr_notification" "example" {
- `on_movie_file_delete_for_upgrade` (Boolean) On movie file delete for upgrade flag.
- `on_rename` (Boolean) On rename flag.
- `on_upgrade` (Boolean) On upgrade flag.
- `password` (String) password.
- `password` (String, Sensitive) password.
- `path` (String) Path.
- `port` (Number) Port.
- `priority` (Number) Priority.
Expand Down
2 changes: 1 addition & 1 deletion docs/resources/notification_webhook.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ resource "radarr_notification_webhook" "example" {
- `on_movie_file_delete_for_upgrade` (Boolean) On movie file delete for upgrade flag.
- `on_rename` (Boolean) On rename flag.
- `on_upgrade` (Boolean) On upgrade flag.
- `password` (String) password.
- `password` (String, Sensitive) password.
- `tags` (Set of Number) List of associated tags.
- `username` (String) Username.

Expand Down
7 changes: 0 additions & 7 deletions internal/helpers/errors.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package helpers

import (
"errors"
"fmt"

"github.com/devopsarr/radarr-go/radarr"
Expand All @@ -22,12 +21,6 @@ const (
UnexpectedDataSourceConfigureType = "Unexpected DataSource Configure Type"
)

var ErrDataNotFound = errors.New("data source not found")

func ErrDataNotFoundError(kind, field, search string) error {
return fmt.Errorf("%w: no %s with %s '%s'", ErrDataNotFound, kind, field, search)
}

func ParseNotFoundError(kind, field, search string) string {
return fmt.Sprintf("Unable to find %s, got error: data source not found: no %s with %s '%s'", kind, kind, field, search)
}
Expand Down
44 changes: 26 additions & 18 deletions internal/helpers/errors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,6 @@ import (
"github.com/stretchr/testify/assert"
)

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

tests := map[string]struct {
kind, field, search, expected string
}{
"tag": {"radarr_tag", "label", "test", "data source not found: no radarr_tag with label 'test'"},
}
for name, test := range tests {
test := test

t.Run(name, func(t *testing.T) {
t.Parallel()
assert.Equal(t, test.expected, ErrDataNotFoundError(test.kind, test.field, test.search).Error())
})
}
}

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

Expand Down Expand Up @@ -57,3 +39,29 @@ func TestParseClientError(t *testing.T) {
})
}
}

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

tests := map[string]struct {
kind string
field string
search string
expected string
}{
"generic": {
kind: "radarr_tag",
field: "label",
search: "test",
expected: "Unable to find radarr_tag, got error: data source not found: no radarr_tag with label 'test'",
},
}
for name, test := range tests {
test := test

t.Run(name, func(t *testing.T) {
t.Parallel()
assert.Equal(t, test.expected, ParseNotFoundError(test.kind, test.field, test.search))
})
}
}
15 changes: 14 additions & 1 deletion internal/provider/custom_format_condition_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"github.com/devopsarr/radarr-go/radarr"
"github.com/devopsarr/terraform-provider-radarr/internal/helpers"
"github.com/hashicorp/terraform-plugin-framework/attr"
"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"github.com/hashicorp/terraform-plugin-framework/path"
Expand Down Expand Up @@ -43,6 +44,19 @@ type CustomFormatCondition struct {
Required types.Bool `tfsdk:"required"`
}

func (c CustomFormatCondition) getType() attr.Type {
return types.ObjectType{}.WithAttributeTypes(
map[string]attr.Type{
"negate": types.BoolType,
"required": types.BoolType,
"min": types.Int64Type,
"max": types.Int64Type,
"name": types.StringType,
"value": types.StringType,
"implementation": types.StringType,
})
}

// CustomFormatValue describes the custom format value data model.
type CustomFormatConditionValue struct {
Name types.String `tfsdk:"name"`
Expand Down Expand Up @@ -142,7 +156,6 @@ func (c *CustomFormatCondition) write(ctx context.Context, spec *radarr.CustomFo
func (c *CustomFormatCondition) read(ctx context.Context) *radarr.CustomFormatSpecificationSchema {
spec := radarr.NewCustomFormatSpecificationSchema()
spec.SetName(c.Name.ValueString())

spec.SetImplementation(c.Implementation.ValueString())
spec.SetNegate(c.Negate.ValueBool())
spec.SetRequired(c.Required.ValueBool())
Expand Down
19 changes: 7 additions & 12 deletions internal/provider/custom_format_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ package provider

import (
"context"
"fmt"

"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/diag"
"github.com/hashicorp/terraform-plugin-log/tflog"
)

Expand Down Expand Up @@ -109,24 +109,19 @@ func (d *CustomFormatDataSource) Read(ctx context.Context, req datasource.ReadRe
return
}

customFormat, err := findCustomFormat(data.Name.ValueString(), response)
if err != nil {
resp.Diagnostics.AddError(helpers.DataSourceError, fmt.Sprintf("Unable to find %s, got error: %s", customFormatDataSourceName, err))

return
}

data.find(ctx, data.Name.ValueString(), response, &resp.Diagnostics)
tflog.Trace(ctx, "read "+customFormatDataSourceName)
data.write(ctx, customFormat, &resp.Diagnostics)
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
}

func findCustomFormat(name string, customFormats []*radarr.CustomFormatResource) (*radarr.CustomFormatResource, error) {
func (c *CustomFormat) find(ctx context.Context, name string, customFormats []*radarr.CustomFormatResource, diags *diag.Diagnostics) {
for _, i := range customFormats {
if i.GetName() == name {
return i, nil
c.write(ctx, i, diags)

return
}
}

return nil, helpers.ErrDataNotFoundError(customFormatDataSourceName, "name", name)
diags.AddError(helpers.DataSourceError, helpers.ParseNotFoundError(customFormatDataSourceName, "name", name))
}
23 changes: 16 additions & 7 deletions internal/provider/custom_format_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ import (

"github.com/devopsarr/radarr-go/radarr"
"github.com/devopsarr/terraform-provider-radarr/internal/helpers"
"github.com/hashicorp/terraform-plugin-framework/attr"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/int64planmodifier"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-log/tflog"
)
Expand Down Expand Up @@ -42,6 +42,16 @@ type CustomFormat struct {
IncludeCustomFormatWhenRenaming types.Bool `tfsdk:"include_custom_format_when_renaming"`
}

func (c CustomFormat) getType() attr.Type {
return types.ObjectType{}.WithAttributeTypes(
map[string]attr.Type{
"include_custom_format_when_renaming": types.BoolType,
"id": types.Int64Type,
"name": types.StringType,
"specifications": types.SetType{}.WithElementType(CustomFormatCondition{}.getType()),
})
}

func (r *CustomFormatResource) Metadata(ctx context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_" + customFormatResourceName
}
Expand Down Expand Up @@ -240,17 +250,16 @@ func (r *CustomFormatResource) ImportState(ctx context.Context, req resource.Imp
func (c *CustomFormat) write(ctx context.Context, customFormat *radarr.CustomFormatResource, diags *diag.Diagnostics) {
var tempDiag diag.Diagnostics

c.ID = types.Int64Value(int64(customFormat.GetId()))
c.Name = types.StringValue(customFormat.GetName())
c.IncludeCustomFormatWhenRenaming = types.BoolValue(customFormat.GetIncludeCustomFormatWhenRenaming())
c.Specifications = types.SetValueMust(CustomFormatResource{}.getSpecificationSchema().Type(), nil)

specs := make([]CustomFormatCondition, len(customFormat.Specifications))
for n, s := range customFormat.Specifications {
specs[n].write(ctx, s)
}

tfsdk.ValueFrom(ctx, specs, c.Specifications.Type(ctx), &c.Specifications)
c.ID = types.Int64Value(int64(customFormat.GetId()))
c.Name = types.StringValue(customFormat.GetName())
c.IncludeCustomFormatWhenRenaming = types.BoolValue(customFormat.GetIncludeCustomFormatWhenRenaming())
c.Specifications, tempDiag = types.SetValueFrom(ctx, CustomFormatCondition{}.getType(), specs)
diags.Append(tempDiag...)
}

func (c *CustomFormat) read(ctx context.Context, diags *diag.Diagnostics) *radarr.CustomFormatResource {
Expand Down
19 changes: 5 additions & 14 deletions internal/provider/custom_formats_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"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"
)
Expand Down Expand Up @@ -114,13 +113,6 @@ func (d *CustomFormatsDataSource) Configure(ctx context.Context, req datasource.
}

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

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

if resp.Diagnostics.HasError() {
return
}
// Get custom formatss current value
response, _, err := d.client.CustomFormatApi.ListCustomFormat(ctx).Execute()
if err != nil {
Expand All @@ -131,13 +123,12 @@ func (d *CustomFormatsDataSource) Read(ctx context.Context, req datasource.ReadR

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

tfsdk.ValueFrom(ctx, profiles, data.CustomFormats.Type(ctx), &data.CustomFormats)
// 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)...)
formatList, diags := types.SetValueFrom(ctx, CustomFormat{}.getType(), formats)
resp.Diagnostics.Append(diags...)
resp.Diagnostics.Append(resp.State.Set(ctx, CustomFormats{CustomFormats: formatList, ID: types.StringValue(strconv.Itoa(len(response)))})...)
}
29 changes: 13 additions & 16 deletions internal/provider/delay_profile_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ 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/diag"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-log/tflog"
)
Expand Down Expand Up @@ -84,9 +84,9 @@ func (d *DelayProfileDataSource) Configure(ctx context.Context, req datasource.C
}

func (d *DelayProfileDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
var delayProfile *DelayProfile
var data *DelayProfile

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

if resp.Diagnostics.HasError() {
return
Expand All @@ -99,24 +99,21 @@ func (d *DelayProfileDataSource) Read(ctx context.Context, req datasource.ReadRe
return
}

profile, err := findDelayProfile(delayProfile.ID.ValueInt64(), response)
if err != nil {
resp.Diagnostics.AddError(helpers.DataSourceError, fmt.Sprintf("Unable to find %s, got error: %s", delayProfileDataSourceName, err))

return
}
data.find(ctx, data.ID.ValueInt64(), response, &resp.Diagnostics)

tflog.Trace(ctx, "read "+delayProfileDataSourceName)
delayProfile.write(ctx, profile, &resp.Diagnostics)
resp.Diagnostics.Append(resp.State.Set(ctx, &delayProfile)...)
// Map response body to resource schema attribute
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
}

func findDelayProfile(id int64, profiles []*radarr.DelayProfileResource) (*radarr.DelayProfileResource, error) {
for _, p := range profiles {
if int64(p.GetId()) == id {
return p, nil
func (p *DelayProfile) find(ctx context.Context, id int64, profiles []*radarr.DelayProfileResource, diags *diag.Diagnostics) {
for _, profile := range profiles {
if int64(profile.GetId()) == id {
p.write(ctx, profile, diags)

return
}
}

return nil, helpers.ErrDataNotFoundError(delayProfileDataSourceName, "id", strconv.Itoa(int(id)))
diags.AddError(helpers.DataSourceError, helpers.ParseNotFoundError(delayProfileDataSourceName, "id", strconv.Itoa(int(id))))
}
18 changes: 17 additions & 1 deletion internal/provider/delay_profile_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/devopsarr/radarr-go/radarr"
"github.com/devopsarr/terraform-provider-radarr/internal/helpers"
"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
"github.com/hashicorp/terraform-plugin-framework/attr"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/resource"
Expand Down Expand Up @@ -48,6 +49,21 @@ type DelayProfile struct {
BypassIfHighestQuality types.Bool `tfsdk:"bypass_if_highest_quality"`
}

func (p DelayProfile) getType() attr.Type {
return types.ObjectType{}.WithAttributeTypes(
map[string]attr.Type{
"tags": types.SetType{}.WithElementType(types.Int64Type),
"preferred_protocol": types.StringType,
"usenet_delay": types.Int64Type,
"torrent_delay": types.Int64Type,
"id": types.Int64Type,
"order": types.Int64Type,
"enable_usenet": types.BoolType,
"enable_torrent": types.BoolType,
"bypass_if_highest_quality": types.BoolType,
})
}

func (r *DelayProfileResource) Metadata(ctx context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_" + delayProfileResourceName
}
Expand Down Expand Up @@ -233,7 +249,7 @@ func (r *DelayProfileResource) ImportState(ctx context.Context, req resource.Imp
tflog.Trace(ctx, "imported "+delayProfileResourceName+": "+req.ID)
}

func (p *DelayProfile) write(ctx context.Context, profile *radarrDelayProfileResource, diags *diag.Diagnostics) {
func (p *DelayProfile) write(ctx context.Context, profile *radarr.DelayProfileResource, diags *diag.Diagnostics) {
var tempDiag diag.Diagnostics

p.ID = types.Int64Value(int64(profile.GetId()))
Expand Down
Loading

0 comments on commit 0e97afe

Please sign in to comment.