Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

v1/api: Image name transposition from Blueprint name (HMS-3801) #1093

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 7 additions & 1 deletion internal/v1/handler_blueprints.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"net/url"
"regexp"
"strconv"
"strings"
"time"

"github.com/jackc/pgerrcode"
Expand All @@ -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 {
Expand All @@ -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,
Expand Down Expand Up @@ -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,
}
Expand Down
6 changes: 5 additions & 1 deletion internal/v1/handler_blueprints_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -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) {
Expand Down