@@ -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
1839func 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