-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata_source_teams_integration.go
More file actions
103 lines (88 loc) · 3.29 KB
/
Copy pathdata_source_teams_integration.go
File metadata and controls
103 lines (88 loc) · 3.29 KB
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
package main
import (
"context"
"fmt"
axonopsClient "terraform-provider-axonops/client"
"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"github.com/hashicorp/terraform-plugin-framework/types"
)
var _ datasource.DataSource = (*teamsIntegrationDataSource)(nil)
var _ datasource.DataSourceWithConfigure = (*teamsIntegrationDataSource)(nil)
type teamsIntegrationDataSource struct {
client *axonopsClient.AxonopsHttpClient
}
func NewTeamsIntegrationDataSource() datasource.DataSource {
return &teamsIntegrationDataSource{}
}
func (d *teamsIntegrationDataSource) Configure(ctx context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) {
if req.ProviderData == nil {
return
}
client, ok := req.ProviderData.(*axonopsClient.AxonopsHttpClient)
if !ok {
resp.Diagnostics.AddError("Unexpected DataSource Configure Type", fmt.Sprintf("Expected *axonopsClient.AxonopsHttpClient, got: %T.", req.ProviderData))
return
}
d.client = client
}
func (d *teamsIntegrationDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_teams_integration"
}
func (d *teamsIntegrationDataSource) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) {
resp.Schema = schema.Schema{
Description: "Reads a Microsoft Teams integration.",
Attributes: map[string]schema.Attribute{
"id": schema.StringAttribute{
Computed: true,
Description: "The integration ID.",
},
"cluster_name": schema.StringAttribute{
Required: true,
Description: "The name of the cluster.",
},
"cluster_type": schema.StringAttribute{
Required: true,
Description: "The cluster type (cassandra, kafka, or dse).",
},
"name": schema.StringAttribute{
Required: true,
Description: "The name of the integration.",
},
"webhook_url": schema.StringAttribute{
Computed: true,
Sensitive: true,
Description: "The Microsoft Teams webhook URL.",
},
},
}
}
type teamsIntegrationDataSourceData struct {
ID types.String `tfsdk:"id"`
ClusterName types.String `tfsdk:"cluster_name"`
ClusterType types.String `tfsdk:"cluster_type"`
Name types.String `tfsdk:"name"`
WebhookURL types.String `tfsdk:"webhook_url"`
}
func (d *teamsIntegrationDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
var data teamsIntegrationDataSourceData
diags := req.Config.Get(ctx, &data)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
integrations, err := d.client.GetIntegrations(data.ClusterType.ValueString(), data.ClusterName.ValueString())
if err != nil {
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to get integrations: %s", err))
return
}
def := axonopsClient.FindIntegrationByNameAndType(integrations, data.Name.ValueString(), "microsoft_teams")
if def == nil {
resp.Diagnostics.AddError("Not Found", fmt.Sprintf("Teams integration '%s' not found", data.Name.ValueString()))
return
}
data.ID = types.StringValue(def.ID)
data.WebhookURL = types.StringValue(def.Params["webHookURL"])
diags = resp.State.Set(ctx, &data)
resp.Diagnostics.Append(diags...)
}