-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add filter and user rules resources (#15)
- Loading branch information
Showing
25 changed files
with
1,359 additions
and
190 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
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,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) | ||
} |
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,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"), | ||
), | ||
}, | ||
}, | ||
}) | ||
} |
Oops, something went wrong.