Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[fix] Set default creation options when importing retool_space resource #34

Merged
merged 1 commit into from
Sep 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 38 additions & 16 deletions internal/provider/space/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/tryretool/terraform-provider-retool/internal/sdk/api"

"github.com/hashicorp/terraform-plugin-framework/attr"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
Expand Down Expand Up @@ -49,20 +50,11 @@ type spaceCreateOptionsModel struct {
CreateAdminUser types.Bool `tfsdk:"create_admin_user"`
}

// Create new Space resource.
func NewResource() resource.Resource {
return &spaceResource{}
}

func (r *spaceResource) Metadata(_ context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_space"
}

func (r *spaceResource) Schema(_ context.Context, _ resource.SchemaRequest, resp *resource.SchemaResponse) {
emptyList, diags := types.ListValue(types.StringType, []attr.Value{})
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
func getDefaultCreateOptions(diags *diag.Diagnostics) *basetypes.ObjectValue {
emptyList, localDiags := types.ListValue(types.StringType, []attr.Value{})
diags.Append(localDiags...)
if diags.HasError() {
return nil
}

defaultCreateOptionsTypes := map[string]attr.Type{
Expand All @@ -77,12 +69,35 @@ func (r *spaceResource) Schema(_ context.Context, _ resource.SchemaRequest, resp
"users_to_copy_as_admins": emptyList,
"create_admin_user": types.BoolValue(true),
}
defaultCreateOptions, diags := types.ObjectValue(defaultCreateOptionsTypes, defaultCreateOptionsValues)
defaultCreateOptions, localDiags := types.ObjectValue(defaultCreateOptionsTypes, defaultCreateOptionsValues)
diags.Append(localDiags...)
if diags.HasError() {
return nil
}
return &defaultCreateOptions
}

// Create new Space resource.
func NewResource() resource.Resource {
return &spaceResource{}
}

func (r *spaceResource) Metadata(_ context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_space"
}

func (r *spaceResource) Schema(_ context.Context, _ resource.SchemaRequest, resp *resource.SchemaResponse) {
emptyList, diags := types.ListValue(types.StringType, []attr.Value{})
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}

defaultCreateOptions := getDefaultCreateOptions(&resp.Diagnostics)
if resp.Diagnostics.HasError() || defaultCreateOptions == nil {
return
}

resp.Schema = schema.Schema{
Description: "Space resource allows you to create and manage Spaces in Retool. The provider must be configured using the hostname of the admin Space.",
Attributes: map[string]schema.Attribute{
Expand Down Expand Up @@ -144,7 +159,7 @@ func (r *spaceResource) Schema(_ context.Context, _ resource.SchemaRequest, resp
},
},
},
Default: objectdefault.StaticValue(defaultCreateOptions),
Default: objectdefault.StaticValue(*defaultCreateOptions),
PlanModifiers: []planmodifier.Object{
objectplanmodifier.UseStateForUnknown(),
objectplanmodifier.RequiresReplace(),
Expand Down Expand Up @@ -318,4 +333,11 @@ func (r *spaceResource) Delete(ctx context.Context, req resource.DeleteRequest,
func (r *spaceResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) {
// Retrieve import ID and save to id attribute.
resource.ImportStatePassthroughID(ctx, path.Root("id"), req, resp)

// Need to set default create options so that TF doesn't attempt to re-create the space.
defaultCreateOptions := getDefaultCreateOptions(&resp.Diagnostics)
if resp.Diagnostics.HasError() || defaultCreateOptions == nil {
return
}
resp.State.SetAttribute(ctx, path.Root("create_options"), *defaultCreateOptions)
}
26 changes: 26 additions & 0 deletions internal/provider/space/resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"testing"

"github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/hashicorp/terraform-plugin-testing/terraform"

"github.com/tryretool/terraform-provider-retool/internal/acctest"
)
Expand Down Expand Up @@ -69,6 +70,31 @@ func TestAccSpace(t *testing.T) {
{
ResourceName: "retool_space.test_space",
ImportState: true,
ImportStateCheck: func(state []*terraform.InstanceState) error {
if len(state) != 1 {
return fmt.Errorf("Unexpected number of objects in state %d", len(state))
}
stateObj := state[0]
if stateObj.Attributes["name"] != "tf-acc-test-space" {
return fmt.Errorf("Unexpected name %s", stateObj.Attributes["name"])
}
if stateObj.Attributes["domain"] != "tfspace.localhost" {
return fmt.Errorf("Unexpected domain %s", stateObj.Attributes["domain"])
}
if stateObj.Attributes["create_options.copy_sso_settings"] != "false" {
return fmt.Errorf("Unexpected copy_sso_settings %s", stateObj.Attributes["create_options.copy_sso_settings"])
}
if stateObj.Attributes["create_options.copy_branding_and_themes_settings"] != "false" {
return fmt.Errorf("Unexpected copy_branding_and_themes_settings %s", stateObj.Attributes["create_options.copy_branding_and_themes_settings"])
}
if stateObj.Attributes["create_options.create_admin_user"] != "true" {
return fmt.Errorf("Unexpected create_admin_user %s", stateObj.Attributes["create_options.create_admin_user"])
}
if stateObj.Attributes["create_options.users_to_copy_as_admins.#"] != "0" {
return fmt.Errorf("Unexpected users_to_copy_as_admins %s", stateObj.Attributes["create_options.users_to_copy_as_admins.#"])
}
return nil
},
},
// Update and Read.
{
Expand Down