Skip to content

Commit

Permalink
feat: add import list tmdb list resource
Browse files Browse the repository at this point in the history
  • Loading branch information
Fuochi committed Jan 26, 2023
1 parent 0383fef commit 2393836
Show file tree
Hide file tree
Showing 10 changed files with 544 additions and 96 deletions.
62 changes: 62 additions & 0 deletions docs/resources/import_list_tmdb_list.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "radarr_import_list_tmdb_list Resource - terraform-provider-radarr"
subcategory: "Import Lists"
description: |-
Import List TMDB List resource.
For more information refer to Import List https://wiki.servarr.com/radarr/settings#import-lists and TMDB List https://wiki.servarr.com/radarr/supported#tmdblistimport.
---

# radarr_import_list_tmdb_list (Resource)

<!-- subcategory:Import Lists -->Import List TMDB List resource.
For more information refer to [Import List](https://wiki.servarr.com/radarr/settings#import-lists) and [TMDB List](https://wiki.servarr.com/radarr/supported#tmdblistimport).

## Example Usage

```terraform
resource "radarr_import_list_tmdb_list" "example" {
enabled = true
enable_auto = false
search_on_add = false
root_folder_path = "/config"
monitor = "none"
minimum_availability = "tba"
quality_profile_id = 1
name = "Example"
list_id = "11842"
}
```

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

### Required

- `list_id` (String) List ID.
- `minimum_availability` (String) Minimum availability.
- `monitor` (String) Should monitor.
- `name` (String) Import List name.
- `quality_profile_id` (Number) Quality profile ID.
- `root_folder_path` (String) Root folder path.

### Optional

- `enable_auto` (Boolean) Enable automatic add flag.
- `enabled` (Boolean) Enabled flag.
- `list_order` (Number) List order.
- `search_on_add` (Boolean) Search on add flag.
- `tags` (Set of Number) List of associated tags.

### Read-Only

- `id` (Number) Import List ID.

## Import

Import is supported using the following syntax:

```shell
# import using the API/UI ID
terraform import radarr_import_list_tmdb_list.example 1
```
2 changes: 2 additions & 0 deletions examples/resources/radarr_import_list_tmdb_list/import.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# import using the API/UI ID
terraform import radarr_import_list_tmdb_list.example 1
11 changes: 11 additions & 0 deletions examples/resources/radarr_import_list_tmdb_list/resource.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
resource "radarr_import_list_tmdb_list" "example" {
enabled = true
enable_auto = false
search_on_add = false
root_folder_path = "/config"
monitor = "none"
minimum_availability = "tba"
quality_profile_id = 1
name = "Example"
list_id = "11842"
}
90 changes: 45 additions & 45 deletions internal/provider/import_list_tmdb_company_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,29 +19,29 @@ import (
)

const (
importListTMDCompanyBResourceName = "import_list_tmdb_company"
importListTMDCompanyBImplementation = "TMDbCompanyImport"
importListTMDCompanyBConfigContract = "TMDbCompanySettings"
importListTMDCompanyBType = "advanced"
importListTMDBCompanyResourceName = "import_list_tmdb_company"
importListTMDBCompanyImplementation = "TMDbCompanyImport"
importListTMDBCompanyConfigContract = "TMDbCompanySettings"
importListTMDBCompanyType = "advanced"
)

// Ensure provider defined types fully satisfy framework interfaces.
var (
_ resource.Resource = &ImportListTMDCompanyBResource{}
_ resource.ResourceWithImportState = &ImportListTMDCompanyBResource{}
_ resource.Resource = &ImportListTMDBCompanyResource{}
_ resource.ResourceWithImportState = &ImportListTMDBCompanyResource{}
)

func NewImportListTMDCompanyBResource() resource.Resource {
return &ImportListTMDCompanyBResource{}
func NewImportListTMDBCompanyResource() resource.Resource {
return &ImportListTMDBCompanyResource{}
}

// ImportListTMDCompanyBResource defines the import list implementation.
type ImportListTMDCompanyBResource struct {
// ImportListTMDBCompanyResource defines the import list implementation.
type ImportListTMDBCompanyResource struct {
client *radarr.APIClient
}

// ImportListTMDCompanyB describes the import list data model.
type ImportListTMDCompanyB struct {
// ImportListTMDBCompany describes the import list data model.
type ImportListTMDBCompany struct {
Tags types.Set `tfsdk:"tags"`
Name types.String `tfsdk:"name"`
Monitor types.String `tfsdk:"monitor"`
Expand All @@ -56,7 +56,7 @@ type ImportListTMDCompanyB struct {
SearchOnAdd types.Bool `tfsdk:"search_on_add"`
}

func (i ImportListTMDCompanyB) toImportList() *ImportList {
func (i ImportListTMDBCompany) toImportList() *ImportList {
return &ImportList{
Tags: i.Tags,
Name: i.Name,
Expand All @@ -73,7 +73,7 @@ func (i ImportListTMDCompanyB) toImportList() *ImportList {
}
}

func (i *ImportListTMDCompanyB) fromImportList(importList *ImportList) {
func (i *ImportListTMDBCompany) fromImportList(importList *ImportList) {
i.Tags = importList.Tags
i.Name = importList.Name
i.Monitor = importList.Monitor
Expand All @@ -88,11 +88,11 @@ func (i *ImportListTMDCompanyB) fromImportList(importList *ImportList) {
i.SearchOnAdd = importList.SearchOnAdd
}

func (r *ImportListTMDCompanyBResource) Metadata(ctx context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_" + importListTMDCompanyBResourceName
func (r *ImportListTMDBCompanyResource) Metadata(ctx context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_" + importListTMDBCompanyResourceName
}

func (r *ImportListTMDCompanyBResource) Schema(ctx context.Context, req resource.SchemaRequest, resp *resource.SchemaResponse) {
func (r *ImportListTMDBCompanyResource) Schema(ctx context.Context, req resource.SchemaRequest, resp *resource.SchemaResponse) {
resp.Schema = schema.Schema{
MarkdownDescription: "<!-- subcategory:Import Lists -->Import List TMDB Company resource.\nFor more information refer to [Import List](https://wiki.servarr.com/radarr/settings#import-lists) and [TMDB Company](https://wiki.servarr.com/radarr/supported#tmdbcompanyimport).",
Attributes: map[string]schema.Attribute{
Expand Down Expand Up @@ -164,115 +164,115 @@ func (r *ImportListTMDCompanyBResource) Schema(ctx context.Context, req resource
}
}

func (r *ImportListTMDCompanyBResource) Configure(ctx context.Context, req resource.ConfigureRequest, resp *resource.ConfigureResponse) {
func (r *ImportListTMDBCompanyResource) Configure(ctx context.Context, req resource.ConfigureRequest, resp *resource.ConfigureResponse) {
if client := helpers.ResourceConfigure(ctx, req, resp); client != nil {
r.client = client
}
}

func (r *ImportListTMDCompanyBResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
func (r *ImportListTMDBCompanyResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
// Retrieve values from plan
var importList *ImportListTMDCompanyB
var importList *ImportListTMDBCompany

resp.Diagnostics.Append(req.Plan.Get(ctx, &importList)...)

if resp.Diagnostics.HasError() {
return
}

// Create new ImportListTMDCompanyB
// Create new ImportListTMDBCompany
request := importList.read(ctx)

response, _, err := r.client.ImportListApi.CreateImportList(ctx).ImportListResource(*request).Execute()
if err != nil {
resp.Diagnostics.AddError(helpers.ClientError, helpers.ParseClientError(helpers.Create, importListTMDCompanyBResourceName, err))
resp.Diagnostics.AddError(helpers.ClientError, helpers.ParseClientError(helpers.Create, importListTMDBCompanyResourceName, err))

return
}

tflog.Trace(ctx, "created "+importListTMDCompanyBResourceName+": "+strconv.Itoa(int(response.GetId())))
tflog.Trace(ctx, "created "+importListTMDBCompanyResourceName+": "+strconv.Itoa(int(response.GetId())))
// Generate resource state struct
importList.write(ctx, response)
resp.Diagnostics.Append(resp.State.Set(ctx, &importList)...)
}

func (r *ImportListTMDCompanyBResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) {
func (r *ImportListTMDBCompanyResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) {
// Get current state
var importList *ImportListTMDCompanyB
var importList *ImportListTMDBCompany

resp.Diagnostics.Append(req.State.Get(ctx, &importList)...)

if resp.Diagnostics.HasError() {
return
}

// Get ImportListTMDCompanyB current value
// Get ImportListTMDBCompany current value
response, _, err := r.client.ImportListApi.GetImportListById(ctx, int32(importList.ID.ValueInt64())).Execute()
if err != nil {
resp.Diagnostics.AddError(helpers.ClientError, helpers.ParseClientError(helpers.Read, importListTMDCompanyBResourceName, err))
resp.Diagnostics.AddError(helpers.ClientError, helpers.ParseClientError(helpers.Read, importListTMDBCompanyResourceName, err))

return
}

tflog.Trace(ctx, "read "+importListTMDCompanyBResourceName+": "+strconv.Itoa(int(response.GetId())))
tflog.Trace(ctx, "read "+importListTMDBCompanyResourceName+": "+strconv.Itoa(int(response.GetId())))
// Map response body to resource schema attribute
importList.write(ctx, response)
resp.Diagnostics.Append(resp.State.Set(ctx, &importList)...)
}

func (r *ImportListTMDCompanyBResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) {
func (r *ImportListTMDBCompanyResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) {
// Get plan values
var importList *ImportListTMDCompanyB
var importList *ImportListTMDBCompany

resp.Diagnostics.Append(req.Plan.Get(ctx, &importList)...)

if resp.Diagnostics.HasError() {
return
}

// Update ImportListTMDCompanyB
// Update ImportListTMDBCompany
request := importList.read(ctx)

response, _, err := r.client.ImportListApi.UpdateImportList(ctx, strconv.Itoa(int(request.GetId()))).ImportListResource(*request).Execute()
if err != nil {
resp.Diagnostics.AddError(helpers.ClientError, helpers.ParseClientError(helpers.Update, importListTMDCompanyBResourceName, err))
resp.Diagnostics.AddError(helpers.ClientError, helpers.ParseClientError(helpers.Update, importListTMDBCompanyResourceName, err))

return
}

tflog.Trace(ctx, "updated "+importListTMDCompanyBResourceName+": "+strconv.Itoa(int(response.GetId())))
tflog.Trace(ctx, "updated "+importListTMDBCompanyResourceName+": "+strconv.Itoa(int(response.GetId())))
// Generate resource state struct
importList.write(ctx, response)
resp.Diagnostics.Append(resp.State.Set(ctx, &importList)...)
}

func (r *ImportListTMDCompanyBResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) {
var importList *ImportListTMDCompanyB
func (r *ImportListTMDBCompanyResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) {
var importList *ImportListTMDBCompany

resp.Diagnostics.Append(req.State.Get(ctx, &importList)...)

if resp.Diagnostics.HasError() {
return
}

// Delete ImportListTMDCompanyB current value
// Delete ImportListTMDBCompany current value
_, err := r.client.ImportListApi.DeleteImportList(ctx, int32(importList.ID.ValueInt64())).Execute()
if err != nil {
resp.Diagnostics.AddError(helpers.ClientError, helpers.ParseClientError(helpers.Read, importListTMDCompanyBResourceName, err))
resp.Diagnostics.AddError(helpers.ClientError, helpers.ParseClientError(helpers.Read, importListTMDBCompanyResourceName, err))

return
}

tflog.Trace(ctx, "deleted "+importListTMDCompanyBResourceName+": "+strconv.Itoa(int(importList.ID.ValueInt64())))
tflog.Trace(ctx, "deleted "+importListTMDBCompanyResourceName+": "+strconv.Itoa(int(importList.ID.ValueInt64())))
resp.State.RemoveResource(ctx)
}

func (r *ImportListTMDCompanyBResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) {
func (r *ImportListTMDBCompanyResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) {
helpers.ImportStatePassthroughIntID(ctx, path.Root("id"), req, resp)
tflog.Trace(ctx, "imported "+importListTMDCompanyBResourceName+": "+req.ID)
tflog.Trace(ctx, "imported "+importListTMDBCompanyResourceName+": "+req.ID)
}

func (i *ImportListTMDCompanyB) write(ctx context.Context, importList *radarr.ImportListResource) {
func (i *ImportListTMDBCompany) write(ctx context.Context, importList *radarr.ImportListResource) {
genericImportList := ImportList{
Name: types.StringValue(importList.GetName()),
Monitor: types.StringValue(string(importList.GetMonitor())),
Expand All @@ -290,7 +290,7 @@ func (i *ImportListTMDCompanyB) write(ctx context.Context, importList *radarr.Im
i.fromImportList(&genericImportList)
}

func (i *ImportListTMDCompanyB) read(ctx context.Context) *radarr.ImportListResource {
func (i *ImportListTMDBCompany) read(ctx context.Context) *radarr.ImportListResource {
tags := make([]*int32, len(i.Tags.Elements()))
tfsdk.ValueAs(ctx, i.Tags, &tags)

Expand All @@ -303,9 +303,9 @@ func (i *ImportListTMDCompanyB) read(ctx context.Context) *radarr.ImportListReso
list.SetEnableAuto(i.EnableAuto.ValueBool())
list.SetEnabled(i.Enabled.ValueBool())
list.SetSearchOnAdd(i.SearchOnAdd.ValueBool())
list.SetListType(importListTMDCompanyBType)
list.SetConfigContract(importListTMDCompanyBConfigContract)
list.SetImplementation(importListTMDCompanyBImplementation)
list.SetListType(importListTMDBCompanyType)
list.SetConfigContract(importListTMDBCompanyConfigContract)
list.SetImplementation(importListTMDBCompanyImplementation)
list.SetId(int32(i.ID.ValueInt64()))
list.SetName(i.Name.ValueString())
list.SetTags(tags)
Expand Down
4 changes: 2 additions & 2 deletions internal/provider/import_list_tmdb_company_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@ func TestAccImportListTMDBCompanyResource(t *testing.T) {
// Create and Read testing
{
PreConfig: rootFolderDSInit,
Config: testAccImportListTMDBCompanyResourceConfig("resourceTMDBCompanyTest", "none"),
Config: testAccImportListTMDBCompanyResourceConfig("resourceTMDCompanyTest", "none"),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr("radarr_import_list_tmdb_company.test", "monitor", "none"),
resource.TestCheckResourceAttrSet("radarr_import_list_tmdb_company.test", "id"),
),
},
// Update and Read testing
{
Config: testAccImportListTMDBCompanyResourceConfig("resourceTMDBCompanyTest", "movieOnly"),
Config: testAccImportListTMDBCompanyResourceConfig("resourceTMDCompanyTest", "movieOnly"),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr("radarr_import_list_tmdb_company.test", "monitor", "movieOnly"),
),
Expand Down
Loading

0 comments on commit 2393836

Please sign in to comment.