248 lines
7.1 KiB
Go
248 lines
7.1 KiB
Go
package provider
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"git.davepedu.com/dave/terraform-provider-system/system"
|
|
"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/types"
|
|
"github.com/hashicorp/terraform-plugin-log/tflog"
|
|
)
|
|
|
|
// Ensure provider defined types fully satisfy framework interfaces.
|
|
var _ resource.Resource = &FileResource{}
|
|
var _ resource.ResourceWithImportState = &FileResource{}
|
|
|
|
func NewFileResource() resource.Resource {
|
|
return &FileResource{}
|
|
}
|
|
|
|
// FileResource defines the resource implementation.
|
|
type FileResource struct {
|
|
client *system.SystemManager
|
|
}
|
|
|
|
// FileResourceModel describes the resource data model.
|
|
type FileResourceModel struct {
|
|
ConfigurableAttribute types.String `tfsdk:"configurable_attribute"`
|
|
Id types.String `tfsdk:"id"`
|
|
}
|
|
|
|
func (r *FileResource) Metadata(ctx context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) {
|
|
resp.TypeName = req.ProviderTypeName + "_file"
|
|
}
|
|
|
|
func (r *FileResource) Schema(ctx context.Context, req resource.SchemaRequest, resp *resource.SchemaResponse) {
|
|
resp.Schema = schema.Schema{
|
|
MarkdownDescription: "File resource",
|
|
Attributes: map[string]schema.Attribute{
|
|
"path": schema.StringAttribute{
|
|
MarkdownDescription: "File path",
|
|
Required: true,
|
|
},
|
|
"mode": schema.StringAttribute{
|
|
MarkdownDescription: "Example configurable attribute",
|
|
Optional: true,
|
|
PlanModifiers: []planmodifier.String{
|
|
StringDefaultValue(types.StringValue("0755")),
|
|
},
|
|
},
|
|
"owner": schema.StringAttribute{
|
|
MarkdownDescription: "Example configurable attribute",
|
|
Optional: true,
|
|
PlanModifiers: []planmodifier.String{
|
|
StringDefaultValue(types.StringValue("root")), // TODO we won't always run as root, default to current user
|
|
},
|
|
},
|
|
"group": schema.StringAttribute{
|
|
MarkdownDescription: "Example configurable attribute",
|
|
Optional: true,
|
|
PlanModifiers: []planmodifier.String{
|
|
StringDefaultValue(types.StringValue("root")), // TODO we won't always run as root, default to current user
|
|
},
|
|
},
|
|
"content": schema.StringAttribute{
|
|
MarkdownDescription: "Example configurable attribute",
|
|
Required: true,
|
|
},
|
|
},
|
|
}
|
|
}
|
|
|
|
func (r *FileResource) 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.(*system.SystemManager)
|
|
|
|
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 *FileResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
|
|
var data *FileResourceModel
|
|
|
|
// 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 *FileResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) {
|
|
var data *FileResourceModel
|
|
|
|
// 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 *FileResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) {
|
|
var data *FileResourceModel
|
|
|
|
// 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 *FileResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) {
|
|
var data *FileResourceModel
|
|
|
|
// 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 *FileResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) {
|
|
resource.ImportStatePassthroughID(ctx, path.Root("id"), req, resp)
|
|
}
|
|
|
|
/*
|
|
package resources
|
|
|
|
import (
|
|
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
|
|
)
|
|
|
|
func ResourceFile() *schema.Resource {
|
|
return &schema.Resource{
|
|
Create: resourceFileCreate,
|
|
Read: resourceFileRead,
|
|
Delete: resourceFileDelete,
|
|
Importer: &schema.ResourceImporter{
|
|
State: ipamBlockImport,
|
|
},
|
|
Schema: map[string]*schema.Schema{
|
|
"path": {
|
|
Type: schema.TypeString,
|
|
Required: true,
|
|
ForceNew: true,
|
|
},
|
|
"mode": {
|
|
Type: schema.TypeInt,
|
|
Optional: true,
|
|
ForceNew: false,
|
|
Default: "0755",
|
|
},
|
|
"owner": {
|
|
Type: schema.TypeString,
|
|
Optional: true,
|
|
ForceNew: false,
|
|
Default: "root",
|
|
},
|
|
"group": {
|
|
Type: schema.TypeString,
|
|
Optional: true,
|
|
ForceNew: false,
|
|
Default: "root",
|
|
},
|
|
},
|
|
}
|
|
}
|
|
|
|
func resourceFileCreate(d *schema.ResourceData, m interface{}) error {
|
|
|
|
return nil
|
|
}
|
|
|
|
func resourceFileRead(d *schema.ResourceData, m interface{}) error {
|
|
return nil
|
|
}
|
|
|
|
func resourceFileDelete(d *schema.ResourceData, m interface{}) error {
|
|
return nil
|
|
}
|
|
*/
|