-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathresource_teams_integration.go
More file actions
241 lines (205 loc) · 8.14 KB
/
Copy pathresource_teams_integration.go
File metadata and controls
241 lines (205 loc) · 8.14 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
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
package main
import (
"context"
"fmt"
"strings"
axonopsClient "terraform-provider-axonops/client"
"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-log/tflog"
)
var _ resource.Resource = (*teamsIntegrationResource)(nil)
var _ resource.ResourceWithImportState = (*teamsIntegrationResource)(nil)
type teamsIntegrationResource struct {
client *axonopsClient.AxonopsHttpClient
}
func NewTeamsIntegrationResource() resource.Resource {
return &teamsIntegrationResource{}
}
func (r *teamsIntegrationResource) Configure(ctx context.Context, req resource.ConfigureRequest, resp *resource.ConfigureResponse) {
if req.ProviderData == nil {
return
}
client, ok := req.ProviderData.(*axonopsClient.AxonopsHttpClient)
if !ok {
resp.Diagnostics.AddError("Unexpected Resource Configure Type", fmt.Sprintf("Expected *axonopsClient.AxonopsHttpClient, got: %T.", req.ProviderData))
return
}
r.client = client
}
func (r *teamsIntegrationResource) Metadata(_ context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_teams_integration"
}
func (r *teamsIntegrationResource) Schema(ctx context.Context, req resource.SchemaRequest, resp *resource.SchemaResponse) {
resp.Schema = schema.Schema{
Description: "Manages a Microsoft Teams integration for AxonOps alerting.",
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.",
PlanModifiers: []planmodifier.String{
stringplanmodifier.RequiresReplace(),
},
},
"cluster_type": schema.StringAttribute{
Required: true,
Description: "The cluster type (cassandra, kafka, or dse).",
PlanModifiers: []planmodifier.String{
stringplanmodifier.RequiresReplace(),
},
},
"name": schema.StringAttribute{
Required: true,
Description: "The name of the integration.",
PlanModifiers: []planmodifier.String{
stringplanmodifier.RequiresReplace(),
},
},
"webhook_url": schema.StringAttribute{
Required: true,
Sensitive: true,
Description: "The Microsoft Teams webhook URL.",
},
},
}
}
type teamsIntegrationResourceData 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 (r *teamsIntegrationResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
var data teamsIntegrationResourceData
diags := req.Plan.Get(ctx, &data)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
payload := axonopsClient.IntegrationPayload{
Type: "microsoft_teams",
Params: map[string]string{
"name": data.Name.ValueString(),
"webHookURL": data.WebhookURL.ValueString(),
},
}
err := r.client.CreateOrUpdateIntegration(data.ClusterType.ValueString(), data.ClusterName.ValueString(), payload)
if err != nil {
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to create Teams integration: %s", err))
return
}
integrations, err := r.client.GetIntegrations(data.ClusterType.ValueString(), data.ClusterName.ValueString())
if err != nil {
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to read integrations: %s", err))
return
}
def := axonopsClient.FindIntegrationByNameAndType(integrations, data.Name.ValueString(), "microsoft_teams")
if def == nil {
resp.Diagnostics.AddError("Client Error", "Integration was created but could not be found")
return
}
data.ID = types.StringValue(def.ID)
tflog.Info(ctx, "Created Teams integration resource")
diags = resp.State.Set(ctx, &data)
resp.Diagnostics.Append(diags...)
}
func (r *teamsIntegrationResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) {
var data teamsIntegrationResourceData
diags := req.State.Get(ctx, &data)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
integrations, err := r.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.State.RemoveResource(ctx)
return
}
data.ID = types.StringValue(def.ID)
// Preserve webhook_url from state to avoid overwriting with masked values
diags = resp.State.Set(ctx, &data)
resp.Diagnostics.Append(diags...)
}
func (r *teamsIntegrationResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) {
var planData teamsIntegrationResourceData
var stateData teamsIntegrationResourceData
diags := req.Plan.Get(ctx, &planData)
resp.Diagnostics.Append(diags...)
diags = req.State.Get(ctx, &stateData)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
payload := axonopsClient.IntegrationPayload{
ID: stateData.ID.ValueString(),
Type: "microsoft_teams",
Params: map[string]string{
"name": planData.Name.ValueString(),
"webHookURL": planData.WebhookURL.ValueString(),
},
}
err := r.client.CreateOrUpdateIntegration(planData.ClusterType.ValueString(), planData.ClusterName.ValueString(), payload)
if err != nil {
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to update Teams integration: %s", err))
return
}
planData.ID = stateData.ID
tflog.Info(ctx, "Updated Teams integration resource")
diags = resp.State.Set(ctx, &planData)
resp.Diagnostics.Append(diags...)
}
func (r *teamsIntegrationResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) {
var data teamsIntegrationResourceData
diags := req.State.Get(ctx, &data)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
err := r.client.DeleteIntegration(data.ClusterType.ValueString(), data.ClusterName.ValueString(), data.ID.ValueString())
if err != nil {
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to delete Teams integration: %s", err))
return
}
tflog.Info(ctx, "Deleted Teams integration resource")
}
// Import ID format: cluster_type/cluster_name/name
func (r *teamsIntegrationResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) {
parts := strings.Split(req.ID, "/")
if len(parts) != 3 {
resp.Diagnostics.AddError("Invalid Import ID", fmt.Sprintf("Expected format: cluster_type/cluster_name/name, got: %s", req.ID))
return
}
clusterType := parts[0]
clusterName := parts[1]
name := parts[2]
integrations, err := r.client.GetIntegrations(clusterType, clusterName)
if err != nil {
resp.Diagnostics.AddError("Import Error", fmt.Sprintf("Unable to get integrations: %s", err))
return
}
def := axonopsClient.FindIntegrationByNameAndType(integrations, name, "microsoft_teams")
if def == nil {
resp.Diagnostics.AddError("Import Error", fmt.Sprintf("Teams integration '%s' not found", name))
return
}
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("id"), def.ID)...)
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("cluster_type"), clusterType)...)
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("cluster_name"), clusterName)...)
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("name"), def.Params["name"])...)
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("webhook_url"), def.Params["webHookURL"])...)
tflog.Info(ctx, fmt.Sprintf("Imported Teams integration '%s' for %s/%s", name, clusterType, clusterName))
}