Skip to content

Commit

Permalink
fix: groups attached to root keep null parent_group_id
Browse files Browse the repository at this point in the history
  • Loading branch information
1riatsila1 committed Nov 5, 2024
1 parent 9da54cb commit a90bf25
Show file tree
Hide file tree
Showing 41 changed files with 408 additions and 176 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module github.com/aruba-uxi/terraform-provider-configuration-api
module github.com/aruba-uxi/terraform-provider-configuration

go 1.21

Expand Down
2 changes: 1 addition & 1 deletion internal/provider/datasources/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package datasources
import (
"context"

"github.com/aruba-uxi/terraform-provider-configuration-api/internal/provider/util"
config_api_client "github.com/aruba-uxi/terraform-provider-configuration-api/pkg/config-api-client"
"github.com/aruba-uxi/terraform-provider-configuration/internal/provider/util"
"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"github.com/hashicorp/terraform-plugin-framework/types"
Expand Down
2 changes: 1 addition & 1 deletion internal/provider/datasources/group.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package datasources
import (
"context"

"github.com/aruba-uxi/terraform-provider-configuration-api/internal/provider/util"
config_api_client "github.com/aruba-uxi/terraform-provider-configuration-api/pkg/config-api-client"
"github.com/aruba-uxi/terraform-provider-configuration/internal/provider/util"
"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"github.com/hashicorp/terraform-plugin-framework/types"
Expand Down
2 changes: 1 addition & 1 deletion internal/provider/datasources/network_group_assignment.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package datasources
import (
"context"

"github.com/aruba-uxi/terraform-provider-configuration-api/internal/provider/util"
config_api_client "github.com/aruba-uxi/terraform-provider-configuration-api/pkg/config-api-client"
"github.com/aruba-uxi/terraform-provider-configuration/internal/provider/util"
"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"github.com/hashicorp/terraform-plugin-framework/types"
Expand Down
2 changes: 1 addition & 1 deletion internal/provider/datasources/sensor.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package datasources
import (
"context"

"github.com/aruba-uxi/terraform-provider-configuration-api/internal/provider/util"
config_api_client "github.com/aruba-uxi/terraform-provider-configuration-api/pkg/config-api-client"
"github.com/aruba-uxi/terraform-provider-configuration/internal/provider/util"
"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"github.com/hashicorp/terraform-plugin-framework/types"
Expand Down
2 changes: 1 addition & 1 deletion internal/provider/datasources/sensor_group_assignment.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package datasources
import (
"context"

"github.com/aruba-uxi/terraform-provider-configuration-api/internal/provider/util"
config_api_client "github.com/aruba-uxi/terraform-provider-configuration-api/pkg/config-api-client"
"github.com/aruba-uxi/terraform-provider-configuration/internal/provider/util"
"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"github.com/hashicorp/terraform-plugin-framework/types"
Expand Down
2 changes: 1 addition & 1 deletion internal/provider/datasources/wired_network.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package datasources
import (
"context"

"github.com/aruba-uxi/terraform-provider-configuration-api/internal/provider/util"
config_api_client "github.com/aruba-uxi/terraform-provider-configuration-api/pkg/config-api-client"
"github.com/aruba-uxi/terraform-provider-configuration/internal/provider/util"
"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"github.com/hashicorp/terraform-plugin-framework/types"
Expand Down
2 changes: 1 addition & 1 deletion internal/provider/datasources/wireless_network.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package datasources
import (
"context"

"github.com/aruba-uxi/terraform-provider-configuration-api/internal/provider/util"
config_api_client "github.com/aruba-uxi/terraform-provider-configuration-api/pkg/config-api-client"
"github.com/aruba-uxi/terraform-provider-configuration/internal/provider/util"
"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"github.com/hashicorp/terraform-plugin-framework/types"
Expand Down
4 changes: 2 additions & 2 deletions internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import (
"context"
"os"

"github.com/aruba-uxi/terraform-provider-configuration-api/internal/provider/datasources"
"github.com/aruba-uxi/terraform-provider-configuration-api/internal/provider/resources"
"github.com/aruba-uxi/terraform-provider-configuration/internal/provider/datasources"
"github.com/aruba-uxi/terraform-provider-configuration/internal/provider/resources"
"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/provider"
Expand Down
2 changes: 1 addition & 1 deletion internal/provider/resources/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package resources
import (
"context"

"github.com/aruba-uxi/terraform-provider-configuration-api/internal/provider/util"
"github.com/aruba-uxi/terraform-provider-configuration-api/pkg/config-api-client"
"github.com/aruba-uxi/terraform-provider-configuration/internal/provider/util"

"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/resource"
Expand Down
63 changes: 44 additions & 19 deletions internal/provider/resources/group.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package resources
import (
"context"

"github.com/aruba-uxi/terraform-provider-configuration-api/internal/provider/util"
"github.com/aruba-uxi/terraform-provider-configuration-api/pkg/config-api-client"
"github.com/aruba-uxi/terraform-provider-configuration/internal/provider/util"

"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/resource"
Expand All @@ -14,6 +14,8 @@ import (
"github.com/hashicorp/terraform-plugin-framework/types"
)

const groupNotFoundErrorString = "not found"

// Ensure the implementation satisfies the expected interfaces.
var (
_ resource.Resource = &groupResource{}
Expand Down Expand Up @@ -64,7 +66,6 @@ func (r *groupResource) Schema(
// UXI business logic does not permit moving of groups
stringplanmodifier.RequiresReplace(),
},
Computed: true,
},
},
}
Expand Down Expand Up @@ -125,7 +126,11 @@ func (r *groupResource) Create(
// Update the state to match the plan (replace with response from client)
plan.ID = types.StringValue(group.Id)
plan.Name = types.StringValue(group.Name)
plan.ParentGroupId = types.StringValue(group.Parent.Id)
// only update parent if not attached to root node (else leave it as null)
parentGroup, _ := r.getGroup(ctx, group.Parent.Id)
if parentGroup != nil && !util.IsRoot(*parentGroup) {
plan.ParentGroupId = types.StringValue(group.Parent.Id)
}

// Set state to fully populated data
diags = resp.State.Set(ctx, plan)
Expand All @@ -148,33 +153,31 @@ func (r *groupResource) Read(
return
}

request := r.client.ConfigurationAPI.
GroupsGet(ctx).
Id(state.ID.ValueString())
groupResponse, response, err := util.RetryFor429(request.Execute)
errorPresent, errorDetail := util.RaiseForStatus(response, err)
group, errorDetail := r.getGroup(ctx, state.ID.ValueString())

errorSummary := util.GenerateErrorSummary("read", "uxi_group")

if errorPresent {
resp.Diagnostics.AddError(errorSummary, errorDetail)
if errorDetail != nil {
if *errorDetail == groupNotFoundErrorString {
resp.State.RemoveResource(ctx)
return
}
resp.Diagnostics.AddError(errorSummary, *errorDetail)
return
}

if len(groupResponse.Items) != 1 {
resp.State.RemoveResource(ctx)
return
}
group := groupResponse.Items[0]

if util.IsRoot(group) {
if util.IsRoot(*group) {
resp.Diagnostics.AddError(errorSummary, "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)
// only update parent if not attached to root node (else leave it as null)
parentGroup, _ := r.getGroup(ctx, group.Parent.Get().Id)
if parentGroup != nil && !util.IsRoot(*parentGroup) {
state.ParentGroupId = types.StringValue(group.Parent.Get().Id)
}

// Set refreshed state
diags = resp.State.Set(ctx, &state)
Expand All @@ -190,9 +193,11 @@ func (r *groupResource) Update(
resp *resource.UpdateResponse,
) {
// Retrieve values from plan
var plan groupResourceModel
var plan, state groupResourceModel
diags := req.Plan.Get(ctx, &plan)
resp.Diagnostics.Append(diags...)
diags = req.State.Get(ctx, &state)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
Expand Down Expand Up @@ -254,3 +259,23 @@ func (r *groupResource) ImportState(
) {
resource.ImportStatePassthroughID(ctx, path.Root("id"), req, resp)
}

func (r *groupResource) getGroup(
ctx context.Context,
id string,
) (*config_api_client.GroupsGetItem, *string) {
request := r.client.ConfigurationAPI.GroupsGet(ctx).Id(id)
groupResponse, response, err := util.RetryFor429(request.Execute)
errorPresent, errorDetail := util.RaiseForStatus(response, err)

if errorPresent {
return nil, &errorDetail
}

if len(groupResponse.Items) != 1 {
notFound := groupNotFoundErrorString
return nil, &notFound
}

return &groupResponse.Items[0], nil
}
2 changes: 1 addition & 1 deletion internal/provider/resources/network_group_assignment.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package resources

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

"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/resource"
Expand Down
2 changes: 1 addition & 1 deletion internal/provider/resources/sensor.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package resources

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

"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/resource"
Expand Down
2 changes: 1 addition & 1 deletion internal/provider/resources/sensor_group_assignment.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package resources
import (
"context"

"github.com/aruba-uxi/terraform-provider-configuration-api/internal/provider/util"
config_api_client "github.com/aruba-uxi/terraform-provider-configuration-api/pkg/config-api-client"
"github.com/aruba-uxi/terraform-provider-configuration/internal/provider/util"
"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
Expand Down
2 changes: 1 addition & 1 deletion internal/provider/resources/service_group_assignment.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package resources

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

"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/resource"
Expand Down
2 changes: 1 addition & 1 deletion internal/provider/resources/wired_network.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package resources
import (
"context"

"github.com/aruba-uxi/terraform-provider-configuration-api/internal/provider/util"
"github.com/aruba-uxi/terraform-provider-configuration-api/pkg/config-api-client"
"github.com/aruba-uxi/terraform-provider-configuration/internal/provider/util"
"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
Expand Down
2 changes: 1 addition & 1 deletion internal/provider/resources/wireless_network.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package resources
import (
"context"

"github.com/aruba-uxi/terraform-provider-configuration-api/internal/provider/util"
"github.com/aruba-uxi/terraform-provider-configuration-api/pkg/config-api-client"
"github.com/aruba-uxi/terraform-provider-configuration/internal/provider/util"
"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
Expand Down
2 changes: 1 addition & 1 deletion internal/provider/util/retry.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"strconv"
"time"

"github.com/aruba-uxi/terraform-provider-configuration-api/internal/provider/config"
"github.com/aruba-uxi/terraform-provider-configuration/internal/provider/config"
)

func RetryFor429[T any](f func() (T, *http.Response, error)) (T, *http.Response, error) {
Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"flag"
"log"

"github.com/aruba-uxi/terraform-provider-configuration-api/internal/provider"
"github.com/aruba-uxi/terraform-provider-configuration/internal/provider"
"github.com/hashicorp/terraform-plugin-framework/providerserver"
)

Expand Down
4 changes: 2 additions & 2 deletions test/live/provider/provider.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package provider

import (
"github.com/aruba-uxi/terraform-provider-configuration-api/internal/provider"
"github.com/aruba-uxi/terraform-provider-configuration-api/test/live/util"
"github.com/aruba-uxi/terraform-provider-configuration/internal/provider"
"github.com/aruba-uxi/terraform-provider-configuration/test/live/util"
"github.com/hashicorp/terraform-plugin-framework/providerserver"
"github.com/hashicorp/terraform-plugin-go/tfprotov6"
)
Expand Down
6 changes: 3 additions & 3 deletions test/live/resources/group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import (
"regexp"
"testing"

"github.com/aruba-uxi/terraform-provider-configuration-api/test/live/provider"
"github.com/aruba-uxi/terraform-provider-configuration-api/test/live/util"
"github.com/aruba-uxi/terraform-provider-configuration/test/live/provider"
"github.com/aruba-uxi/terraform-provider-configuration/test/live/util"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
)

Expand Down Expand Up @@ -36,7 +36,7 @@ func TestGroupResource(t *testing.T) {
resource.TestCheckResourceAttrPtr(
"uxi_group.parent",
"parent_group_id",
&rootGroup.Id,
nil,
),
),
},
Expand Down
2 changes: 1 addition & 1 deletion test/live/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import (
"context"
"os"

resources_util "github.com/aruba-uxi/terraform-provider-configuration-api/internal/provider/util"
"github.com/aruba-uxi/terraform-provider-configuration-api/pkg/config-api-client"
resources_util "github.com/aruba-uxi/terraform-provider-configuration/internal/provider/util"
"golang.org/x/oauth2"
"golang.org/x/oauth2/clientcredentials"
)
Expand Down
4 changes: 2 additions & 2 deletions test/mocked/data-sources/agent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import (
"regexp"
"testing"

"github.com/aruba-uxi/terraform-provider-configuration-api/test/mocked/provider"
"github.com/aruba-uxi/terraform-provider-configuration-api/test/mocked/util"
"github.com/aruba-uxi/terraform-provider-configuration/test/mocked/provider"
"github.com/aruba-uxi/terraform-provider-configuration/test/mocked/util"
"github.com/h2non/gock"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/hashicorp/terraform-plugin-testing/terraform"
Expand Down
8 changes: 4 additions & 4 deletions test/mocked/data-sources/group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import (
"regexp"
"testing"

"github.com/aruba-uxi/terraform-provider-configuration-api/test/mocked/provider"
"github.com/aruba-uxi/terraform-provider-configuration-api/test/mocked/util"
"github.com/aruba-uxi/terraform-provider-configuration/test/mocked/provider"
"github.com/aruba-uxi/terraform-provider-configuration/test/mocked/util"
"github.com/h2non/gock"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/hashicorp/terraform-plugin-testing/terraform"
Expand All @@ -27,7 +27,7 @@ func TestGroupDataSource(t *testing.T) {
"uid",
util.GeneratePaginatedResponse(
[]map[string]interface{}{
util.GenerateGroupResponseModel("uid", "", ""),
util.GenerateNonRootGroupResponseModel("uid", "", ""),
},
),
3,
Expand Down Expand Up @@ -97,7 +97,7 @@ func TestGroupDataSource429Handling(t *testing.T) {
"uid",
util.GeneratePaginatedResponse(
[]map[string]interface{}{
util.GenerateGroupResponseModel("uid", "", ""),
util.GenerateNonRootGroupResponseModel("uid", "", ""),
},
),
3,
Expand Down
4 changes: 2 additions & 2 deletions test/mocked/data-sources/network_group_assignment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import (
"regexp"
"testing"

"github.com/aruba-uxi/terraform-provider-configuration-api/test/mocked/provider"
"github.com/aruba-uxi/terraform-provider-configuration-api/test/mocked/util"
"github.com/aruba-uxi/terraform-provider-configuration/test/mocked/provider"
"github.com/aruba-uxi/terraform-provider-configuration/test/mocked/util"
"github.com/h2non/gock"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/hashicorp/terraform-plugin-testing/terraform"
Expand Down
Loading

0 comments on commit a90bf25

Please sign in to comment.