/
data_source_flink_compute_pool.go
203 lines (182 loc) · 7.51 KB
/
data_source_flink_compute_pool.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
// Copyright 2022 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"
fcpm "github.com/confluentinc/ccloud-sdk-go-v2/flink/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/listNetworkingV1ComputePools
listComputePoolsPageSize = 99
)
func computePoolDataSource() *schema.Resource {
return &schema.Resource{
ReadContext: computePoolDataSourceRead,
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 Compute Pool, for example, `lfcp-abc123`.",
},
// Similarly, paramEnvironment is required as well
paramEnvironment: environmentDataSourceSchema(),
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},
},
paramCloud: {
Type: schema.TypeString,
Computed: true,
},
paramRegion: {
Type: schema.TypeString,
Computed: true,
},
paramMaxCfu: {
Type: schema.TypeInt,
Computed: true,
},
paramApiVersion: {
Type: schema.TypeString,
Computed: true,
},
paramKind: {
Type: schema.TypeString,
Computed: true,
},
paramResourceName: {
Type: schema.TypeString,
Computed: true,
},
},
}
}
func computePoolDataSourceRead(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.
computePoolId := d.Get(paramId).(string)
displayName := d.Get(paramDisplayName).(string)
environmentId := extractStringValueFromBlock(d, paramEnvironment, paramId)
if computePoolId != "" {
return computePoolDataSourceReadUsingId(ctx, d, meta, environmentId, computePoolId)
} else if displayName != "" {
return computePoolDataSourceReadUsingDisplayName(ctx, d, meta, environmentId, displayName)
} else {
return diag.Errorf("error reading Flink Compute Pool: exactly one of %q or %q must be specified but they're both empty", paramId, paramDisplayName)
}
}
func computePoolDataSourceReadUsingDisplayName(ctx context.Context, d *schema.ResourceData, meta interface{}, environmentId, displayName string) diag.Diagnostics {
tflog.Debug(ctx, fmt.Sprintf("Reading Flink Compute Pool %q=%q", paramDisplayName, displayName))
c := meta.(*Client)
computePools, err := loadComputePools(ctx, c, environmentId)
if err != nil {
return diag.Errorf("error reading Flink Compute Pool %q: %s", displayName, createDescriptiveError(err))
}
if orgHasMultipleComputePoolsWithTargetDisplayName(computePools, displayName) {
return diag.Errorf("error reading Flink Compute Pool: there are multiple ComputePools with %q=%q", paramDisplayName, displayName)
}
for _, computePool := range computePools {
if computePool.Spec.GetDisplayName() == displayName {
if _, err := setComputePoolAttributes(d, computePool); err != nil {
return diag.FromErr(createDescriptiveError(err))
}
return nil
}
}
return diag.Errorf("error reading Flink Compute Pool: Flink Compute Pool with %q=%q was not found", paramDisplayName, displayName)
}
func computePoolDataSourceReadUsingId(ctx context.Context, d *schema.ResourceData, meta interface{}, environmentId, computePoolId string) diag.Diagnostics {
tflog.Debug(ctx, fmt.Sprintf("Reading Flink Compute Pool %q=%q", paramId, computePoolId), map[string]interface{}{computePoolLoggingKey: computePoolId})
c := meta.(*Client)
computePool, _, err := executeComputePoolRead(c.fcpmApiContext(ctx), c, environmentId, computePoolId)
if err != nil {
return diag.Errorf("error reading Flink Compute Pool %q: %s", computePoolId, createDescriptiveError(err))
}
computePoolJson, err := json.Marshal(computePool)
if err != nil {
return diag.Errorf("error reading Flink Compute Pool %q: error marshaling %#v to json: %s", computePoolId, computePool, createDescriptiveError(err))
}
tflog.Debug(ctx, fmt.Sprintf("Fetched Flink Compute Pool %q: %s", computePoolId, computePoolJson), map[string]interface{}{computePoolLoggingKey: computePoolId})
if _, err := setComputePoolAttributes(d, computePool); err != nil {
return diag.FromErr(createDescriptiveError(err))
}
return nil
}
func orgHasMultipleComputePoolsWithTargetDisplayName(clusters []fcpm.FcpmV2ComputePool, displayName string) bool {
var numberOfComputePoolsWithTargetDisplayName = 0
for _, cluster := range clusters {
if cluster.Spec.GetDisplayName() == displayName {
numberOfComputePoolsWithTargetDisplayName += 1
}
}
return numberOfComputePoolsWithTargetDisplayName > 1
}
func loadComputePools(ctx context.Context, c *Client, environmentId string) ([]fcpm.FcpmV2ComputePool, error) {
computePools := make([]fcpm.FcpmV2ComputePool, 0)
allComputePoolsAreCollected := false
pageToken := ""
for !allComputePoolsAreCollected {
computePoolsPageList, _, err := executeListComputePools(ctx, c, environmentId, pageToken)
if err != nil {
return nil, fmt.Errorf("error reading ComputePools: %s", createDescriptiveError(err))
}
computePools = append(computePools, computePoolsPageList.GetData()...)
// nextPageUrlStringNullable is nil for the last page
nextPageUrlStringNullable := computePoolsPageList.GetMetadata().Next
if nextPageUrlStringNullable.IsSet() {
nextPageUrlString := *nextPageUrlStringNullable.Get()
if nextPageUrlString == "" {
allComputePoolsAreCollected = true
} else {
pageToken, err = extractPageToken(nextPageUrlString)
if err != nil {
return nil, fmt.Errorf("error reading ComputePools: %s", createDescriptiveError(err))
}
}
} else {
allComputePoolsAreCollected = true
}
}
return computePools, nil
}
func executeListComputePools(ctx context.Context, c *Client, environmentId, pageToken string) (fcpm.FcpmV2ComputePoolList, *http.Response, error) {
if pageToken != "" {
return c.fcpmClient.ComputePoolsFcpmV2Api.ListFcpmV2ComputePools(c.fcpmApiContext(ctx)).Environment(environmentId).PageSize(listComputePoolsPageSize).PageToken(pageToken).Execute()
} else {
return c.fcpmClient.ComputePoolsFcpmV2Api.ListFcpmV2ComputePools(c.fcpmApiContext(ctx)).Environment(environmentId).PageSize(listComputePoolsPageSize).Execute()
}
}
func standardComputePoolDataSourceSchema() *schema.Schema {
return &schema.Schema{
Type: schema.TypeList,
Optional: true,
MaxItems: 0,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{},
},
}
}