Skip to content

Commit 15196cd

Browse files
committed
improving read implementation
1 parent b3f55cc commit 15196cd

File tree

2 files changed

+47
-4
lines changed

2 files changed

+47
-4
lines changed

internal/powerplatform/services/data_record/api_data_record.go

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,12 @@ import (
1010
"net/http"
1111
"net/url"
1212
"regexp"
13+
"strconv"
1314
"strings"
1415

16+
"github.com/hashicorp/terraform-plugin-framework/attr"
17+
"github.com/hashicorp/terraform-plugin-framework/types"
18+
"github.com/hashicorp/terraform-plugin-framework/types/basetypes"
1519
constants "github.com/microsoft/terraform-provider-power-platform/constants"
1620
api "github.com/microsoft/terraform-provider-power-platform/internal/powerplatform/api"
1721
)
@@ -96,7 +100,7 @@ func (client *DataRecordClient) getEnvironment(ctx context.Context, environmentI
96100
return &env, nil
97101
}
98102

99-
func (client *DataRecordClient) GetDataRecord(ctx context.Context, recordId string, environmentId string, tableName string) (*map[string]interface{}, error) {
103+
func (client *DataRecordClient) GetDataRecord(ctx context.Context, recordId string, environmentId string, tableName string, columns types.Dynamic) (*basetypes.DynamicValue, error) {
100104
environmentUrl, err := client.GetEnvironmentUrlById(ctx, environmentId)
101105
if err != nil {
102106
return nil, err
@@ -118,7 +122,46 @@ func (client *DataRecordClient) GetDataRecord(ctx context.Context, recordId stri
118122
return nil, err
119123
}
120124

121-
return &result, nil
125+
var mapColumns map[string]interface{}
126+
jsonColumns, _ := json.Marshal(columns.String())
127+
unquotedJsonColumns, _ := strconv.Unquote(string(jsonColumns))
128+
json.Unmarshal([]byte(unquotedJsonColumns), &mapColumns)
129+
130+
attributeTypes := make(map[string]attr.Type)
131+
attributes := make(map[string]attr.Value)
132+
133+
for key, value := range mapColumns {
134+
switch value.(type) {
135+
case bool:
136+
v, ok := result[key].(bool)
137+
if ok {
138+
attributeTypes[key] = types.BoolType
139+
attributes[key] = types.BoolValue(v)
140+
}
141+
case int64:
142+
v, ok := result[key].(int64)
143+
if ok {
144+
attributeTypes[key] = types.Int64Type
145+
attributes[key] = types.Int64Value(v)
146+
}
147+
case float64:
148+
v, ok := result[key].(float64)
149+
if ok {
150+
attributeTypes[key] = types.Float64Type
151+
attributes[key] = types.Float64Value(v)
152+
}
153+
case string:
154+
v, ok := result[key].(string)
155+
if ok {
156+
attributeTypes[key] = types.StringType
157+
attributes[key] = types.StringValue(v)
158+
}
159+
}
160+
}
161+
stateValue, _ := types.ObjectValue(attributeTypes, attributes)
162+
newState := basetypes.NewDynamicValue(stateValue)
163+
164+
return &newState, nil
122165
}
123166

124167
func (client *DataRecordClient) ApplyDataRecord(ctx context.Context, recordId string, environmentId string, tableName string, columns map[string]interface{}) (*DataRecordDto, error) {

internal/powerplatform/services/data_record/resource_data_record.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,15 +147,15 @@ func (r *DataRecordResource) Read(ctx context.Context, req resource.ReadRequest,
147147
return
148148
}
149149

150-
_, err := r.DataRecordClient.GetDataRecord(ctx, state.Id.ValueString(), state.EnvironmentId.ValueString(), state.TableLogicalName.ValueString())
150+
newState, err := r.DataRecordClient.GetDataRecord(ctx, state.Id.ValueString(), state.EnvironmentId.ValueString(), state.TableLogicalName.ValueString(), state.Columns)
151151
if err != nil {
152152
resp.Diagnostics.AddError(fmt.Sprintf("Client error when reading %s", r.ProviderTypeName), err.Error())
153153
return
154154
}
155155

156156
tflog.Debug(ctx, fmt.Sprintf("READ: %s_data_record with table_name %s", r.ProviderTypeName, state.TableLogicalName.ValueString()))
157157

158-
resp.Diagnostics.Append(resp.State.Set(ctx, &state)...)
158+
resp.Diagnostics.Append(resp.State.Set(ctx, &newState)...)
159159

160160
tflog.Debug(ctx, fmt.Sprintf("READ RESOURCE END: %s", r.ProviderTypeName))
161161
}

0 commit comments

Comments
 (0)