Skip to content

Commit cc7ed7a

Browse files
authored
Merge pull request #57 from jetstack/cert_template_datasource
Add Certificate Template Data Source
2 parents 39f6b63 + 5f71517 commit cc7ed7a

File tree

5 files changed

+198
-0
lines changed

5 files changed

+198
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
---
2+
# generated by https://github.com/hashicorp/terraform-plugin-docs
3+
page_title: "tlspc_certificate_template Data Source - tlspc"
4+
subcategory: ""
5+
description: |-
6+
Look up properties of a Certificate Template
7+
---
8+
9+
# tlspc_certificate_template (Data Source)
10+
11+
Look up properties of a Certificate Template
12+
13+
## Example Usage
14+
15+
```terraform
16+
data "tlspc_certificate_template" "default" {
17+
ca_type = "BUILTIN"
18+
name = "Default"
19+
}
20+
```
21+
22+
<!-- schema generated by tfplugindocs -->
23+
## Schema
24+
25+
### Required
26+
27+
- `ca_type` (String) Type of Certificate Authority (see Certificate Authority Product Option data source)
28+
- `name` (String) Name of the Certificate Issuing Template
29+
30+
### Read-Only
31+
32+
- `ca_product_id` (String) The ID of a Certificate Authority Product Option
33+
- `id` (String) The ID of this resource.
34+
- `key_reuse` (Boolean) Allow Private Key Reuse
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
data "tlspc_certificate_template" "default" {
2+
ca_type = "BUILTIN"
3+
name = "Default"
4+
}
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
// Copyright (c) Venafi, Inc.
2+
// SPDX-License-Identifier: MPL-2.0
3+
4+
package provider
5+
6+
import (
7+
"context"
8+
"fmt"
9+
10+
"terraform-provider-tlspc/internal/tlspc"
11+
12+
"github.com/hashicorp/terraform-plugin-framework/datasource"
13+
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
14+
"github.com/hashicorp/terraform-plugin-framework/types"
15+
)
16+
17+
// Ensure the implementation satisfies the expected interfaces.
18+
var (
19+
_ datasource.DataSource = &certTemplateDataSource{}
20+
_ datasource.DataSourceWithConfigure = &certTemplateDataSource{}
21+
)
22+
23+
// NewCertificateTemplateDataSource is a helper function to simplify the provider implementation.
24+
func NewCertificateTemplateDataSource() datasource.DataSource {
25+
return &certTemplateDataSource{}
26+
}
27+
28+
// certTemplateDataSource is the data source implementation.
29+
type certTemplateDataSource struct {
30+
client *tlspc.Client
31+
}
32+
33+
// Configure adds the provider configured client to the data source.
34+
func (d *certTemplateDataSource) Configure(_ context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) {
35+
// Add a nil check when handling ProviderData because Terraform
36+
// sets that data after it calls the ConfigureProvider RPC.
37+
if req.ProviderData == nil {
38+
return
39+
}
40+
41+
client, ok := req.ProviderData.(*tlspc.Client)
42+
if !ok {
43+
resp.Diagnostics.AddError(
44+
"Unexpected Data Source Configure Type",
45+
fmt.Sprintf("Expected *tlspc.Client, got: %T. Please report this issue to the provider developers.", req.ProviderData),
46+
)
47+
48+
return
49+
}
50+
51+
d.client = client
52+
}
53+
54+
// Metadata returns the data source type name.
55+
func (d *certTemplateDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
56+
resp.TypeName = req.ProviderTypeName + "_certificate_template"
57+
}
58+
59+
// Schema defines the schema for the data source.
60+
func (d *certTemplateDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) {
61+
resp.Schema = schema.Schema{
62+
MarkdownDescription: "Look up properties of a Certificate Template",
63+
Attributes: map[string]schema.Attribute{
64+
"id": schema.StringAttribute{
65+
Computed: true,
66+
},
67+
"name": schema.StringAttribute{
68+
Required: true,
69+
MarkdownDescription: "Name of the Certificate Issuing Template",
70+
},
71+
"ca_type": schema.StringAttribute{
72+
Required: true,
73+
MarkdownDescription: "Type of Certificate Authority (see Certificate Authority Product Option data source)",
74+
},
75+
"ca_product_id": schema.StringAttribute{
76+
Computed: true,
77+
MarkdownDescription: "The ID of a Certificate Authority Product Option",
78+
},
79+
"key_reuse": schema.BoolAttribute{
80+
Computed: true,
81+
MarkdownDescription: "Allow Private Key Reuse",
82+
},
83+
},
84+
}
85+
}
86+
87+
type certTemplateDataSourceModel struct {
88+
ID types.String `tfsdk:"id"`
89+
Name types.String `tfsdk:"name"`
90+
CAType types.String `tfsdk:"ca_type"`
91+
CAProductID types.String `tfsdk:"ca_product_id"`
92+
KeyReuse types.Bool `tfsdk:"key_reuse"`
93+
}
94+
95+
// Read refreshes the Terraform state with the latest data.
96+
func (d *certTemplateDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
97+
var model certTemplateDataSourceModel
98+
diags := req.Config.Get(ctx, &model)
99+
resp.Diagnostics.Append(diags...)
100+
if resp.Diagnostics.HasError() {
101+
return
102+
}
103+
104+
certTemplates, err := d.client.GetCertTemplates()
105+
if err != nil {
106+
resp.Diagnostics.AddError(
107+
"Error retrieving Certificate Templates",
108+
fmt.Sprintf("Error retrieving Certificate Templates: %s", err.Error()),
109+
)
110+
return
111+
}
112+
113+
found := false
114+
for _, v := range certTemplates {
115+
if model.CAType.ValueString() == v.CertificateAuthorityType && model.Name.ValueString() == v.Name {
116+
model.ID = types.StringValue(v.ID)
117+
model.CAProductID = types.StringValue(v.CertificateAuthorityProductOptionID)
118+
model.KeyReuse = types.BoolValue(v.KeyReuse)
119+
found = true
120+
continue
121+
}
122+
}
123+
124+
if !found {
125+
resp.Diagnostics.AddError(
126+
"Certificate Template not found",
127+
"",
128+
)
129+
return
130+
}
131+
diags = resp.State.Set(ctx, &model)
132+
resp.Diagnostics.Append(diags...)
133+
}

