Skip to content

Commit 5776911

Browse files
authored
conditionally publish catalog item (#122)
* conditionally publish catalog item * add self service attribute to example and docs
1 parent 342355c commit 5776911

File tree

4 files changed

+58
-26
lines changed

4 files changed

+58
-26
lines changed

docs/resources/catalog_item.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ resource "torque_catalog_item" "catalog_item" {
3131
space_name = "space"
3232
blueprint_name = "blueprint_name"
3333
repository_name = "repository_name"
34+
self_service = true
3435
display_name = "display_name"
3536
max_duration = "PT2H"
3637
default_duration = "PT2H"
@@ -63,3 +64,4 @@ resource "torque_catalog_item" "catalog_item" {
6364
- `labels` (List of String) List of labels to associate with this catalog item.
6465
- `max_active_environments` (Number) Sets the maximum number of concurrent active environments insantiated from this blueprint.
6566
- `max_duration` (String) The maximum duration of an environment instantiated from this blueprint.
67+
- `self_service` (Boolean) Specify if environments launched from this blueprint should be always on or not.

examples/resources/torque_catalog_item/resource.tf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ resource "torque_catalog_item" "catalog_item" {
1616
space_name = "space"
1717
blueprint_name = "blueprint_name"
1818
repository_name = "repository_name"
19+
self_service = true
1920
display_name = "display_name"
2021
max_duration = "PT2H"
2122
default_duration = "PT2H"

internal/provider/resources/torque_catalog_item_resource.go

Lines changed: 52 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
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

272285
func (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

301314
func (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
}

internal/provider/tests/catalog_item_resource_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,10 @@ func TestCatalogItemResource(t *testing.T) {
8181
space_name = "%s"
8282
blueprint_name = "%s"
8383
repository_name = "%s"
84+
display_name ="display_name"
8485
default_duration = "PT3H"
8586
default_extend = "PT9H"
86-
max_duration = "PT30H"
87+
max_duration = "P1DT6H"
8788
labels = ["k8s"]
8889
}
8990
`, spaceName, new_unique_blueprint_name, repository_name),
@@ -94,7 +95,7 @@ func TestCatalogItemResource(t *testing.T) {
9495
resource.TestCheckResourceAttr("torque_catalog_item.catalog_item", "always_on", "false"),
9596
resource.TestCheckResourceAttr("torque_catalog_item.catalog_item", "default_duration", "PT3H"),
9697
resource.TestCheckResourceAttr("torque_catalog_item.catalog_item", "default_extend", "PT9H"),
97-
resource.TestCheckResourceAttr("torque_catalog_item.catalog_item", "max_duration", "PT30H"),
98+
resource.TestCheckResourceAttr("torque_catalog_item.catalog_item", "max_duration", "P1DT6H"),
9899
resource.TestCheckResourceAttr("torque_catalog_item.catalog_item", "labels.#", "1"),
99100
testBlueprintPublished(new_unique_blueprint_name),
100101
),

0 commit comments

Comments
 (0)