mirror of
https://github.com/coder/terraform-provider-envbuilder.git
synced 2025-11-14 16:08:08 +00:00
Envbuilder
This commit is contained in:
parent
76d8aa974e
commit
1a49822fc9
25 changed files with 350 additions and 691 deletions
182
internal/provider/cached_image_data_source.go
Normal file
182
internal/provider/cached_image_data_source.go
Normal file
|
|
@ -0,0 +1,182 @@
|
|||
// Copyright (c) HashiCorp, Inc.
|
||||
// SPDX-License-Identifier: MPL-2.0
|
||||
|
||||
package provider
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"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 = &CachedImageDataSource{}
|
||||
|
||||
func NewCachedImageDataSource() datasource.DataSource {
|
||||
return &CachedImageDataSource{}
|
||||
}
|
||||
|
||||
// CachedImageDataSource defines the data source implementation.
|
||||
type CachedImageDataSource struct {
|
||||
client *http.Client
|
||||
}
|
||||
|
||||
// CachedImageDataSourceModel describes the data source data model.
|
||||
type CachedImageDataSourceModel struct {
|
||||
BuilderImage types.String `tfsdk:"builder_image"`
|
||||
CacheRepo types.String `tfsdk:"cache_repo"`
|
||||
CacheTTLDays types.Number `tfsdk:"cache_ttl_days"`
|
||||
Env types.List `tfsdk:"env"`
|
||||
Exists types.Bool `tfsdk:"exists"`
|
||||
ExtraEnv types.Map `tfsdk:"extra_env"`
|
||||
GitPassword types.String `tfsdk:"git_password"`
|
||||
GitURL types.String `tfsdk:"git_url"`
|
||||
GitUsername types.String `tfsdk:"git_username"`
|
||||
ID types.String `tfsdk:"id"`
|
||||
Image types.String `tfsdk:"image"`
|
||||
}
|
||||
|
||||
func (d *CachedImageDataSource) Metadata(ctx context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
|
||||
resp.TypeName = req.ProviderTypeName + "_cached_image"
|
||||
}
|
||||
|
||||
func (d *CachedImageDataSource) 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: "The cached image data source can be used to retrieve a cached image produced by envbuilder.",
|
||||
|
||||
Attributes: map[string]schema.Attribute{
|
||||
"builder_image": schema.StringAttribute{
|
||||
MarkdownDescription: "The builder image URL to use if the cache does not exist.",
|
||||
Required: true,
|
||||
},
|
||||
"git_url": schema.StringAttribute{
|
||||
MarkdownDescription: "The URL of a Git repository containing a Devcontainer or Docker image to clone.",
|
||||
Required: true,
|
||||
},
|
||||
"git_username": schema.StringAttribute{
|
||||
MarkdownDescription: "The username to use for Git authentication. This is optional.",
|
||||
Optional: true,
|
||||
},
|
||||
"git_password": schema.StringAttribute{
|
||||
MarkdownDescription: "The password to use for Git authentication. This is optional.",
|
||||
Sensitive: true,
|
||||
Optional: true,
|
||||
},
|
||||
"cache_repo": schema.StringAttribute{
|
||||
MarkdownDescription: "The name of the container registry to fetch the cache image from.",
|
||||
Required: true,
|
||||
},
|
||||
"cache_ttl_days": schema.NumberAttribute{
|
||||
MarkdownDescription: "The number of days to use cached layers before expiring them. Defaults to 7 days.",
|
||||
Optional: true,
|
||||
},
|
||||
// TODO(mafredri): Map vs List? Support both?
|
||||
"extra_env": schema.MapAttribute{
|
||||
MarkdownDescription: "Extra environment variables to set for the container. This may include evbuilder options.",
|
||||
ElementType: types.StringType,
|
||||
Optional: true,
|
||||
},
|
||||
"id": schema.StringAttribute{
|
||||
MarkdownDescription: "Cached image identifier",
|
||||
Computed: true,
|
||||
},
|
||||
"exists": schema.BoolAttribute{
|
||||
MarkdownDescription: "Whether the cached image was exists or not for the given config.",
|
||||
Computed: true,
|
||||
},
|
||||
"image": schema.StringAttribute{
|
||||
MarkdownDescription: "Outputs the cached image URL if it exists, otherwise the builder image URL is output instead.",
|
||||
Computed: true,
|
||||
},
|
||||
// TODO(mafredri): Map vs List? Support both?
|
||||
"env": schema.ListAttribute{
|
||||
MarkdownDescription: "Computed envbuilder configuration to be set for the container.",
|
||||
ElementType: types.StringType,
|
||||
Computed: true,
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func (d *CachedImageDataSource) Configure(ctx context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) {
|
||||
// Prevent panic if the provider has not been configured.
|
||||
if req.ProviderData == nil {
|
||||
return
|
||||
}
|
||||
|
||||
client, ok := req.ProviderData.(*http.Client)
|
||||
|
||||
if !ok {
|
||||
resp.Diagnostics.AddError(
|
||||
"Unexpected Data Source Configure Type",
|
||||
fmt.Sprintf("Expected *http.Client, got: %T. Please report this issue to the provider developers.", req.ProviderData),
|
||||
)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
d.client = client
|
||||
}
|
||||
|
||||
func (d *CachedImageDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
|
||||
var data CachedImageDataSourceModel
|
||||
|
||||
// Read Terraform configuration data into the model
|
||||
resp.Diagnostics.Append(req.Config.Get(ctx, &data)...)
|
||||
|
||||
if resp.Diagnostics.HasError() {
|
||||
return
|
||||
}
|
||||
|
||||
// If applicable, this is a great opportunity to initialize any necessary
|
||||
// provider client data and make a call using it.
|
||||
// httpResp, err := d.client.Do(httpReq)
|
||||
// if err != nil {
|
||||
// resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to read cached image, got error: %s", err))
|
||||
// return
|
||||
// }
|
||||
|
||||
// TODO(mafredri): Implement the actual data source read logic.
|
||||
data.ID = types.StringValue("cached-image-id")
|
||||
data.Exists = types.BoolValue(false)
|
||||
data.Image = data.BuilderImage
|
||||
|
||||
// Compute the env attribute from the config map.
|
||||
// TODO(mafredri): Convert any other relevant attributes given via schema.
|
||||
for key, elem := range data.ExtraEnv.Elements() {
|
||||
data.Env = appendKnownEnvToList(data.Env, key, elem)
|
||||
}
|
||||
|
||||
data.Env = appendKnownEnvToList(data.Env, "ENVBUILDER_CACHE_REPO", data.CacheRepo)
|
||||
data.Env = appendKnownEnvToList(data.Env, "ENVBUILDER_CACHE_TTL_DAYS", data.CacheTTLDays)
|
||||
data.Env = appendKnownEnvToList(data.Env, "ENVBUILDER_GIT_URL", data.GitURL)
|
||||
data.Env = appendKnownEnvToList(data.Env, "ENVBUILDER_GIT_USERNAME", data.GitUsername)
|
||||
data.Env = appendKnownEnvToList(data.Env, "ENVBUILDER_GIT_PASSWORD", data.GitPassword)
|
||||
|
||||
// Write logs using the tflog package
|
||||
// Documentation: https://terraform.io/plugin/log
|
||||
tflog.Trace(ctx, "read a data source")
|
||||
|
||||
// Save data into Terraform state
|
||||
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
|
||||
}
|
||||
|
||||
type stringable interface {
|
||||
IsUnknown() bool
|
||||
String() string
|
||||
}
|
||||
|
||||
func appendKnownEnvToList(list types.List, key string, value stringable) types.List {
|
||||
if value.IsUnknown() {
|
||||
return list
|
||||
}
|
||||
elem := types.StringValue(fmt.Sprintf("%s=%s", key, value.String()))
|
||||
list, _ = types.ListValue(types.StringType, append(list.Elements(), elem))
|
||||
return list
|
||||
}
|
||||
48
internal/provider/cached_image_data_source_test.go
Normal file
48
internal/provider/cached_image_data_source_test.go
Normal file
|
|
@ -0,0 +1,48 @@
|
|||
// Copyright (c) HashiCorp, Inc.
|
||||
// SPDX-License-Identifier: MPL-2.0
|
||||
|
||||
package provider
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
|
||||
)
|
||||
|
||||
func TestAccExampleDataSource(t *testing.T) {
|
||||
resource.Test(t, resource.TestCase{
|
||||
PreCheck: func() { testAccPreCheck(t) },
|
||||
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
|
||||
Steps: []resource.TestStep{
|
||||
// Read testing
|
||||
{
|
||||
Config: testAccCachedImageDataSourceConfig,
|
||||
Check: resource.ComposeAggregateTestCheckFunc(
|
||||
// Input
|
||||
resource.TestCheckResourceAttr("data.envbuilder_cached_image.test", "extra_env.ENVBUILDER_VERBOSE", "true"),
|
||||
resource.TestCheckResourceAttr("data.envbuilder_cached_image.test", "git_url", "https://github.com/coder/envbuilder-starter-devcontainer"),
|
||||
resource.TestCheckNoResourceAttr("data.envbuilder_cached_image.test", "git_username"),
|
||||
resource.TestCheckNoResourceAttr("data.envbuilder_cached_image.test", "git_password"),
|
||||
resource.TestCheckResourceAttr("data.envbuilder_cached_image.test", "cache_repo", "localhost:5000/local/test-cache"),
|
||||
resource.TestCheckNoResourceAttr("data.envbuilder_cached_image.test", "cache_ttl_days"),
|
||||
// Computed
|
||||
resource.TestCheckResourceAttr("data.envbuilder_cached_image.test", "id", "cached-image-id"),
|
||||
resource.TestCheckResourceAttr("data.envbuilder_cached_image.test", "exists", "false"),
|
||||
resource.TestCheckResourceAttr("data.envbuilder_cached_image.test", "image", "ghcr.io/coder/envbuilder:latest"),
|
||||
resource.TestCheckResourceAttr("data.envbuilder_cached_image.test", "env.0", "ENVBUILDER_VERBOSE=\"true\""),
|
||||
),
|
||||
},
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
const testAccCachedImageDataSourceConfig = `
|
||||
data "envbuilder_cached_image" "test" {
|
||||
builder_image = "ghcr.io/coder/envbuilder:latest"
|
||||
git_url = "https://github.com/coder/envbuilder-starter-devcontainer"
|
||||
cache_repo = "localhost:5000/local/test-cache"
|
||||
extra_env = {
|
||||
"ENVBUILDER_VERBOSE" : "true"
|
||||
}
|
||||
}
|
||||
`
|
||||
|
|
@ -1,105 +0,0 @@
|
|||
// Copyright (c) HashiCorp, Inc.
|
||||
// SPDX-License-Identifier: MPL-2.0
|
||||
|
||||
package provider
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"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 = &ExampleDataSource{}
|
||||
|
||||
func NewExampleDataSource() datasource.DataSource {
|
||||
return &ExampleDataSource{}
|
||||
}
|
||||
|
||||
// ExampleDataSource defines the data source implementation.
|
||||
type ExampleDataSource struct {
|
||||
client *http.Client
|
||||
}
|
||||
|
||||
// ExampleDataSourceModel describes the data source data model.
|
||||
type ExampleDataSourceModel struct {
|
||||
ConfigurableAttribute types.String `tfsdk:"configurable_attribute"`
|
||||
Id types.String `tfsdk:"id"`
|
||||
}
|
||||
|
||||
func (d *ExampleDataSource) Metadata(ctx context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
|
||||
resp.TypeName = req.ProviderTypeName + "_example"
|
||||
}
|
||||
|
||||
func (d *ExampleDataSource) 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: "Example data source",
|
||||
|
||||
Attributes: map[string]schema.Attribute{
|
||||
"configurable_attribute": schema.StringAttribute{
|
||||
MarkdownDescription: "Example configurable attribute",
|
||||
Optional: true,
|
||||
},
|
||||
"id": schema.StringAttribute{
|
||||
MarkdownDescription: "Example identifier",
|
||||
Computed: true,
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func (d *ExampleDataSource) Configure(ctx context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) {
|
||||
// Prevent panic if the provider has not been configured.
|
||||
if req.ProviderData == nil {
|
||||
return
|
||||
}
|
||||
|
||||
client, ok := req.ProviderData.(*http.Client)
|
||||
|
||||
if !ok {
|
||||
resp.Diagnostics.AddError(
|
||||
"Unexpected Data Source Configure Type",
|
||||
fmt.Sprintf("Expected *http.Client, got: %T. Please report this issue to the provider developers.", req.ProviderData),
|
||||
)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
d.client = client
|
||||
}
|
||||
|
||||
func (d *ExampleDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
|
||||
var data ExampleDataSourceModel
|
||||
|
||||
// Read Terraform configuration data into the model
|
||||
resp.Diagnostics.Append(req.Config.Get(ctx, &data)...)
|
||||
|
||||
if resp.Diagnostics.HasError() {
|
||||
return
|
||||
}
|
||||
|
||||
// If applicable, this is a great opportunity to initialize any necessary
|
||||
// provider client data and make a call using it.
|
||||
// httpResp, err := d.client.Do(httpReq)
|
||||
// if err != nil {
|
||||
// resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to read example, got error: %s", err))
|
||||
// return
|
||||
// }
|
||||
|
||||
// For the purposes of this example code, hardcoding a response value to
|
||||
// save into the Terraform state.
|
||||
data.Id = types.StringValue("example-id")
|
||||
|
||||
// Write logs using the tflog package
|
||||
// Documentation: https://terraform.io/plugin/log
|
||||
tflog.Trace(ctx, "read a data source")
|
||||
|
||||
// Save data into Terraform state
|
||||
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
|
||||
}
|
||||
|
|
@ -1,32 +0,0 @@
|
|||
// Copyright (c) HashiCorp, Inc.
|
||||
// SPDX-License-Identifier: MPL-2.0
|
||||
|
||||
package provider
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
|
||||
)
|
||||
|
||||
func TestAccExampleDataSource(t *testing.T) {
|
||||
resource.Test(t, resource.TestCase{
|
||||
PreCheck: func() { testAccPreCheck(t) },
|
||||
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
|
||||
Steps: []resource.TestStep{
|
||||
// Read testing
|
||||
{
|
||||
Config: testAccExampleDataSourceConfig,
|
||||
Check: resource.ComposeAggregateTestCheckFunc(
|
||||
resource.TestCheckResourceAttr("data.scaffolding_example.test", "id", "example-id"),
|
||||
),
|
||||
},
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
const testAccExampleDataSourceConfig = `
|
||||
data "scaffolding_example" "test" {
|
||||
configurable_attribute = "example"
|
||||
}
|
||||
`
|
||||
|
|
@ -1,50 +0,0 @@
|
|||
// Copyright (c) HashiCorp, Inc.
|
||||
// SPDX-License-Identifier: MPL-2.0
|
||||
|
||||
package provider
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/hashicorp/terraform-plugin-framework/function"
|
||||
)
|
||||
|
||||
var (
|
||||
_ function.Function = ExampleFunction{}
|
||||
)
|
||||
|
||||
func NewExampleFunction() function.Function {
|
||||
return ExampleFunction{}
|
||||
}
|
||||
|
||||
type ExampleFunction struct{}
|
||||
|
||||
func (r ExampleFunction) Metadata(_ context.Context, req function.MetadataRequest, resp *function.MetadataResponse) {
|
||||
resp.Name = "example"
|
||||
}
|
||||
|
||||
func (r ExampleFunction) Definition(_ context.Context, _ function.DefinitionRequest, resp *function.DefinitionResponse) {
|
||||
resp.Definition = function.Definition{
|
||||
Summary: "Example function",
|
||||
MarkdownDescription: "Echoes given argument as result",
|
||||
Parameters: []function.Parameter{
|
||||
function.StringParameter{
|
||||
Name: "input",
|
||||
MarkdownDescription: "String to echo",
|
||||
},
|
||||
},
|
||||
Return: function.StringReturn{},
|
||||
}
|
||||
}
|
||||
|
||||
func (r ExampleFunction) Run(ctx context.Context, req function.RunRequest, resp *function.RunResponse) {
|
||||
var data string
|
||||
|
||||
resp.Error = function.ConcatFuncErrors(req.Arguments.Get(ctx, &data))
|
||||
|
||||
if resp.Error != nil {
|
||||
return
|
||||
}
|
||||
|
||||
resp.Error = function.ConcatFuncErrors(resp.Result.Set(ctx, data))
|
||||
}
|
||||
|
|
@ -1,78 +0,0 @@
|
|||
// Copyright (c) HashiCorp, Inc.
|
||||
// SPDX-License-Identifier: MPL-2.0
|
||||
|
||||
package provider
|
||||
|
||||
import (
|
||||
"regexp"
|
||||
"testing"
|
||||
|
||||
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
|
||||
"github.com/hashicorp/terraform-plugin-testing/tfversion"
|
||||
)
|
||||
|
||||
func TestExampleFunction_Known(t *testing.T) {
|
||||
resource.UnitTest(t, resource.TestCase{
|
||||
TerraformVersionChecks: []tfversion.TerraformVersionCheck{
|
||||
tfversion.SkipBelow(tfversion.Version1_8_0),
|
||||
},
|
||||
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
|
||||
Steps: []resource.TestStep{
|
||||
{
|
||||
Config: `
|
||||
output "test" {
|
||||
value = provider::scaffolding::example("testvalue")
|
||||
}
|
||||
`,
|
||||
Check: resource.ComposeAggregateTestCheckFunc(
|
||||
resource.TestCheckOutput("test", "testvalue"),
|
||||
),
|
||||
},
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
func TestExampleFunction_Null(t *testing.T) {
|
||||
resource.UnitTest(t, resource.TestCase{
|
||||
TerraformVersionChecks: []tfversion.TerraformVersionCheck{
|
||||
tfversion.SkipBelow(tfversion.Version1_8_0),
|
||||
},
|
||||
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
|
||||
Steps: []resource.TestStep{
|
||||
{
|
||||
Config: `
|
||||
output "test" {
|
||||
value = provider::scaffolding::example(null)
|
||||
}
|
||||
`,
|
||||
// The parameter does not enable AllowNullValue
|
||||
ExpectError: regexp.MustCompile(`argument must not be null`),
|
||||
},
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
func TestExampleFunction_Unknown(t *testing.T) {
|
||||
resource.UnitTest(t, resource.TestCase{
|
||||
TerraformVersionChecks: []tfversion.TerraformVersionCheck{
|
||||
tfversion.SkipBelow(tfversion.Version1_8_0),
|
||||
},
|
||||
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
|
||||
Steps: []resource.TestStep{
|
||||
{
|
||||
Config: `
|
||||
resource "terraform_data" "test" {
|
||||
input = "testvalue"
|
||||
}
|
||||
|
||||
output "test" {
|
||||
value = provider::scaffolding::example(terraform_data.test.output)
|
||||
}
|
||||
`,
|
||||
Check: resource.ComposeAggregateTestCheckFunc(
|
||||
resource.TestCheckOutput("test", "testvalue"),
|
||||
),
|
||||
},
|
||||
},
|
||||
})
|
||||
}
|
||||
|
|
@ -1,187 +0,0 @@
|
|||
// Copyright (c) HashiCorp, Inc.
|
||||
// SPDX-License-Identifier: MPL-2.0
|
||||
|
||||
package provider
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"github.com/hashicorp/terraform-plugin-framework/path"
|
||||
"github.com/hashicorp/terraform-plugin-framework/resource"
|
||||
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
|
||||
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
|
||||
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringdefault"
|
||||
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
|
||||
"github.com/hashicorp/terraform-plugin-framework/types"
|
||||
"github.com/hashicorp/terraform-plugin-log/tflog"
|
||||
)
|
||||
|
||||
// Ensure provider defined types fully satisfy framework interfaces.
|
||||
var _ resource.Resource = &ExampleResource{}
|
||||
var _ resource.ResourceWithImportState = &ExampleResource{}
|
||||
|
||||
func NewExampleResource() resource.Resource {
|
||||
return &ExampleResource{}
|
||||
}
|
||||
|
||||
// ExampleResource defines the resource implementation.
|
||||
type ExampleResource struct {
|
||||
client *http.Client
|
||||
}
|
||||
|
||||
// ExampleResourceModel describes the resource data model.
|
||||
type ExampleResourceModel struct {
|
||||
ConfigurableAttribute types.String `tfsdk:"configurable_attribute"`
|
||||
Defaulted types.String `tfsdk:"defaulted"`
|
||||
Id types.String `tfsdk:"id"`
|
||||
}
|
||||
|
||||
func (r *ExampleResource) Metadata(ctx context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) {
|
||||
resp.TypeName = req.ProviderTypeName + "_example"
|
||||
}
|
||||
|
||||
func (r *ExampleResource) Schema(ctx context.Context, req resource.SchemaRequest, resp *resource.SchemaResponse) {
|
||||
resp.Schema = schema.Schema{
|
||||
// This description is used by the documentation generator and the language server.
|
||||
MarkdownDescription: "Example resource",
|
||||
|
||||
Attributes: map[string]schema.Attribute{
|
||||
"configurable_attribute": schema.StringAttribute{
|
||||
MarkdownDescription: "Example configurable attribute",
|
||||
Optional: true,
|
||||
},
|
||||
"defaulted": schema.StringAttribute{
|
||||
MarkdownDescription: "Example configurable attribute with default value",
|
||||
Optional: true,
|
||||
Computed: true,
|
||||
Default: stringdefault.StaticString("example value when not configured"),
|
||||
},
|
||||
"id": schema.StringAttribute{
|
||||
Computed: true,
|
||||
MarkdownDescription: "Example identifier",
|
||||
PlanModifiers: []planmodifier.String{
|
||||
stringplanmodifier.UseStateForUnknown(),
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func (r *ExampleResource) Configure(ctx context.Context, req resource.ConfigureRequest, resp *resource.ConfigureResponse) {
|
||||
// Prevent panic if the provider has not been configured.
|
||||
if req.ProviderData == nil {
|
||||
return
|
||||
}
|
||||
|
||||
client, ok := req.ProviderData.(*http.Client)
|
||||
|
||||
if !ok {
|
||||
resp.Diagnostics.AddError(
|
||||
"Unexpected Resource Configure Type",
|
||||
fmt.Sprintf("Expected *http.Client, got: %T. Please report this issue to the provider developers.", req.ProviderData),
|
||||
)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
r.client = client
|
||||
}
|
||||
|
||||
func (r *ExampleResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
|
||||
var data ExampleResourceModel
|
||||
|
||||
// Read Terraform plan data into the model
|
||||
resp.Diagnostics.Append(req.Plan.Get(ctx, &data)...)
|
||||
|
||||
if resp.Diagnostics.HasError() {
|
||||
return
|
||||
}
|
||||
|
||||
// If applicable, this is a great opportunity to initialize any necessary
|
||||
// provider client data and make a call using it.
|
||||
// httpResp, err := r.client.Do(httpReq)
|
||||
// if err != nil {
|
||||
// resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to create example, got error: %s", err))
|
||||
// return
|
||||
// }
|
||||
|
||||
// For the purposes of this example code, hardcoding a response value to
|
||||
// save into the Terraform state.
|
||||
data.Id = types.StringValue("example-id")
|
||||
|
||||
// Write logs using the tflog package
|
||||
// Documentation: https://terraform.io/plugin/log
|
||||
tflog.Trace(ctx, "created a resource")
|
||||
|
||||
// Save data into Terraform state
|
||||
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
|
||||
}
|
||||
|
||||
func (r *ExampleResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) {
|
||||
var data ExampleResourceModel
|
||||
|
||||
// Read Terraform prior state data into the model
|
||||
resp.Diagnostics.Append(req.State.Get(ctx, &data)...)
|
||||
|
||||
if resp.Diagnostics.HasError() {
|
||||
return
|
||||
}
|
||||
|
||||
// If applicable, this is a great opportunity to initialize any necessary
|
||||
// provider client data and make a call using it.
|
||||
// httpResp, err := r.client.Do(httpReq)
|
||||
// if err != nil {
|
||||
// resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to read example, got error: %s", err))
|
||||
// return
|
||||
// }
|
||||
|
||||
// Save updated data into Terraform state
|
||||
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
|
||||
}
|
||||
|
||||
func (r *ExampleResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) {
|
||||
var data ExampleResourceModel
|
||||
|
||||
// Read Terraform plan data into the model
|
||||
resp.Diagnostics.Append(req.Plan.Get(ctx, &data)...)
|
||||
|
||||
if resp.Diagnostics.HasError() {
|
||||
return
|
||||
}
|
||||
|
||||
// If applicable, this is a great opportunity to initialize any necessary
|
||||
// provider client data and make a call using it.
|
||||
// httpResp, err := r.client.Do(httpReq)
|
||||
// if err != nil {
|
||||
// resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to update example, got error: %s", err))
|
||||
// return
|
||||
// }
|
||||
|
||||
// Save updated data into Terraform state
|
||||
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
|
||||
}
|
||||
|
||||
func (r *ExampleResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) {
|
||||
var data ExampleResourceModel
|
||||
|
||||
// Read Terraform prior state data into the model
|
||||
resp.Diagnostics.Append(req.State.Get(ctx, &data)...)
|
||||
|
||||
if resp.Diagnostics.HasError() {
|
||||
return
|
||||
}
|
||||
|
||||
// If applicable, this is a great opportunity to initialize any necessary
|
||||
// provider client data and make a call using it.
|
||||
// httpResp, err := r.client.Do(httpReq)
|
||||
// if err != nil {
|
||||
// resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to delete example, got error: %s", err))
|
||||
// return
|
||||
// }
|
||||
}
|
||||
|
||||
func (r *ExampleResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) {
|
||||
resource.ImportStatePassthroughID(ctx, path.Root("id"), req, resp)
|
||||
}
|
||||
|
|
@ -1,56 +0,0 @@
|
|||
// Copyright (c) HashiCorp, Inc.
|
||||
// SPDX-License-Identifier: MPL-2.0
|
||||
|
||||
package provider
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
|
||||
)
|
||||
|
||||
func TestAccExampleResource(t *testing.T) {
|
||||
resource.Test(t, resource.TestCase{
|
||||
PreCheck: func() { testAccPreCheck(t) },
|
||||
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
|
||||
Steps: []resource.TestStep{
|
||||
// Create and Read testing
|
||||
{
|
||||
Config: testAccExampleResourceConfig("one"),
|
||||
Check: resource.ComposeAggregateTestCheckFunc(
|
||||
resource.TestCheckResourceAttr("scaffolding_example.test", "configurable_attribute", "one"),
|
||||
resource.TestCheckResourceAttr("scaffolding_example.test", "defaulted", "example value when not configured"),
|
||||
resource.TestCheckResourceAttr("scaffolding_example.test", "id", "example-id"),
|
||||
),
|
||||
},
|
||||
// ImportState testing
|
||||
{
|
||||
ResourceName: "scaffolding_example.test",
|
||||
ImportState: true,
|
||||
ImportStateVerify: true,
|
||||
// This is not normally necessary, but is here because this
|
||||
// example code does not have an actual upstream service.
|
||||
// Once the Read method is able to refresh information from
|
||||
// the upstream service, this can be removed.
|
||||
ImportStateVerifyIgnore: []string{"configurable_attribute", "defaulted"},
|
||||
},
|
||||
// Update and Read testing
|
||||
{
|
||||
Config: testAccExampleResourceConfig("two"),
|
||||
Check: resource.ComposeAggregateTestCheckFunc(
|
||||
resource.TestCheckResourceAttr("scaffolding_example.test", "configurable_attribute", "two"),
|
||||
),
|
||||
},
|
||||
// Delete testing automatically occurs in TestCase
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
func testAccExampleResourceConfig(configurableAttribute string) string {
|
||||
return fmt.Sprintf(`
|
||||
resource "scaffolding_example" "test" {
|
||||
configurable_attribute = %[1]q
|
||||
}
|
||||
`, configurableAttribute)
|
||||
}
|
||||
|
|
@ -12,44 +12,38 @@ import (
|
|||
"github.com/hashicorp/terraform-plugin-framework/provider"
|
||||
"github.com/hashicorp/terraform-plugin-framework/provider/schema"
|
||||
"github.com/hashicorp/terraform-plugin-framework/resource"
|
||||
"github.com/hashicorp/terraform-plugin-framework/types"
|
||||
)
|
||||
|
||||
// Ensure ScaffoldingProvider satisfies various provider interfaces.
|
||||
var _ provider.Provider = &ScaffoldingProvider{}
|
||||
var _ provider.ProviderWithFunctions = &ScaffoldingProvider{}
|
||||
// Ensure EnvbuilderProvider satisfies various provider interfaces.
|
||||
var (
|
||||
_ provider.Provider = &EnvbuilderProvider{}
|
||||
_ provider.ProviderWithFunctions = &EnvbuilderProvider{}
|
||||
)
|
||||
|
||||
// ScaffoldingProvider defines the provider implementation.
|
||||
type ScaffoldingProvider struct {
|
||||
// EnvbuilderProvider defines the provider implementation.
|
||||
type EnvbuilderProvider struct {
|
||||
// version is set to the provider version on release, "dev" when the
|
||||
// provider is built and ran locally, and "test" when running acceptance
|
||||
// testing.
|
||||
version string
|
||||
}
|
||||
|
||||
// ScaffoldingProviderModel describes the provider data model.
|
||||
type ScaffoldingProviderModel struct {
|
||||
Endpoint types.String `tfsdk:"endpoint"`
|
||||
}
|
||||
// EnvbuilderProviderModel describes the provider data model.
|
||||
type EnvbuilderProviderModel struct{}
|
||||
|
||||
func (p *ScaffoldingProvider) Metadata(ctx context.Context, req provider.MetadataRequest, resp *provider.MetadataResponse) {
|
||||
resp.TypeName = "scaffolding"
|
||||
func (p *EnvbuilderProvider) Metadata(ctx context.Context, req provider.MetadataRequest, resp *provider.MetadataResponse) {
|
||||
resp.TypeName = "envbuilder"
|
||||
resp.Version = p.version
|
||||
}
|
||||
|
||||
func (p *ScaffoldingProvider) Schema(ctx context.Context, req provider.SchemaRequest, resp *provider.SchemaResponse) {
|
||||
func (p *EnvbuilderProvider) Schema(ctx context.Context, req provider.SchemaRequest, resp *provider.SchemaResponse) {
|
||||
resp.Schema = schema.Schema{
|
||||
Attributes: map[string]schema.Attribute{
|
||||
"endpoint": schema.StringAttribute{
|
||||
MarkdownDescription: "Example provider attribute",
|
||||
Optional: true,
|
||||
},
|
||||
},
|
||||
Attributes: map[string]schema.Attribute{},
|
||||
}
|
||||
}
|
||||
|
||||
func (p *ScaffoldingProvider) Configure(ctx context.Context, req provider.ConfigureRequest, resp *provider.ConfigureResponse) {
|
||||
var data ScaffoldingProviderModel
|
||||
func (p *EnvbuilderProvider) Configure(ctx context.Context, req provider.ConfigureRequest, resp *provider.ConfigureResponse) {
|
||||
var data EnvbuilderProviderModel
|
||||
|
||||
resp.Diagnostics.Append(req.Config.Get(ctx, &data)...)
|
||||
|
||||
|
|
@ -66,27 +60,23 @@ func (p *ScaffoldingProvider) Configure(ctx context.Context, req provider.Config
|
|||
resp.ResourceData = client
|
||||
}
|
||||
|
||||
func (p *ScaffoldingProvider) Resources(ctx context.Context) []func() resource.Resource {
|
||||
return []func() resource.Resource{
|
||||
NewExampleResource,
|
||||
}
|
||||
func (p *EnvbuilderProvider) Resources(ctx context.Context) []func() resource.Resource {
|
||||
return []func() resource.Resource{}
|
||||
}
|
||||
|
||||
func (p *ScaffoldingProvider) DataSources(ctx context.Context) []func() datasource.DataSource {
|
||||
func (p *EnvbuilderProvider) DataSources(ctx context.Context) []func() datasource.DataSource {
|
||||
return []func() datasource.DataSource{
|
||||
NewExampleDataSource,
|
||||
NewCachedImageDataSource,
|
||||
}
|
||||
}
|
||||
|
||||
func (p *ScaffoldingProvider) Functions(ctx context.Context) []func() function.Function {
|
||||
return []func() function.Function{
|
||||
NewExampleFunction,
|
||||
}
|
||||
func (p *EnvbuilderProvider) Functions(ctx context.Context) []func() function.Function {
|
||||
return []func() function.Function{}
|
||||
}
|
||||
|
||||
func New(version string) func() provider.Provider {
|
||||
return func() provider.Provider {
|
||||
return &ScaffoldingProvider{
|
||||
return &EnvbuilderProvider{
|
||||
version: version,
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -15,7 +15,7 @@ import (
|
|||
// CLI command executed to create a provider server to which the CLI can
|
||||
// reattach.
|
||||
var testAccProtoV6ProviderFactories = map[string]func() (tfprotov6.ProviderServer, error){
|
||||
"scaffolding": providerserver.NewProtocol6WithError(New("test")()),
|
||||
"envbuilder": providerserver.NewProtocol6WithError(New("test")()),
|
||||
}
|
||||
|
||||
func testAccPreCheck(t *testing.T) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue