Skip to content

Commit 263abf4

Browse files
Merge pull request #135 from okta/ulfat/renameUAMDisplayNameToSudoDisplayName
Fix args type bug
2 parents a383cfc + 3ee4c30 commit 263abf4

File tree

3 files changed

+92
-24
lines changed

3 files changed

+92
-24
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ require (
1212
github.com/hashicorp/terraform-plugin-docs v0.8.1
1313
github.com/hashicorp/terraform-plugin-sdk/v2 v2.30.0
1414
github.com/kylelemons/godebug v1.1.0
15-
github.com/mitchellh/mapstructure v1.5.0
1615
github.com/tomnomnom/linkheader v0.0.0-20180905144013-02ca5825eb80
1716
gopkg.in/square/go-jose.v2 v2.6.0
1817
)
@@ -55,6 +54,7 @@ require (
5554
github.com/mitchellh/copystructure v1.2.0 // indirect
5655
github.com/mitchellh/go-testing-interface v1.14.1 // indirect
5756
github.com/mitchellh/go-wordwrap v1.0.1 // indirect
57+
github.com/mitchellh/mapstructure v1.5.0 // indirect
5858
github.com/mitchellh/reflectwalk v1.0.2 // indirect
5959
github.com/oklog/run v1.0.0 // indirect
6060
github.com/opentracing/opentracing-go v1.2.0 // indirect

oktapam/resource_sudo_command_bundle.go

Lines changed: 52 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
1729
func 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

oktapam/resource_sudo_command_bundle_test.go

Lines changed: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,31 @@ func TestAccResourceSudoCommandBundle(t *testing.T) {
3131
),
3232
},
3333
{
34-
Config: createTestAccSudoCommandBundleUpdateConfig(sudoCommandBundleName),
34+
Config: createTestAccSudoCommandBundleUpdateConfigWithoutArgs(sudoCommandBundleName, "/bin/", "directory"),
3535
Check: resource.ComposeAggregateTestCheckFunc(
3636
resource.TestCheckResourceAttr(resourceName, attributes.Name, sudoCommandBundleName),
3737
resource.TestCheckResourceAttr(resourceName, fmt.Sprintf("%s.0.%s", attributes.StructuredCommands, attributes.StructuredCommandType), "directory"),
3838
resource.TestCheckResourceAttr(resourceName, fmt.Sprintf("%s.0.%s", attributes.StructuredCommands, attributes.StructuredCommand), "/bin/"),
3939
),
4040
},
41+
{
42+
Config: createTestAccSudoCommandBundleUpdateConfigWithoutArgs(sudoCommandBundleName, "/bin/ls", "raw"),
43+
Check: resource.ComposeAggregateTestCheckFunc(
44+
resource.TestCheckResourceAttr(resourceName, attributes.Name, sudoCommandBundleName),
45+
resource.TestCheckResourceAttr(resourceName, fmt.Sprintf("%s.0.%s", attributes.StructuredCommands, attributes.StructuredCommandType), "raw"),
46+
resource.TestCheckResourceAttr(resourceName, fmt.Sprintf("%s.0.%s", attributes.StructuredCommands, attributes.StructuredCommand), "/bin/ls"),
47+
),
48+
},
49+
{
50+
Config: createTestAccSudoCommandBundleUpdateConfigWithArgs(sudoCommandBundleName, "/bin/touch", "executable", "custom", "test.txt"),
51+
Check: resource.ComposeAggregateTestCheckFunc(
52+
resource.TestCheckResourceAttr(resourceName, attributes.Name, sudoCommandBundleName),
53+
resource.TestCheckResourceAttr(resourceName, fmt.Sprintf("%s.0.%s", attributes.StructuredCommands, attributes.StructuredCommandType), "executable"),
54+
resource.TestCheckResourceAttr(resourceName, fmt.Sprintf("%s.0.%s", attributes.StructuredCommands, attributes.StructuredCommand), "/bin/touch"),
55+
resource.TestCheckResourceAttr(resourceName, fmt.Sprintf("%s.0.%s", attributes.StructuredCommands, attributes.StructuredCommandArgsType), "custom"),
56+
resource.TestCheckResourceAttr(resourceName, fmt.Sprintf("%s.0.%s", attributes.StructuredCommands, attributes.StructuredCommandArgs), "test.txt"),
57+
),
58+
},
4159
},
4260
})
4361
}
@@ -56,23 +74,37 @@ func createTestAccSudoCommandBundleCreateConfig(name string) string {
5674
structured_commands {
5775
command = "/bin/run.sh"
5876
command_type = "executable"
59-
args_type = "custom"
60-
args = "ls"
77+
args_type = "any"
6178
}
6279
}
6380
`
6481
return fmt.Sprintf(format, name)
6582
}
6683

67-
func createTestAccSudoCommandBundleUpdateConfig(name string) string {
84+
func createTestAccSudoCommandBundleUpdateConfigWithArgs(name, command, commandType, argsType, args string) string {
6885
const format = `
6986
resource "oktapam_sudo_command_bundle" "test_acc_sudo_command_bundle" {
7087
name = "%s"
7188
structured_commands {
72-
command = "/bin/"
73-
command_type = "directory"
89+
command = "%s"
90+
command_type = "%s"
91+
args_type = "%s"
92+
args = "%s"
7493
}
7594
}
7695
`
77-
return fmt.Sprintf(format, name)
96+
return fmt.Sprintf(format, name, command, commandType, argsType, args)
97+
}
98+
99+
func createTestAccSudoCommandBundleUpdateConfigWithoutArgs(name, command, commandType string) string {
100+
const format = `
101+
resource "oktapam_sudo_command_bundle" "test_acc_sudo_command_bundle" {
102+
name = "%s"
103+
structured_commands {
104+
command = "%s"
105+
command_type = "%s"
106+
}
107+
}
108+
`
109+
return fmt.Sprintf(format, name, command, commandType)
78110
}

0 commit comments

Comments
 (0)