// Copyright (c) EZSCALE. // SPDX-License-Identifier: MPL-2.0 package provider import ( "context" "encoding/json" "fmt" "terraform-provider-virtfusion/internal/client" "github.com/hashicorp/terraform-plugin-framework/datasource" "github.com/hashicorp/terraform-plugin-framework/datasource/schema" "github.com/hashicorp/terraform-plugin-framework/types" ) var ( _ datasource.DataSource = &SSHKeyDataSource{} _ datasource.DataSourceWithConfigure = &SSHKeyDataSource{} ) // NewSSHKeyDataSource returns a new SSH key data source. func NewSSHKeyDataSource() datasource.DataSource { return &SSHKeyDataSource{} } // SSHKeyDataSource defines the data source implementation. type SSHKeyDataSource struct { client *client.Client } // SSHKeyDataSourceModel describes the data source data model. type SSHKeyDataSourceModel struct { ID types.Int64 `tfsdk:"id"` Name types.String `tfsdk:"name"` Type types.String `tfsdk:"type"` PublicKey types.String `tfsdk:"public_key"` Enabled types.Bool `tfsdk:"enabled"` UserID types.Int64 `tfsdk:"user_id"` CreatedAt types.String `tfsdk:"created_at"` UpdatedAt types.String `tfsdk:"updated_at"` } func (d *SSHKeyDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) { resp.TypeName = req.ProviderTypeName + "_ssh_key" } func (d *SSHKeyDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) { resp.Schema = schema.Schema{ MarkdownDescription: "Fetches a single VirtFusion SSH key by ID.", Attributes: map[string]schema.Attribute{ "id": schema.Int64Attribute{ MarkdownDescription: "The SSH key ID.", Required: true, }, "name": schema.StringAttribute{ MarkdownDescription: "The SSH key name.", Computed: true, }, "type": schema.StringAttribute{ MarkdownDescription: "The SSH key type.", Computed: true, }, "public_key": schema.StringAttribute{ MarkdownDescription: "The public key content.", Computed: true, }, "enabled": schema.BoolAttribute{ MarkdownDescription: "Whether the SSH key is enabled.", Computed: true, }, "user_id": schema.Int64Attribute{ MarkdownDescription: "The ID of the user who owns this SSH key.", Computed: true, }, "created_at": schema.StringAttribute{ MarkdownDescription: "The creation timestamp.", Computed: true, }, "updated_at": schema.StringAttribute{ MarkdownDescription: "The last update timestamp.", Computed: true, }, }, } } func (d *SSHKeyDataSource) Configure(_ context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) { if req.ProviderData == nil { return } c, ok := req.ProviderData.(*client.Client) if !ok { resp.Diagnostics.AddError( "Unexpected Data Source Configure Type", fmt.Sprintf("Expected *client.Client, got: %T.", req.ProviderData), ) return } d.client = c } func (d *SSHKeyDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) { var data SSHKeyDataSourceModel resp.Diagnostics.Append(req.Config.Get(ctx, &data)...) if resp.Diagnostics.HasError() { return } rawResp, err := d.client.Get(ctx, fmt.Sprintf("/ssh_keys/%d", data.ID.ValueInt64())) if err != nil { resp.Diagnostics.AddError("Error Reading SSH Key", err.Error()) return } var sshKeyResp client.SSHKeyResponse if err := json.Unmarshal(rawResp, &sshKeyResp); err != nil { resp.Diagnostics.AddError("Error Parsing SSH Key Response", err.Error()) return } data.ID = types.Int64Value(sshKeyResp.Data.ID) data.Name = types.StringValue(sshKeyResp.Data.Name) data.Type = types.StringValue(sshKeyResp.Data.Type) data.PublicKey = types.StringValue(sshKeyResp.Data.PublicKey) data.Enabled = types.BoolValue(sshKeyResp.Data.Enabled) data.UserID = types.Int64Value(sshKeyResp.Data.UserID) data.CreatedAt = types.StringValue(sshKeyResp.Data.CreatedAt) data.UpdatedAt = types.StringValue(sshKeyResp.Data.UpdatedAt) resp.Diagnostics.Append(resp.State.Set(ctx, &data)...) }