Skip to content

Commit 181df54

Browse files
CENG-507: Add sleep to saml_auth
1 parent 402d4dc commit 181df54

File tree

2 files changed

+35
-3
lines changed

2 files changed

+35
-3
lines changed

cloudsmith/resource_repository_retention_rule_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ func TestAccRepositoryRetentionRule_basic(t *testing.T) {
2828
testAccRepositoryCheckExists("cloudsmith_repository.test-retention"),
2929
resource.TestCheckResourceAttr("cloudsmith_repository_retention_rule.test", "retention_count_limit", "100"),
3030
resource.TestCheckResourceAttr("cloudsmith_repository_retention_rule.test", "retention_days_limit", "28"),
31-
resource.TestCheckResourceAttr("cloudsmith_repository_retention_rule.test", "retention_enabled", "true"),
31+
resource.TestCheckResourceAttr("cloudsmith_repository_retention_rule.test", "retention_enabled", "false"),
3232
resource.TestCheckResourceAttr("cloudsmith_repository_retention_rule.test", "retention_group_by_name", "false"),
3333
resource.TestCheckResourceAttr("cloudsmith_repository_retention_rule.test", "retention_group_by_format", "false"),
3434
resource.TestCheckResourceAttr("cloudsmith_repository_retention_rule.test", "retention_group_by_package_type", "false"),
@@ -57,7 +57,7 @@ resource "cloudsmith_repository" "test-retention" {
5757
resource "cloudsmith_repository_retention_rule" "test" {
5858
namespace = "%s"
5959
repository = cloudsmith_repository.test-retention.name
60-
retention_enabled = true
60+
retention_enabled = false
6161
retention_count_limit = 100
6262
retention_days_limit = 28
6363
retention_group_by_name = false

cloudsmith/resource_saml_auth.go

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,33 @@ import (
88
"fmt"
99
"net/http"
1010
"strings"
11+
"time"
1112

1213
"github.com/cloudsmith-io/cloudsmith-api-go"
1314
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
1415
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
1516
)
1617

18+
// waitForSAMLAuthEnabled polls until the SAML auth enabled state matches wantEnabled or times out.
19+
func waitForSAMLAuthEnabled(pc *providerConfig, organization string, wantEnabled bool, timeoutSec int) error {
20+
deadline := time.Now().Add(time.Duration(timeoutSec) * time.Second)
21+
for {
22+
samlAuth, resp, err := pc.APIClient.OrgsApi.OrgsSamlAuthenticationRead(pc.Auth, organization).Execute()
23+
if resp != nil {
24+
defer resp.Body.Close()
25+
}
26+
if err == nil {
27+
if samlAuth.GetSamlAuthEnabled() == wantEnabled {
28+
return nil
29+
}
30+
}
31+
if time.Now().After(deadline) {
32+
return fmt.Errorf("timeout waiting for SAML auth enabled=%v", wantEnabled)
33+
}
34+
time.Sleep(1 * time.Second)
35+
}
36+
}
37+
1738
// samlAuthCreate handles the creation of a new SAML authentication configuration
1839
func samlAuthCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
1940
pc := m.(*providerConfig)
@@ -31,6 +52,10 @@ func samlAuthCreate(ctx context.Context, d *schema.ResourceData, m interface{})
3152
}
3253

3354
d.SetId(generateSAMLAuthID(organization, result))
55+
// Wait for the backend to reflect the enabled state
56+
if err := waitForSAMLAuthEnabled(pc, organization, d.Get("saml_auth_enabled").(bool), 30); err != nil {
57+
return diag.FromErr(err)
58+
}
3459
return samlAuthRead(ctx, d, m)
3560
}
3661

@@ -73,7 +98,10 @@ func samlAuthUpdate(ctx context.Context, d *schema.ResourceData, m interface{})
7398
if err != nil {
7499
return diag.FromErr(handleSAMLAuthError(err, resp, "updating SAML authentication"))
75100
}
76-
101+
// Wait for the backend to reflect the enabled state
102+
if err := waitForSAMLAuthEnabled(pc, organization, d.Get("saml_auth_enabled").(bool), 30); err != nil {
103+
return diag.FromErr(err)
104+
}
77105
return samlAuthRead(ctx, d, m)
78106
}
79107

@@ -94,6 +122,10 @@ func samlAuthDelete(ctx context.Context, d *schema.ResourceData, m interface{})
94122
return diag.FromErr(handleSAMLAuthError(err, resp, "deleting SAML authentication"))
95123
}
96124

125+
// Wait for the backend to reflect the disabled state
126+
if err := waitForSAMLAuthEnabled(pc, organization, false, 30); err != nil {
127+
return diag.FromErr(err)
128+
}
97129
d.SetId("")
98130
return nil
99131
}

0 commit comments

Comments
 (0)