Skip to content

Commit

Permalink
fix: required project name (#426)
Browse files Browse the repository at this point in the history
Signed-off-by: Ivan Dagelic <[email protected]>
  • Loading branch information
idagelic authored Apr 24, 2024
1 parent d72417f commit 71e4d3b
Show file tree
Hide file tree
Showing 14 changed files with 189 additions and 137 deletions.
9 changes: 6 additions & 3 deletions pkg/api/docs/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -1084,6 +1084,9 @@ const docTemplate = `{
},
"CreateWorkspaceRequest": {
"type": "object",
"required": [
"projects"
],
"properties": {
"id": {
"type": "string"
Expand All @@ -1104,16 +1107,16 @@ const docTemplate = `{
},
"CreateWorkspaceRequestProject": {
"type": "object",
"required": [
"name"
],
"properties": {
"envVars": {
"type": "object",
"additionalProperties": {
"type": "string"
}
},
"id": {
"type": "string"
},
"image": {
"type": "string"
},
Expand Down
9 changes: 6 additions & 3 deletions pkg/api/docs/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -1081,6 +1081,9 @@
},
"CreateWorkspaceRequest": {
"type": "object",
"required": [
"projects"
],
"properties": {
"id": {
"type": "string"
Expand All @@ -1101,16 +1104,16 @@
},
"CreateWorkspaceRequestProject": {
"type": "object",
"required": [
"name"
],
"properties": {
"envVars": {
"type": "object",
"additionalProperties": {
"type": "string"
}
},
"id": {
"type": "string"
},
"image": {
"type": "string"
},
Expand Down
6 changes: 4 additions & 2 deletions pkg/api/docs/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,15 @@ definitions:
type: array
target:
type: string
required:
- projects
type: object
CreateWorkspaceRequestProject:
properties:
envVars:
additionalProperties:
type: string
type: object
id:
type: string
image:
type: string
name:
Expand All @@ -48,6 +48,8 @@ definitions:
$ref: '#/definitions/CreateWorkspaceRequestProjectSource'
user:
type: string
required:
- name
type: object
CreateWorkspaceRequestProjectSource:
properties:
Expand Down
3 changes: 3 additions & 0 deletions pkg/api/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import (
"github.com/daytonaio/daytona/pkg/api/controllers/workspace"

"github.com/gin-gonic/gin"
"github.com/gin-gonic/gin/binding"
log "github.com/sirupsen/logrus"

swaggerfiles "github.com/swaggo/files"
Expand Down Expand Up @@ -69,6 +70,8 @@ func (a *ApiServer) Start() error {
docs.SwaggerInfo.Description = "Daytona Server API"
docs.SwaggerInfo.Title = "Daytona Server API"

binding.Validator = new(defaultValidator)

if mode, ok := os.LookupEnv("DAYTONA_SERVER_MODE"); ok && mode == "development" {
a.router = gin.Default()
a.router.Use(cors.New(cors.Config{
Expand Down
53 changes: 53 additions & 0 deletions pkg/api/validator.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// Copyright 2024 Daytona Platforms Inc.
// SPDX-License-Identifier: Apache-2.0

package api

import (
"reflect"
"sync"

"github.com/gin-gonic/gin/binding"
"github.com/go-playground/validator/v10"
)

type defaultValidator struct {
once sync.Once
validate *validator.Validate
}

var _ binding.StructValidator = &defaultValidator{}

func (v *defaultValidator) ValidateStruct(obj interface{}) error {
if kindOfData(obj) == reflect.Struct {
v.lazyinit()

if err := v.validate.Struct(obj); err != nil {
return err
}
}

return nil
}

func (v *defaultValidator) Engine() interface{} {
v.lazyinit()
return v.validate
}

func (v *defaultValidator) lazyinit() {
v.once.Do(func() {
v.validate = validator.New(validator.WithRequiredStructEnabled())
})
}

func kindOfData(data interface{}) reflect.Kind {
value := reflect.ValueOf(data)
valueType := value.Kind()

if valueType == reflect.Ptr {
valueType = value.Elem().Kind()
}

return valueType
}
3 changes: 1 addition & 2 deletions pkg/cmd/workspace/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,7 @@ var CreateCmd = &cobra.Command{
projectNameSlugRegex := regexp.MustCompile(`[^a-zA-Z0-9-]`)
projectName := projectNameSlugRegex.ReplaceAllString(strings.TrimSuffix(strings.ToLower(filepath.Base(*repo.Url)), ".git"), "-")
projects = append(projects, serverapiclient.CreateWorkspaceRequestProject{
Id: &projectName,
Name: &projectName,
Name: projectName,
Source: &serverapiclient.CreateWorkspaceRequestProjectSource{
Repository: &repo,
},
Expand Down
5 changes: 2 additions & 3 deletions pkg/server/workspaces/dto/workspace.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ type CreateWorkspaceRequestProjectSource struct {
} // @name CreateWorkspaceRequestProjectSource

type CreateWorkspaceRequestProject struct {
Id string `json:"id"`
Name string `json:"name"`
Name string `json:"name" validate:"required,gt=0"`
Image *string `json:"image,omitempty"`
User *string `json:"user,omitempty"`
Source CreateWorkspaceRequestProjectSource `json:"source"`
Expand All @@ -35,5 +34,5 @@ type CreateWorkspaceRequest struct {
Id string `json:"id"`
Name string `json:"name"`
Target string `json:"target"`
Projects []CreateWorkspaceRequestProject `json:"projects"`
Projects []CreateWorkspaceRequestProject `json:"projects" validate:"required,gt=0,dive"`
} // @name CreateWorkspaceRequest
1 change: 0 additions & 1 deletion pkg/server/workspaces/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ var createWorkspaceRequest = dto.CreateWorkspaceRequest{
Target: target.Name,
Projects: []dto.CreateWorkspaceRequestProject{
{
Id: "project1",
Name: "project1",
Source: dto.CreateWorkspaceRequestProjectSource{
Repository: &gitprovider.GitRepository{
Expand Down
9 changes: 4 additions & 5 deletions pkg/serverapiclient/api/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -787,7 +787,6 @@ components:
envVars:
key: envVars
name: name
id: id
source:
repository:
owner: owner
Expand All @@ -804,7 +803,6 @@ components:
envVars:
key: envVars
name: name
id: id
source:
repository:
owner: owner
Expand All @@ -831,14 +829,15 @@ components:
type: array
target:
type: string
required:
- projects
type: object
CreateWorkspaceRequestProject:
example:
image: image
envVars:
key: envVars
name: name
id: id
source:
repository:
owner: owner
Expand All @@ -856,8 +855,6 @@ components:
additionalProperties:
type: string
type: object
id:
type: string
image:
type: string
name:
Expand All @@ -866,6 +863,8 @@ components:
$ref: '#/components/schemas/CreateWorkspaceRequestProjectSource'
user:
type: string
required:
- name
type: object
CreateWorkspaceRequestProjectSource:
example:
Expand Down
9 changes: 2 additions & 7 deletions pkg/serverapiclient/docs/CreateWorkspaceRequest.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ Name | Type | Description | Notes
------------ | ------------- | ------------- | -------------
**Id** | Pointer to **string** | | [optional]
**Name** | Pointer to **string** | | [optional]
**Projects** | Pointer to [**[]CreateWorkspaceRequestProject**](CreateWorkspaceRequestProject.md) | | [optional]
**Projects** | [**[]CreateWorkspaceRequestProject**](CreateWorkspaceRequestProject.md) | |
**Target** | Pointer to **string** | | [optional]

## Methods

### NewCreateWorkspaceRequest

`func NewCreateWorkspaceRequest() *CreateWorkspaceRequest`
`func NewCreateWorkspaceRequest(projects []CreateWorkspaceRequestProject, ) *CreateWorkspaceRequest`

NewCreateWorkspaceRequest instantiates a new CreateWorkspaceRequest object
This constructor will assign default values to properties that have it defined,
Expand Down Expand Up @@ -97,11 +97,6 @@ and a boolean to check if the value has been set.

SetProjects sets Projects field to given value.

### HasProjects

`func (o *CreateWorkspaceRequest) HasProjects() bool`

HasProjects returns a boolean if a field has been set.

### GetTarget

Expand Down
35 changes: 2 additions & 33 deletions pkg/serverapiclient/docs/CreateWorkspaceRequestProject.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,16 @@
Name | Type | Description | Notes
------------ | ------------- | ------------- | -------------
**EnvVars** | Pointer to **map[string]string** | | [optional]
**Id** | Pointer to **string** | | [optional]
**Image** | Pointer to **string** | | [optional]
**Name** | Pointer to **string** | | [optional]
**Name** | **string** | |
**Source** | Pointer to [**CreateWorkspaceRequestProjectSource**](CreateWorkspaceRequestProjectSource.md) | | [optional]
**User** | Pointer to **string** | | [optional]

## Methods

### NewCreateWorkspaceRequestProject

`func NewCreateWorkspaceRequestProject() *CreateWorkspaceRequestProject`
`func NewCreateWorkspaceRequestProject(name string, ) *CreateWorkspaceRequestProject`

NewCreateWorkspaceRequestProject instantiates a new CreateWorkspaceRequestProject object
This constructor will assign default values to properties that have it defined,
Expand Down Expand Up @@ -55,31 +54,6 @@ SetEnvVars sets EnvVars field to given value.

HasEnvVars returns a boolean if a field has been set.

### GetId

`func (o *CreateWorkspaceRequestProject) GetId() string`

GetId returns the Id field if non-nil, zero value otherwise.

### GetIdOk

`func (o *CreateWorkspaceRequestProject) GetIdOk() (*string, bool)`

GetIdOk returns a tuple with the Id field if it's non-nil, zero value otherwise
and a boolean to check if the value has been set.

### SetId

`func (o *CreateWorkspaceRequestProject) SetId(v string)`

SetId sets Id field to given value.

### HasId

`func (o *CreateWorkspaceRequestProject) HasId() bool`

HasId returns a boolean if a field has been set.

### GetImage

`func (o *CreateWorkspaceRequestProject) GetImage() string`
Expand Down Expand Up @@ -124,11 +98,6 @@ and a boolean to check if the value has been set.

SetName sets Name field to given value.

### HasName

`func (o *CreateWorkspaceRequestProject) HasName() bool`

HasName returns a boolean if a field has been set.

### GetSource

Expand Down
2 changes: 1 addition & 1 deletion pkg/serverapiclient/docs/WorkspaceAPI.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ import (
)

func main() {
workspace := *openapiclient.NewCreateWorkspaceRequest() // CreateWorkspaceRequest | Create workspace
workspace := *openapiclient.NewCreateWorkspaceRequest([]openapiclient.CreateWorkspaceRequestProject{*openapiclient.NewCreateWorkspaceRequestProject("Name_example")}) // CreateWorkspaceRequest | Create workspace

configuration := openapiclient.NewConfiguration()
apiClient := openapiclient.NewAPIClient(configuration)
Expand Down
Loading

0 comments on commit 71e4d3b

Please sign in to comment.