From c68fb96ad1e05fa08366ec336d7987321502b376 Mon Sep 17 00:00:00 2001 From: Ondrej Ezr Date: Thu, 21 Mar 2024 14:39:39 +0100 Subject: [PATCH] api/v1: transpose blueprint name to machine friendly image name On composing Blueprint, transpose its name to image name, that's machine friendly, unlike Blueprint name that's human readable. Refs HMS-3801 --- internal/v1/handler_blueprints.go | 8 +++++++- internal/v1/handler_blueprints_test.go | 6 +++++- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/internal/v1/handler_blueprints.go b/internal/v1/handler_blueprints.go index 433f7dd6f..aa97c8e2f 100644 --- a/internal/v1/handler_blueprints.go +++ b/internal/v1/handler_blueprints.go @@ -8,6 +8,7 @@ import ( "net/url" "regexp" "strconv" + "strings" "time" "github.com/jackc/pgerrcode" @@ -23,6 +24,7 @@ import ( var ( blueprintNameRegex = regexp.MustCompile(`\S+`) blueprintInvalidNameDetail = "The blueprint name must contain at least two characters." + specialChars = regexp.MustCompile(`[^a-zA-Z0-9 _-]`) ) type BlueprintBody struct { @@ -31,6 +33,10 @@ type BlueprintBody struct { ImageRequests []ImageRequest `json:"image_requests"` } +func blueprintToImageName(name string) string { + return strings.ToLower(strings.Replace(specialChars.ReplaceAllString(name, "_"), " ", "-", -1)) +} + func BlueprintFromAPI(cbr CreateBlueprintRequest) BlueprintBody { return BlueprintBody{ Customizations: cbr.Customizations, @@ -196,7 +202,7 @@ func (h *Handlers) ComposeBlueprint(ctx echo.Context, id openapi_types.UUID) err Customizations: &blueprint.Customizations, Distribution: blueprint.Distribution, ImageRequests: []ImageRequest{imageRequest}, - ImageName: &blueprintEntry.Name, + ImageName: common.ToPtr(blueprintToImageName(blueprintEntry.Name)), ImageDescription: &blueprintEntry.Description, ClientId: &clientId, } diff --git a/internal/v1/handler_blueprints_test.go b/internal/v1/handler_blueprints_test.go index 8a6640209..204b107dc 100644 --- a/internal/v1/handler_blueprints_test.go +++ b/internal/v1/handler_blueprints_test.go @@ -152,7 +152,7 @@ func TestHandlers_ComposeBlueprint(t *testing.T) { ShareWithAccounts: common.ToPtr([]string{"test-account"}), }) require.NoError(t, err) - name := "Blueprint Human Name" + name := "Blueprint Human NamešŸ¤£" description := "desc" blueprint := BlueprintBody{ Customizations: Customizations{ @@ -194,6 +194,10 @@ func TestHandlers_ComposeBlueprint(t *testing.T) { require.Len(t, result, 2) require.Equal(t, ids[0], result[0].Id) require.Equal(t, ids[1], result[1].Id) + + compose, err := dbase.GetCompose(ctx, result[0].Id, "000000") + require.NoError(t, err) + require.Equal(t, "blueprint-human-name_", *compose.ImageName) } func TestHandlers_GetBlueprintComposes(t *testing.T) {