Skip to content

Commit 2955c95

Browse files
committed
add datasource
1 parent 5c06226 commit 2955c95

File tree

3 files changed

+467
-1
lines changed

3 files changed

+467
-1
lines changed
Lines changed: 207 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,207 @@
1+
package provider
2+
3+
import (
4+
"context"
5+
"fmt"
6+
7+
"github.com/hashicorp/terraform-plugin-framework/datasource"
8+
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
9+
"github.com/hashicorp/terraform-plugin-framework/diag"
10+
"github.com/hashicorp/terraform-plugin-framework/types"
11+
"github.com/temporalio/terraform-provider-temporalcloud/internal/client"
12+
"github.com/temporalio/terraform-provider-temporalcloud/internal/provider/enums"
13+
internaltypes "github.com/temporalio/terraform-provider-temporalcloud/internal/types"
14+
accountv1 "go.temporal.io/cloud-sdk/api/account/v1"
15+
cloudservicev1 "go.temporal.io/cloud-sdk/api/cloudservice/v1"
16+
)
17+
18+
var (
19+
_ datasource.DataSource = &accountAuditLogSinkDataSource{}
20+
_ datasource.DataSourceWithConfigure = &accountAuditLogSinkDataSource{}
21+
)
22+
23+
func NewAccountAuditLogSinkDataSource() datasource.DataSource {
24+
return &accountAuditLogSinkDataSource{}
25+
}
26+
27+
type (
28+
accountAuditLogSinkDataSource struct {
29+
client *client.Client
30+
}
31+
32+
accountAuditLogSinkDataModel struct {
33+
ID types.String `tfsdk:"id"`
34+
SinkName types.String `tfsdk:"sink_name"`
35+
Enabled types.Bool `tfsdk:"enabled"`
36+
Kinesis types.Object `tfsdk:"kinesis"`
37+
PubSub types.Object `tfsdk:"pubsub"`
38+
State types.String `tfsdk:"state"`
39+
}
40+
)
41+
42+
func accountAuditLogSinkDataSourceSchema(idRequired bool) map[string]schema.Attribute {
43+
idAttribute := schema.StringAttribute{
44+
Description: "The unique identifier of the account audit log sink.",
45+
}
46+
47+
switch idRequired {
48+
case true:
49+
idAttribute.Required = true
50+
case false:
51+
idAttribute.Computed = true
52+
}
53+
54+
return map[string]schema.Attribute{
55+
"id": idAttribute,
56+
"sink_name": schema.StringAttribute{
57+
Description: "The unique name of the audit log sink.",
58+
Required: true,
59+
},
60+
"enabled": schema.BoolAttribute{
61+
Description: "A flag indicating whether the audit log sink is enabled or not.",
62+
Computed: true,
63+
},
64+
"kinesis": schema.SingleNestedAttribute{
65+
Description: "The Kinesis configuration details when destination_type is Kinesis.",
66+
Computed: true,
67+
Attributes: map[string]schema.Attribute{
68+
"role_name": schema.StringAttribute{
69+
Description: "The IAM role that Temporal Cloud assumes for writing records to the customer's Kinesis stream.",
70+
Computed: true,
71+
},
72+
"destination_uri": schema.StringAttribute{
73+
Description: "The destination URI of the Kinesis stream where Temporal will send data.",
74+
Computed: true,
75+
},
76+
"region": schema.StringAttribute{
77+
Description: "The region of the Kinesis stream.",
78+
Computed: true,
79+
},
80+
},
81+
},
82+
"pubsub": schema.SingleNestedAttribute{
83+
Description: "The PubSub configuration details when destination_type is PubSub.",
84+
Computed: true,
85+
Attributes: map[string]schema.Attribute{
86+
"service_account_id": schema.StringAttribute{
87+
Description: "The customer service account ID that Temporal Cloud impersonates for writing records to the customer's PubSub topic.",
88+
Computed: true,
89+
},
90+
"topic_name": schema.StringAttribute{
91+
Description: "The destination PubSub topic name for Temporal.",
92+
Computed: true,
93+
},
94+
"gcp_project_id": schema.StringAttribute{
95+
Description: "The GCP project ID of the PubSub topic and service account.",
96+
Computed: true,
97+
},
98+
},
99+
},
100+
"state": schema.StringAttribute{
101+
Description: "The current state of the audit log sink.",
102+
Computed: true,
103+
},
104+
}
105+
}
106+
107+
func (d *accountAuditLogSinkDataSource) Metadata(ctx context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
108+
resp.TypeName = req.ProviderTypeName + "_account_audit_log_sink"
109+
}
110+
111+
func (d *accountAuditLogSinkDataSource) Configure(ctx context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) {
112+
if req.ProviderData == nil {
113+
return
114+
}
115+
116+
client, ok := req.ProviderData.(*client.Client)
117+
if !ok {
118+
resp.Diagnostics.AddError("Unexpected Data Source Configure Type", fmt.Sprintf("Expected *client.Client, got: %T. Please report this issue to the provider developers.", req.ProviderData))
119+
return
120+
}
121+
122+
d.client = client
123+
}
124+
125+
func (d *accountAuditLogSinkDataSource) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) {
126+
resp.Schema = schema.Schema{
127+
Description: "Fetches details about an account audit log sink.",
128+
Attributes: accountAuditLogSinkDataSourceSchema(true),
129+
}
130+
}
131+
132+
func (d *accountAuditLogSinkDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
133+
var input accountAuditLogSinkDataModel
134+
resp.Diagnostics.Append(req.Config.Get(ctx, &input)...)
135+
if resp.Diagnostics.HasError() {
136+
return
137+
}
138+
139+
if len(input.SinkName.ValueString()) == 0 {
140+
resp.Diagnostics.AddError("invalid account audit log sink sink_name", "account audit log sink sink_name is required")
141+
return
142+
}
143+
144+
auditLogSinkResp, err := d.client.CloudService().GetAccountAuditLogSink(ctx, &cloudservicev1.GetAccountAuditLogSinkRequest{
145+
Name: input.SinkName.ValueString(),
146+
})
147+
if err != nil {
148+
resp.Diagnostics.AddError("Failed to get account audit log sink", err.Error())
149+
return
150+
}
151+
152+
model, diags := accountAuditLogSinkToAccountAuditLogSinkDataModel(ctx, auditLogSinkResp.GetSink())
153+
resp.Diagnostics.Append(diags...)
154+
if resp.Diagnostics.HasError() {
155+
return
156+
}
157+
158+
diags = resp.State.Set(ctx, model)
159+
resp.Diagnostics.Append(diags...)
160+
}
161+
162+
func accountAuditLogSinkToAccountAuditLogSinkDataModel(ctx context.Context, auditLogSink *accountv1.AuditLogSink) (*accountAuditLogSinkDataModel, diag.Diagnostics) {
163+
var diags diag.Diagnostics
164+
stateStr, err := enums.FromResourceState(auditLogSink.State)
165+
if err != nil {
166+
diags.AddError("Failed to convert resource state", err.Error())
167+
return nil, diags
168+
}
169+
170+
model := new(accountAuditLogSinkDataModel)
171+
model.ID = types.StringValue(auditLogSink.GetName())
172+
model.SinkName = types.StringValue(auditLogSink.GetName())
173+
model.Enabled = types.BoolValue(auditLogSink.GetSpec().GetEnabled())
174+
model.State = types.StringValue(stateStr)
175+
176+
kinesisObj := types.ObjectNull(internaltypes.KinesisSpecModelAttrTypes)
177+
if auditLogSink.GetSpec().GetKinesisSink() != nil {
178+
kinesisSpec := internaltypes.KinesisSpecModel{
179+
RoleName: types.StringValue(auditLogSink.GetSpec().GetKinesisSink().GetRoleName()),
180+
DestinationUri: types.StringValue(auditLogSink.GetSpec().GetKinesisSink().GetDestinationUri()),
181+
Region: types.StringValue(auditLogSink.GetSpec().GetKinesisSink().GetRegion()),
182+
}
183+
184+
kinesisObj, diags = types.ObjectValueFrom(ctx, internaltypes.KinesisSpecModelAttrTypes, kinesisSpec)
185+
if diags.HasError() {
186+
return nil, diags
187+
}
188+
}
189+
190+
pubsubObj := types.ObjectNull(internaltypes.PubSubSpecModelAttrTypes)
191+
if auditLogSink.GetSpec().GetPubSubSink() != nil {
192+
pubsubSpec := internaltypes.PubSubSpecModel{
193+
ServiceAccountId: types.StringValue(auditLogSink.GetSpec().GetPubSubSink().GetServiceAccountId()),
194+
TopicName: types.StringValue(auditLogSink.GetSpec().GetPubSubSink().GetTopicName()),
195+
GcpProjectId: types.StringValue(auditLogSink.GetSpec().GetPubSubSink().GetGcpProjectId()),
196+
}
197+
198+
pubsubObj, diags = types.ObjectValueFrom(ctx, internaltypes.PubSubSpecModelAttrTypes, pubsubSpec)
199+
if diags.HasError() {
200+
return nil, diags
201+
}
202+
}
203+
204+
model.Kinesis = kinesisObj
205+
model.PubSub = pubsubObj
206+
return model, diags
207+
}

0 commit comments

Comments
 (0)