Skip to content

Commit

Permalink
feat: add filter and user rules resources (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
gmichels authored Mar 23, 2023
1 parent 2d79d90 commit 55d5a6e
Show file tree
Hide file tree
Showing 25 changed files with 1,359 additions and 190 deletions.
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,7 @@ test:
echo $(TEST) | xargs -t -n4 go test $(TESTARGS) -timeout=30s -parallel=4

testacc:
docker compose -f ./docker/docker-compose.yaml up -d
TF_ACC=1 go test $(TEST) -v $(TESTARGS) -timeout 120m
docker compose -f ./docker/docker-compose.yaml down
git checkout HEAD -- ./docker/conf/AdGuardHome.yaml
127 changes: 127 additions & 0 deletions adguard/list_filter_data_source.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
package adguard

import (
"context"

"github.com/gmichels/adguard-client-go"
"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"github.com/hashicorp/terraform-plugin-framework/types"
)

// ensure the implementation satisfies the expected interfaces
var (
_ datasource.DataSource = &listFilterDataSource{}
_ datasource.DataSourceWithConfigure = &listFilterDataSource{}
)

// listFilterDataSource is the data source implementation
type listFilterDataSource struct {
adg *adguard.ADG
}

// listFilterDataModel maps filter schema data
type listFilterDataModel struct {
ID types.Int64 `tfsdk:"id"`
Url types.String `tfsdk:"url"`
Name types.String `tfsdk:"name"`
LastUpdated types.String `tfsdk:"last_updated"`
RulesCount types.Int64 `tfsdk:"rules_count"`
Enabled types.Bool `tfsdk:"enabled"`
Whitelist types.Bool `tfsdk:"whitelist"`
}

// NewListFilterDataSource is a helper function to simplify the provider implementation
func NewListFilterDataSource() datasource.DataSource {
return &listFilterDataSource{}
}

// Metadata returns the data source type name
func (d *listFilterDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_list_filter"
}

// Schema defines the schema for the data source
func (d *listFilterDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) {
resp.Schema = schema.Schema{
Attributes: map[string]schema.Attribute{
"name": schema.StringAttribute{
Description: "Name of the list filter",
Required: true,
},
"whitelist": schema.BoolAttribute{
Description: "When `true`, will consider this list filter of type whitelist",
Optional: true,
},
"url": schema.StringAttribute{
Description: "Url of the list filter",
Computed: true,
},
"last_updated": schema.StringAttribute{
Description: "Timestamp of last synchronization",
Computed: true,
},
"id": schema.Int64Attribute{
Description: "Identifier attribute",
Computed: true,
},
"rules_count": schema.Int64Attribute{
Description: "Number of rules in the list filter",
Computed: true,
},
"enabled": schema.BoolAttribute{
Description: "Whether this list filter is enabled",
Computed: true,
},
},
}
}

// Read refreshes the Terraform state with the latest data
func (d *listFilterDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
// read Terraform configuration data into the model
var state listFilterDataModel
diags := req.Config.Get(ctx, &state)
resp.Diagnostics.Append(diags...)

// retrieve list filter info
listFilter, whitelist, err := d.adg.GetListFilterByName(state.Name.ValueString())
if err != nil {
resp.Diagnostics.AddError(
"Unable to Read AdGuard Home List Filter",
err.Error(),
)
return
}
if listFilter == nil {
resp.Diagnostics.AddError(
"Unable to Locate AdGuard Home List Filter",
"No list filter with name `"+state.Name.ValueString()+"` exists in AdGuard Home.",
)
return
}

// map response body to model
state.Url = types.StringValue(listFilter.Url)
state.LastUpdated = types.StringValue(listFilter.LastUpdated)
state.ID = types.Int64Value(listFilter.Id)
state.RulesCount = types.Int64Value(int64(listFilter.RulesCount))
state.Enabled = types.BoolValue(listFilter.Enabled)
state.Whitelist = types.BoolValue(whitelist)

// set state
diags = resp.State.Set(ctx, &state)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
}

// Configure adds the provider configured client to the data source
func (d *listFilterDataSource) Configure(_ context.Context, req datasource.ConfigureRequest, _ *datasource.ConfigureResponse) {
if req.ProviderData == nil {
return
}

d.adg = req.ProviderData.(*adguard.ADG)
}
45 changes: 45 additions & 0 deletions adguard/list_filter_data_source_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package adguard

import (
"testing"

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

func TestAccListFilterDataSource(t *testing.T) {
resource.Test(t, resource.TestCase{
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
Steps: []resource.TestStep{
// Read testing
{
Config: providerConfig + `
data "adguard_list_filter" "test_blacklist" {
name = "AdGuard DNS filter"
}
`,
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr("data.adguard_list_filter.test_blacklist", "url", "https://adguardteam.github.io/HostlistsRegistry/assets/filter_1.txt"),
resource.TestCheckResourceAttr("data.adguard_list_filter.test_blacklist", "enabled", "true"),
resource.TestCheckResourceAttr("data.adguard_list_filter.test_blacklist", "whitelist", "false"),
resource.TestCheckResourceAttrSet("data.adguard_list_filter.test_blacklist", "rules_count"),
resource.TestCheckResourceAttr("data.adguard_list_filter.test_blacklist", "id", "1"),
),
},
{
Config: providerConfig + `
data "adguard_list_filter" "test_whitelist" {
name = "Test Allow List"
whitelist = true
}
`,
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr("data.adguard_list_filter.test_whitelist", "url", "https://adguardteam.github.io/HostlistsRegistry/assets/filter_3.txt"),
resource.TestCheckResourceAttr("data.adguard_list_filter.test_whitelist", "enabled", "false"),
resource.TestCheckResourceAttr("data.adguard_list_filter.test_whitelist", "whitelist", "true"),
resource.TestCheckResourceAttrSet("data.adguard_list_filter.test_whitelist", "rules_count"),
resource.TestCheckResourceAttr("data.adguard_list_filter.test_whitelist", "id", "3"),
),
},
},
})
}
Loading

0 comments on commit 55d5a6e

Please sign in to comment.