Skip to content

Commit

Permalink
chore: added debug logging to datasources (#761)
Browse files Browse the repository at this point in the history
  • Loading branch information
benPearce1 authored Aug 22, 2024
1 parent 1e4bcd7 commit beb42f7
Show file tree
Hide file tree
Showing 13 changed files with 60 additions and 6 deletions.
5 changes: 5 additions & 0 deletions octopusdeploy_framework/data_source_library_variable_sets.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,16 @@ func (l *libraryVariableSetDataSource) Read(ctx context.Context, req datasource.
Take: int(data.Take.ValueInt64()),
}

util.DatasourceReading(ctx, "library variable set", query)

existingLibraryVariableSets, err := libraryvariablesets.Get(l.Config.Client, data.SpaceID.ValueString(), query)
if err != nil {
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to read library variable sets, got error: %s", err))
return
}

util.DatasourceResultCount(ctx, "library variable set", len(existingLibraryVariableSets.Items))

data.LibraryVariableSets = flattenLibraryVariableSets(existingLibraryVariableSets.Items)

data.ID = types.StringValue("Library Variables Sets " + time.Now().UTC().String())
Expand All @@ -94,5 +98,6 @@ func flattenLibraryVariableSets(items []*variables.LibraryVariableSet) types.Lis
}
libraryVariableSetList = append(libraryVariableSetList, types.ObjectValueMust(schemas.GetLibraryVariableSetObjectType(), libraryVariableSetMap))
}

return types.ListValueMust(types.ObjectType{AttrTypes: schemas.GetLibraryVariableSetObjectType()}, libraryVariableSetList)
}
3 changes: 3 additions & 0 deletions octopusdeploy_framework/data_source_script_modules.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ func (l *scriptModulesDataSource) Read(ctx context.Context, req datasource.ReadR
Take: int(data.Take.ValueInt64()),
}

util.DatasourceReading(ctx, "script modules", query)

spaceID := data.SpaceID.ValueString()
existingScriptModules, err := scriptmodules.Get(l.Config.Client, spaceID, query)
if err != nil {
Expand All @@ -70,5 +72,6 @@ func (l *scriptModulesDataSource) Read(ctx context.Context, req datasource.ReadR
flattenedScriptModules)
data.ID = types.StringValue("Script Modules " + time.Now().UTC().String())

util.DatasourceResultCount(ctx, "script modules", len(existingScriptModules.Items))
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
}
4 changes: 4 additions & 0 deletions octopusdeploy_framework/datasource_environments.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ func (e *environmentDataSource) Read(ctx context.Context, req datasource.ReadReq
Take: util.GetNumber(data.Take),
}

util.DatasourceReading(ctx, "environments", query)

existingEnvironments, err := environments.Get(e.Client, data.SpaceID.ValueString(), query)
if err != nil {
resp.Diagnostics.AddError("unable to load environments", err.Error())
Expand All @@ -109,6 +111,8 @@ func (e *environmentDataSource) Read(ctx context.Context, req datasource.ReadReq
}
}

util.DatasourceResultCount(ctx, "environments", len(mappedEnvironments))

data.Environments, _ = types.ListValueFrom(ctx, types.ObjectType{AttrTypes: schemas.EnvironmentObjectType()}, mappedEnvironments)
data.ID = types.StringValue("Environments " + time.Now().UTC().String())

Expand Down
7 changes: 4 additions & 3 deletions octopusdeploy_framework/datasource_feeds.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,11 @@ package octopusdeploy_framework

