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: disallow root group imports #33

Merged
merged 3 commits into from
Oct 2, 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
3 changes: 0 additions & 3 deletions pkg/config-api-provider/examples/full-demo/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,8 @@ provider "uxi" {
token_url = "https://test.sso.common.cloud.hpe.com/as/token.oauth2"
}

data "uxi_root_group" "my_root_group" {}

resource "uxi_group" "my_group" {
name = "parent"
parent_group_id = data.uxi_root_group.my_root_group.id
}

resource "uxi_group" "my_group_2" {
Expand Down
31 changes: 10 additions & 21 deletions pkg/config-api-provider/provider/data-sources/group.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ type groupDataSourceModel struct {
Name types.String `tfsdk:"name"`
Filter struct {
GroupID *string `tfsdk:"group_id"`
IsRoot *bool `tfsdk:"is_root"`
} `tfsdk:"filter"`
}

Expand All @@ -46,7 +45,6 @@ func (d *groupDataSource) Schema(_ context.Context, _ datasource.SchemaRequest,
},
"path": schema.StringAttribute{
Computed: true,
Optional: true,
},
"parent_group_id": schema.StringAttribute{
Computed: true,
Expand All @@ -58,10 +56,7 @@ func (d *groupDataSource) Schema(_ context.Context, _ datasource.SchemaRequest,
Required: true,
Attributes: map[string]schema.Attribute{
"group_id": schema.StringAttribute{
Optional: true,
},
"is_root": schema.BoolAttribute{
Optional: true,
Required: true,
},
},
},
Expand All @@ -74,23 +69,14 @@ func (d *groupDataSource) Read(ctx context.Context, req datasource.ReadRequest,

// Read configuration from request
diags := req.Config.Get(ctx, &state)
if state.Filter.GroupID == nil && (state.Filter.IsRoot == nil || !*state.Filter.IsRoot) {
diags.AddError("invalid Group data source", "either filter.group_id must be set or 'filter.is_root = true' is required")
} else if state.Filter.GroupID != nil && state.Filter.IsRoot != nil && *state.Filter.IsRoot {
diags.AddError("invalid Group data source", "group_id and 'is_root = true' cannot both be set")
}
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}

request := d.client.ConfigurationAPI.GroupsGetUxiV1alpha1GroupsGet(context.Background())

if state.Filter.IsRoot != nil && *state.Filter.IsRoot {
request = request.Uid(*state.Filter.GroupID) // TODO: use root group filter here
} else {
request = request.Uid(*state.Filter.GroupID)
}
request := d.client.ConfigurationAPI.
GroupsGetUxiV1alpha1GroupsGet(context.Background()).
Uid(*state.Filter.GroupID)

groupResponse, _, err := util.RetryFor429(request.Execute)

Expand All @@ -103,12 +89,15 @@ func (d *groupDataSource) Read(ctx context.Context, req datasource.ReadRequest,
}

group := groupResponse.Items[0]
if util.IsRoot(group) {
resp.Diagnostics.AddError("operation not supported", "the root group cannot be used as a data source")
return
}

state.ID = types.StringValue(group.Id)
state.Name = types.StringValue(group.Name)
state.Path = types.StringValue(group.Path)
if group.Parent.IsSet() {
state.ParentGroupID = types.StringValue(group.Parent.Get().Id)
}
state.ParentGroupID = types.StringValue(group.Parent.Get().Id)

// Set state
diags = resp.State.Set(ctx, &state)
Expand Down
14 changes: 5 additions & 9 deletions pkg/config-api-provider/provider/resources/group.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,6 @@ 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 All @@ -146,9 +143,13 @@ func (r *groupResource) Read(ctx context.Context, req resource.ReadRequest, resp
)
return
}

group := groupResponse.Items[0]

if util.IsRoot(group) {
resp.Diagnostics.AddError("operation not supported", "the root group cannot be used as a resource")
return
}

// Update state from client response
state.Name = types.StringValue(group.Name)
state.ParentGroupId = types.StringValue(group.Parent.Get().Id)
Expand Down Expand Up @@ -215,8 +216,3 @@ var UpdateGroup = func(request GroupUpdateRequestModel) config_api_client.Groups
Path: "mock_path",
}
}

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

