|
| 1 | +package certificates |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "net/http" |
| 7 | + |
| 8 | + "github.com/hashicorp/terraform-plugin-framework/datasource" |
| 9 | + "github.com/hashicorp/terraform-plugin-framework/datasource/schema" |
| 10 | + "github.com/hashicorp/terraform-plugin-log/tflog" |
| 11 | + certSdk "github.com/stackitcloud/stackit-sdk-go/services/certificates" |
| 12 | + "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/conversion" |
| 13 | + "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/core" |
| 14 | + certUtils "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/services/albcertificates/utils" |
| 15 | + "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/utils" |
| 16 | +) |
| 17 | + |
| 18 | +// Ensure the implementation satisfies the expected interfaces. |
| 19 | +var ( |
| 20 | + _ datasource.DataSource = &certDataSource{} |
| 21 | +) |
| 22 | + |
| 23 | +// NewCertificatesDataSource is a helper function to simplify the provider implementation. |
| 24 | +func NewCertificatesDataSource() datasource.DataSource { |
| 25 | + return &certDataSource{} |
| 26 | +} |
| 27 | + |
| 28 | +// certDataSource is the data source implementation. |
| 29 | +type certDataSource struct { |
| 30 | + client *certSdk.APIClient |
| 31 | + providerData core.ProviderData |
| 32 | +} |
| 33 | + |
| 34 | +// Metadata returns the data source type name. |
| 35 | +func (r *certDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) { |
| 36 | + resp.TypeName = req.ProviderTypeName + "_certificates" |
| 37 | +} |
| 38 | + |
| 39 | +// Configure adds the provider configured client to the data source. |
| 40 | +func (r *certDataSource) Configure(ctx context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) { |
| 41 | + var ok bool |
| 42 | + r.providerData, ok = conversion.ParseProviderData(ctx, req.ProviderData, &resp.Diagnostics) |
| 43 | + if !ok { |
| 44 | + return |
| 45 | + } |
| 46 | + |
| 47 | + apiClient := certUtils.ConfigureClient(ctx, &r.providerData, &resp.Diagnostics) |
| 48 | + if resp.Diagnostics.HasError() { |
| 49 | + return |
| 50 | + } |
| 51 | + r.client = apiClient |
| 52 | + tflog.Info(ctx, "Certificate client configured") |
| 53 | +} |
| 54 | + |
| 55 | +// Schema defines the schema for the resource. |
| 56 | +func (r *certDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) { |
| 57 | + descriptions := map[string]string{ |
| 58 | + "main": "Certificates resource schema.", |
| 59 | + "id": "Terraform's internal resource ID. It is structured as `project_id`,`region`,`name`.", |
| 60 | + "project_id": "STACKIT project ID to which the certificates is associated.", |
| 61 | + "region": "The resource region (e.g. eu01). If not defined, the provider region is used.", |
| 62 | + "cert-id": "The ID of the certificate.", |
| 63 | + "name": "Certificate name.", |
| 64 | + "private_key": "The PEM encoded private key part", |
| 65 | + "public_key": "The PEM encoded public key part", |
| 66 | + } |
| 67 | + |
| 68 | + resp.Schema = schema.Schema{ |
| 69 | + Description: descriptions["main"], |
| 70 | + MarkdownDescription: ` |
| 71 | +## Setting up supporting infrastructure` + "\n" + ` |
| 72 | +
|
| 73 | +The example below creates the supporting infrastructure using the STACKIT Terraform provider, including the network, network interface, a public IP address and server resources. |
| 74 | +`, |
| 75 | + Attributes: map[string]schema.Attribute{ |
| 76 | + "id": schema.StringAttribute{ |
| 77 | + Description: descriptions["id"], |
| 78 | + Computed: true, |
| 79 | + }, |
| 80 | + "project_id": schema.StringAttribute{ |
| 81 | + Description: descriptions["project_id"], |
| 82 | + Computed: true, |
| 83 | + }, |
| 84 | + "region": schema.StringAttribute{ |
| 85 | + Description: descriptions["region"], |
| 86 | + Computed: true, |
| 87 | + }, |
| 88 | + "name": schema.StringAttribute{ |
| 89 | + Description: descriptions["name"], |
| 90 | + Computed: true, |
| 91 | + }, |
| 92 | + "cert_id": schema.StringAttribute{ |
| 93 | + Description: descriptions["cert-id"], |
| 94 | + Computed: true, |
| 95 | + }, |
| 96 | + "private_key": schema.StringAttribute{ |
| 97 | + Description: descriptions["private_key"], |
| 98 | + Computed: true, |
| 99 | + }, |
| 100 | + "public_key": schema.StringAttribute{ |
| 101 | + Description: descriptions["public_key"], |
| 102 | + Computed: true, |
| 103 | + }, |
| 104 | + }, |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | +// Read refreshes the Terraform state with the latest data. |
| 109 | +func (r *certDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) { // nolint:gocritic // function signature required by Terraform |
| 110 | + var model Model |
| 111 | + diags := req.Config.Get(ctx, &model) |
| 112 | + resp.Diagnostics.Append(diags...) |
| 113 | + if resp.Diagnostics.HasError() { |
| 114 | + return |
| 115 | + } |
| 116 | + |
| 117 | + ctx = core.InitProviderContext(ctx) |
| 118 | + |
| 119 | + projectId := model.ProjectId.ValueString() |
| 120 | + certId := model.CertID.ValueString() |
| 121 | + region := r.providerData.GetRegionWithOverride(model.Region) |
| 122 | + ctx = tflog.SetField(ctx, "project_id", projectId) |
| 123 | + ctx = tflog.SetField(ctx, "cert_id", certId) |
| 124 | + ctx = tflog.SetField(ctx, "region", region) |
| 125 | + |
| 126 | + certResp, err := r.client.GetCertificate(ctx, projectId, region, certId).Execute() |
| 127 | + if err != nil { |
| 128 | + utils.LogError( |
| 129 | + ctx, |
| 130 | + &resp.Diagnostics, |
| 131 | + err, |
| 132 | + "Reading certificate", |
| 133 | + fmt.Sprintf("Certificate with ID %q does not exist in project %q.", certId, projectId), |
| 134 | + map[int]string{ |
| 135 | + http.StatusForbidden: fmt.Sprintf("Project with ID %q not found or forbidden access", projectId), |
| 136 | + }, |
| 137 | + ) |
| 138 | + resp.State.RemoveResource(ctx) |
| 139 | + return |
| 140 | + } |
| 141 | + |
| 142 | + ctx = core.LogResponse(ctx) |
| 143 | + |
| 144 | + // Map response body to schema |
| 145 | + err = mapFields(certResp, &model, region) |
| 146 | + if err != nil { |
| 147 | + core.LogAndAddError(ctx, &resp.Diagnostics, "Error reading certificate", fmt.Sprintf("Processing API payload: %v", err)) |
| 148 | + return |
| 149 | + } |
| 150 | + |
| 151 | + // Set refreshed state |
| 152 | + diags = resp.State.Set(ctx, model) |
| 153 | + resp.Diagnostics.Append(diags...) |
| 154 | + if resp.Diagnostics.HasError() { |
| 155 | + return |
| 156 | + } |
| 157 | + tflog.Info(ctx, "Certificate read") |
| 158 | +} |
0 commit comments