@@ -4,16 +4,17 @@ import (
44 "context"
55 "fmt"
66 "strings"
7+ "time"
78
89 "github.com/hashicorp/terraform-plugin-framework/path"
910 "github.com/hashicorp/terraform-plugin-framework/resource"
1011 "github.com/hashicorp/terraform-plugin-framework/resource/schema"
1112 "github.com/hashicorp/terraform-plugin-framework/resource/schema/booldefault"
13+ "github.com/hashicorp/terraform-plugin-framework/resource/schema/int32default"
1214 "github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
1315 "github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
1416 "github.com/hashicorp/terraform-plugin-framework/schema/validator"
1517 "github.com/hashicorp/terraform-plugin-framework/types"
16- "github.com/hashicorp/terraform-plugin-log/tflog"
1718 "github.com/qualitorque/terraform-provider-torque/client"
1819)
1920
@@ -39,6 +40,7 @@ type TorqueSpaceGitlabEnterpriseRepositoryResourceModel struct {
3940 CredentialName types.String `tfsdk:"credential_name"`
4041 UseAllAgents types.Bool `tfsdk:"use_all_agents"`
4142 Agents types.List `tfsdk:"agents"`
43+ TimeOut types.Int32 `tfsdk:"timeout"`
4244}
4345
4446func (r * TorqueSpaceGitlabEnterpriseRepositoryResource ) Metadata (ctx context.Context , req resource.MetadataRequest , resp * resource.MetadataResponse ) {
@@ -103,6 +105,13 @@ func (r *TorqueSpaceGitlabEnterpriseRepositoryResource) Schema(ctx context.Conte
103105 Optional : true ,
104106 ElementType : types .StringType ,
105107 },
108+ "timeout" : schema.Int32Attribute {
109+ Description : "Time in minutes to wait for Torque to sync the repository during the onboarding. Default is 1 minute." ,
110+ Required : false ,
111+ Optional : true ,
112+ Computed : true ,
113+ Default : int32default .StaticInt32 (1 ),
114+ },
106115 },
107116 }
108117}
@@ -129,7 +138,11 @@ func (r *TorqueSpaceGitlabEnterpriseRepositoryResource) Configure(ctx context.Co
129138
130139func (r * TorqueSpaceGitlabEnterpriseRepositoryResource ) Create (ctx context.Context , req resource.CreateRequest , resp * resource.CreateResponse ) {
131140 var data TorqueSpaceGitlabEnterpriseRepositoryResourceModel
132-
141+ const (
142+ StatusSyncing = "Syncing"
143+ StatusConnected = "Connected"
144+ Interval = 4 * time .Second
145+ )
133146 resp .Diagnostics .Append (req .Plan .Get (ctx , & data )... )
134147
135148 if resp .Diagnostics .HasError () {
@@ -141,16 +154,35 @@ func (r *TorqueSpaceGitlabEnterpriseRepositoryResource) Create(ctx context.Conte
141154 agents = append (agents , strings .Trim (agent .String (), "\" " ))
142155 }
143156 }
157+ start := time .Now ()
144158 err := r .client .OnboardGitlabEnterpriseRepoToSpace (data .SpaceName .ValueString (), data .RepositoryName .ValueString (),
145159 data .RepositoryUrl .ValueString (), data .Token .ValueStringPointer (), data .Branch .ValueString (), data .CredentialName .ValueString (), agents , data .UseAllAgents .ValueBool ())
146160 if err != nil {
161+ repo , err := r .client .GetRepoDetails (data .SpaceName .ValueString (), data .RepositoryName .ValueString ())
162+ if repo == nil {
163+ resp .Diagnostics .AddError ("Client Error" , fmt .Sprintf ("Unable to onboard repository to space, got error: %s" , err ))
164+ return
165+ }
166+ if repo .Status == StatusSyncing {
167+ timeout := time .Duration (data .TimeOut .ValueInt32 ()) * time .Minute
168+ for time .Since (start ) < timeout {
169+ repo , err := r .client .GetRepoDetails (data .SpaceName .ValueString (), data .RepositoryName .ValueString ())
170+ if err != nil {
171+ resp .Diagnostics .AddError ("Client Error" , fmt .Sprintf ("Error while polling repository status: %s" , err ))
172+ return
173+ }
174+ if repo .Status == StatusConnected {
175+ resp .Diagnostics .Append (resp .State .Set (ctx , & data )... )
176+ return
177+ }
178+ time .Sleep (Interval )
179+ }
180+ resp .Diagnostics .AddError ("Sync Timeout" , "Timed out while syncing repository" )
181+ return
182+ }
147183 resp .Diagnostics .AddError ("Client Error" , fmt .Sprintf ("Unable to onboard repository to space, got error: %s" , err ))
148184 return
149185 }
150-
151- tflog .Trace (ctx , "Resource Created Successful!" )
152-
153- // Save data into Terraform state.
154186 resp .Diagnostics .Append (resp .State .Set (ctx , & data )... )
155187}
156188
0 commit comments