Skip to content

Commit

Permalink
Merge pull request #161 from alrayyes/feat/implement-crud-warnings
Browse files Browse the repository at this point in the history
feat(docs): let end user know which actions aren't supporetd
  • Loading branch information
alrayyes authored Nov 19, 2024
2 parents b392033 + dd66460 commit b09c20c
Show file tree
Hide file tree
Showing 8 changed files with 182 additions and 10 deletions.
7 changes: 5 additions & 2 deletions docs/resources/dedicated_server.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@
page_title: "leaseweb_dedicated_server Resource - leaseweb"
subcategory: ""
description: |-
Note:
Once imported, this resource cannot be created.Once imported, this resource cannot be deleted.
---

# leaseweb_dedicated_server (Resource)


**Note:**
- Once imported, this resource cannot be created.
- Once imported, this resource cannot be deleted.

## Example Usage

Expand Down
8 changes: 6 additions & 2 deletions docs/resources/dedicated_server_installation.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,16 @@
page_title: "leaseweb_dedicated_server_installation Resource - leaseweb"
subcategory: ""
description: |-
Note:
Once created, this resource cannot be read.Once created, this resource cannot be updated.Once created, this resource cannot be deleted.
---

# leaseweb_dedicated_server_installation (Resource)


**Note:**
- Once created, this resource cannot be read.
- Once created, this resource cannot be updated.
- Once created, this resource cannot be deleted.

## Example Usage

Expand Down
6 changes: 4 additions & 2 deletions docs/resources/public_cloud_image.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
page_title: "leaseweb_public_cloud_image Resource - leaseweb"
subcategory: ""
description: |-
Once created, an image resource cannot be deleted via Terraform
Note:
Once created, this resource cannot be deleted.
---

# leaseweb_public_cloud_image (Resource)

Once created, an image resource cannot be deleted via Terraform
**Note:**
- Once created, this resource cannot be deleted.

## Example Usage

Expand Down
5 changes: 4 additions & 1 deletion internal/provider/dedicatedserver/installation_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,10 @@ func (i *installationResource) Schema(
},
}

utils.AddUnsupportedActionsNotation(
resp,
[]utils.Action{utils.ReadAction, utils.UpdateAction, utils.DeleteAction},
)
}

func (i *installationResource) Create(
Expand Down Expand Up @@ -409,7 +413,6 @@ func (i *installationResource) Create(
plan.Partitions = partitionsList

resp.Diagnostics.Append(resp.State.Set(ctx, plan)...)

}

func (i *installationResource) Read(
Expand Down
8 changes: 6 additions & 2 deletions internal/provider/dedicatedserver/server_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,11 @@ func (s *serverResource) Schema(
},
},
}

utils.AddUnsupportedActionsNotation(
resp,
[]utils.Action{utils.CreateAction, utils.DeleteAction},
)
}

func (s *serverResource) Read(
Expand Down Expand Up @@ -464,15 +469,14 @@ func (s *serverResource) Create(
_ resource.CreateRequest,
_ *resource.CreateResponse,
) {
panic("unimplemented")

}

func (s *serverResource) Delete(
_ context.Context,
_ resource.DeleteRequest,
_ *resource.DeleteResponse,
) {
panic("unimplemented")
}

func (s *serverResource) getServer(
Expand Down
6 changes: 5 additions & 1 deletion internal/provider/publiccloud/image_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,6 @@ func (i *imageResource) Schema(
response *resource.SchemaResponse,
) {
response.Schema = schema.Schema{
Description: "Once created, an image resource cannot be deleted via Terraform",
Attributes: map[string]schema.Attribute{
"id": schema.StringAttribute{
Computed: true,
Expand Down Expand Up @@ -203,6 +202,11 @@ The id of the instance which the custom image is based on. The following rules a
},
},
}

utils.AddUnsupportedActionsNotation(
response,
[]utils.Action{utils.DeleteAction},
)
}

func (i *imageResource) Create(
Expand Down
64 changes: 64 additions & 0 deletions internal/utils/schema_helpers.go
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),
)
}
}
88 changes: 88 additions & 0 deletions internal/utils/schema_helpers_test.go
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.
}

0 comments on commit b09c20c

Please sign in to comment.