-
Notifications
You must be signed in to change notification settings - Fork 62
/
data_source_kafka_cluster.go
269 lines (245 loc) · 9.05 KB
/
data_source_kafka_cluster.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
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
// Copyright 2021 Confluent Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package provider
import (
"context"
"encoding/json"
"fmt"
v2 "github.com/confluentinc/ccloud-sdk-go-v2/cmk/v2"
"github.com/hashicorp/terraform-plugin-log/tflog"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"net/http"
)
const (
// The maximum allowable page size - 1 (to avoid off-by-one errors) when listing service accounts using CMK V2 API
// https://docs.confluent.io/cloud/current/api.html#operation/listCmkV2Clusters
listKafkaClustersPageSize = 99
)
func kafkaDataSource() *schema.Resource {
return &schema.Resource{
ReadContext: kafkaDataSourceRead,
Schema: map[string]*schema.Schema{
paramId: {
Type: schema.TypeString,
Computed: true,
Optional: true,
// A user should provide a value for either "id" or "display_name" attribute, not both
ExactlyOneOf: []string{paramId, paramDisplayName},
Description: "The ID of the Kafka cluster, for example, `lkc-abc123`.",
},
// Similarly, paramEnvironment is required as well
paramEnvironment: environmentDataSourceSchema(),
paramNetwork: optionalNetworkDataSourceSchema(),
paramConfluentCustomerKey: optionalByokDataSourceSchema(),
paramApiVersion: {
Type: schema.TypeString,
Computed: true,
},
paramKind: {
Type: schema.TypeString,
Computed: true,
},
paramDisplayName: {
Type: schema.TypeString,
Computed: true,
Optional: true,
// A user should provide a value for either "id" or "display_name" attribute, not both
ExactlyOneOf: []string{paramId, paramDisplayName},
},
paramAvailability: {
Type: schema.TypeString,
Computed: true,
},
paramCloud: {
Type: schema.TypeString,
Computed: true,
},
paramRegion: {
Type: schema.TypeString,
Computed: true,
},
paramBasicCluster: basicClusterDataSourceSchema(),
paramStandardCluster: standardClusterDataSourceSchema(),
paramDedicatedCluster: dedicatedClusterDataSourceSchema(),
paramEnterpriseCluster: enterpriseClusterDataSourceSchema(),
paramBootStrapEndpoint: {
Type: schema.TypeString,
Computed: true,
},
paramRestEndpoint: {
Type: schema.TypeString,
Computed: true,
},
paramRbacCrn: {
Type: schema.TypeString,
Computed: true,
},
},
}
}
func kafkaDataSourceRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
// ExactlyOneOf specified in the schema ensures one of paramId or paramDisplayName is specified.
// The next step is to figure out which one exactly is set.
clusterId := d.Get(paramId).(string)
displayName := d.Get(paramDisplayName).(string)
environmentId := extractStringValueFromBlock(d, paramEnvironment, paramId)
if clusterId != "" {
return kafkaDataSourceReadUsingId(ctx, d, meta, environmentId, clusterId)
} else if displayName != "" {
return kafkaDataSourceReadUsingDisplayName(ctx, d, meta, environmentId, displayName)
} else {
return diag.Errorf("error reading Kafka Cluster: exactly one of %q or %q must be specified but they're both empty", paramId, paramDisplayName)
}
}
func kafkaDataSourceReadUsingDisplayName(ctx context.Context, d *schema.ResourceData, meta interface{}, environmentId, displayName string) diag.Diagnostics {
tflog.Debug(ctx, fmt.Sprintf("Reading Kafka Cluster %q=%q", paramDisplayName, displayName))
c := meta.(*Client)
kafkaClusters, err := loadKafkaClusters(ctx, c, environmentId)
if err != nil {
return diag.Errorf("error reading Kafka Cluster %q: %s", displayName, createDescriptiveError(err))
}
if orgHasMultipleKafkaClustersWithTargetDisplayName(kafkaClusters, displayName) {
return diag.Errorf("error reading Kafka Cluster: there are multiple Kafka Clusters with %q=%q", paramDisplayName, displayName)
}
for _, cluster := range kafkaClusters {
if cluster.Spec.GetDisplayName() == displayName {
if _, err := setKafkaClusterAttributes(d, cluster); err != nil {
return diag.FromErr(createDescriptiveError(err))
}
return nil
}
}
return diag.Errorf("error reading Kafka Cluster: Kafka Cluster with %q=%q was not found", paramDisplayName, displayName)
}
func kafkaDataSourceReadUsingId(ctx context.Context, d *schema.ResourceData, meta interface{}, environmentId, clusterId string) diag.Diagnostics {
tflog.Debug(ctx, fmt.Sprintf("Reading Kafka Cluster %q=%q", paramId, clusterId), map[string]interface{}{kafkaClusterLoggingKey: clusterId})
c := meta.(*Client)
cluster, _, err := executeKafkaRead(c.cmkApiContext(ctx), c, environmentId, clusterId)
if err != nil {
return diag.Errorf("error reading Kafka Cluster %q: %s", clusterId, createDescriptiveError(err))
}
clusterJson, err := json.Marshal(cluster)
if err != nil {
return diag.Errorf("error reading Kafka Cluster %q: error marshaling %#v to json: %s", clusterId, cluster, createDescriptiveError(err))
}
tflog.Debug(ctx, fmt.Sprintf("Fetched Kafka Cluster %q: %s", clusterId, clusterJson), map[string]interface{}{kafkaClusterLoggingKey: clusterId})
if _, err := setKafkaClusterAttributes(d, cluster); err != nil {
return diag.FromErr(createDescriptiveError(err))
}
return nil
}
func orgHasMultipleKafkaClustersWithTargetDisplayName(clusters []v2.CmkV2Cluster, displayName string) bool {
var numberOfClustersWithTargetDisplayName = 0
for _, cluster := range clusters {
if cluster.Spec.GetDisplayName() == displayName {
numberOfClustersWithTargetDisplayName += 1
}
}
return numberOfClustersWithTargetDisplayName > 1
}
func loadKafkaClusters(ctx context.Context, c *Client, environmentId string) ([]v2.CmkV2Cluster, error) {
clusters := make([]v2.CmkV2Cluster, 0)
allClustersAreCollected := false
pageToken := ""
for !allClustersAreCollected {
clustersPageList, _, err := executeListKafkaClusters(ctx, c, environmentId, pageToken)
if err != nil {
return nil, fmt.Errorf("error reading Kafka Clusters: %s", createDescriptiveError(err))
}
clusters = append(clusters, clustersPageList.GetData()...)
// nextPageUrlStringNullable is nil for the last page
nextPageUrlStringNullable := clustersPageList.GetMetadata().Next
if nextPageUrlStringNullable.IsSet() {
nextPageUrlString := *nextPageUrlStringNullable.Get()
if nextPageUrlString == "" {
allClustersAreCollected = true
} else {
pageToken, err = extractPageToken(nextPageUrlString)
if err != nil {
return nil, fmt.Errorf("error reading Kafka Clusters: %s", createDescriptiveError(err))
}
}
} else {
allClustersAreCollected = true
}
}
return clusters, nil
}
func executeListKafkaClusters(ctx context.Context, c *Client, environmentId, pageToken string) (v2.CmkV2ClusterList, *http.Response, error) {
if pageToken != "" {
return c.cmkClient.ClustersCmkV2Api.ListCmkV2Clusters(c.cmkApiContext(ctx)).Environment(environmentId).PageSize(listKafkaClustersPageSize).PageToken(pageToken).Execute()
} else {
return c.cmkClient.ClustersCmkV2Api.ListCmkV2Clusters(c.cmkApiContext(ctx)).Environment(environmentId).PageSize(listKafkaClustersPageSize).Execute()
}
}
func basicClusterDataSourceSchema() *schema.Schema {
return &schema.Schema{
Type: schema.TypeList,
Optional: true,
MaxItems: 0,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{},
},
}
}
func standardClusterDataSourceSchema() *schema.Schema {
return &schema.Schema{
Type: schema.TypeList,
Optional: true,
MaxItems: 0,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{},
},
}
}
func dedicatedClusterDataSourceSchema() *schema.Schema {
return &schema.Schema{
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
paramCku: {
Type: schema.TypeInt,
Computed: true,
Description: "The number of Confluent Kafka Units (CKUs) for Dedicated cluster types. MULTI_ZONE dedicated clusters must have at least two CKUs.",
},
paramEncryptionKey: {
Type: schema.TypeString,
Computed: true,
Description: "The ID of the encryption key that is used to encrypt the data in the Kafka cluster.",
},
paramZones: {
Type: schema.TypeList,
Elem: &schema.Schema{
Type: schema.TypeString,
},
Computed: true,
Description: "The list of zones the cluster is in.",
},
},
},
}
}
func enterpriseClusterDataSourceSchema() *schema.Schema {
return &schema.Schema{
Type: schema.TypeList,
Optional: true,
MaxItems: 0,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{},
},
}
}