Skip to content

Commit

Permalink
feat: add validate and update methods in domain data source
Browse files Browse the repository at this point in the history
  • Loading branch information
gmeligio committed Apr 4, 2024
1 parent b661a9a commit 88abbbc
Show file tree
Hide file tree
Showing 6 changed files with 182 additions and 120 deletions.
13 changes: 13 additions & 0 deletions examples/provider-install-verification/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
terraform {
required_providers {
publicsuffix = {
source = "registry.terraform.io/gmeligio/publicsuffix"
}
}
}

provider "publicsuffix" {}

data "publicsuffix_domain" "example" {
# domain = "www.example.com"
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ require (
github.com/hashicorp/terraform-plugin-go v0.22.1
github.com/hashicorp/terraform-plugin-log v0.9.0
github.com/hashicorp/terraform-plugin-testing v1.7.0
golang.org/x/net v0.21.0
)

require (
Expand Down Expand Up @@ -68,7 +69,6 @@ require (
golang.org/x/crypto v0.21.0 // indirect
golang.org/x/exp v0.0.0-20230809150735-7b3493d9a819 // indirect
golang.org/x/mod v0.15.0 // indirect
golang.org/x/net v0.21.0 // indirect
golang.org/x/sys v0.18.0 // indirect
golang.org/x/text v0.14.0 // indirect
golang.org/x/tools v0.13.0 // indirect
Expand Down
91 changes: 91 additions & 0 deletions internal/provider/domain.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package provider

import (
"context"
"strings"

"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/types"
"golang.org/x/net/publicsuffix"
)

// domainDataSourceModel describes the data source data model.
type domainDataSourceModel struct {
Domain types.String `tfsdk:"domain"`
}

func (d domainDataSourceModel) validate(_ context.Context) diag.Diagnostics {
var diags diag.Diagnostics

if d.Domain.IsUnknown() || d.Domain.IsNull() {
return diags
}

domain := d.Domain.ValueString()

eTLD, icann := publicsuffix.PublicSuffix(domain)

manager := "Unmanaged"
if icann {
manager = "ICANN Managed"
} else if strings.IndexByte(eTLD, '.') >= 0 {
manager = "Privately Managed"
}

if manager == "Unmanaged" {
diags.AddAttributeError(
path.Root("domain"),
"Invalid Attribute Configuration",
"Expected domain to be either ICANN managede or privately managed.",
)
}

return diags
}

// func (d *domainDataSourceModel) update(ctx context.Context) diag.Diagnostics {
// var buffer bytes.Buffer
// var diags diag.Diagnostics
// var err error

// // cloudinit Provider 'v2.2.0' doesn't actually set default values in state properly, so we need to make sure
// // that we don't use any known empty values from previous versions of state
// diags.Append(d.setDefaults(ctx)...)
// if diags.HasError() {
// return diags
// }

// var configParts []configPartModel
// diags.Append(d.Parts.ElementsAs(ctx, &configParts, false)...)
// if diags.HasError() {
// return diags
// }

// if d.Gzip.ValueBool() {
// gzipWriter := gzip.NewWriter(&buffer)

// err = renderPartsToWriter(ctx, d.Boundary.ValueString(), configParts, gzipWriter)

// gzipWriter.Close()
// } else {
// err = renderPartsToWriter(ctx, d.Boundary.ValueString(), configParts, &buffer)
// }

// if err != nil {
// diags.AddError("Unable to render cloudinit config to MIME multi-part file", err.Error())
// return diags
// }

// output := ""
// if d.Base64Encode.ValueBool() {
// output = base64.StdEncoding.EncodeToString(buffer.Bytes())
// } else {
// output = buffer.String()
// }

// d.ID = types.StringValue(strconv.Itoa(hashcode.String(output)))
// d.Rendered = types.StringValue(output)

// return diags
// }
76 changes: 76 additions & 0 deletions internal/provider/domain_data_source.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package provider

import (
"context"
"net/http"

"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-log/tflog"
)

// Ensure provider defined types fully satisfy framework interfaces.
var _ datasource.DataSource = &domainDataSource{}

func NewDomainDataSource() datasource.DataSource {
return &domainDataSource{}
}

// domainDataSource defines the data source implementation.
type domainDataSource struct {
client *http.Client
}

func (d *domainDataSource) Metadata(ctx context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_domain"
}

func (d *domainDataSource) ValidateConfig(ctx context.Context, req datasource.ValidateConfigRequest, resp *datasource.ValidateConfigResponse) {
var data domainDataSourceModel

resp.Diagnostics.Append(req.Config.Get(ctx, &data)...)
if resp.Diagnostics.HasError() {
return
}

resp.Diagnostics.Append(data.validate(ctx)...)
}

func (d *domainDataSource) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) {
resp.Schema = schema.Schema{
// This description is used by the documentation generator and the language server.
MarkdownDescription: "Parses Public Suffix List properties from a domain",

Attributes: map[string]schema.Attribute{
"domain": schema.StringAttribute{
MarkdownDescription: "The domain is the Second Level Domain (SLD) + Top Level Domain (TLD). For example: example.domain.org",
},
},
}
}

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

// Read Terraform configuration data into the model
resp.Diagnostics.Append(req.Config.Get(ctx, &data)...)

if resp.Diagnostics.HasError() {
return
}

// For the purposes of this example code, hardcoding a response value to
// save into the Terraform state.
data.Domain = types.StringValue("foo.example.com")

// Write logs using the tflog package
// Documentation: https://terraform.io/plugin/log
tflog.Trace(ctx, "read a data source")

// resp.Diagnostics.Append(data.update(ctx)...)


// Save data into Terraform state
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
}
102 changes: 0 additions & 102 deletions internal/provider/example_data_source.go

This file was deleted.

18 changes: 1 addition & 17 deletions internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package provider

import (
"context"
"net/http"

"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/function"
Expand Down Expand Up @@ -39,21 +38,6 @@ func (p *PublicsuffixProvider) Schema(ctx context.Context, req provider.SchemaRe
}

func (p *PublicsuffixProvider) Configure(ctx context.Context, req provider.ConfigureRequest, resp *provider.ConfigureResponse) {
var data PublicsuffixProviderModel

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

if resp.Diagnostics.HasError() {
return
}

// Configuration values are now available.
// if data.Endpoint.IsNull() { /* ... */ }

// Example client configuration for data sources and resources
client := http.DefaultClient
resp.DataSourceData = client
resp.ResourceData = client
}

func (p *PublicsuffixProvider) Resources(ctx context.Context) []func() resource.Resource {
Expand All @@ -64,7 +48,7 @@ func (p *PublicsuffixProvider) Resources(ctx context.Context) []func() resource.

func (p *PublicsuffixProvider) DataSources(ctx context.Context) []func() datasource.DataSource {
return []func() datasource.DataSource{
NewExampleDataSource,
NewDomainDataSource,
}
}

Expand Down

0 comments on commit 88abbbc

Please sign in to comment.