-
Notifications
You must be signed in to change notification settings - Fork 0
/
clusters_data_source.go
164 lines (141 loc) · 5.6 KB
/
clusters_data_source.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
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package provider
import (
"context"
"fmt"
"strconv"
"time"
"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-log/tflog"
zilliz "github.com/zilliztech/terraform-provider-zillizcloud/client"
)
// Ensure provider defined types fully satisfy framework interfaces.
var _ datasource.DataSource = &ClustersDataSource{}
func NewClustersDataSource() datasource.DataSource {
return &ClustersDataSource{}
}
// ClusterDataSource defines the data source implementation.
type ClustersDataSource struct {
client *zilliz.Client
}
// ClustersDataSourceModel describes the clusters data model.
type ClustersDataSourceModel struct {
Clusters []ClusterDataSourceModel `tfsdk:"clusters"`
Id types.String `tfsdk:"id"`
}
func (d *ClustersDataSource) Metadata(ctx context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_clusters"
}
func (d *ClustersDataSource) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) {
resp.Schema = schema.Schema{
// This description is used by the documentation generator and the language server.
MarkdownDescription: "Cluster data source",
Attributes: map[string]schema.Attribute{
"clusters": schema.ListNestedAttribute{
MarkdownDescription: "List of Clusters",
Computed: true,
NestedObject: schema.NestedAttributeObject{
Attributes: map[string]schema.Attribute{
"id": schema.StringAttribute{
MarkdownDescription: "The ID of the cluster.",
Computed: true,
},
"cluster_name": schema.StringAttribute{
MarkdownDescription: "The name of the cluster.",
Computed: true,
},
"description": schema.StringAttribute{
MarkdownDescription: "An optional description about the cluster.",
Computed: true,
},
"region_id": schema.StringAttribute{
MarkdownDescription: "The ID of the region where the cluster exists.",
Computed: true,
},
"cluster_type": schema.StringAttribute{
MarkdownDescription: "The type of CU associated with the cluster. Possible values are Performance-optimized and Capacity-optimized.",
Computed: true,
},
"cu_size": schema.Int64Attribute{
MarkdownDescription: "The size of the CU associated with the cluster.",
Computed: true,
},
"status": schema.StringAttribute{
MarkdownDescription: "The current status of the cluster. Possible values are INITIALIZING, RUNNING, SUSPENDING, and RESUMING.",
Computed: true,
},
"connect_address": schema.StringAttribute{
MarkdownDescription: "The public endpoint of the cluster. You can connect to the cluster using this endpoint from the public network.",
Computed: true,
},
"private_link_address": schema.StringAttribute{
MarkdownDescription: "The private endpoint of the cluster. You can set up a private link to allow your VPS in the same cloud region to access your cluster.",
Computed: true,
},
"create_time": schema.StringAttribute{
MarkdownDescription: "The time at which the cluster has been created.",
Computed: true,
},
},
},
},
"id": schema.StringAttribute{
MarkdownDescription: "Clusters identifier",
Computed: true,
},
},
}
}
func (d *ClustersDataSource) Configure(ctx context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) {
// Prevent panic if the provider has not been configured.
if req.ProviderData == nil {
return
}
client, ok := req.ProviderData.(*zilliz.Client)
if !ok {
resp.Diagnostics.AddError(
"Unexpected Data Source Configure Type",
fmt.Sprintf("Expected *http.Client, got: %T. Please report this issue to the provider developers.", req.ProviderData),
)
return
}
d.client = client
}
func (d *ClustersDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
var state ClustersDataSourceModel
// Read Terraform configuration data into the model
resp.Diagnostics.Append(req.Config.Get(ctx, &state)...)
if resp.Diagnostics.HasError() {
return
}
tflog.Trace(ctx, "sending ListClusters request...")
clusters, err := d.client.ListClusters()
if err != nil {
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to ListClusters, got error: %s", err))
return
}
// Save data into Terraform state
state.Id = types.StringValue(strconv.FormatInt(time.Now().Unix(), 10))
// var cs []ClusterDataSourceModel
for _, c := range clusters.Clusters {
state.Clusters = append(state.Clusters, ClusterDataSourceModel{
ClusterId: types.StringValue(c.ClusterId),
ClusterName: types.StringValue(c.ClusterName),
Description: types.StringValue(c.Description),
RegionId: types.StringValue(c.RegionId),
ClusterType: types.StringValue(c.ClusterType),
CuSize: types.Int64Value(c.CuSize),
Status: types.StringValue(c.Status),
ConnectAddress: types.StringValue(c.ConnectAddress),
PrivateLinkAddress: types.StringValue(c.PrivateLinkAddress),
CreateTime: types.StringValue(c.CreateTime)})
}
diags := resp.State.Set(ctx, &state)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
}