import (
"github.com/aruba-uxi/configuration-api-terraform-provider/pkg/config-api-client"
)

func IsRoot(group config_api_client.GroupsGetItem) bool {
_, set := group.Parent.Get().GetIdOk()
return !set
}
50 changes: 14 additions & 36 deletions pkg/config-api-provider/test/data-sources/group_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package data_source_test

import (
config_api_client "github.com/aruba-uxi/configuration-api-terraform-provider/pkg/config-api-client"
"regexp"
"testing"

Expand All @@ -19,31 +20,7 @@ func TestGroupDataSource(t *testing.T) {
resource.Test(t, resource.TestCase{
ProtoV6ProviderFactories: provider.TestAccProtoV6ProviderFactories,
Steps: []resource.TestStep{
// Test no filters set
{
Config: provider.ProviderConfig + `
data "uxi_group" "my_group" {
filter = {}
}
`,
ExpectError: regexp.MustCompile(`either filter.group_id must be set or 'filter.is_root = true' is required`),
},
// Test too many filters set
{
Config: provider.ProviderConfig + `
data "uxi_group" "my_group" {
filter = {
is_root = true
group_id = "uid"
}
}
`,
ExpectError: regexp.MustCompile(`group_id and 'is_root = true' cannot both be set`),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr("data.uxi_root_group.my_root_group", "id", "mock_uid"),
),
},
// Test Read, is_root not set
// Test Read
{
PreConfig: func() {
util.MockGetGroup(
Expand All @@ -63,28 +40,29 @@ func TestGroupDataSource(t *testing.T) {
resource.TestCheckResourceAttr("data.uxi_group.my_group", "id", "uid"),
),
},
// Test Read, is_root is false
// TODO: Test retrieving the root group
{
PreConfig: func() {
util.MockGetGroup(
"uid",
util.GeneratePaginatedResponse([]map[string]interface{}{util.StructToMap(util.GenerateGroupResponseGetModel("uid", "", ""))}),
3,
"my_root_group_uid",
util.GeneratePaginatedResponse([]map[string]interface{}{util.StructToMap(config_api_client.GroupsGetItem{
Id: "my_root_group_uid",
Name: "root",
Parent: *config_api_client.NewNullableParent(nil),
Path: "my_root_group_uid",
})}),
1,
)
},
Config: provider.ProviderConfig + `
data "uxi_group" "my_group" {
filter = {
is_root = false
group_id = "uid"
group_id = "my_root_group_uid"
}
}
`,
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr("data.uxi_group.my_group", "id", "uid"),
),
ExpectError: regexp.MustCompile(`the root group cannot be used as a data source`),
},
// TODO: Test retrieving the root group
},
})

Expand All @@ -100,7 +78,7 @@ func TestGroupDataSource429Handling(t *testing.T) {
ProtoV6ProviderFactories: provider.TestAccProtoV6ProviderFactories,
Steps: []resource.TestStep{

// Test Read, is_root not set
// Test Read
{
PreConfig: func() {
mock429 = gock.New("https://test.api.capenetworks.com").
Expand Down
18 changes: 15 additions & 3 deletions pkg/config-api-provider/test/resources/group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,29 +114,41 @@ func TestGroupResource(t *testing.T) {
}

func TestRootGroupResource(t *testing.T) {
defer gock.Off()
mockOAuth := util.MockOAuth()

resource.Test(t, resource.TestCase{
ProtoV6ProviderFactories: provider.TestAccProtoV6ProviderFactories,
Steps: []resource.TestStep{
// Importing the root group does not work
{
PreConfig: func() {
resources.GetRootGroupUID = func() string { return "my_root_group_uid" }
util.MockGetGroup(
"my_root_group_uid",
util.GeneratePaginatedResponse([]map[string]interface{}{util.StructToMap(config_api_client.GroupsGetItem{
Id: "my_root_group_uid",
Name: "root",
Parent: *config_api_client.NewNullableParent(nil),
Path: "my_root_group_uid",
})}),
1,
)
},
Config: provider.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`),
ExpectError: regexp.MustCompile(`the root group cannot be used as a resource`),
},
},
})

mockOAuth.Mock.Disable()
}

func TestGroupResource429Handling(t *testing.T) {
Expand Down
Loading