Files
terraform-provider-virtfusion/internal/provider/data_source_ssh_keys_by_user.go
Andrew 39c8cf0b58
Some checks failed
CI / build (push) Failing after 34s
Fix golangci-lint: formatting, nilerr false positive, deprecated linter names
- gofmt: fix struct field alignment in types.go, resource_server.go,
  data_source_ssh_keys_by_user.go
- nilerr: refactor GetAllPages pagination detection to avoid returning
  nil error when json.Unmarshal fails (intentional passthrough for
  non-paginated responses)
- .golangci.yml: replace deprecated linter names (vet -> govet,
  tenv -> usetesting)

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-16 02:17:13 -04:00

143 lines
4.4 KiB
Go

// 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 = &SSHKeysByUserDataSource{}
_ datasource.DataSourceWithConfigure = &SSHKeysByUserDataSource{}
)
// NewSSHKeysByUserDataSource returns a new SSH keys by user data source.
func NewSSHKeysByUserDataSource() datasource.DataSource {
return &SSHKeysByUserDataSource{}
}
// SSHKeysByUserDataSource defines the data source implementation.
type SSHKeysByUserDataSource struct {
client *client.Client
}
// SSHKeysByUserDataSourceModel describes the data source data model.
type SSHKeysByUserDataSourceModel struct {
UserID types.Int64 `tfsdk:"user_id"`
Results types.Int64 `tfsdk:"results"`
SSHKeys []SSHKeyByUserItemModel `tfsdk:"ssh_keys"`
}
// SSHKeyByUserItemModel describes a single SSH key in the list.
type SSHKeyByUserItemModel 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"`
}
func (d *SSHKeysByUserDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_ssh_keys_by_user"
}
func (d *SSHKeysByUserDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) {
resp.Schema = schema.Schema{
MarkdownDescription: "Fetches all SSH keys for a VirtFusion user.",
Attributes: map[string]schema.Attribute{
"user_id": schema.Int64Attribute{
MarkdownDescription: "The user ID to fetch SSH keys for.",
Required: true,
},
"results": resultsSchemaAttribute(),
"ssh_keys": schema.ListNestedAttribute{
MarkdownDescription: "List of SSH keys belonging to the user.",
Computed: true,
NestedObject: schema.NestedAttributeObject{
Attributes: map[string]schema.Attribute{
"id": schema.Int64Attribute{
MarkdownDescription: "The SSH key ID.",
Computed: 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,
},
},
},
},
},
}
}
func (d *SSHKeysByUserDataSource) 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 *SSHKeysByUserDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
var data SSHKeysByUserDataSourceModel
resp.Diagnostics.Append(req.Config.Get(ctx, &data)...)
if resp.Diagnostics.HasError() {
return
}
rawResp, err := d.client.GetAllPages(ctx, fmt.Sprintf("/ssh_keys/user/%d?%s", data.UserID.ValueInt64(), resultsQueryParam(data.Results)))
if err != nil {
resp.Diagnostics.AddError("Error Reading SSH Keys By User", err.Error())
return
}
var listResp client.SSHKeyListResponse
if err := json.Unmarshal(rawResp, &listResp); err != nil {
resp.Diagnostics.AddError("Error Parsing SSH Keys Response", err.Error())
return
}
data.SSHKeys = make([]SSHKeyByUserItemModel, len(listResp.Data))
for i, k := range listResp.Data {
data.SSHKeys[i] = SSHKeyByUserItemModel{
ID: types.Int64Value(k.ID),
Name: types.StringValue(k.Name),
Type: types.StringValue(k.Type),
PublicKey: types.StringValue(k.PublicKey),
Enabled: types.BoolValue(k.Enabled),
}
}
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
}