|
| 1 | +package rdp |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + |
| 7 | + "github.com/hashicorp/terraform-plugin-framework/datasource" |
| 8 | + "github.com/hashicorp/terraform-plugin-framework/datasource/schema" |
| 9 | + "github.com/hashicorp/terraform-plugin-framework/schema/validator" |
| 10 | + "github.com/hashicorp/terraform-plugin-framework/types" |
| 11 | + "github.com/hashicorp/terraform-plugin-log/tflog" |
| 12 | + |
| 13 | + "terraform-provider-kasm/internal/client" |
| 14 | + "terraform-provider-kasm/internal/validators" |
| 15 | +) |
| 16 | + |
| 17 | +// Ensure the implementation satisfies the expected interfaces. |
| 18 | +var ( |
| 19 | + _ datasource.DataSource = &rdpClientConnectionInfoDataSource{} |
| 20 | +) |
| 21 | + |
| 22 | +// NewRDPClientConnectionInfoDataSource is a helper function to simplify the provider implementation. |
| 23 | +func NewRDPClientConnectionInfoDataSource() datasource.DataSource { |
| 24 | + return &rdpClientConnectionInfoDataSource{ |
| 25 | + client: &client.Client{}, |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +// rdpClientConnectionInfoDataSource is the data source implementation. |
| 30 | +type rdpClientConnectionInfoDataSource struct { |
| 31 | + client *client.Client |
| 32 | +} |
| 33 | + |
| 34 | +// Metadata returns the data source type name. |
| 35 | +func (d *rdpClientConnectionInfoDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) { |
| 36 | + resp.TypeName = req.ProviderTypeName + "_rdp_client_connection_info" |
| 37 | +} |
| 38 | + |
| 39 | +// Schema defines the schema for the data source. |
| 40 | +func (d *rdpClientConnectionInfoDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) { |
| 41 | + resp.Schema = schema.Schema{ |
| 42 | + Description: "Retrieves RDP client connection information for a Kasm session.", |
| 43 | + Attributes: map[string]schema.Attribute{ |
| 44 | + "id": schema.StringAttribute{ |
| 45 | + Computed: true, |
| 46 | + Description: "Identifier of the RDP client connection info.", |
| 47 | + }, |
| 48 | + "user_id": schema.StringAttribute{ |
| 49 | + Required: true, |
| 50 | + Description: "The ID of the user for which to retrieve the RDP connection info.", |
| 51 | + }, |
| 52 | + "kasm_id": schema.StringAttribute{ |
| 53 | + Required: true, |
| 54 | + Description: "The ID of the Kasm session for which to retrieve the RDP connection info.", |
| 55 | + }, |
| 56 | + "connection_type": schema.StringAttribute{ |
| 57 | + Optional: true, |
| 58 | + Description: "Type of connection to retrieve (url or file)", |
| 59 | + Validators: []validator.String{ |
| 60 | + validators.StringOneOf("url", "file"), |
| 61 | + }, |
| 62 | + }, |
| 63 | + "file": schema.StringAttribute{ |
| 64 | + Computed: true, |
| 65 | + Description: "The RDP file content (if connection_type is 'file' or not specified).", |
| 66 | + }, |
| 67 | + "url": schema.StringAttribute{ |
| 68 | + Computed: true, |
| 69 | + Description: "The RDP URL (if connection_type is 'url').", |
| 70 | + }, |
| 71 | + }, |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +// Configure adds the provider configured client to the data source. |
| 76 | +func (d *rdpClientConnectionInfoDataSource) Configure(_ context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) { |
| 77 | + if req.ProviderData == nil { |
| 78 | + return |
| 79 | + } |
| 80 | + |
| 81 | + client, ok := req.ProviderData.(*client.Client) |
| 82 | + if !ok { |
| 83 | + resp.Diagnostics.AddError( |
| 84 | + "Unexpected Data Source Configure Type", |
| 85 | + fmt.Sprintf("Expected *client.Client, got: %T. Please report this issue to the provider developers.", req.ProviderData), |
| 86 | + ) |
| 87 | + return |
| 88 | + } |
| 89 | + |
| 90 | + d.client = client |
| 91 | +} |
| 92 | + |
| 93 | +// Read refreshes the Terraform state with the latest data. |
| 94 | +func (d *rdpClientConnectionInfoDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) { |
| 95 | + var state rdpClientConnectionInfoModel |
| 96 | + |
| 97 | + // Read Terraform configuration data into the model |
| 98 | + resp.Diagnostics.Append(req.Config.Get(ctx, &state)...) |
| 99 | + if resp.Diagnostics.HasError() { |
| 100 | + return |
| 101 | + } |
| 102 | + |
| 103 | + // Get the connection info from the API |
| 104 | + connectionType := client.RDPConnectionTypeFile |
| 105 | + if !state.ConnectionType.IsNull() && state.ConnectionType.ValueString() == "url" { |
| 106 | + connectionType = client.RDPConnectionTypeURL |
| 107 | + } |
| 108 | + |
| 109 | + tflog.Debug(ctx, fmt.Sprintf("Getting RDP connection info for user %s, kasm %s, type %s", state.UserID.ValueString(), state.KasmID.ValueString(), connectionType)) |
| 110 | + connectionInfo, err := d.client.GetRDPConnectionInfo(state.UserID.ValueString(), state.KasmID.ValueString(), connectionType) |
| 111 | + if err != nil { |
| 112 | + resp.Diagnostics.AddError( |
| 113 | + "Error getting RDP connection info", |
| 114 | + fmt.Sprintf("Could not get RDP connection info: %v", err), |
| 115 | + ) |
| 116 | + return |
| 117 | + } |
| 118 | + |
| 119 | + // Set the ID |
| 120 | + state.ID = types.StringValue(fmt.Sprintf("%s-%s-%s", state.UserID.ValueString(), state.KasmID.ValueString(), connectionType)) |
| 121 | + |
| 122 | + // Set the connection info |
| 123 | + state.File = types.StringValue(connectionInfo.File) |
| 124 | + state.URL = types.StringValue(connectionInfo.URL) |
| 125 | + |
| 126 | + // Save the data into Terraform state |
| 127 | + resp.Diagnostics.Append(resp.State.Set(ctx, &state)...) |
| 128 | +} |
| 129 | + |
| 130 | +// rdpClientConnectionInfoModel maps the data source schema data. |
| 131 | +type rdpClientConnectionInfoModel struct { |
| 132 | + ID types.String `tfsdk:"id"` |
| 133 | + UserID types.String `tfsdk:"user_id"` |
| 134 | + KasmID types.String `tfsdk:"kasm_id"` |
| 135 | + ConnectionType types.String `tfsdk:"connection_type"` |
| 136 | + File types.String `tfsdk:"file"` |
| 137 | + URL types.String `tfsdk:"url"` |
| 138 | +} |
0 commit comments