Skip to content

Commit 7fd2b9f

Browse files
STACKITLB-1837 | add ALB tls certificates provider
1 parent 060b50d commit 7fd2b9f

File tree

8 files changed

+697
-3
lines changed

8 files changed

+697
-3
lines changed

go.mod

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,10 @@ require (
1111
github.com/hashicorp/terraform-plugin-go v0.30.0
1212
github.com/hashicorp/terraform-plugin-log v0.10.0
1313
github.com/hashicorp/terraform-plugin-testing v1.14.0
14-
github.com/stackitcloud/stackit-sdk-go/core v0.22.0
14+
github.com/stackitcloud/stackit-sdk-go/core v0.23.0
1515
github.com/stackitcloud/stackit-sdk-go/services/alb v0.9.3
1616
github.com/stackitcloud/stackit-sdk-go/services/cdn v1.10.0
17+
github.com/stackitcloud/stackit-sdk-go/services/certificates v1.4.1
1718
github.com/stackitcloud/stackit-sdk-go/services/dns v0.17.6
1819
github.com/stackitcloud/stackit-sdk-go/services/edge v0.4.3
1920
github.com/stackitcloud/stackit-sdk-go/services/git v0.10.3

go.sum

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,14 +151,16 @@ github.com/sergi/go-diff v1.3.2-0.20230802210424-5b0b94c5c0d3 h1:n661drycOFuPLCN
151151
github.com/sergi/go-diff v1.3.2-0.20230802210424-5b0b94c5c0d3/go.mod h1:A0bzQcvG0E7Rwjx0REVgAGH58e96+X0MeOfepqsbeW4=
152152
github.com/skeema/knownhosts v1.3.1 h1:X2osQ+RAjK76shCbvhHHHVl3ZlgDm8apHEHFqRjnBY8=
153153
github.com/skeema/knownhosts v1.3.1/go.mod h1:r7KTdC8l4uxWRyK2TpQZ/1o5HaSzh06ePQNxPwTcfiY=
154-
github.com/stackitcloud/stackit-sdk-go/core v0.22.0 h1:6rViz7GnNwXSh51Lur5xuDzO8EWSZfN9J0HvEkBKq6c=
155-
github.com/stackitcloud/stackit-sdk-go/core v0.22.0/go.mod h1:osMglDby4csGZ5sIfhNyYq1bS1TxIdPY88+skE/kkmI=
154+
github.com/stackitcloud/stackit-sdk-go/core v0.23.0 h1:zPrOhf3Xe47rKRs1fg/AqKYUiJJRYjdcv+3qsS50mEs=
155+
github.com/stackitcloud/stackit-sdk-go/core v0.23.0/go.mod h1:osMglDby4csGZ5sIfhNyYq1bS1TxIdPY88+skE/kkmI=
156156
github.com/stackitcloud/stackit-sdk-go/services/alb v0.9.3 h1:X82TZfc6lg8ZoYdckiv5+OsV0d+81Q2TFMJh1TfxGWk=
157157
github.com/stackitcloud/stackit-sdk-go/services/alb v0.9.3/go.mod h1:V6+MolxM/M2FWyWZA+FRFKEzzUe10MU9eEVfMvxHGi8=
158158
github.com/stackitcloud/stackit-sdk-go/services/authorization v0.12.0 h1:HxPgBu04j5tj6nfZ2r0l6v4VXC0/tYOGe4sA5Addra8=
159159
github.com/stackitcloud/stackit-sdk-go/services/authorization v0.12.0/go.mod h1:uYI9pHAA2g84jJN25ejFUxa0/JtfpPZqMDkctQ1BzJk=
160160
github.com/stackitcloud/stackit-sdk-go/services/cdn v1.10.0 h1:YALzjYAApyQMKyt4C2LKhPRZHa6brmbFeKuuwl+KOTs=
161161
github.com/stackitcloud/stackit-sdk-go/services/cdn v1.10.0/go.mod h1:915b/lJgDikYFEoRQ8wc8aCtPvUCceYk7gGm9nViJe0=
162+
github.com/stackitcloud/stackit-sdk-go/services/certificates v1.4.1 h1:RBY/mNR4H8Vd/7z0nky+AQNvoaZ16hvrGSuYi1YLLao=
163+
github.com/stackitcloud/stackit-sdk-go/services/certificates v1.4.1/go.mod h1:3R/RwYdBc1s6WZNhToWs0rBDropbNRM7okOAdjY3rpU=
162164
github.com/stackitcloud/stackit-sdk-go/services/dns v0.17.6 h1:GBRb49x5Nax/oQQaaf2F3kKwv8DQQOL0TQOC0C/v/Ew=
163165
github.com/stackitcloud/stackit-sdk-go/services/dns v0.17.6/go.mod h1:IX9iL3MigDZUmzwswTJMfYvyi118KAHrFMfjJUy5NYk=
164166
github.com/stackitcloud/stackit-sdk-go/services/edge v0.4.3 h1:TxChb2qbO82JiQEBYClSSD5HZxqKeKJ6dIvkEUCJmbs=

stackit/internal/core/core.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ type ProviderData struct {
3939
DefaultRegion string
4040
ALBCustomEndpoint string
4141
AuthorizationCustomEndpoint string
42+
CertificatesCustomEndpoint string
4243
CdnCustomEndpoint string
4344
DnsCustomEndpoint string
4445
EdgeCloudCustomEndpoint string
Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
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

Comments
 (0)