/
datasource.go
303 lines (281 loc) · 11 KB
/
datasource.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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
/*
Copyright (c) 2024 Red Hat, Inc.
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 hcp
import (
"context"
"fmt"
"net/http"
"github.com/hashicorp/terraform-plugin-framework-validators/listvalidator"
"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-log/tflog"
sdk "github.com/openshift-online/ocm-sdk-go"
cmv1 "github.com/openshift-online/ocm-sdk-go/clustersmgmt/v1"
rosa "github.com/terraform-redhat/terraform-provider-rhcs/provider/clusterrosa/common"
rosaTypes "github.com/terraform-redhat/terraform-provider-rhcs/provider/clusterrosa/common/types"
"github.com/terraform-redhat/terraform-provider-rhcs/provider/clusterrosa/sts"
"github.com/terraform-redhat/terraform-provider-rhcs/provider/proxy"
"github.com/terraform-redhat/terraform-provider-rhcs/provider/common"
)
type ClusterRosaHcpDatasource struct {
clusterCollection *cmv1.ClustersClient
versionCollection *cmv1.VersionsClient
clusterWait common.ClusterWait
}
var _ datasource.DataSource = &ClusterRosaHcpDatasource{}
var _ datasource.DataSourceWithConfigure = &ClusterRosaHcpDatasource{}
const deprecatedMessage = "This attribute is not supported for cluster data source. Therefore, it will not be displayed as an output of the datasource"
func NewDataSource() datasource.DataSource {
return &ClusterRosaHcpDatasource{}
}
func (r *ClusterRosaHcpDatasource) Metadata(ctx context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_cluster_rosa_hcp"
}
func (r *ClusterRosaHcpDatasource) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) {
resp.Schema = schema.Schema{
Description: "OpenShift managed cluster using rosa sts.",
Attributes: map[string]schema.Attribute{
"id": schema.StringAttribute{
Description: "Unique identifier of the cluster.",
Required: true,
},
"external_id": schema.StringAttribute{
Description: "Unique external identifier of the cluster. " + common.ValueCannotBeChangedStringDescription,
Computed: true,
},
"name": schema.StringAttribute{
Description: "Name of the cluster. Cannot exceed 15 characters in length. " + common.ValueCannotBeChangedStringDescription,
Computed: true,
},
"cloud_region": schema.StringAttribute{
Description: "Cloud region identifier, for example 'us-east-1'.",
Computed: true,
},
"sts": schema.SingleNestedAttribute{
Description: "STS configuration.",
Attributes: sts.HcpStsDatasource(),
Computed: true,
},
"properties": schema.MapAttribute{
Description: "User defined properties.",
ElementType: types.StringType,
Computed: true,
},
"ocm_properties": schema.MapAttribute{
Description: "Merged properties defined by OCM and the user defined 'properties'.",
ElementType: types.StringType,
Computed: true,
},
"tags": schema.MapAttribute{
Description: "Apply user defined tags to all cluster resources created in AWS. " + common.ValueCannotBeChangedStringDescription,
ElementType: types.StringType,
Computed: true,
},
"etcd_encryption": schema.BoolAttribute{
Description: "Encrypt etcd data. Note that all AWS storage is already encrypted. " + common.ValueCannotBeChangedStringDescription,
Computed: true,
},
"api_url": schema.StringAttribute{
Description: "URL of the API server.",
Computed: true,
},
"console_url": schema.StringAttribute{
Description: "URL of the console.",
Computed: true,
},
"domain": schema.StringAttribute{
Description: "DNS domain of cluster.",
Computed: true,
},
"replicas": schema.Int64Attribute{
Description: deprecatedMessage,
Computed: true,
},
"compute_machine_type": schema.StringAttribute{
Description: deprecatedMessage,
Computed: true,
},
"aws_account_id": schema.StringAttribute{
Description: "Identifier of the AWS account. " + common.ValueCannotBeChangedStringDescription,
Computed: true,
},
"aws_billing_account_id": schema.StringAttribute{
Description: "Identifier of the AWS account for billing. " + common.ValueCannotBeChangedStringDescription,
Computed: true,
},
"aws_subnet_ids": schema.ListAttribute{
Description: "AWS subnet IDs. " + common.ValueCannotBeChangedStringDescription,
ElementType: types.StringType,
Computed: true,
},
"kms_key_arn": schema.StringAttribute{
Description: "Used to encrypt root volume of compute node pools. The key ARN is the Amazon Resource Name (ARN) of a AWS Key Management Service (KMS) Key. It is a unique, " +
"fully qualified identifier for the AWS KMS Key. A key ARN includes the AWS account, Region, and the key ID" +
"(optional). " + common.ValueCannotBeChangedStringDescription,
Optional: true,
},
"etcd_kms_key_arn": schema.StringAttribute{
Description: "Used for etcd encryption. The key ARN is the Amazon Resource Name (ARN) of a AWS Key Management Service (KMS) Key. It is a unique, " +
"fully qualified identifier for the AWS KMS Key. A key ARN includes the AWS account, Region, and the key ID" +
"(optional). " + common.ValueCannotBeChangedStringDescription,
Computed: true,
},
"private": schema.BoolAttribute{
Description: "Provides private connectivity from your cluster's VPC to Red Hat SRE, without exposing traffic to the public internet. " + common.ValueCannotBeChangedStringDescription,
Computed: true,
},
"availability_zones": schema.ListAttribute{
Description: "Availability zones. " + rosaTypes.PoolMessage,
ElementType: types.StringType,
Computed: true,
Validators: []validator.List{
listvalidator.ValueStringsAre(rosa.AvailabilityZoneValidator),
},
},
"machine_cidr": schema.StringAttribute{
Description: "Block of IP addresses for nodes. " + common.ValueCannotBeChangedStringDescription,
Computed: true,
},
"proxy": schema.SingleNestedAttribute{
Description: "proxy",
Attributes: proxy.ProxyDatasource(),
Computed: true,
},
"service_cidr": schema.StringAttribute{
Description: "Block of IP addresses for the cluster service network. " + common.ValueCannotBeChangedStringDescription,
Computed: true,
},
"pod_cidr": schema.StringAttribute{
Description: "Block of IP addresses for pods. " + common.ValueCannotBeChangedStringDescription,
Computed: true,
},
"host_prefix": schema.Int64Attribute{
Description: "Length of the prefix of the subnet assigned to each node. " + common.ValueCannotBeChangedStringDescription,
Computed: true,
},
"channel_group": schema.StringAttribute{
Description: deprecatedMessage,
Computed: true,
},
"version": schema.StringAttribute{
Description: deprecatedMessage,
Computed: true,
},
"current_version": schema.StringAttribute{
Description: "The currently running version of OpenShift on the cluster, for example '4.11.0'.",
Computed: true,
},
"state": schema.StringAttribute{
Description: "State of the cluster.",
Computed: true,
},
"upgrade_acknowledgements_for": schema.StringAttribute{
Description: deprecatedMessage,
Computed: true,
},
// Deprecated Attributes:
// Those attributes were copied from cluster_rosa_clasic resource in order to use the same state struct.
// We can't change the rosa_classic struct to include Embedded Structs due to that issue: https://github.com/hashicorp/terraform-plugin-framework/issues/242
// If we will remove those attributes from the schema we will get a parsing error in the Read function
"disable_waiting_in_destroy": schema.BoolAttribute{
Description: deprecatedMessage,
Computed: true,
},
"destroy_timeout": schema.Int64Attribute{
Description: deprecatedMessage,
Computed: true,
},
"wait_for_create_complete": schema.BoolAttribute{
Description: deprecatedMessage,
Computed: true,
},
"wait_for_std_compute_nodes_complete": schema.BoolAttribute{
Description: deprecatedMessage,
Computed: true,
},
},
}
}
func (r *ClusterRosaHcpDatasource) Configure(ctx context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) {
// Prevent panic if the provider has not been configured.
if req.ProviderData == nil {
return
}
connection, ok := req.ProviderData.(*sdk.Connection)
if !ok {
resp.Diagnostics.AddError(
"Unexpected Resource Configure Type",
fmt.Sprintf("Expected *sdk.Connaction, got: %T. Please report this issue to the provider developers.", req.ProviderData),
)
return
}
r.clusterCollection = connection.ClustersMgmt().V1().Clusters()
r.versionCollection = connection.ClustersMgmt().V1().Versions()
r.clusterWait = common.NewClusterWait(r.clusterCollection)
}
func (r *ClusterRosaHcpDatasource) Read(ctx context.Context, request datasource.ReadRequest,
response *datasource.ReadResponse) {
tflog.Debug(ctx, "begin Read()")
// Get the current state:
state := &ClusterRosaHcpState{}
diags := request.Config.Get(ctx, state)
response.Diagnostics.Append(diags...)
if response.Diagnostics.HasError() {
return
}
// Find the cluster:
get, err := r.clusterCollection.Cluster(state.ID.ValueString()).Get().SendContext(ctx)
if err != nil {
if get.Status() == http.StatusNotFound {
tflog.Warn(ctx, fmt.Sprintf("cluster (%s) not found, removing from state",
state.ID.ValueString(),
))
response.State.RemoveResource(ctx)
return
}
response.Diagnostics.AddError(
"Can't find cluster",
fmt.Sprintf(
"Can't find cluster with identifier '%s': %v",
state.ID.ValueString(), err,
),
)
return
}
object := get.Body()
// Save the state:
err = populateRosaHcpClusterState(ctx, object, state, common.DefaultHttpClient{})
if err != nil {
response.Diagnostics.AddError(
"Can't populate cluster state",
fmt.Sprintf(
"Received error %v", err,
),
)
return
}
// set deprecated attributes to null:
state.DisableWaitingInDestroy = types.BoolNull()
state.ChannelGroup = types.StringNull()
state.Version = types.StringNull()
state.DestroyTimeout = types.Int64Null()
state.UpgradeAcksFor = types.StringNull()
state.WaitForCreateComplete = types.BoolNull()
state.WaitForStdComputeNodesComplete = types.BoolNull()
state.Replicas = types.Int64Null()
state.ComputeMachineType = types.StringNull()
diags = response.State.Set(ctx, state)
response.Diagnostics.Append(diags...)
}