|
| 1 | +// Copyright (c) HashiCorp, Inc. |
| 2 | +// SPDX-License-Identifier: MPL-2.0 |
| 3 | + |
| 4 | +// https://developer.hashicorp.com/terraform/tutorials/providers-plugin-framework/providers-plugin-framework-data-source-read |
| 5 | +// https://developer.hashicorp.com/terraform/plugin/framework/migrating |
| 6 | + |
| 7 | +package provider |
| 8 | + |
| 9 | +import ( |
| 10 | + "context" |
| 11 | + "fmt" |
| 12 | + |
| 13 | + "github.com/hashicorp/terraform-plugin-framework/attr" |
| 14 | + "github.com/hashicorp/terraform-plugin-framework/datasource" |
| 15 | + "github.com/hashicorp/terraform-plugin-framework/datasource/schema" |
| 16 | + "github.com/hashicorp/terraform-plugin-framework/types" |
| 17 | + "github.com/hashicorp/terraform-plugin-log/tflog" |
| 18 | + "github.com/hashicorp/terraform-provider-hypercore/internal/utils" |
| 19 | +) |
| 20 | + |
| 21 | +// Ensure the implementation satisfies the expected interfaces. |
| 22 | +var ( |
| 23 | + _ datasource.DataSource = &hypercoreRemoteClusterConnectionDataSource{} |
| 24 | + _ datasource.DataSourceWithConfigure = &hypercoreRemoteClusterConnectionDataSource{} |
| 25 | +) |
| 26 | + |
| 27 | +// NewHypercoreRemoteClusterConnectionDataSource is a helper function to simplify the provider implementation. |
| 28 | +func NewHypercoreRemoteClusterConnectionDataSource() datasource.DataSource { |
| 29 | + return &hypercoreRemoteClusterConnectionDataSource{} |
| 30 | +} |
| 31 | + |
| 32 | +// hypercoreRemoteClusterConnectionDataSource is the data source implementation. |
| 33 | +type hypercoreRemoteClusterConnectionDataSource struct { |
| 34 | + client *utils.RestClient |
| 35 | +} |
| 36 | + |
| 37 | +// coffeesDataSourceModel maps the data source schema data. |
| 38 | +type hypercoreRemoteClusterConnectionsDataSourceModel struct { |
| 39 | + FilterRemoteClusterName types.String `tfsdk:"remote_cluster_name"` |
| 40 | + RemoteClusterConnections []hypercoreRemoteClusterConnectionModel `tfsdk:"remote_clusters"` |
| 41 | +} |
| 42 | + |
| 43 | +// hypercoreVMModel maps VM schema data. |
| 44 | +type hypercoreRemoteClusterConnectionModel struct { |
| 45 | + UUID types.String `tfsdk:"uuid"` |
| 46 | + ClusterName types.String `tfsdk:"cluster_name"` |
| 47 | + ConnectionStatus types.String `tfsdk:"connection_status"` |
| 48 | + ReplicationOk types.Bool `tfsdk:"replication_ok"` |
| 49 | + RemoteNodeIPs types.List `tfsdk:"remote_node_ips"` |
| 50 | + RemoteNodeUUIDs types.List `tfsdk:"remote_node_uuids"` |
| 51 | +} |
| 52 | + |
| 53 | +// Metadata returns the data source type name. |
| 54 | +func (d *hypercoreRemoteClusterConnectionDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) { |
| 55 | + resp.TypeName = req.ProviderTypeName + "_remote_cluster_connection" |
| 56 | +} |
| 57 | + |
| 58 | +// Schema defines the schema for the data source. |
| 59 | +func (d *hypercoreRemoteClusterConnectionDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) { |
| 60 | + resp.Schema = schema.Schema{ |
| 61 | + Attributes: map[string]schema.Attribute{ |
| 62 | + "remote_cluster_name": schema.StringAttribute{ |
| 63 | + Optional: true, |
| 64 | + }, |
| 65 | + "remote_clusters": schema.ListNestedAttribute{ |
| 66 | + Computed: true, |
| 67 | + NestedObject: schema.NestedAttributeObject{ |
| 68 | + Attributes: map[string]schema.Attribute{ |
| 69 | + "uuid": schema.StringAttribute{ |
| 70 | + Computed: true, |
| 71 | + }, |
| 72 | + "cluster_name": schema.StringAttribute{ |
| 73 | + Computed: true, |
| 74 | + }, |
| 75 | + "connection_status": schema.StringAttribute{ |
| 76 | + Computed: true, |
| 77 | + }, |
| 78 | + "replication_ok": schema.BoolAttribute{ |
| 79 | + Computed: true, |
| 80 | + }, |
| 81 | + "remote_node_ips": schema.ListAttribute{ |
| 82 | + ElementType: types.StringType, |
| 83 | + Computed: true, |
| 84 | + }, |
| 85 | + "remote_node_uuids": schema.ListAttribute{ |
| 86 | + ElementType: types.StringType, |
| 87 | + Computed: true, |
| 88 | + }, |
| 89 | + }, |
| 90 | + }, |
| 91 | + }, |
| 92 | + }, |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +// Configure adds the provider configured client to the data source. |
| 97 | +func (d *hypercoreRemoteClusterConnectionDataSource) Configure(_ context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) { |
| 98 | + // Add a nil check when handling ProviderData because Terraform |
| 99 | + // sets that data after it calls the ConfigureProvider RPC. |
| 100 | + if req.ProviderData == nil { |
| 101 | + return |
| 102 | + } |
| 103 | + |
| 104 | + restClient, ok := req.ProviderData.(*utils.RestClient) |
| 105 | + if !ok { |
| 106 | + resp.Diagnostics.AddError( |
| 107 | + "Unexpected Data Source Configure Type", |
| 108 | + fmt.Sprintf("Expected *http.Client, got: %T. Please report this issue to the provider developers.", req.ProviderData), |
| 109 | + ) |
| 110 | + return |
| 111 | + } |
| 112 | + |
| 113 | + d.client = restClient |
| 114 | +} |
| 115 | + |
| 116 | +// Read refreshes the Terraform state with the latest data. |
| 117 | +func (d *hypercoreRemoteClusterConnectionDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) { |
| 118 | + var conf hypercoreRemoteClusterConnectionsDataSourceModel |
| 119 | + req.Config.Get(ctx, &conf) |
| 120 | + |
| 121 | + filterName := conf.FilterRemoteClusterName.ValueString() |
| 122 | + |
| 123 | + query := map[string]any{} |
| 124 | + if filterName != "" { |
| 125 | + query = map[string]any{ |
| 126 | + "remoteClusterInfo": map[string]any{ |
| 127 | + "clusterName": filterName, |
| 128 | + }, |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + hc3RemoteClusters := d.client.ListRecords( |
| 133 | + "/rest/v1/RemoteClusterConnection", |
| 134 | + query, |
| 135 | + -1.0, |
| 136 | + true, |
| 137 | + ) |
| 138 | + |
| 139 | + tflog.Info(ctx, fmt.Sprintf("TTRT: filter_name=%v remote_cluster_count=%d\n", filterName, len(hc3RemoteClusters))) |
| 140 | + |
| 141 | + var state hypercoreRemoteClusterConnectionsDataSourceModel |
| 142 | + for _, remoteCluster := range hc3RemoteClusters { |
| 143 | + remoteClusterInfo := utils.AnyToMap(remoteCluster["remoteClusterInfo"]) |
| 144 | + |
| 145 | + remoteNodeIPs := utils.AnyToListOfStrings(remoteCluster["remoteNodeIPs"]) |
| 146 | + remoteNodeUUIDs := utils.AnyToListOfStrings(remoteCluster["remoteNodeUUIDs"]) |
| 147 | + |
| 148 | + // Go list of string to Terraform list of string |
| 149 | + remoteNodeIPsValues := make([]attr.Value, len(remoteNodeIPs)) |
| 150 | + for i, remoteIP := range remoteNodeIPs { |
| 151 | + remoteNodeIPsValues[i] = types.StringValue(remoteIP) |
| 152 | + } |
| 153 | + remoteNodeUUIDsValues := make([]attr.Value, len(remoteNodeUUIDs)) |
| 154 | + for i, remoteUUID := range remoteNodeUUIDs { |
| 155 | + remoteNodeUUIDsValues[i] = types.StringValue(remoteUUID) |
| 156 | + } |
| 157 | + |
| 158 | + tfRemoteNodeIPs, _diag := types.ListValue(types.StringType, remoteNodeIPsValues) |
| 159 | + if _diag.HasError() { |
| 160 | + resp.Diagnostics.Append(_diag...) |
| 161 | + return |
| 162 | + } |
| 163 | + tfRemoteNodeUUIDs, _diag := types.ListValue(types.StringType, remoteNodeUUIDsValues) |
| 164 | + if _diag.HasError() { |
| 165 | + resp.Diagnostics.Append(_diag...) |
| 166 | + return |
| 167 | + } |
| 168 | + |
| 169 | + // Save into state |
| 170 | + hypercoreRemoteClusterConnectionState := hypercoreRemoteClusterConnectionModel{ |
| 171 | + UUID: types.StringValue(utils.AnyToString(remoteCluster["uuid"])), |
| 172 | + ClusterName: types.StringValue(utils.AnyToString(remoteClusterInfo["clusterName"])), |
| 173 | + ConnectionStatus: types.StringValue(utils.AnyToString(remoteCluster["connectionStatus"])), |
| 174 | + ReplicationOk: types.BoolValue(utils.AnyToBool(remoteCluster["replicationOK"])), |
| 175 | + RemoteNodeIPs: tfRemoteNodeIPs, |
| 176 | + RemoteNodeUUIDs: tfRemoteNodeUUIDs, |
| 177 | + } |
| 178 | + state.RemoteClusterConnections = append(state.RemoteClusterConnections, hypercoreRemoteClusterConnectionState) |
| 179 | + } |
| 180 | + |
| 181 | + // Set state |
| 182 | + diags := resp.State.Set(ctx, &state) |
| 183 | + resp.Diagnostics.Append(diags...) |
| 184 | + if resp.Diagnostics.HasError() { |
| 185 | + return |
| 186 | + } |
| 187 | +} |
0 commit comments