/
instance_secrets_data_source.go
162 lines (137 loc) · 5.4 KB
/
instance_secrets_data_source.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package provider
import (
"context"
"fmt"
"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-framework/types/basetypes"
"github.com/hashicorp/terraform-plugin-log/tflog"
"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
tembodataclient "github.com/tembo-io/terraform-provider-tembo/tembodataclient"
)
// Ensure the implementation satisfies the expected interfaces.
var (
_ datasource.DataSource = &temboInstanceSecretsDataSource{}
_ datasource.DataSourceWithConfigure = &temboInstanceSecretsDataSource{}
)
// NewTemboInstanceSecretsDataSource is a helper function to simplify the provider implementation.
func NewTemboInstanceSecretsDataSource() datasource.DataSource {
return &temboInstanceSecretsDataSource{}
}
// TemboInstanceSecretsDataSource is the data source implementation.
type temboInstanceSecretsDataSource struct {
temboInstanceSecretsConfig instanceSecretsConfig
}
// Tembo Cluster Configuration.
type instanceSecretsConfig struct {
client *tembodataclient.APIClient
accessToken string
}
// Metadata returns the data source type name.
func (d *temboInstanceSecretsDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_instance_secrets"
}
// Schema defines the schema for the data source.
func (d *temboInstanceSecretsDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) {
resp.Schema = schema.Schema{
Description: "Data Source for Tembo Instance Secrets.",
Attributes: map[string]schema.Attribute{
"org_id": schema.StringAttribute{
MarkdownDescription: "Id of the organization in which the instance will be created",
Required: true,
},
"instance_id": schema.StringAttribute{
MarkdownDescription: "Unique ID for the instance generated by Tembo",
Required: true,
},
"available_secrets": schema.ListNestedAttribute{
Computed: true,
NestedObject: schema.NestedAttributeObject{
Attributes: map[string]schema.Attribute{
"name": schema.StringAttribute{
MarkdownDescription: "Name of the secret",
Optional: true,
},
"possible_keys": schema.ListAttribute{
MarkdownDescription: "Possible Keys for the secret",
Optional: true,
ElementType: types.StringType,
},
},
},
},
},
}
}
// Configure adds the provider configured client to the data source.
func (d *temboInstanceSecretsDataSource) Configure(_ context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) {
if req.ProviderData == nil {
return
}
temboInstanceSecretsConfig, ok := req.ProviderData.(instanceSecretsConfig)
if !ok {
resp.Diagnostics.AddError(
"Unexpected Data Source Configure Type",
fmt.Sprintf("Expected *instanceSecretsConfig, got: %T. Please report this issue to the provider developers.", req.ProviderData),
)
return
}
d.temboInstanceSecretsConfig = temboInstanceSecretsConfig
}
// temboInstanceSecretsDataSourceModel maps the data source schema data.
type temboInstanceSecretsDataSourceModel struct {
OrgId types.String `tfsdk:"org_id"`
InstanceId types.String `tfsdk:"instance_id"`
AvailableSecrets []availableSecret `tfsdk:"available_secrets"`
}
// availableSecretsModel maps instanceSecrets schema data.
type availableSecret struct {
// The name of an available secret
Name types.String `tfsdk:"name"`
// For this secret, available keys
PossibleKeys []types.String `tfsdk:"possible_keys"`
}
// Read refreshes the Terraform state with the latest data.
func (d *temboInstanceSecretsDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
// Get current state
var state temboInstanceSecretsDataSourceModel
ctx = context.WithValue(ctx, tembodataclient.ContextAccessToken, d.temboInstanceSecretsConfig.accessToken)
var orgId string
resp.Diagnostics.Append(req.Config.GetAttribute(ctx, path.Root("org_id"), &orgId)...)
var instanceId string
resp.Diagnostics.Append(req.Config.GetAttribute(ctx, path.Root("instance_id"), &instanceId)...)
if resp.Diagnostics.HasError() {
tflog.Error(ctx, fmt.Sprintf("error reading terraform plan %v", resp.Diagnostics.Errors()))
return
}
// Get Secret value from API
availableSecrets, _, err := d.temboInstanceSecretsConfig.client.SecretsAPI.GetSecretNamesV1(ctx, orgId, instanceId).Execute()
if err != nil {
resp.Diagnostics.AddError(
"Unable to Read Tembo Instance Available Secrets",
err.Error(),
)
return
}
if len(availableSecrets) > 0 {
for _, aSecret := range availableSecrets {
state.AvailableSecrets = append(state.AvailableSecrets, availableSecret{Name: types.StringValue(aSecret.Name), PossibleKeys: getPossibleKeys(aSecret.PossibleKeys)})
}
}
// Set refreshed state
diags := resp.State.Set(ctx, &state)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
}
func getPossibleKeys(possibleKeys []string) []basetypes.StringValue {
var localPossibleKeys []basetypes.StringValue
if len(possibleKeys) > 0 {
for _, possibleKey := range possibleKeys {
localPossibleKeys = append(localPossibleKeys, types.StringValue(possibleKey))
}
}
return localPossibleKeys
}