Skip to content

Commit

Permalink
[chore] Re-enable whitespace/comments/naming linters
Browse files Browse the repository at this point in the history
  • Loading branch information
Dzmitry Kishylau committed Aug 13, 2024
1 parent 4adbefe commit 1bcaa4e
Show file tree
Hide file tree
Showing 31 changed files with 231 additions and 204 deletions.
9 changes: 3 additions & 6 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ linters:
- gofmt
- staticcheck
- revive
# - godot
# - whitespace
# - goimports
- godot
- whitespace
- goimports
- gosimple
- errcheck
- unconvert
Expand Down Expand Up @@ -40,16 +40,13 @@ issues:
linters:
- gosec
exclude:
- "should have a package comment"
- "var-naming:.*Id.* should be .*ID"
- "var-naming:.*Api.* should be .*API"
- "var-naming:.*Http.* should be .*HTTP"
- "exported: exported .* should have comment"
- "exported: comment on exported .* should be of the form"
- "var-naming: don't use an underscore in package name"
- "unused-parameter:"
- "ifElseChain:"
- "var-naming:.*ALL_CAPS.*"
- "superfluous-else"
- "package-comments:"
- "Error return value of .* is not checked"
1 change: 1 addition & 0 deletions internal/acctest/acctest.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// Package acctest provides a harness for our acceptance tests and test sweepers.
package acctest

