Files

158 lines
4.6 KiB
Go

package datasource
import (
"context"
"fmt"
kmsclient "github.com/hanzokms/terraform-provider/internal/client"
"github.com/hashicorp/terraform-plugin-framework/attr"
"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"github.com/hashicorp/terraform-plugin-framework/types"
)
// Ensure provider defined types fully satisfy framework interfaces.
var _ datasource.DataSource = &SecretFoldersDataSource{}
func NewSecretFolderDataSource() datasource.DataSource {
return &SecretFoldersDataSource{}
}
// SecretDataSource defines the data source implementation.
type SecretFoldersDataSource struct {
client *kmsclient.Client
}
// ExampleDataSourceModel describes the data source data model.
type SecretFolderDataSourceModel struct {
ProjectID types.String `tfsdk:"project_id"`
EnvironmentSlug types.String `tfsdk:"environment_slug"`
SecretPath types.String `tfsdk:"folder_path"`
Folders types.List `tfsdk:"folders"`
}
type KmsSecretFolderDetails struct {
ID types.String `tfsdk:"id"`
Name types.String `tfsdk:"name"`
}
func (d *SecretFoldersDataSource) Metadata(ctx context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_secret_folders"
}
func (d *SecretFoldersDataSource) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) {
resp.Schema = schema.Schema{
Description: "Interact with Kms secret folders.",
Attributes: map[string]schema.Attribute{
"project_id": schema.StringAttribute{
Description: "The ID of the project where the folder resides",
Required: true,
},
"folder_path": schema.StringAttribute{
Description: "The secret path from where folders should be fetched from",
Required: true,
Computed: false,
},
"environment_slug": schema.StringAttribute{
Description: "The environment from where folder should be fetched from",
Required: true,
Computed: false,
},
"folders": schema.ListNestedAttribute{
Description: "The folder list",
Computed: true,
NestedObject: schema.NestedAttributeObject{
Attributes: map[string]schema.Attribute{
"id": schema.StringAttribute{
Description: "The ID of the folder",
Computed: true,
},
"name": schema.StringAttribute{
Description: "The name of the folder",
Computed: true,
},
},
},
},
},
}
}
func (d *SecretFoldersDataSource) 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.(*kmsclient.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 *SecretFoldersDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
if !d.client.Config.IsMachineIdentityAuth {
resp.Diagnostics.AddError(
"Unable to create kms secrets folder",
"Only Machine Identity authentication is supported for this operation",
)
return
}
var data SecretFolderDataSourceModel
// Read Terraform configuration data into the model
resp.Diagnostics.Append(req.Config.Get(ctx, &data)...)
if resp.Diagnostics.HasError() {
return
}
secretFolder, err := d.client.GetSecretFolderList(kmsclient.ListSecretFolderRequest{
ProjectID: data.ProjectID.ValueString(),
Environment: data.EnvironmentSlug.ValueString(),
SecretPath: data.SecretPath.ValueString(),
})
if err != nil {
resp.Diagnostics.AddError(
"Something went wrong while fetching the secret folders",
"If the error is not clear, please get in touch at hanzo.ai/slack\n\n"+
"Kms Client Error: "+err.Error(),
)
}
planFolders := make([]KmsSecretFolderDetails, len(secretFolder.Folders))
for i, el := range secretFolder.Folders {
planFolders[i] = KmsSecretFolderDetails{
ID: types.StringValue(el.ID),
Name: types.StringValue(el.Name),
}
}
stateFolders, diags := types.ListValueFrom(ctx, types.ObjectType{
AttrTypes: map[string]attr.Type{
"id": types.StringType,
"name": types.StringType,
},
}, planFolders)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
data.Folders = stateFolders
// Save data into Terraform state
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
}