@@ -2,12 +2,15 @@ package stringfunc
2
2
3
3
import (
4
4
"context"
5
+ "fmt"
6
+ "strings"
5
7
6
8
"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
7
9
"github.com/hashicorp/terraform-plugin-framework/datasource"
8
10
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
9
11
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
10
12
"github.com/hashicorp/terraform-plugin-framework/types"
13
+ "github.com/hashicorp/terraform-plugin-log/tflog"
11
14
12
15
util "github.com/mschuchard/terraform-provider-stdlib/internal"
13
16
)
@@ -52,7 +55,7 @@ func (*cutDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, resp
52
55
},
53
56
"separator" : schema.StringAttribute {
54
57
Description : "The separator for cutting the input string." ,
55
- Optional : true ,
58
+ Required : true ,
56
59
Validators : []validator.String {
57
60
stringvalidator .LengthAtLeast (1 ),
58
61
},
@@ -82,4 +85,30 @@ func (*cutDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp
82
85
if resp .Diagnostics .HasError () {
83
86
return
84
87
}
88
+
89
+ // initialize input string param and separator
90
+ inputString := state .Param .ValueString ()
91
+ separator := state .Separator .ValueString ()
92
+
93
+ // determine string cut
94
+ before , after , found := strings .Cut (inputString , separator )
95
+
96
+ // provide debug logging
97
+ ctx = tflog .SetField (ctx , "stdlib_cut_before" , before )
98
+ ctx = tflog .SetField (ctx , "stdlib_cut_after" , after )
99
+ ctx = tflog .SetField (ctx , "stdlib_cut_found" , found )
100
+ tflog .Debug (ctx , fmt .Sprintf ("Input string parameter \" %s\" with separator \" %s\" has before \" %s\" , after \" %s\" , and found \" %t\" " , inputString , separator , before , after , found ))
101
+
102
+ // store returned values in state
103
+ state .ID = types .StringValue (inputString )
104
+ state .Before = types .StringValue (before )
105
+ state .After = types .StringValue (after )
106
+ state .Found = types .BoolValue (found )
107
+
108
+ // set state
109
+ resp .Diagnostics .Append (resp .State .Set (ctx , & state )... )
110
+ if resp .Diagnostics .HasError () {
111
+ return
112
+ }
113
+ tflog .Info (ctx , "Determined string cut" , map [string ]any {"success" : true })
85
114
}
0 commit comments