Skip to content

Commit

Permalink
forbid importing root group as resource
Browse files Browse the repository at this point in the history
  • Loading branch information
1riatsila1 committed Sep 3, 2024
1 parent bf34ec1 commit a892576
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
10 changes: 9 additions & 1 deletion pkg/config-api-provider/provider/resources/group.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func (r *groupResource) Schema(_ context.Context, _ resource.SchemaRequest, resp
Required: true,
},
"parent_group_id": schema.StringAttribute{
Optional: true,
Required: true,
PlanModifiers: []planmodifier.String{
// UXI business logic does not permit moving of groups
stringplanmodifier.RequiresReplace(),
Expand Down Expand Up @@ -108,6 +108,9 @@ func (r *groupResource) Read(ctx context.Context, req resource.ReadRequest, resp
// Get current state
var state groupResourceModel
diags := req.State.Get(ctx, &state)
if state.ID.ValueString() == GetRootGroupUID() {
diags.AddError("operation not supported", "the root node cannot be used as a resource")
}
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
Expand Down Expand Up @@ -209,3 +212,8 @@ var UpdateGroup = func(request GroupUpdateRequestModel) GroupResponseModel {
Path: "mock_path",
}
}

var GetRootGroupUID = func() string {
// Get root node here using the client
return "root_group_uid"
}
27 changes: 27 additions & 0 deletions pkg/config-api-provider/test/group_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package test

import (
"regexp"
"testing"

"github.com/aruba-uxi/configuration-api-terraform-provider/pkg/terraform-provider-configuration/provider/resources"
Expand Down Expand Up @@ -94,3 +95,29 @@ func TestGroupResource(t *testing.T) {
},
})
}

func TestRootGroupResource(t *testing.T) {

resource.Test(t, resource.TestCase{
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
Steps: []resource.TestStep{
// Importing the root group does not work
{
PreConfig: func() {
resources.GetRootGroupUID = func() string { return "my_root_group_uid" }
},
Config: providerConfig + `
resource "uxi_group" "my_root_group" {
name = "name"
parent_group_id = "some_random_string"
}
import {
to = uxi_group.my_root_group
id = "my_root_group_uid"
}`,
ExpectError: regexp.MustCompile(`the root node cannot be used as a resource`),
},
},
})
}

0 comments on commit a892576

Please sign in to comment.