more base layout and setup
This commit is contained in:
parent
b7039c7fb9
commit
02c675c796
|
@ -0,0 +1,3 @@
|
||||||
|
*.test
|
||||||
|
.idea
|
||||||
|
.DS_Store
|
|
@ -3,13 +3,11 @@ package provider
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"git.davepedu.com/dave/terraform-provider-system/system"
|
||||||
|
|
||||||
"github.com/hashicorp/terraform-plugin-framework/path"
|
"github.com/hashicorp/terraform-plugin-framework/path"
|
||||||
"github.com/hashicorp/terraform-plugin-framework/resource"
|
"github.com/hashicorp/terraform-plugin-framework/resource"
|
||||||
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
|
"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/planmodifier"
|
||||||
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
|
|
||||||
"github.com/hashicorp/terraform-plugin-framework/types"
|
"github.com/hashicorp/terraform-plugin-framework/types"
|
||||||
"github.com/hashicorp/terraform-plugin-log/tflog"
|
"github.com/hashicorp/terraform-plugin-log/tflog"
|
||||||
)
|
)
|
||||||
|
@ -24,7 +22,7 @@ func NewDirResource() resource.Resource {
|
||||||
|
|
||||||
// DirResource defines the resource implementation.
|
// DirResource defines the resource implementation.
|
||||||
type DirResource struct {
|
type DirResource struct {
|
||||||
client *http.Client
|
client *system.SystemManager
|
||||||
}
|
}
|
||||||
|
|
||||||
// DirResourceModel describes the resource data model.
|
// DirResourceModel describes the resource data model.
|
||||||
|
@ -34,7 +32,7 @@ type DirResourceModel struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *DirResource) Metadata(ctx context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) {
|
func (r *DirResource) Metadata(ctx context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) {
|
||||||
resp.TypeName = req.ProviderTypeName + "_example"
|
resp.TypeName = req.ProviderTypeName + "_dir"
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *DirResource) Schema(ctx context.Context, req resource.SchemaRequest, resp *resource.SchemaResponse) {
|
func (r *DirResource) Schema(ctx context.Context, req resource.SchemaRequest, resp *resource.SchemaResponse) {
|
||||||
|
@ -43,15 +41,29 @@ func (r *DirResource) Schema(ctx context.Context, req resource.SchemaRequest, re
|
||||||
MarkdownDescription: "Example resource",
|
MarkdownDescription: "Example resource",
|
||||||
|
|
||||||
Attributes: map[string]schema.Attribute{
|
Attributes: map[string]schema.Attribute{
|
||||||
"configurable_attribute": schema.StringAttribute{
|
"path": schema.StringAttribute{
|
||||||
|
MarkdownDescription: "File path",
|
||||||
|
Required: true,
|
||||||
|
},
|
||||||
|
"mode": schema.StringAttribute{
|
||||||
MarkdownDescription: "Example configurable attribute",
|
MarkdownDescription: "Example configurable attribute",
|
||||||
Optional: true,
|
Optional: true,
|
||||||
},
|
|
||||||
"id": schema.StringAttribute{
|
|
||||||
Computed: true,
|
|
||||||
MarkdownDescription: "Example identifier",
|
|
||||||
PlanModifiers: []planmodifier.String{
|
PlanModifiers: []planmodifier.String{
|
||||||
stringplanmodifier.UseStateForUnknown(),
|
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
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
@ -64,7 +76,7 @@ func (r *DirResource) Configure(ctx context.Context, req resource.ConfigureReque
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
client, ok := req.ProviderData.(*http.Client)
|
client, ok := req.ProviderData.(*system.SystemManager)
|
||||||
|
|
||||||
if !ok {
|
if !ok {
|
||||||
resp.Diagnostics.AddError(
|
resp.Diagnostics.AddError(
|
||||||
|
|
|
@ -3,13 +3,11 @@ package provider
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"git.davepedu.com/dave/terraform-provider-system/system"
|
||||||
|
|
||||||
"github.com/hashicorp/terraform-plugin-framework/path"
|
"github.com/hashicorp/terraform-plugin-framework/path"
|
||||||
"github.com/hashicorp/terraform-plugin-framework/resource"
|
"github.com/hashicorp/terraform-plugin-framework/resource"
|
||||||
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
|
"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/planmodifier"
|
||||||
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
|
|
||||||
"github.com/hashicorp/terraform-plugin-framework/types"
|
"github.com/hashicorp/terraform-plugin-framework/types"
|
||||||
"github.com/hashicorp/terraform-plugin-log/tflog"
|
"github.com/hashicorp/terraform-plugin-log/tflog"
|
||||||
)
|
)
|
||||||
|
@ -24,7 +22,7 @@ func NewFileResource() resource.Resource {
|
||||||
|
|
||||||
// FileResource defines the resource implementation.
|
// FileResource defines the resource implementation.
|
||||||
type FileResource struct {
|
type FileResource struct {
|
||||||
client *http.Client
|
client *system.SystemManager
|
||||||
}
|
}
|
||||||
|
|
||||||
// FileResourceModel describes the resource data model.
|
// FileResourceModel describes the resource data model.
|
||||||
|
@ -34,26 +32,42 @@ type FileResourceModel struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *FileResource) Metadata(ctx context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) {
|
func (r *FileResource) Metadata(ctx context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) {
|
||||||
resp.TypeName = req.ProviderTypeName + "_example"
|
resp.TypeName = req.ProviderTypeName + "_file"
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *FileResource) Schema(ctx context.Context, req resource.SchemaRequest, resp *resource.SchemaResponse) {
|
func (r *FileResource) Schema(ctx context.Context, req resource.SchemaRequest, resp *resource.SchemaResponse) {
|
||||||
resp.Schema = schema.Schema{
|
resp.Schema = schema.Schema{
|
||||||
// This description is used by the documentation generator and the language server.
|
MarkdownDescription: "File resource",
|
||||||
MarkdownDescription: "Example resource",
|
|
||||||
|
|
||||||
Attributes: map[string]schema.Attribute{
|
Attributes: map[string]schema.Attribute{
|
||||||
"configurable_attribute": schema.StringAttribute{
|
"path": schema.StringAttribute{
|
||||||
|
MarkdownDescription: "File path",
|
||||||
|
Required: true,
|
||||||
|
},
|
||||||
|
"mode": schema.StringAttribute{
|
||||||
MarkdownDescription: "Example configurable attribute",
|
MarkdownDescription: "Example configurable attribute",
|
||||||
Optional: true,
|
Optional: true,
|
||||||
},
|
|
||||||
"id": schema.StringAttribute{
|
|
||||||
Computed: true,
|
|
||||||
MarkdownDescription: "Example identifier",
|
|
||||||
PlanModifiers: []planmodifier.String{
|
PlanModifiers: []planmodifier.String{
|
||||||
stringplanmodifier.UseStateForUnknown(),
|
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,
|
||||||
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -64,7 +78,7 @@ func (r *FileResource) Configure(ctx context.Context, req resource.ConfigureRequ
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
client, ok := req.ProviderData.(*http.Client)
|
client, ok := req.ProviderData.(*system.SystemManager)
|
||||||
|
|
||||||
if !ok {
|
if !ok {
|
||||||
resp.Diagnostics.AddError(
|
resp.Diagnostics.AddError(
|
||||||
|
|
|
@ -0,0 +1,42 @@
|
||||||
|
package provider
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
|
||||||
|
"github.com/hashicorp/terraform-plugin-framework/types"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Hashicorp, what the fuck are you making me write this shit for?. Give me back sdkv2 defaults. This is insane
|
||||||
|
|
||||||
|
func StringDefaultValue(v types.String) *stringDefaultValue {
|
||||||
|
return &stringDefaultValue{v}
|
||||||
|
}
|
||||||
|
|
||||||
|
type stringDefaultValue struct {
|
||||||
|
DefaultValue types.String
|
||||||
|
}
|
||||||
|
|
||||||
|
var _ planmodifier.String = (*stringDefaultValue)(nil)
|
||||||
|
|
||||||
|
func (s *stringDefaultValue) Description(ctx context.Context) string {
|
||||||
|
return "stringDefaultValue Description"
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *stringDefaultValue) MarkdownDescription(ctx context.Context) string {
|
||||||
|
return "stringDefaultValue MarkdownDescription"
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *stringDefaultValue) PlanModifyString(ctx context.Context, req planmodifier.StringRequest, res *planmodifier.StringResponse) {
|
||||||
|
// If the attribute configuration is not null, we are done here
|
||||||
|
if !req.ConfigValue.IsNull() {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// If the attribute plan is "known" and "not null", then a previous plan modifier in the sequence
|
||||||
|
// has already been applied, and we don't want to interfere.
|
||||||
|
if !req.PlanValue.IsUnknown() && !req.PlanValue.IsNull() {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
res.PlanValue = s.DefaultValue
|
||||||
|
}
|
|
@ -2,6 +2,8 @@ package provider
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"fmt"
|
||||||
|
"git.davepedu.com/dave/terraform-provider-system/system"
|
||||||
"github.com/hashicorp/terraform-plugin-framework/datasource"
|
"github.com/hashicorp/terraform-plugin-framework/datasource"
|
||||||
"github.com/hashicorp/terraform-plugin-framework/provider"
|
"github.com/hashicorp/terraform-plugin-framework/provider"
|
||||||
"github.com/hashicorp/terraform-plugin-framework/provider/schema"
|
"github.com/hashicorp/terraform-plugin-framework/provider/schema"
|
||||||
|
@ -22,7 +24,7 @@ type SystemProvider struct {
|
||||||
|
|
||||||
// SystemProviderModel describes the provider data model.
|
// SystemProviderModel describes the provider data model.
|
||||||
type SystemProviderModel struct {
|
type SystemProviderModel struct {
|
||||||
Endpoint types.String `tfsdk:"connection_string"`
|
ConnectionString types.String `tfsdk:"connection_string"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *SystemProvider) Metadata(ctx context.Context, req provider.MetadataRequest, resp *provider.MetadataResponse) {
|
func (p *SystemProvider) Metadata(ctx context.Context, req provider.MetadataRequest, resp *provider.MetadataResponse) {
|
||||||
|
@ -35,7 +37,7 @@ func (p *SystemProvider) Schema(ctx context.Context, req provider.SchemaRequest,
|
||||||
Attributes: map[string]schema.Attribute{
|
Attributes: map[string]schema.Attribute{
|
||||||
"connection_string": schema.StringAttribute{
|
"connection_string": schema.StringAttribute{
|
||||||
MarkdownDescription: "SSH connection string",
|
MarkdownDescription: "SSH connection string",
|
||||||
Optional: false,
|
Required: true,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
@ -50,13 +52,21 @@ func (p *SystemProvider) Configure(ctx context.Context, req provider.ConfigureRe
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Configuration values are now available.
|
client, err := system.NewSystemManagerFromUri(data.ConnectionString.String())
|
||||||
// if data.Endpoint.IsNull() { /* ... */ }
|
if err != nil {
|
||||||
|
resp.Diagnostics.AddError(
|
||||||
// Example client configuration for data sources and resources
|
"Unable to create System Manager API client",
|
||||||
/*client := http.DefaultClient
|
fmt.Sprintf("%s", err),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
if err := client.Test(); err != nil {
|
||||||
|
resp.Diagnostics.AddError(
|
||||||
|
"System Manager API Client connection test failed",
|
||||||
|
fmt.Sprintf("%s", err),
|
||||||
|
)
|
||||||
|
}
|
||||||
resp.DataSourceData = client
|
resp.DataSourceData = client
|
||||||
resp.ResourceData = client*/
|
resp.ResourceData = client
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *SystemProvider) Resources(ctx context.Context) []func() resource.Resource {
|
func (p *SystemProvider) Resources(ctx context.Context) []func() resource.Resource {
|
||||||
|
|
|
@ -0,0 +1,28 @@
|
||||||
|
package system
|
||||||
|
|
||||||
|
// SystemManager providers the functionality to interrogate / interact with target systems
|
||||||
|
type SystemManager struct {
|
||||||
|
connection *SystemConnection
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewSystemManagerFromUri(uri string) (*SystemManager, error) {
|
||||||
|
conn, err := NewSystemConnectionFromUri(uri)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return &SystemManager{
|
||||||
|
connection: conn,
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *SystemManager) Test() error {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// SystemConnection provides an unopinionated interface for interacting with remote systems on a low level
|
||||||
|
type SystemConnection struct {
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewSystemConnectionFromUri(uri string) (*SystemConnection, error) {
|
||||||
|
return nil, nil
|
||||||
|
}
|
Loading…
Reference in New Issue