import (
"context"
"fmt"
"github.com/OctopusDeploy/terraform-provider-octopusdeploy/octopusdeploy_framework/schemas"
"github.com/OctopusDeploy/terraform-provider-octopusdeploy/octopusdeploy_framework/util"
"github.com/hashicorp/terraform-plugin-framework/datasource"
datasourceSchema "github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-log/tflog"
"time"

"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/feeds"
Expand Down Expand Up @@ -60,12 +58,15 @@ func (e *feedsDataSource) Read(ctx context.Context, req datasource.ReadRequest,
Take: util.GetNumber(data.Take),
}

util.DatasourceReading(ctx, "feeds", query)

existingFeeds, err := feeds.Get(e.Client, data.SpaceID.ValueString(), query)
if err != nil {
resp.Diagnostics.AddError("unable to load feeds", err.Error())
return
}
tflog.Debug(ctx, fmt.Sprintf("environments returned from API: %#v", existingFeeds))

util.DatasourceResultCount(ctx, "feeds", len(existingFeeds.Items))

flattenedFeeds := []interface{}{}
for _, feed := range existingFeeds.Items {
Expand Down
5 changes: 5 additions & 0 deletions octopusdeploy_framework/datasource_git_credentials.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ func (g *gitCredentialsDataSource) Read(ctx context.Context, req datasource.Read
Skip: int(data.Skip.ValueInt64()),
Take: int(data.Take.ValueInt64()),
}

util.DatasourceReading(ctx, "git credentials", query)

spaceID := data.SpaceID.ValueString()

existingGitCredentials, err := credentials.Get(g.Client, spaceID, query)
Expand All @@ -72,6 +75,8 @@ func (g *gitCredentialsDataSource) Read(ctx context.Context, req datasource.Read
return
}

util.DatasourceResultCount(ctx, "git credentials", len(existingGitCredentials.Items))

flattenedGitCredentials := make([]GitCredentialDatasourceModel, 0, len(existingGitCredentials.Items))
for _, gitCredential := range existingGitCredentials.Items {
flattenedGitCredential := FlattenGitCredential(gitCredential)
Expand Down
4 changes: 4 additions & 0 deletions octopusdeploy_framework/datasource_lifecycle.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,16 @@ func (l *lifecyclesDataSource) Read(ctx context.Context, req datasource.ReadRequ
Take: int(data.Take.ValueInt64()),
}

util.DatasourceReading(ctx, "lifecycles", query)

lifecyclesResult, err := lifecycles.Get(l.Config.Client, data.SpaceID.ValueString(), query)
if err != nil {
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to read lifecycles, got error: %s", err))
return
}

util.DatasourceResultCount(ctx, "lifecycles", len(lifecyclesResult.Items))

data.Lifecycles = flattenLifecycles(lifecyclesResult.Items)
data.ID = types.StringValue("Lifecycles " + time.Now().UTC().String())

Expand Down
4 changes: 4 additions & 0 deletions octopusdeploy_framework/datasource_project.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ func (p *projectsDataSource) Read(ctx context.Context, req datasource.ReadReques
Take: int(data.Take.ValueInt64()),
}

util.DatasourceReading(ctx, "projects", query)

if !data.IDs.IsNull() {
var ids []string
resp.Diagnostics.Append(data.IDs.ElementsAs(ctx, &ids, false)...)
Expand All @@ -79,6 +81,8 @@ func (p *projectsDataSource) Read(ctx context.Context, req datasource.ReadReques
return
}

util.DatasourceResultCount(ctx, "projects", len(existingProjects.Items))

data.Projects = make([]projectResourceModel, 0, len(existingProjects.Items))
for _, project := range existingProjects.Items {
flattenedProject, diags := flattenProject(ctx, project, &projectResourceModel{})
Expand Down
5 changes: 4 additions & 1 deletion octopusdeploy_framework/datasource_project_groups.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@ func (p *projectGroupsDataSource) Read(ctx context.Context, req datasource.ReadR
}
spaceID := data.SpaceID.ValueString()

util.DatasourceReading(ctx, "project groups", query)

existingProjectGroups, err := projectgroups.Get(p.Client, spaceID, query)
if err != nil {
resp.Diagnostics.AddError("unable to load project groups", err.Error())
Expand All @@ -121,7 +123,8 @@ func (p *projectGroupsDataSource) Read(ctx context.Context, req datasource.ReadR
newGroups = append(newGroups, g)
}

//groups, _ := types.ObjectValueFrom(ctx, types.ObjectType{AttrTypes: getNestedGroupAttributes()}, newGroups)
util.DatasourceResultCount(ctx, "project groups", len(newGroups))

for _, projectGroup := range newGroups {
tflog.Debug(ctx, "mapped group "+projectGroup.Name.ValueString())
}
Expand Down
6 changes: 6 additions & 0 deletions octopusdeploy_framework/datasource_space.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package octopusdeploy_framework
import (
"context"
"fmt"
"github.com/hashicorp/terraform-plugin-log/tflog"
"strings"

"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/spaces"
Expand Down Expand Up @@ -46,6 +47,9 @@ func (b *spaceDataSource) Read(ctx context.Context, req datasource.ReadRequest,

// construct query
query := spaces.SpacesQuery{PartialName: data.Name.ValueString()}

util.DatasourceReading(ctx, "space", query)

spacesResult, err := spaces.Get(b.Client, query)

if err != nil {
Expand All @@ -64,6 +68,8 @@ func (b *spaceDataSource) Read(ctx context.Context, req datasource.ReadRequest,
return
}

tflog.Debug(ctx, fmt.Sprintf("Reading space returned ID %s", matchedSpace.ID))

mapSpaceToState(ctx, &data, matchedSpace)

resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
Expand Down
4 changes: 4 additions & 0 deletions octopusdeploy_framework/datasource_spaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ func (b *spacesDataSource) Read(ctx context.Context, req datasource.ReadRequest,
Take: schemas.GetNumber(data.Take),
}

util.DatasourceReading(ctx, "spaces", query)

existingSpaces, err := spaces.Get(b.Client, query)
if err != nil {
resp.Diagnostics.AddError("unable to load spaces", err.Error())
Expand All @@ -87,6 +89,8 @@ func (b *spacesDataSource) Read(ctx context.Context, req datasource.ReadRequest,
mappedSpaces = append(mappedSpaces, s)
}

util.DatasourceResultCount(ctx, "spaces", len(mappedSpaces))

data.Spaces, _ = types.ListValueFrom(ctx, schemas.GetSpaceTypeAttributes(), mappedSpaces)
data.ID = types.StringValue("Spaces " + time.Now().UTC().String())

Expand Down
6 changes: 5 additions & 1 deletion octopusdeploy_framework/datasource_tag_sets.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ func (t *tagSetsDataSource) Read(ctx context.Context, req datasource.ReadRequest
Skip: int(data.Skip.ValueInt64()),
Take: int(data.Take.ValueInt64()),
}

util.DatasourceReading(ctx, "tag sets", query)

spaceID := data.SpaceID.ValueString()

existingTagSets, err := tagsets.Get(t.Client, spaceID, query)
Expand All @@ -55,8 +58,9 @@ func (t *tagSetsDataSource) Read(ctx context.Context, req datasource.ReadRequest
return
}

data.TagSets = flattenTagSets(existingTagSets.Items)
util.DatasourceResultCount(ctx, "tag sets", len(existingTagSets.Items))

data.TagSets = flattenTagSets(existingTagSets.Items)
data.ID = types.StringValue(fmt.Sprintf("TagSets-%s", time.Now().UTC().String()))

resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
Expand Down
4 changes: 4 additions & 0 deletions octopusdeploy_framework/datasource_tenants.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ func (b *tenantsDataSource) Read(ctx context.Context, req datasource.ReadRequest
Take: int(data.Take.ValueInt64()),
}

util.DatasourceReading(ctx, "tenants", query)

existingTenants, err := tenants.Get(b.Client, data.SpaceID.ValueString(), query)
if err != nil {
resp.Diagnostics.AddError("unable to load tenants", err.Error())
Expand All @@ -73,6 +75,8 @@ func (b *tenantsDataSource) Read(ctx context.Context, req datasource.ReadRequest
flattenedTenants = append(flattenedTenants, schemas.FlattenTenant(tenant))
}

util.DatasourceResultCount(ctx, "tenants", len(flattenedTenants))

data.ID = types.StringValue("Tenants " + time.Now().UTC().String())
data.Tenants, _ = types.ListValueFrom(ctx, types.ObjectType{AttrTypes: schemas.TenantObjectType()}, flattenedTenants)

Expand Down
9 changes: 8 additions & 1 deletion octopusdeploy_framework/util/logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package util
import (
"context"
"fmt"

"github.com/hashicorp/terraform-plugin-log/tflog"
)

Expand All @@ -27,6 +26,14 @@ func Reading(ctx context.Context, resource string, v ...any) {
tflog.Info(ctx, fmt.Sprintf("reading %s: %#v", resource, v))
}

func DatasourceReading(ctx context.Context, resource string, v ...any) {
tflog.Debug(ctx, fmt.Sprintf("reading %s data source with query: %+v", resource, v))
}

func DatasourceResultCount(ctx context.Context, resource string, count int) {
tflog.Debug(ctx, fmt.Sprintf("reading %s returned %d items", resource, count))
}

func Read(ctx context.Context, resource string, v ...any) {
tflog.Info(ctx, fmt.Sprintf("read %s: %#v", resource, v))
}
Expand Down

0 comments on commit beb42f7

Please sign in to comment.