Skip to content

Commit

Permalink
feat: Finished wrapping all functions in Data Sources and Functions.
Browse files Browse the repository at this point in the history
  • Loading branch information
skyzyx committed Nov 10, 2024
1 parent 614a849 commit b87bcbf
Show file tree
Hide file tree
Showing 41 changed files with 874 additions and 138 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ docs: docs-provider docs-cli
## docs-provider: [docs] Generate Terraform Registry documentation.
docs-provider: clean-ds
@ $(HEADER) "=====> Generating Terraform provider documentation..."
$(GO) get -u github.com/hashicorp/terraform-plugin-docs/cmd/tfplugindocs
$(GO) generate -v ./...

@ $(HEADER) "=====> Remove tfplugindocs comments..."
Expand Down
Binary file modified acc-coverage.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
188 changes: 107 additions & 81 deletions acc-coverage.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
10 changes: 10 additions & 0 deletions bats/ds_str_base_64_gunzip.bats.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/usr/bin/env bats
# https://bats-core.readthedocs.io/en/stable/writing-tests.html

@test "corefunc_str_base64_gunzip: attrs" {
run bash -c "tfschema data show -format=json corefunc_str_base64_gunzip | jq -Mrc '.attributes[]'"

[[ ${status} -eq 0 ]]
[[ ${lines[0]} == '{"name":"gzipped_base64","type":"string","required":true,"optional":false,"computed":false,"sensitive":false}' ]]
[[ ${lines[1]} == '{"name":"value","type":"string","required":false,"optional":false,"computed":true,"sensitive":false}' ]]
}
21 changes: 11 additions & 10 deletions bats/tfschema_listing.bats.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,15 @@
[[ ${lines[6]} == "corefunc_runtime_goroot" ]]
[[ ${lines[7]} == "corefunc_runtime_numcpus" ]]
[[ ${lines[8]} == "corefunc_runtime_os" ]]
[[ ${lines[9]} == "corefunc_str_camel" ]]
[[ ${lines[10]} == "corefunc_str_constant" ]]
[[ ${lines[11]} == "corefunc_str_iterative_replace" ]]
[[ ${lines[12]} == "corefunc_str_kebab" ]]
[[ ${lines[13]} == "corefunc_str_leftpad" ]]
[[ ${lines[14]} == "corefunc_str_pascal" ]]
[[ ${lines[15]} == "corefunc_str_snake" ]]
[[ ${lines[16]} == "corefunc_str_truncate_label" ]]
[[ ${lines[17]} == "corefunc_url_decode" ]]
[[ ${lines[18]} == "corefunc_url_parse" ]]
[[ ${lines[9]} == "corefunc_str_base64_gunzip" ]]
[[ ${lines[10]} == "corefunc_str_camel" ]]
[[ ${lines[11]} == "corefunc_str_constant" ]]
[[ ${lines[12]} == "corefunc_str_iterative_replace" ]]
[[ ${lines[13]} == "corefunc_str_kebab" ]]
[[ ${lines[14]} == "corefunc_str_leftpad" ]]
[[ ${lines[15]} == "corefunc_str_pascal" ]]
[[ ${lines[16]} == "corefunc_str_snake" ]]
[[ ${lines[17]} == "corefunc_str_truncate_label" ]]
[[ ${lines[18]} == "corefunc_url_decode" ]]
[[ ${lines[19]} == "corefunc_url_parse" ]]
}
4 changes: 3 additions & 1 deletion corefunc/cidr.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ import (
CIDRContains checks to see if an IP address or CIDR block is contained within
another CIDR block.
Ported from OpenTofu. This function licensed as MPL-2.0.
This functionality is built into OpenTofu 1.8, but is missing in Terraform 1.9.
This also provides a 1:1 implementation that can be used with Terratest or other
Go code. This function licensed as MPL-2.0.
----
Expand Down
4 changes: 4 additions & 0 deletions corefunc/url.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ func URLParse(rawURL string, canon ...types.URLCanonicalizer) (*url.Url, error)
/*
URLDecode decodes a URL-encoded string.
This functionality is built into OpenTofu 1.8, but is missing in Terraform 1.9.
This also provides a 1:1 implementation that can be used with Terratest or other
Go code.
----
- s (string): An encoded URL.
Expand Down
4 changes: 3 additions & 1 deletion corefuncprovider/net_cidr_contains_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,9 @@ func (d *netCidrContainsDataSource) Schema( // lint:no_dupe
CIDRContains checks to see if an IP address or CIDR block is contained
within another CIDR block.
-> Ported from OpenTofu.
-> This functionality is built into OpenTofu 1.8, but is missing in Terraform 1.9.
This also provides a 1:1 implementation that can be used with Terratest or other
Go code.
Maps to the ` + linkPackage("CIDRContains") + ` Go method, which can be used in ` + Terratest + `.
`)),
Expand Down
4 changes: 3 additions & 1 deletion corefuncprovider/net_cidr_contains_function.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,9 @@ func (f *netCidrContainsFunction) Definition( // lint:no_dupe
CIDRContains checks to see if an IP address or CIDR block is contained
within another CIDR block.
-> Ported from OpenTofu.
-> This functionality is built into OpenTofu 1.8, but is missing in Terraform 1.9.
This also provides a 1:1 implementation that can be used with Terratest or other
Go code.
Maps to the ` + linkPackage("CIDRContains") + ` Go method, which can be used in ` + Terratest + `.
`)),
Expand Down
2 changes: 2 additions & 0 deletions corefuncprovider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ func (p *coreFuncProvider) DataSources(ctx context.Context) []func() datasource.
RuntimeGorootDataSource,
RuntimeNumcpusDataSource,
RuntimeOsDataSource,
StrBase64GunzipDataSource,
StrCamelDataSource,
StrConstantDataSource,
StrIterativeReplaceDataSource,
Expand All @@ -139,6 +140,7 @@ func (p *coreFuncProvider) Functions(ctx context.Context) []func() function.Func
RuntimeGorootFunction,
RuntimeNumcpusFunction,
RuntimeOsFunction,
StrBase64GunzipFunction,
StrCamelFunction,
StrConstantFunction,
StrKebabFunction,
Expand Down
175 changes: 175 additions & 0 deletions corefuncprovider/str_base64_gunzip_data_source.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
// Copyright 2023-2024, Northwood Labs
// Copyright 2023-2024, Ryan Parman <rparman@northwood-labs.com>
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package corefuncprovider // lint:no_dupe

import (
"context"
"fmt"
"strings"

"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-log/tflog"
"github.com/lithammer/dedent"

"github.com/northwood-labs/terraform-provider-corefunc/corefunc"
)

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

// strBase64GunzipDataSource is the data source implementation.
type (
strBase64GunzipDataSource struct{}

// strBase64GunzipDataSourceModel maps the data source schema data.
strBase64GunzipDataSourceModel struct {
GzippedBase64 types.String `tfsdk:"gzipped_base64"`
Value types.String `tfsdk:"value"`
}
)

// StrBase64GunzipDataSource is a method that exposes its paired Go function as a
// Terraform Data Source.
func StrBase64GunzipDataSource() datasource.DataSource { // lint:allow_return_interface
return &strBase64GunzipDataSource{}
}

// Metadata returns the data source type name.
func (d *strBase64GunzipDataSource) Metadata(
ctx context.Context,
req datasource.MetadataRequest,
resp *datasource.MetadataResponse,
) {
tflog.Debug(ctx, "Starting StrBase64Gunzip DataSource Metadata method.")

resp.TypeName = req.ProviderTypeName + "_str_base64_gunzip"

tflog.Debug(ctx, fmt.Sprintf("req.ProviderTypeName = %s", req.ProviderTypeName))
tflog.Debug(ctx, fmt.Sprintf("resp.TypeName = %s", resp.TypeName))

tflog.Debug(ctx, "Ending StrBase64Gunzip DataSource Metadata method.")
}

// Schema defines the schema for the data source.
func (d *strBase64GunzipDataSource) Schema(
ctx context.Context,
_ datasource.SchemaRequest,
resp *datasource.SchemaResponse,
) {
tflog.Debug(ctx, "Starting StrBase64Gunzip DataSource Schema method.")

resp.Schema = schema.Schema{
MarkdownDescription: strings.TrimSpace(dedent.Dedent(`
Base64Gunzip is a function that decodes a Base64-encoded string, then
decompresses the result with gzip. Supports both padded and non-padded Base64
strings.
-> This functionality is built into OpenTofu 1.8, but is missing in Terraform 1.9.
This also provides a 1:1 implementation that can be used with Terratest or other
Go code.
Maps to the ` + linkPackage("Base64Gunzip") + ` Go method, which can be used in ` + Terratest + `.
`)),
Attributes: map[string]schema.Attribute{
"gzipped_base64": schema.StringAttribute{
MarkdownDescription: "A string of gzipped then Base64-encoded data.",
Required: true,
},
"value": schema.StringAttribute{
MarkdownDescription: "The Base64-decoded, then un-gzipped data.",
Computed: true,
},
},
}

tflog.Debug(ctx, "Ending StrBase64Gunzip DataSource Schema method.")
}

// Configure adds the provider configured client to the data source.
func (d *strBase64GunzipDataSource) Configure(
ctx context.Context,
req datasource.ConfigureRequest,
_ *datasource.ConfigureResponse,
) {
tflog.Debug(ctx, "Starting StrBase64Gunzip DataSource Configure method.")

if req.ProviderData == nil {
return
}

tflog.Debug(ctx, "Ending StrBase64Gunzip DataSource Configure method.")
}

func (d *strBase64GunzipDataSource) Create(
ctx context.Context,
req resource.CreateRequest, // lint:allow_large_memory
resp *resource.CreateResponse,
) {
tflog.Debug(ctx, "Starting StrBase64Gunzip DataSource Create method.")

var plan strBase64GunzipDataSourceModel

diags := req.Plan.Get(ctx, &plan)
resp.Diagnostics.Append(diags...)

if resp.Diagnostics.HasError() {
return
}

tflog.Debug(ctx, "Ending StrBase64Gunzip DataSource Create method.")
}

// Read refreshes the Terraform state with the latest data.
func (d *strBase64GunzipDataSource) Read( // lint:no_dupe
ctx context.Context,
_ datasource.ReadRequest, // lint:allow_large_memory
resp *datasource.ReadResponse,
) {
tflog.Debug(ctx, "Starting StrBase64Gunzip DataSource Read method.")

var state strBase64GunzipDataSourceModel
diags := resp.State.Get(ctx, &state)
resp.Diagnostics.Append(diags...)

gunzipped, err := corefunc.Base64Gunzip(
state.GzippedBase64.ValueString(),
)
if err != nil {
resp.Diagnostics.AddError(
"Problem with Base64 decoding and Gzip decompression",
err.Error(),
)

return
}

state.Value = types.StringValue(gunzipped)

diags = resp.State.Set(ctx, &state)
resp.Diagnostics.Append(diags...)

if resp.Diagnostics.HasError() {
return
}

tflog.Debug(ctx, "Ending StrBase64Gunzip DataSource Read method.")
}
5 changes: 5 additions & 0 deletions corefuncprovider/str_base64_gunzip_data_source_fixture.tftpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
data "corefunc_str_base64_gunzip" "str" {
gzipped_base64 = "{{ .Input }}"
}

#=> {{ .Expected }}
85 changes: 85 additions & 0 deletions corefuncprovider/str_base64_gunzip_data_source_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
// Copyright 2023-2024, Northwood Labs
// Copyright 2023-2024, Ryan Parman <rparman@northwood-labs.com>
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package corefuncprovider // lint:no_dupe

import (
"bytes"
"fmt"
"log"
"os"
"strings"
"testing"
"text/template"

"github.com/northwood-labs/terraform-provider-corefunc/testfixtures"

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

func TestAccStrBase64GunzipDataSource(t *testing.T) {
t.Parallel()

funcName := traceFuncName()

for name, tc := range testfixtures.Base64GunzipTestTable { // lint:no_dupe
fmt.Printf(
"=== RUN %s/%s\n",
strings.TrimSpace(funcName),
strings.TrimSpace(name),
)

buf := &bytes.Buffer{}
tmpl := template.Must(
template.ParseFiles("str_base64_gunzip_data_source_fixture.tftpl"),
)

err := tmpl.Execute(buf, tc)
if err != nil {
log.Fatalln(err)
}

if os.Getenv("PROVIDER_DEBUG") != "" {
fmt.Fprintln(os.Stderr, buf.String())
}

if tc.ExpectError != nil {
resource.Test(t, resource.TestCase{
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
Steps: []resource.TestStep{
{
Config: providerConfig + buf.String(),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr("data.corefunc_str_base64_gunzip.str", "value", tc.Expected),
),
ExpectError: tc.ExpectError,
},
},
})
} else {
resource.Test(t, resource.TestCase{
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
Steps: []resource.TestStep{
{
Config: providerConfig + buf.String(),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr("data.corefunc_str_base64_gunzip.str", "value", tc.Expected),
),
},
},
})
}
}
}
Loading

0 comments on commit b87bcbf

Please sign in to comment.