44 "context"
55 "fmt"
66 "regexp"
7- "time"
87
98 "github.com/hashicorp/terraform-plugin-framework-validators/boolvalidator"
109 "github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
@@ -38,6 +37,7 @@ type TorqueCatalogItemResourceModel struct {
3837 SpaceName types.String `tfsdk:"space_name"`
3938 BlueprintName types.String `tfsdk:"blueprint_name"`
4039 DisplayName types.String `tfsdk:"display_name"`
40+ SelfService types.Bool `tfsdk:"self_service"`
4141 RepositoryName types.String `tfsdk:"repository_name"`
4242 MaxDuration types.String `tfsdk:"max_duration"`
4343 DefaultDuration types.String `tfsdk:"default_duration"`
@@ -77,7 +77,7 @@ func (r *TorqueCatalogItemResource) Schema(ctx context.Context, req resource.Sch
7777 "display_name" : schema.StringAttribute {
7878 MarkdownDescription : "The display name of the blueprint as it will be displayed in the self-service catalog." ,
7979 Required : false ,
80- Computed : false ,
80+ Computed : true ,
8181 Optional : true ,
8282 },
8383 "repository_name" : schema.StringAttribute {
@@ -88,6 +88,13 @@ func (r *TorqueCatalogItemResource) Schema(ctx context.Context, req resource.Sch
8888 stringplanmodifier .RequiresReplace (),
8989 },
9090 },
91+ "self_service" : schema.BoolAttribute {
92+ MarkdownDescription : "Specify if environments launched from this blueprint should be always on or not." ,
93+ Required : false ,
94+ Optional : true ,
95+ Computed : true ,
96+ Default : booldefault .StaticBool (true ),
97+ },
9198 "max_duration" : schema.StringAttribute {
9299 MarkdownDescription : "The maximum duration of an environment instantiated from this blueprint." ,
93100 Required : false ,
@@ -208,6 +215,7 @@ func (r *TorqueCatalogItemResource) Create(ctx context.Context, req resource.Cre
208215 if resp .Diagnostics .HasError () {
209216 return
210217 }
218+
211219 if data .AlwaysOn .ValueBool () {
212220 data .MaxDuration = types .StringNull ()
213221 data .DefaultExtend = types .StringNull ()
@@ -240,18 +248,23 @@ func (r *TorqueCatalogItemResource) Create(ctx context.Context, req resource.Cre
240248 resp .Diagnostics .AddError ("Client Error" , fmt .Sprintf ("Unable to create Catalog Item, failed to set blueprint policies, got error: %s" , err ))
241249 return
242250 }
243- if ! data .DisplayName .IsNull () {
251+ if ! data .DisplayName .IsNull () && ! data . DisplayName . IsUnknown () {
244252 err = r .client .UpdateBlueprintDisplayName (data .SpaceName .ValueString (), data .RepositoryName .ValueString (), data .BlueprintName .ValueString (), data .DisplayName .ValueString ())
245253 if err != nil {
246254 resp .Diagnostics .AddError ("Client Error" , fmt .Sprintf ("Unable to create Catalog Item, failed to set blueprint display name, got error: %s" , err ))
247255 return
248256 }
257+ } else {
258+ data .DisplayName = data .BlueprintName
249259 }
250- err = r .client .PublishBlueprintInSpace (data .SpaceName .ValueString (), data .RepositoryName .ValueString (), data .BlueprintName .ValueString ())
251- if err != nil {
252- resp .Diagnostics .AddError ("Client Error" , fmt .Sprintf ("Unable to create Catalog Item, failed to publish blueprint in space, got error: %s" , err ))
253- return
260+ if data .SelfService .ValueBool () {
261+ err = r .client .PublishBlueprintInSpace (data .SpaceName .ValueString (), data .RepositoryName .ValueString (), data .BlueprintName .ValueString ())
262+ if err != nil {
263+ resp .Diagnostics .AddError ("Client Error" , fmt .Sprintf ("Unable to create Catalog Item, failed to publish blueprint in space, got error: %s" , err ))
264+ return
265+ }
254266 }
267+
255268 if ! data .Labels .IsNull () {
256269 var labels []string
257270 data .Labels .ElementsAs (ctx , & labels , false )
@@ -271,31 +284,31 @@ func (r *TorqueCatalogItemResource) Create(ctx context.Context, req resource.Cre
271284
272285func (r * TorqueCatalogItemResource ) Read (ctx context.Context , req resource.ReadRequest , resp * resource.ReadResponse ) {
273286 var data TorqueCatalogItemResourceModel
274-
275287 // Read Terraform prior state data into the model.
288+
276289 resp .Diagnostics .Append (req .State .Get (ctx , & data )... )
277290 diags := req .State .Get (ctx , & data )
278291 resp .Diagnostics .Append (diags ... )
279292 if resp .Diagnostics .HasError () {
280293 return
281294 }
282- start := time .Now ()
283- for time .Since (start ) < 20 * time .Second {
284- blueprint , err := r .client .GetBlueprint (data .SpaceName .ValueString (), data .BlueprintName .ValueString ())
285- if err != nil {
286- resp .Diagnostics .AddError ("Client Error" , fmt .Sprintf ("Unable to get catalog items in space, got error: %s" , err .Error ()))
287- return
288- }
289295
290- if blueprint != nil && blueprint .Published {
291- resp .Diagnostics .Append (resp .State .Set (ctx , & data )... )
292- return
293- }
294- time .Sleep (500 * time .Millisecond ) // Retry every 500ms
296+ blueprint , err := r .client .GetBlueprint (data .SpaceName .ValueString (), data .BlueprintName .ValueString ())
297+ if err != nil {
298+ resp .Diagnostics .AddError ("Client Error" , fmt .Sprintf ("Unable to get catalog items in space, got error: %s" , err .Error ()))
299+ return
295300 }
301+ data .DisplayName = types .StringValue (blueprint .DisplayName )
302+ data .SelfService = types .BoolValue (blueprint .Published )
303+ data .RepositoryName = types .StringValue (blueprint .RepoName )
304+ data .MaxDuration = types .StringValue (blueprint .Policies .MaxDuration )
305+ data .DefaultDuration = types .StringValue (blueprint .Policies .DefaultDuration )
306+ data .DefaultExtend = types .StringValue (blueprint .Policies .DefaultExtend )
307+ data .MaxActiveEnvironments = types .Int32PointerValue (blueprint .Policies .MaxActiveEnvironments )
308+ data .AlwaysOn = types .BoolValue (blueprint .Policies .AlwaysOn )
309+ data .AllowScheduling = types .BoolValue (blueprint .Policies .AllowScheduling )
296310
297- // Save updated data into Terraform state.
298- resp .Diagnostics .AddError ("Blueprint not published to catalog" , "Blueprint was found in space but it is not published to catalog" )
311+ resp .Diagnostics .Append (resp .State .Set (ctx , & data )... )
299312}
300313
301314func (r * TorqueCatalogItemResource ) Update (ctx context.Context , req resource.UpdateRequest , resp * resource.UpdateResponse ) {
@@ -338,12 +351,14 @@ func (r *TorqueCatalogItemResource) Update(ctx context.Context, req resource.Upd
338351 resp .Diagnostics .AddError ("Client Error" , fmt .Sprintf ("Unable to set blueprint policies, got error: %s" , err ))
339352 return
340353 }
341- if ! data .DisplayName .IsNull () {
354+ if ! data .DisplayName .IsNull () && ! data . DisplayName . IsUnknown () {
342355 err = r .client .UpdateBlueprintDisplayName (data .SpaceName .ValueString (), data .RepositoryName .ValueString (), data .BlueprintName .ValueString (), data .DisplayName .ValueString ())
343356 if err != nil {
344- resp .Diagnostics .AddError ("Client Error" , fmt .Sprintf ("Unable to update Catalog Item, failed to set blueprint display name, got error: %s" , err ))
357+ resp .Diagnostics .AddError ("Client Error" , fmt .Sprintf ("Unable to create Catalog Item, failed to set blueprint display name, got error: %s" , err ))
345358 return
346359 }
360+ } else {
361+ data .DisplayName = data .BlueprintName
347362 }
348363 if data .CustomIcon .IsNull () {
349364 err := r .client .SetCatalogItemIcon (data .SpaceName .ValueString (), data .BlueprintName .ValueString (), data .RepositoryName .ValueString (), default_icon )
@@ -367,6 +382,19 @@ func (r *TorqueCatalogItemResource) Update(ctx context.Context, req resource.Upd
367382 resp .Diagnostics .AddError ("Client Error" , fmt .Sprintf ("Unable to update Catalog Item, failed to update labels, got error: %s" , err ))
368383 return
369384 }
385+ if data .SelfService .ValueBool () && ! state .SelfService .ValueBool () {
386+ err = r .client .PublishBlueprintInSpace (data .SpaceName .ValueString (), data .RepositoryName .ValueString (), data .BlueprintName .ValueString ())
387+ if err != nil {
388+ resp .Diagnostics .AddError ("Client Error" , fmt .Sprintf ("Unable to edit Catalog Item, failed to publish blueprint in space, got error: %s" , err ))
389+ return
390+ }
391+ } else if ! data .SelfService .ValueBool () && state .SelfService .ValueBool () {
392+ err = r .client .UnpublishBlueprintInSpace (data .SpaceName .ValueString (), data .RepositoryName .ValueString (), data .BlueprintName .ValueString ())
393+ if err != nil {
394+ resp .Diagnostics .AddError ("Client Error" , fmt .Sprintf ("Unable to edit Catalog Item, failed to unpublish blueprint in space, got error: %s" , err ))
395+ return
396+ }
397+ }
370398 // Save updated data into Terraform state
371399 resp .Diagnostics .Append (resp .State .Set (ctx , & data )... )
372400}
0 commit comments