-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Ported runtime functions to functions.
- Loading branch information
Showing
40 changed files
with
1,328 additions
and
216 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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 @@ | ||
// Copyright 2023-2024, Northwood Labs | ||
// Copyright 2023-2024, Ryan Parman <[email protected]> | ||
// | ||
// 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" | ||
"runtime" | ||
"strings" | ||
|
||
"github.com/hashicorp/terraform-plugin-framework/function" | ||
"github.com/hashicorp/terraform-plugin-log/tflog" | ||
"github.com/lithammer/dedent" | ||
) | ||
|
||
// Ensure the implementation satisfies the expected interfaces. | ||
var _ function.Function = &runtimeCpuarchFunction{} | ||
|
||
type ( | ||
// runtimeCpuarchFunction is the function implementation. | ||
runtimeCpuarchFunction struct{} | ||
) | ||
|
||
// RuntimeCpuarchFunction is a method that exposes its paired Go function as a | ||
// Terraform Function. | ||
// https://developer.hashicorp.com/terraform/plugin/framework/functions/implementation | ||
func RuntimeCpuarchFunction() function.Function { // lint:allow_return_interface | ||
return &runtimeCpuarchFunction{} | ||
} | ||
|
||
func (f *runtimeCpuarchFunction) Metadata( | ||
ctx context.Context, | ||
req function.MetadataRequest, | ||
resp *function.MetadataResponse, | ||
) { | ||
tflog.Debug(ctx, "Starting RuntimeCpuarch Function Metadata method.") | ||
|
||
resp.Name = "runtime_cpuarch" | ||
|
||
tflog.Debug(ctx, fmt.Sprintf("resp.Name = %s", resp.Name)) | ||
|
||
tflog.Debug(ctx, "Ending RuntimeCpuarch Function Metadata method.") | ||
} | ||
|
||
// Definition defines the parameters and return type for the function. | ||
func (f *runtimeCpuarchFunction) Definition( | ||
ctx context.Context, | ||
req function.DefinitionRequest, | ||
resp *function.DefinitionResponse, | ||
) { | ||
tflog.Debug(ctx, "Starting RuntimeCpuarch Function Definition method.") | ||
|
||
resp.Definition = function.Definition{ | ||
Summary: "Returns the CPU architecture for which the provider was compiled.", | ||
MarkdownDescription: strings.TrimSpace(dedent.Dedent(` | ||
Returns the CPU architecture for which the provider was compiled. | ||
Generally, this represents the current system. | ||
-> If you are running the provider via Rosetta on an Apple Silicon | ||
machine, using QEMU, or similar CPU emulation software, this will return | ||
the architecture of the emulated CPU, not the host CPU. | ||
Maps to the [` + "`" + `runtime.GOARCH` + "`" + `](https://pkg.go.dev/runtime#GOARCH) | ||
Go property, which can be used in ` + Terratest + `. | ||
`)), | ||
Return: function.StringReturn{}, | ||
} | ||
|
||
tflog.Debug(ctx, "Ending RuntimeCpuarch Function Definition method.") | ||
} | ||
|
||
func (f *runtimeCpuarchFunction) Run(ctx context.Context, req function.RunRequest, resp *function.RunResponse) { | ||
tflog.Debug(ctx, "Starting RuntimeCpuarch Function Run method.") | ||
|
||
resp.Error = function.ConcatFuncErrors(resp.Result.Set(ctx, runtime.GOARCH)) | ||
|
||
tflog.Debug(ctx, "Ending RuntimeCpuarch Function Run method.") | ||
} |
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,4 @@ | ||
output "cpuarch" { | ||
value = provider::corefunc::runtime_cpuarch() | ||
} | ||
#=> {{ .Expected }} |
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,77 @@ | ||
// Copyright 2023-2024, Northwood Labs | ||
// Copyright 2023-2024, Ryan Parman <[email protected]> | ||
// | ||
// 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/go-version" | ||
"github.com/hashicorp/terraform-plugin-testing/helper/resource" | ||
"github.com/hashicorp/terraform-plugin-testing/tfversion" | ||
) | ||
|
||
func TestAccRuntimeCpuarchFunction(t *testing.T) { | ||
t.Parallel() | ||
|
||
funcName := traceFuncName() | ||
|
||
for name, tc := range testfixtures.RuntimeCpuarchTestTable { // lint:no_dupe | ||
fmt.Printf( | ||
"=== RUN %s/%s\n", | ||
strings.TrimSpace(funcName), | ||
strings.TrimSpace(name), | ||
) | ||
|
||
buf := &bytes.Buffer{} | ||
tmpl := template.Must( | ||
template.ParseFiles("runtime_cpuarch_function_fixture.tftpl"), | ||
) | ||
|
||
err := tmpl.Execute(buf, tc) | ||
if err != nil { | ||
log.Fatalln(err) | ||
} | ||
|
||
if os.Getenv("PROVIDER_DEBUG") != "" { | ||
fmt.Fprintln(os.Stderr, buf.String()) | ||
} | ||
|
||
resource.UnitTest(t, resource.TestCase{ | ||
TerraformVersionChecks: []tfversion.TerraformVersionCheck{ | ||
tfversion.SkipBelow( | ||
version.Must(version.NewVersion("1.7.999")), | ||
), | ||
}, | ||
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: providerConfig + buf.String(), | ||
Check: resource.ComposeTestCheckFunc( | ||
resource.TestCheckOutput("cpuarch", tc.Expected), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
} |
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,86 @@ | ||
// Copyright 2023-2024, Northwood Labs | ||
// Copyright 2023-2024, Ryan Parman <[email protected]> | ||
// | ||
// 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" | ||
"runtime" | ||
"strings" | ||
|
||
"github.com/hashicorp/terraform-plugin-framework/function" | ||
"github.com/hashicorp/terraform-plugin-log/tflog" | ||
"github.com/lithammer/dedent" | ||
) | ||
|
||
// Ensure the implementation satisfies the expected interfaces. | ||
var _ function.Function = &runtimeGorootFunction{} | ||
|
||
type ( | ||
// runtimeGorootFunction is the function implementation. | ||
runtimeGorootFunction struct{} | ||
) | ||
|
||
// RuntimeGorootFunction is a method that exposes its paired Go function as a | ||
// Terraform Function. | ||
// https://developer.hashicorp.com/terraform/plugin/framework/functions/implementation | ||
func RuntimeGorootFunction() function.Function { // lint:allow_return_interface | ||
return &runtimeGorootFunction{} | ||
} | ||
|
||
func (f *runtimeGorootFunction) Metadata( | ||
ctx context.Context, | ||
req function.MetadataRequest, | ||
resp *function.MetadataResponse, | ||
) { | ||
tflog.Debug(ctx, "Starting RuntimeGoroot Function Metadata method.") | ||
|
||
resp.Name = "runtime_goroot" | ||
|
||
tflog.Debug(ctx, fmt.Sprintf("resp.Name = %s", resp.Name)) | ||
|
||
tflog.Debug(ctx, "Ending RuntimeGoroot Function Metadata method.") | ||
} | ||
|
||
// Definition defines the parameters and return type for the function. | ||
func (f *runtimeGorootFunction) Definition( | ||
ctx context.Context, | ||
req function.DefinitionRequest, | ||
resp *function.DefinitionResponse, | ||
) { | ||
tflog.Debug(ctx, "Starting RuntimeGoroot Function Definition method.") | ||
|
||
resp.Definition = function.Definition{ | ||
Summary: "Returns the GOROOT path for the current system, if one exists.", | ||
MarkdownDescription: strings.TrimSpace(dedent.Dedent(` | ||
Returns the GOROOT path for the current system, if one exists. | ||
Maps to the [` + "`" + `runtime.GOROOT()` + "`" + `](https://pkg.go.dev/runtime#GOROOT) | ||
Go method, which can be used in ` + Terratest + `. | ||
`)), | ||
Return: function.StringReturn{}, | ||
} | ||
|
||
tflog.Debug(ctx, "Ending RuntimeGoroot Function Definition method.") | ||
} | ||
|
||
func (f *runtimeGorootFunction) Run(ctx context.Context, req function.RunRequest, resp *function.RunResponse) { | ||
tflog.Debug(ctx, "Starting RuntimeGoroot Function Run method.") | ||
|
||
resp.Error = function.ConcatFuncErrors(resp.Result.Set(ctx, runtime.GOROOT())) | ||
|
||
tflog.Debug(ctx, "Ending RuntimeGoroot Function Run method.") | ||
} |
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,4 @@ | ||
output "goroot" { | ||
value = provider::corefunc::runtime_goroot() | ||
} | ||
#=> {{ .Expected }} |
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,77 @@ | ||
// Copyright 2023-2024, Northwood Labs | ||
// Copyright 2023-2024, Ryan Parman <[email protected]> | ||
// | ||
// 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/go-version" | ||
"github.com/hashicorp/terraform-plugin-testing/helper/resource" | ||
"github.com/hashicorp/terraform-plugin-testing/tfversion" | ||
) | ||
|
||
func TestAccRuntimeGorootFunction(t *testing.T) { | ||
t.Parallel() | ||
|
||
funcName := traceFuncName() | ||
|
||
for name, tc := range testfixtures.RuntimeGorootTestTable { // lint:no_dupe | ||
fmt.Printf( | ||
"=== RUN %s/%s\n", | ||
strings.TrimSpace(funcName), | ||
strings.TrimSpace(name), | ||
) | ||
|
||
buf := &bytes.Buffer{} | ||
tmpl := template.Must( | ||
template.ParseFiles("runtime_goroot_function_fixture.tftpl"), | ||
) | ||
|
||
err := tmpl.Execute(buf, tc) | ||
if err != nil { | ||
log.Fatalln(err) | ||
} | ||
|
||
if os.Getenv("PROVIDER_DEBUG") != "" { | ||
fmt.Fprintln(os.Stderr, buf.String()) | ||
} | ||
|
||
resource.UnitTest(t, resource.TestCase{ | ||
TerraformVersionChecks: []tfversion.TerraformVersionCheck{ | ||
tfversion.SkipBelow( | ||
version.Must(version.NewVersion("1.7.999")), | ||
), | ||
}, | ||
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: providerConfig + buf.String(), | ||
Check: resource.ComposeTestCheckFunc( | ||
resource.TestCheckOutput("goroot", tc.Expected), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
} |
Oops, something went wrong.