From 5c54613cea51a8d864c6cb4435b56dd740453044 Mon Sep 17 00:00:00 2001 From: s1ntaxe770r Date: Wed, 8 May 2024 16:41:27 +0100 Subject: [PATCH] feat: add secret data sources feat: add secret data sources --- docs/data-sources/certificate_secret.md | 35 +++++ docs/data-sources/registry_secret.md | 35 +++++ internal/provider/provider.go | 2 + .../datasource_certificate_secret.go | 140 +++++++++++++++++ .../resources/datasource_registry_secret.go | 141 ++++++++++++++++++ 5 files changed, 353 insertions(+) create mode 100644 docs/data-sources/certificate_secret.md create mode 100644 docs/data-sources/registry_secret.md create mode 100644 internal/resources/datasource_certificate_secret.go create mode 100644 internal/resources/datasource_registry_secret.go diff --git a/docs/data-sources/certificate_secret.md b/docs/data-sources/certificate_secret.md new file mode 100644 index 0000000..f5e72e9 --- /dev/null +++ b/docs/data-sources/certificate_secret.md @@ -0,0 +1,35 @@ +--- +# generated by https://github.com/hashicorp/terraform-plugin-docs +page_title: "qernal_certificate_secret Data Source - qernal" +subcategory: "" +description: |- + +--- + +# qernal_certificate_secret (Data Source) + + + + + + +## Schema + +### Required + +- `name` (String) Name of the certficate +- `project_id` (String) + +### Read-Only + +- `certificate` (String) Public key of the certificate +- `date` (Attributes) (see [below for nested schema](#nestedatt--date)) +- `revision` (Number) + + +### Nested Schema for `date` + +Optional: + +- `created_at` (String) +- `updated_at` (String) diff --git a/docs/data-sources/registry_secret.md b/docs/data-sources/registry_secret.md new file mode 100644 index 0000000..62a13d7 --- /dev/null +++ b/docs/data-sources/registry_secret.md @@ -0,0 +1,35 @@ +--- +# generated by https://github.com/hashicorp/terraform-plugin-docs +page_title: "qernal_registry_secret Data Source - qernal" +subcategory: "" +description: |- + +--- + +# qernal_registry_secret (Data Source) + + + + + + +## Schema + +### Required + +- `name` (String) Name of the registry secret +- `project_id` (String) + +### Read-Only + +- `date` (Attributes) (see [below for nested schema](#nestedatt--date)) +- `registry` (String) url of the registry +- `revision` (Number) + + +### Nested Schema for `date` + +Optional: + +- `created_at` (String) +- `updated_at` (String) diff --git a/internal/provider/provider.go b/internal/provider/provider.go index 48ea834..36b2cd5 100644 --- a/internal/provider/provider.go +++ b/internal/provider/provider.go @@ -150,7 +150,9 @@ func (p *qernalProvider) Configure(ctx context.Context, req provider.ConfigureRe func (p *qernalProvider) DataSources(_ context.Context) []func() datasource.DataSource { return []func() datasource.DataSource{ + qernalresource.NewcertificateDataSource, qernalresource.NewenvironmentDataSource, + qernalresource.NewregistryDataSource, } } diff --git a/internal/resources/datasource_certificate_secret.go b/internal/resources/datasource_certificate_secret.go new file mode 100644 index 0000000..fb6f108 --- /dev/null +++ b/internal/resources/datasource_certificate_secret.go @@ -0,0 +1,140 @@ +package resources + +import ( + "context" + "fmt" + qernalclient "terraform-provider-qernal/internal/client" + + "github.com/hashicorp/terraform-plugin-framework/datasource" + "github.com/hashicorp/terraform-plugin-framework/datasource/schema" + "github.com/hashicorp/terraform-plugin-framework/types" + "github.com/hashicorp/terraform-plugin-framework/types/basetypes" +) + +// Ensure the implementation satisfies the expected interfaces. +var ( + _ datasource.DataSource = &certificateDataSource{} +) + +func NewcertificateDataSource() datasource.DataSource { + return &certificateDataSource{} +} + +type certificateDataSource struct { + client qernalclient.QernalAPIClient +} + +func (r *certificateDataSource) Configure(ctx context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) { + if req.ProviderData == nil { + return + } + + client, ok := req.ProviderData.(qernalclient.QernalAPIClient) + + if !ok { + resp.Diagnostics.AddError( + "Unexpected Data Source Configure Type", + fmt.Sprintf("Expected client.QernalAPIClient, got: %T. Please report this issue to the provider developers.", req.ProviderData), + ) + return + } + r.client = client +} + +// Metadata returns the data source type name. +func (d *certificateDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) { + resp.TypeName = req.ProviderTypeName + "_certificate_secret" +} + +// Schema defines the schema for the data source. +func (d *certificateDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) { + resp.Schema = schema.Schema{ + Attributes: map[string]schema.Attribute{ + "project_id": schema.StringAttribute{ + Required: true, + }, + + "name": schema.StringAttribute{ + Required: true, + Description: "Name of the certficate", + }, + + "certificate": schema.StringAttribute{ + Computed: true, + Description: "Public key of the certificate", + }, + + "revision": schema.Int64Attribute{ + Computed: true, + Required: false, + }, + "date": schema.SingleNestedAttribute{ + Computed: true, + Required: false, + Attributes: map[string]schema.Attribute{ + "created_at": schema.StringAttribute{ + Optional: true, + }, + "updated_at": schema.StringAttribute{ + Optional: true, + }, + }, + }, + }, + } +} + +// Read refreshes the Terraform data with the latest data. +func (d *certificateDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) { + + var data certificatesecretDataSourceModel + + // Read Terraform configuration data into the model + + diags := req.Config.Get(ctx, &data) + resp.Diagnostics.Append(diags...) + + if resp.Diagnostics.HasError() { + return + } + + secret, httpRes, err := d.client.SecretsAPI.ProjectsSecretsGet(ctx, data.ProjectID.ValueString(), data.Name.ValueString()).Execute() + + if err != nil { + + resData, _ := qernalclient.ParseResponseData(httpRes) + resp.Diagnostics.AddError( + "Error creating Secret", + "Could not create Secret, unexpected error: "+err.Error()+" with"+fmt.Sprintf(", detail: %v", resData)) + return + } + + data.Name = types.StringValue(secret.Name) + + data.ProjectID = types.StringValue(data.ProjectID.ValueString()) + + data.Revision = types.Int64Value(int64(secret.Revision)) + + data.Certificate = types.StringValue(secret.Payload.SecretMetaResponseCertificatePayload.Certificate) + date := resourceDate{ + CreatedAt: secret.Date.CreatedAt, + UpdatedAt: secret.Date.UpdatedAt, + } + data.Date = date.GetDateObject() + + // Set refreshed data + diags = resp.State.Set(ctx, &data) + resp.Diagnostics.Append(diags...) + if resp.Diagnostics.HasError() { + return + } + +} + +type certificatesecretDataSourceModel struct { + ProjectID types.String `tfsdk:"project_id"` + Name types.String `tfsdk:"name"` + Certificate types.String `tfsdk:"certificate"` + Revision types.Int64 `tfsdk:"revision"` + Date basetypes.ObjectValue `tfsdk:"date"` +} diff --git a/internal/resources/datasource_registry_secret.go b/internal/resources/datasource_registry_secret.go new file mode 100644 index 0000000..94b2c96 --- /dev/null +++ b/internal/resources/datasource_registry_secret.go @@ -0,0 +1,141 @@ +package resources + +import ( + "context" + "fmt" + qernalclient "terraform-provider-qernal/internal/client" + + "github.com/hashicorp/terraform-plugin-framework/datasource" + "github.com/hashicorp/terraform-plugin-framework/datasource/schema" + "github.com/hashicorp/terraform-plugin-framework/types" + "github.com/hashicorp/terraform-plugin-framework/types/basetypes" +) + +// Ensure the implementation satisfies the expected interfaces. +var ( + _ datasource.DataSource = ®istryDataSource{} +) + +func NewregistryDataSource() datasource.DataSource { + return ®istryDataSource{} +} + +type registryDataSource struct { + client qernalclient.QernalAPIClient +} + +func (r *registryDataSource) Configure(ctx context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) { + if req.ProviderData == nil { + return + } + + client, ok := req.ProviderData.(qernalclient.QernalAPIClient) + + if !ok { + resp.Diagnostics.AddError( + "Unexpected Data Source Configure Type", + fmt.Sprintf("Expected client.QernalAPIClient, got: %T. Please report this issue to the provider developers.", req.ProviderData), + ) + return + } + r.client = client +} + +// Metadata returns the data source type name. +func (d *registryDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) { + resp.TypeName = req.ProviderTypeName + "_registry_secret" +} + +// Schema defines the schema for the data source. +func (d *registryDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) { + resp.Schema = schema.Schema{ + Attributes: map[string]schema.Attribute{ + "project_id": schema.StringAttribute{ + Required: true, + }, + + "name": schema.StringAttribute{ + Required: true, + Description: "Name of the registry secret", + }, + + "registry": schema.StringAttribute{ + Computed: true, + Description: "url of the registry", + }, + + "revision": schema.Int64Attribute{ + Computed: true, + Required: false, + }, + "date": schema.SingleNestedAttribute{ + Computed: true, + Required: false, + Attributes: map[string]schema.Attribute{ + "created_at": schema.StringAttribute{ + Optional: true, + }, + "updated_at": schema.StringAttribute{ + Optional: true, + }, + }, + }, + }, + } +} + +// Read refreshes the Terraform data with the latest data. +func (d *registryDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) { + + var data registrysecretDataSourceModel + + // Read Terraform configuration data into the model + + diags := req.Config.Get(ctx, &data) + resp.Diagnostics.Append(diags...) + + if resp.Diagnostics.HasError() { + return + } + + secret, httpRes, err := d.client.SecretsAPI.ProjectsSecretsGet(ctx, data.ProjectID.ValueString(), data.Name.ValueString()).Execute() + + if err != nil { + + resData, _ := qernalclient.ParseResponseData(httpRes) + resp.Diagnostics.AddError( + "Error retreivng Secret", + "Could not get Secret, unexpected error: "+err.Error()+" with"+fmt.Sprintf(", detail: %v", resData)) + return + } + + data.Name = types.StringValue(secret.Name) + + data.ProjectID = types.StringValue(data.ProjectID.ValueString()) + + data.Registry = types.StringValue(secret.Payload.SecretMetaResponseRegistryPayload.Registry) + + data.Revision = types.Int64Value(int64(secret.Revision)) + + date := resourceDate{ + CreatedAt: secret.Date.CreatedAt, + UpdatedAt: secret.Date.UpdatedAt, + } + data.Date = date.GetDateObject() + + // Set refreshed data + diags = resp.State.Set(ctx, &data) + resp.Diagnostics.Append(diags...) + if resp.Diagnostics.HasError() { + return + } + +} + +type registrysecretDataSourceModel struct { + ProjectID types.String `tfsdk:"project_id"` + Name types.String `tfsdk:"name"` + Registry types.String `tfsdk:"registry"` + Revision types.Int64 `tfsdk:"revision"` + Date basetypes.ObjectValue `tfsdk:"date"` +}