-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #161 from alrayyes/feat/implement-crud-warnings
feat(docs): let end user know which actions aren't supporetd
- Loading branch information
Showing
8 changed files
with
182 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package utils | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"slices" | ||
|
||
"github.com/hashicorp/terraform-plugin-framework/resource" | ||
) | ||
|
||
type Action int | ||
|
||
const ( | ||
CreateAction Action = iota | ||
ReadAction | ||
UpdateAction | ||
DeleteAction | ||
) | ||
|
||
func (a Action) string(unsupportedActions []Action) string { | ||
var secondAction string | ||
var format = "Once %s, this resource cannot be %s" | ||
|
||
switch a { | ||
case CreateAction: | ||
secondAction = "created" | ||
case ReadAction: | ||
secondAction = "read" | ||
case UpdateAction: | ||
secondAction = "updated" | ||
case DeleteAction: | ||
secondAction = "deleted" | ||
default: | ||
log.Fatal(fmt.Printf("do not know how to handle action: %q", a)) | ||
} | ||
|
||
return fmt.Sprintf( | ||
format, | ||
a.firstAction(unsupportedActions), | ||
secondAction, | ||
) | ||
} | ||
|
||
func (a Action) firstAction(unsupportedActions []Action) string { | ||
if slices.Contains(unsupportedActions, CreateAction) { | ||
return "imported" | ||
} | ||
|
||
return "created" | ||
} | ||
|
||
// AddUnsupportedActionsNotation lets the end user know which actions aren't supported in the markdown description. | ||
func AddUnsupportedActionsNotation( | ||
response *resource.SchemaResponse, | ||
unsupportedActions []Action, | ||
) { | ||
response.Schema.MarkdownDescription += "**Note:**" | ||
for _, action := range unsupportedActions { | ||
response.Schema.MarkdownDescription += fmt.Sprintf( | ||
"\n- %s.", | ||
action.string(unsupportedActions), | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
package utils | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform-plugin-framework/resource" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestAction_firstAction(t *testing.T) { | ||
t.Run( | ||
"return `imported` when unsupported actions contain `createAction`", | ||
func(t *testing.T) { | ||
got := CreateAction.firstAction([]Action{CreateAction}) | ||
want := "imported" | ||
|
||
assert.Equal(t, want, got) | ||
}, | ||
) | ||
|
||
t.Run( | ||
"return `created` when unsupported actions do not contain `createAction`", | ||
func(t *testing.T) { | ||
got := CreateAction.firstAction([]Action{CreateAction}) | ||
want := "imported" | ||
|
||
assert.Equal(t, want, got) | ||
}, | ||
) | ||
|
||
} | ||
|
||
func TestAction_string(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
a Action | ||
want string | ||
}{ | ||
{ | ||
name: "create action", | ||
a: CreateAction, | ||
want: "Once created, this resource cannot be created", | ||
}, | ||
{ | ||
name: "read action", | ||
a: ReadAction, | ||
want: "Once created, this resource cannot be read", | ||
}, | ||
{ | ||
name: "update action", | ||
a: UpdateAction, | ||
want: "Once created, this resource cannot be updated", | ||
}, | ||
{ | ||
name: "delete action", | ||
a: DeleteAction, | ||
want: "Once created, this resource cannot be deleted", | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
got := tt.a.string(nil) | ||
assert.Equal(t, tt.want, got) | ||
}) | ||
} | ||
} | ||
|
||
func TestAddUnsupportedActionsNotation(t *testing.T) { | ||
response := resource.SchemaResponse{} | ||
AddUnsupportedActionsNotation(&response, []Action{UpdateAction}) | ||
|
||
assert.Equal( | ||
t, | ||
"**Note:**\n- Once created, this resource cannot be updated.", | ||
response.Schema.GetMarkdownDescription(), | ||
) | ||
} | ||
|
||
func ExampleAddUnsupportedActionsNotation() { | ||
response := resource.SchemaResponse{} | ||
AddUnsupportedActionsNotation(&response, []Action{UpdateAction}) | ||
|
||
fmt.Println(response.Schema.GetMarkdownDescription()) | ||
// Output: | ||
// **Note:** | ||
// - Once created, this resource cannot be updated. | ||
} |