import (
Expand Down
4 changes: 3 additions & 1 deletion internal/provider/folder/data_source.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// Package folder provides implementation of the Folder resource and Folders data source.
package folder

import (
Expand All @@ -8,6 +9,7 @@ import (
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-log/tflog"

"github.com/tryretool/terraform-provider-retool/internal/provider/utils"
"github.com/tryretool/terraform-provider-retool/internal/sdk/api"
)
Expand Down Expand Up @@ -133,7 +135,7 @@ func (d *foldersDataSource) Read(ctx context.Context, req datasource.ReadRequest
}
}

// Set state
// Set state.
diags := resp.State.Set(ctx, &state)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
Expand Down
1 change: 1 addition & 0 deletions internal/provider/folder/data_source_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"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
37 changes: 19 additions & 18 deletions internal/provider/folder/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-log/tflog"

"github.com/tryretool/terraform-provider-retool/internal/provider/utils"
"github.com/tryretool/terraform-provider-retool/internal/sdk/api"
)
Expand Down Expand Up @@ -103,7 +104,7 @@ func (r *folderResource) Schema(_ context.Context, _ resource.SchemaRequest, res
Required: true,
Description: "The type of the folder: (app|file|resource|workflow).",
PlanModifiers: []planmodifier.String{
stringplanmodifier.RequiresReplace(), // Changing the folder type requires replacing the resource
stringplanmodifier.RequiresReplace(), // Changing the folder type requires replacing the resource.
},
Validators: []validator.String{
stringvalidator.OneOf("app", "file", "resource", "workflow"),
Expand All @@ -115,7 +116,7 @@ func (r *folderResource) Schema(_ context.Context, _ resource.SchemaRequest, res

func (r *folderResource) getTrueParentFolderId(ctx context.Context, folderType, parentFolderId string, diags *diag.Diagnostics) string {
if parentFolderId == ROOT_FOLDER_ID {
// Get root folder ID
// Get root folder ID.
rootFolderId, err := getRootFolderId(ctx, folderType, r.client, r.rootFolderIdCache)
if err != nil {
diags.AddError(
Expand All @@ -131,15 +132,15 @@ func (r *folderResource) getTrueParentFolderId(ctx context.Context, folderType,

// Create creates the resource and sets the initial Terraform state.
func (r *folderResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
// Retrieve values from plan
// Retrieve values from plan.
var plan folderModel
diags := req.Plan.Get(ctx, &plan)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}

// Generate API request body from plan
// Generate API request body from plan.
var folder api.FoldersPostRequest
parentFolderId := r.getTrueParentFolderId(ctx, plan.FolderType.ValueString(), plan.ParentFolderId.ValueString(), &resp.Diagnostics)
if resp.Diagnostics.HasError() {
Expand All @@ -151,7 +152,7 @@ func (r *folderResource) Create(ctx context.Context, req resource.CreateRequest,
folder.FolderType = plan.FolderType.ValueString()

tflog.Info(ctx, "Creating a folder", map[string]any{"name": plan.Name.ValueString(), "folderType": plan.FolderType.ValueString(), "parentFolderId": parentFolderId})
// Create new folder
// Create new folder.
response, httpResponse, err := r.client.FoldersAPI.FoldersPost(ctx).FoldersPostRequest(folder).Execute()

if err != nil {
Expand All @@ -164,12 +165,12 @@ func (r *folderResource) Create(ctx context.Context, req resource.CreateRequest,
}
tflog.Info(ctx, "Folder created", map[string]any{"id": response.Data.Id, "legacyId": response.Data.LegacyId, "isSystemFolder": response.Data.IsSystemFolder})

// Map response body to schema and populate Computed attribute values
// Map response body to schema and populate Computed attribute values.
plan.Id = types.StringValue(response.Data.Id)
plan.LegacyId = types.StringValue(response.Data.LegacyId)
plan.IsSystemFolder = types.BoolValue(response.Data.IsSystemFolder)

// Set state to fully populated data
// Set state to fully populated data.
diags = resp.State.Set(ctx, plan)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
Expand All @@ -179,15 +180,15 @@ func (r *folderResource) Create(ctx context.Context, req resource.CreateRequest,

// Read resource information.
func (r *folderResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) {
// Get current state
// Get current state.
var state folderModel
diags := req.State.Get(ctx, &state)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}

// Get refreshed folder value from Retool API
// Get refreshed folder value from Retool API.
tflog.Info(ctx, "Reading folder", map[string]any{"id": state.Id})
response, httpResponse, err := r.client.FoldersAPI.FoldersFolderIdGet(ctx, state.Id.ValueString()).Execute()
if err != nil {
Expand All @@ -204,18 +205,18 @@ func (r *folderResource) Read(ctx context.Context, req resource.ReadRequest, res
return
}

// Update the state
// Update the state.
state.LegacyId = types.StringValue(response.Data.LegacyId)
state.Name = types.StringValue(response.Data.Name)
// To keep the state consistent, we need to convert the root folder ID to the string constant ROOT_FOLDER_ID
// To keep the state consistent, we need to convert the root folder ID to the string constant ROOT_FOLDER_ID.
state.ParentFolderId = types.StringPointerValue(maybeReplaceRootFolderIdWithConstant(ctx, response.Data.FolderType, response.Data.ParentFolderId.Get(), r.client, r.rootFolderIdCache, &resp.Diagnostics))
state.IsSystemFolder = types.BoolValue(response.Data.IsSystemFolder)
state.FolderType = types.StringValue(response.Data.FolderType)
if resp.Diagnostics.HasError() {
return
}

// Set refreshed state
// Set refreshed state.
diags = resp.State.Set(ctx, &state)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
Expand All @@ -240,12 +241,12 @@ func (r *folderResource) Update(ctx context.Context, req resource.UpdateRequest,
}

if plan.Name.Equal(state.Name) && plan.ParentFolderId.Equal(state.ParentFolderId) {
// No changes
// No changes.
tflog.Info(ctx, "No changes detected for folder", map[string]any{"id": state.Id})
return
}

// Generate API request body
// Generate API request body.
patchReq := api.FoldersFolderIdPatchRequest{}
if !plan.Name.Equal(state.Name) {
op := api.NewUsersUserIdPatchRequestOperationsInnerAnyOf("replace", "/name")
Expand Down Expand Up @@ -280,21 +281,21 @@ func (r *folderResource) Update(ctx context.Context, req resource.UpdateRequest,

// Delete deletes the resource and removes the Terraform state on success.
func (r *folderResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) {
// Retrieve values from state
// Retrieve values from state.
var state folderModel
diags := req.State.Get(ctx, &state)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}

// Delete existing order
// Delete existing order.
recursive := true
deleteRequest := api.FoldersFolderIdDeleteRequest{}
deleteRequest.Recursive = &recursive

httpResponse, err := r.client.FoldersAPI.FoldersFolderIdDelete(ctx, state.Id.ValueString()).FoldersFolderIdDeleteRequest(deleteRequest).Execute()
if err != nil && !(httpResponse != nil && httpResponse.StatusCode == 404) { // it's ok to not find the resource being deleted
if err != nil && !(httpResponse != nil && httpResponse.StatusCode == 404) { // It's ok to not find the resource being deleted.
resp.Diagnostics.AddError(
"Error Deleting Folder",
"Could not delete folder"+state.Id.ValueString()+", unexpected error: "+err.Error(),
Expand All @@ -305,6 +306,6 @@ func (r *folderResource) Delete(ctx context.Context, req resource.DeleteRequest,
}

func (r *folderResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) {
// Retrieve import ID and save to id attribute
// Retrieve import ID and save to id attribute.
resource.ImportStatePassthroughID(ctx, path.Root("id"), req, resp)
}
7 changes: 4 additions & 3 deletions internal/provider/folder/resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

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

"github.com/tryretool/terraform-provider-retool/internal/acctest"
"github.com/tryretool/terraform-provider-retool/internal/sdk/api"
)
Expand Down Expand Up @@ -86,7 +87,7 @@ func init() {
func TestAccFolder(t *testing.T) {
acctest.Test(t, resource.TestCase{
Steps: []resource.TestStep{
// Read and Create
// Read and Create.
{
Config: testFolderConfig,
Check: resource.ComposeTestCheckFunc(
Expand All @@ -105,13 +106,13 @@ func TestAccFolder(t *testing.T) {
resource.TestCheckResourceAttrSet("retool_folder.child_folder", "legacy_id"),
),
},
// Import state
// Import state.
{
ResourceName: "retool_folder.test_folder",
ImportState: true,
ImportStateVerify: true,
},
// Update and Read
// Update and Read.
{
Config: testUpdatedFolderConfig,
Check: resource.ComposeTestCheckFunc(
Expand Down
1 change: 1 addition & 0 deletions internal/provider/folder/root_folder_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-log/tflog"

"github.com/tryretool/terraform-provider-retool/internal/provider/utils"
"github.com/tryretool/terraform-provider-retool/internal/sdk/api"
)
Expand Down
Loading

0 comments on commit 1bcaa4e

Please sign in to comment.