internal/provider/provider.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ func (p *tlspcProvider) DataSources(ctx context.Context) []func() datasource.Dat
106106
return []func() datasource.DataSource{
107107
NewUserDataSource,
108108
NewCAProductDataSource,
109+
NewCertificateTemplateDataSource,
109110
}
110111
}
111112

internal/tlspc/tlspc.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -860,3 +860,29 @@ func (c *Client) DeleteApplication(id string) error {
860860

861861
return nil
862862
}
863+
864+
type CertificateTemplates struct {
865+
Templates []CertificateTemplate `json:"certificateIssuingTemplates"`
866+
}
867+
868+
func (c *Client) GetCertTemplates() ([]CertificateTemplate, error) {
869+
path := c.Path(`%s/v1/certificateissuingtemplates/`)
870+
871+
resp, err := c.Get(path)
872+
if err != nil {
873+
return nil, fmt.Errorf("Error getting certificate template: %s", err)
874+
}
875+
876+
respBody, err := io.ReadAll(resp.Body)
877+
if err != nil {
878+
return nil, fmt.Errorf("Error reading response body: %s", err)
879+
}
880+
var ct CertificateTemplates
881+
err = json.Unmarshal(respBody, &ct)
882+
if err != nil {
883+
return nil, fmt.Errorf("Error decoding response: %s", string(respBody))
884+
}
885+
886+
return ct.Templates, nil
887+
888+
}

0 commit comments

Comments
 (0)