-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add validate and update methods in domain data source
- Loading branch information
Showing
6 changed files
with
182 additions
and
120 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
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" | ||
} |
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,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 | ||
// } |
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,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)...) | ||
} |
This file was deleted.
Oops, something went wrong.
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