Skip to content

Commit

Permalink
More investigate log
Browse files Browse the repository at this point in the history
  • Loading branch information
HuyPhanNguyen committed Dec 6, 2024
1 parent 1a920da commit 7a9a749
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 18 deletions.
2 changes: 1 addition & 1 deletion build.ps1
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Set variables
$VERSION = "0.7.104"
$VERSION = "0.7.106"
$BINARY = "terraform-provider-octopusdeploy.exe"
$HOSTNAME = "octopus.com"
$NAMESPACE = "com"
Expand Down
9 changes: 8 additions & 1 deletion octopusdeploy_framework/resource_deployment_freeze_tenant.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package octopusdeploy_framework

import (
"context"
"encoding/json"
"fmt"
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/core"
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/deploymentfreezes"
Expand Down Expand Up @@ -51,7 +52,7 @@ func (d *deploymentFreezeTenantResource) Create(ctx context.Context, req resourc
return
}

tflog.Debug(ctx, fmt.Sprintf("adding tenant (%s) to deployment freeze (%s)", plan.TenantID.ValueString(), plan.DeploymentFreezeID.ValueString()))
tflog.Info(ctx, fmt.Sprintf("adding tenant (%s) to deployment freeze (%s)", plan.TenantID.ValueString(), plan.DeploymentFreezeID.ValueString()))
freeze, err := deploymentfreezes.GetById(d.Client, plan.DeploymentFreezeID.ValueString())
if err != nil {
resp.Diagnostics.AddError("cannot load deployment freeze", err.Error())
Expand All @@ -68,12 +69,18 @@ func (d *deploymentFreezeTenantResource) Create(ctx context.Context, req resourc
// Add to existing scopes
freeze.TenantProjectEnvironmentScope = append(freeze.TenantProjectEnvironmentScope, tenantScope)

tflog.Info(ctx, fmt.Sprintf("[API Request] Updating deployment freeze with new scope. Total scopes: %d", len(freeze.TenantProjectEnvironmentScope)))

freeze, err = deploymentfreezes.Update(d.Client, freeze)
if err != nil {
resp.Diagnostics.AddError("error while updating deployment freeze", err.Error())
return
}

if freezeJSON, err := json.MarshalIndent(freeze, "", " "); err == nil {
tflog.Info(ctx, fmt.Sprintf("[API Response] Updated deployment freeze:\n%s", string(freezeJSON)))
}

plan.ID = types.StringValue(util.BuildCompositeId(plan.DeploymentFreezeID.ValueString(), plan.TenantID.ValueString(), plan.ProjectID.ValueString(), plan.EnvironmentID.ValueString()))
resp.Diagnostics.Append(resp.State.Set(ctx, &plan)...)
tflog.Debug(ctx, fmt.Sprintf("tenant scope (%s) added to deployment freeze (%s)", plan.TenantID.ValueString(), plan.DeploymentFreezeID.ValueString()))
Expand Down
41 changes: 25 additions & 16 deletions octopusdeploy_framework/resource_deployment_freeze_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,18 @@ import (
"github.com/hashicorp/terraform-plugin-testing/helper/acctest"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/hashicorp/terraform-plugin-testing/terraform"
"log"
"os"
"strings"
"testing"
"time"
)

func TestNewDeploymentFreezeResource(t *testing.T) {
if os.Getenv("TF_LOG") == "" {
os.Setenv("TF_LOG", "INFO")
os.Setenv("OCTOPUS__FeatureToggles__DeploymentFreezeByTenantFeatureToggle", "true")
}

localName := acctest.RandStringFromCharSet(20, acctest.CharSetAlpha)
resourceName := "octopusdeploy_deployment_freeze." + localName
name := acctest.RandStringFromCharSet(20, acctest.CharSetAlpha)
Expand Down Expand Up @@ -54,7 +59,7 @@ func TestNewDeploymentFreezeResource(t *testing.T) {
{
Check: resource.ComposeTestCheckFunc(
testDeploymentFreezeExists(resourceName),
testDeploymentFreezeTenantExists(fmt.Sprintf("octopusdeploy_deployment_freeze_tenant.tenant_%s", localName))),
testDeploymentFreezeTenantExists(fmt.Sprintf("octopusdeploy_deployment_freeze_tenant.tenant_%s", localName), t)),
Config: testDeploymentFreezeBasic(localName, name+"1", start, updatedEnd, spaceName, []string{environmentName1, environmentName2}, projectName, projectGroupName, lifecycleName, tenantName, true),
},
},
Expand Down Expand Up @@ -156,9 +161,12 @@ func testDeploymentFreezeExists(prefix string) resource.TestCheckFunc {
}
}

func testDeploymentFreezeTenantExists(prefix string) resource.TestCheckFunc {
func testDeploymentFreezeTenantExists(prefix string, t *testing.T) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[prefix]
featureToggle := os.Getenv("OCTOPUS__FeatureToggles__DeploymentFreezeByTenantFeatureToggle")
t.Logf("DeploymentFreezeByTenantFeatureToggle value: '%s'", featureToggle)

if !ok {
return fmt.Errorf("Resource not found: %s", prefix)
}
Expand All @@ -173,49 +181,48 @@ func testDeploymentFreezeTenantExists(prefix string) resource.TestCheckFunc {
projectId := bits[2]
environmentId := bits[3]

log.Printf("[DEBUG] Starting tenant scope check for deployment freeze %s", freezeId)
log.Printf("[DEBUG] Looking for tenant: %s, project: %s, environment: %s", tenantId, projectId, environmentId)
t.Logf("Starting tenant scope check for deployment freeze %s", freezeId)
t.Logf("Looking for tenant: %s, project: %s, environment: %s", tenantId, projectId, environmentId)

retryErr := resource.RetryContext(context.Background(), 2*time.Minute, func() *resource.RetryError {
freeze, err := deploymentfreezes.GetById(octoClient, freezeId)
if err != nil {
log.Printf("[ERROR] Failed to get deployment freeze: %v", err)
t.Logf("Failed to get deployment freeze: %v", err)
return resource.NonRetryableError(fmt.Errorf("Error getting deployment freeze: %v", err))
}

if freezeJSON, err := json.MarshalIndent(freeze, "", " "); err == nil {
log.Printf("[DEBUG] Deployment freeze as JSON:\n%s", string(freezeJSON))
t.Logf("Deployment freeze as JSON:\n%s", string(freezeJSON))
} else {
log.Printf("[ERROR] Failed to marshal freeze object to JSON: %v", err)
t.Logf("Failed to marshal freeze object to JSON: %v", err)
}

log.Printf("[DEBUG] Retrieved deployment freeze with %d tenant scopes", len(freeze.TenantProjectEnvironmentScope))
t.Logf("Retrieved deployment freeze with %d tenant scopes", len(freeze.TenantProjectEnvironmentScope))

// Log all existing scopes for debugging
for i, scope := range freeze.TenantProjectEnvironmentScope {
log.Printf("[DEBUG] Scope %d - Tenant: %s, Project: %s, Environment: %s",
t.Logf("Scope %d - Tenant: %s, Project: %s, Environment: %s",
i+1, scope.TenantId, scope.ProjectId, scope.EnvironmentId)
}

for _, scope := range freeze.TenantProjectEnvironmentScope {
if scope.TenantId == tenantId && scope.ProjectId == projectId && scope.EnvironmentId == environmentId {
log.Printf("[INFO] Found matching tenant scope in deployment freeze")
t.Log("Found matching tenant scope in deployment freeze")
return nil
}
}

log.Printf("[DEBUG] Tenant scope not yet found, will retry...")
t.Log("Tenant scope not yet found, will retry...")
return resource.RetryableError(fmt.Errorf("Tenant scope not yet found in deployment freeze (freezeId: %s)", freezeId))
})

if retryErr != nil {
freeze, err := deploymentfreezes.GetById(octoClient, freezeId)
if err != nil {
log.Printf("[ERROR] Final attempt to get deployment freeze failed: %v", err)
t.Logf("Final attempt to get deployment freeze failed: %v", err)
} else {
log.Printf("[ERROR] Final state - Deployment freeze has %d tenant scopes", len(freeze.TenantProjectEnvironmentScope))
t.Logf("Final state - Deployment freeze has %d tenant scopes", len(freeze.TenantProjectEnvironmentScope))
for i, scope := range freeze.TenantProjectEnvironmentScope {
log.Printf("[ERROR] Final Scope %d - Tenant: %s, Project: %s, Environment: %s",
t.Logf("Final Scope %d - Tenant: %s, Project: %s, Environment: %s",
i+1, scope.TenantId, scope.ProjectId, scope.EnvironmentId)
}
}
Expand All @@ -238,5 +245,7 @@ func testDeploymentFreezeCheckDestroy(s *terraform.State) error {
return fmt.Errorf("Deployment Freeze (%s) still exists", rs.Primary.ID)
}
}

os.Setenv("TF_LOG", "")
return nil
}

0 comments on commit 7a9a749

Please sign in to comment.