@@ -7,13 +7,25 @@ import (
77 "github.com/atko-pam/pam-sdk-go/client/pam"
88 "github.com/hashicorp/terraform-plugin-sdk/v2/diag"
99 "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
10- "github.com/mitchellh/mapstructure"
1110 "github.com/okta/terraform-provider-oktapam/oktapam/client"
1211 "github.com/okta/terraform-provider-oktapam/oktapam/client/wrappers"
1312 "github.com/okta/terraform-provider-oktapam/oktapam/constants/attributes"
1413 "github.com/okta/terraform-provider-oktapam/oktapam/constants/descriptions"
14+ "github.com/okta/terraform-provider-oktapam/oktapam/utils"
1515)
1616
17+ var commandTypeMap = map [string ]pam.CommandType {
18+ "raw" : pam .CommandType_RAW ,
19+ "executable" : pam .CommandType_EXECUTABLE ,
20+ "directory" : pam .CommandType_DIRECTORY ,
21+ }
22+
23+ var argsTypeMap = map [string ]pam.ArgsType {
24+ "custom" : pam .ArgsType_CUSTOM ,
25+ "any" : pam .ArgsType_ANY ,
26+ "none" : pam .ArgsType_NONE ,
27+ }
28+
1729func resourceSudoCommandBundle () * schema.Resource {
1830 return & schema.Resource {
1931 Description : descriptions .ResourceSudoCommandsBundle ,
@@ -122,36 +134,60 @@ func readSudoCommandBundleFromResource(d *schema.ResourceData) (*pam.SudoCommand
122134 var diags diag.Diagnostics
123135 var structuredCommands []pam.SudoCommandBundleStructuredCommandsInner
124136 if scs , ok := d .GetOk (attributes .StructuredCommands ); ok {
125- structuredCommandResources , ok := scs .([]interface {} )
137+ structuredCommandResources , ok := scs .([]any )
126138 if ! ok {
127139 diags = append (diags , diag.Diagnostic {
128140 Severity : diag .Error ,
129141 Summary : fmt .Sprintf ("%s is invalid" , attributes .StructuredCommands ),
130142 })
131143 }
132- for _ , sc := range structuredCommandResources {
133- var structuredCommandResource pam.SudoCommandBundleStructuredCommandsInner
134- cfg := & mapstructure.DecoderConfig {
135- Metadata : nil ,
136- Result : & structuredCommandResource ,
137- TagName : "json" ,
144+ for _ , scI := range structuredCommandResources {
145+ sc , ok := scI .(map [string ]any )
146+ if ! ok {
147+ diags = append (diags , diag.Diagnostic {
148+ Severity : diag .Error ,
149+ Summary : fmt .Sprintf ("%s is invalid" , attributes .StructuredCommands ),
150+ })
138151 }
139- decoder , dErr := mapstructure .NewDecoder (cfg )
140- if dErr != nil {
152+
153+ command , ok := sc [attributes .StructuredCommand ].(string )
154+ if ! ok {
141155 diags = append (diags , diag.Diagnostic {
142156 Severity : diag .Error ,
143- Summary : fmt .Sprintf ("error creating decoder for %s " , attributes .StructuredCommands ),
157+ Summary : fmt .Sprintf ("%s is invalid " , attributes .StructuredCommand ),
144158 })
145- continue
146159 }
147- if dErr := decoder .Decode (sc ); dErr != nil {
160+
161+ structuredCommand := pam.SudoCommandBundleStructuredCommandsInner {
162+ Command : command ,
163+ }
164+
165+ commandTypeStr , ok := sc [attributes .StructuredCommandType ].(string )
166+ if ! ok {
148167 diags = append (diags , diag.Diagnostic {
149168 Severity : diag .Error ,
150- Summary : fmt .Sprintf ("error decoding for %s " , attributes .StructuredCommands ),
169+ Summary : fmt .Sprintf ("%s is invalid " , attributes .StructuredCommandType ),
151170 })
152- continue
153171 }
154- structuredCommands = append (structuredCommands , structuredCommandResource )
172+
173+ structuredCommand .CommandType = commandTypeMap [commandTypeStr ]
174+
175+ if args , ok := sc [attributes .StructuredCommandArgs ].(string ); ok {
176+ structuredCommand .Args = utils .AsStringPtrZero (args , false )
177+ }
178+
179+ argsTypeStr , ok := sc [attributes .StructuredCommandArgsType ].(string )
180+ if ! ok {
181+ diags = append (diags , diag.Diagnostic {
182+ Severity : diag .Error ,
183+ Summary : fmt .Sprintf ("%s is invalid" , attributes .StructuredCommandArgsType ),
184+ })
185+ }
186+
187+ argsType := argsTypeMap [argsTypeStr ]
188+ structuredCommand .ArgsType = & argsType
189+
190+ structuredCommands = append (structuredCommands , structuredCommand )
155191 }
156192 }
157193
0 commit comments