Skip to content

Commit

Permalink
feat: set default target (#1269)
Browse files Browse the repository at this point in the history
BREAKING CHANGE: provider interface has been changed so they need to be updated.

Signed-off-by: Ivan Dagelic <[email protected]>
  • Loading branch information
idagelic authored Oct 18, 2024
1 parent 9b10908 commit 5130f09
Show file tree
Hide file tree
Showing 53 changed files with 1,144 additions and 120 deletions.
1 change: 1 addition & 0 deletions docs/daytona_git-providers_delete.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ daytona git-providers delete [flags]

```
-a, --all Remove all Git providers
-y, --yes Confirm deletion without prompt
```

### Options inherited from parent commands
Expand Down
1 change: 1 addition & 0 deletions docs/daytona_target.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ Manage provider targets
* [daytona target list](daytona_target_list.md) - List targets
* [daytona target remove](daytona_target_remove.md) - Remove target
* [daytona target set](daytona_target_set.md) - Set provider target
* [daytona target set-default](daytona_target_set-default.md) - Set target to be used by default

18 changes: 18 additions & 0 deletions docs/daytona_target_set-default.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
## daytona target set-default

Set target to be used by default

```
daytona target set-default [TARGET_NAME] [flags]
```

### Options inherited from parent commands

```
--help help for daytona
```

### SEE ALSO

* [daytona target](daytona_target.md) - Manage provider targets

4 changes: 4 additions & 0 deletions hack/docs/daytona_git-providers_delete.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ options:
shorthand: a
default_value: "false"
usage: Remove all Git providers
- name: "yes"
shorthand: "y"
default_value: "false"
usage: Confirm deletion without prompt
inherited_options:
- name: help
default_value: "false"
Expand Down
1 change: 1 addition & 0 deletions hack/docs/daytona_target.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ see_also:
- daytona target list - List targets
- daytona target remove - Remove target
- daytona target set - Set provider target
- daytona target set-default - Set target to be used by default
9 changes: 9 additions & 0 deletions hack/docs/daytona_target_set-default.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
name: daytona target set-default
synopsis: Set target to be used by default
usage: daytona target set-default [TARGET_NAME] [flags]
inherited_options:
- name: help
default_value: "false"
usage: help for daytona
see_also:
- daytona target - Manage provider targets
54 changes: 43 additions & 11 deletions internal/testing/provider/targets/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
package targets

import (
"fmt"

"github.com/daytonaio/daytona/pkg/provider"
)

Expand All @@ -19,22 +21,20 @@ func NewInMemoryTargetStore() provider.TargetStore {
}
}

func (s *InMemoryTargetStore) List() ([]*provider.ProviderTarget, error) {
targets := []*provider.ProviderTarget{}
for _, t := range s.targets {
targets = append(targets, t)
}

return targets, nil
func (s *InMemoryTargetStore) List(filter *provider.TargetFilter) ([]*provider.ProviderTarget, error) {
return s.processFilters(filter)
}

func (s *InMemoryTargetStore) Find(targetName string) (*provider.ProviderTarget, error) {
target, ok := s.targets[targetName]
if !ok {
func (s *InMemoryTargetStore) Find(filter *provider.TargetFilter) (*provider.ProviderTarget, error) {
targets, err := s.processFilters(filter)
if err != nil {
return nil, err
}
if len(targets) == 0 {
return nil, provider.ErrTargetNotFound
}

return target, nil
return targets[0], nil
}

func (s *InMemoryTargetStore) Save(target *provider.ProviderTarget) error {
Expand All @@ -46,3 +46,35 @@ func (s *InMemoryTargetStore) Delete(target *provider.ProviderTarget) error {
delete(s.targets, target.Name)
return nil
}

func (s *InMemoryTargetStore) processFilters(filter *provider.TargetFilter) ([]*provider.ProviderTarget, error) {
var result []*provider.ProviderTarget
filteredTargets := make(map[string]*provider.ProviderTarget)
for k, v := range s.targets {
filteredTargets[k] = v
}

if filter != nil {
if filter.Name != nil {
target, ok := s.targets[*filter.Name]
if ok {
return []*provider.ProviderTarget{target}, nil
} else {
return []*provider.ProviderTarget{}, fmt.Errorf("target with name %s not found", *filter.Name)
}
}
if filter.Default != nil {
for _, target := range filteredTargets {
if target.IsDefault != *filter.Default {
delete(filteredTargets, target.Name)
}
}
}
}

for _, target := range filteredTargets {
result = append(result, target)
}

return result, nil
}
17 changes: 17 additions & 0 deletions internal/util/apiclient/conversion/target.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Copyright 2024 Daytona Platforms Inc.
// SPDX-License-Identifier: Apache-2.0

package conversion

import (
"github.com/daytonaio/daytona/pkg/provider"
"github.com/daytonaio/daytona/pkg/server/providertargets/dto"
)

func ToProviderTarget(createProviderTargetDto dto.CreateProviderTargetDTO) *provider.ProviderTarget {
return &provider.ProviderTarget{
Name: createProviderTargetDto.Name,
ProviderInfo: createProviderTargetDto.ProviderInfo,
Options: createProviderTargetDto.Options,
}
}
2 changes: 1 addition & 1 deletion pkg/api/controllers/target/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (
func ListTargets(ctx *gin.Context) {
server := server.GetInstance(nil)

targets, err := server.ProviderTargetService.List()
targets, err := server.ProviderTargetService.List(nil)
if err != nil {
ctx.AbortWithError(http.StatusInternalServerError, fmt.Errorf("failed to list targets: %w", err))
return
Expand Down
5 changes: 4 additions & 1 deletion pkg/api/controllers/target/remove.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"fmt"
"net/http"

"github.com/daytonaio/daytona/pkg/provider"
"github.com/daytonaio/daytona/pkg/server"
"github.com/gin-gonic/gin"
)
Expand All @@ -26,7 +27,9 @@ func RemoveTarget(ctx *gin.Context) {

server := server.GetInstance(nil)

target, err := server.ProviderTargetService.Find(targetName)
target, err := server.ProviderTargetService.Find(&provider.TargetFilter{
Name: &targetName,
})
if err != nil {
ctx.AbortWithError(http.StatusNotFound, fmt.Errorf("failed to find target: %w", err))
return
Expand Down
46 changes: 37 additions & 9 deletions pkg/api/controllers/target/set.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ import (
"fmt"
"net/http"

"github.com/daytonaio/daytona/internal/util/apiclient/conversion"
"github.com/daytonaio/daytona/pkg/provider"
"github.com/daytonaio/daytona/pkg/server"
"github.com/daytonaio/daytona/pkg/server/providertargets/dto"
"github.com/gin-gonic/gin"
)

Expand All @@ -17,13 +19,13 @@ import (
// @Tags target
// @Summary Set a target
// @Description Set a target
// @Param target body ProviderTarget true "Target to set"
// @Param target body CreateProviderTargetDTO true "Target to set"
// @Success 201
// @Router /target [put]
//
// @id SetTarget
func SetTarget(ctx *gin.Context) {
var req provider.ProviderTarget
var req dto.CreateProviderTargetDTO
err := ctx.BindJSON(&req)
if err != nil {
ctx.AbortWithError(http.StatusBadRequest, fmt.Errorf("invalid request body: %w", err))
Expand All @@ -32,13 +34,7 @@ func SetTarget(ctx *gin.Context) {

server := server.GetInstance(nil)

target, err := server.ProviderTargetService.Find(req.Name)
if err == nil {
target.Options = req.Options
target.ProviderInfo = req.ProviderInfo
} else {
target = &req
}
target := conversion.ToProviderTarget(req)

err = server.ProviderTargetService.Save(target)
if err != nil {
Expand All @@ -48,3 +44,35 @@ func SetTarget(ctx *gin.Context) {

ctx.Status(201)
}

// SetDefaultTarget godoc
//
// @Tags target
// @Summary Set target to default
// @Description Set target to default
// @Param target path string true "Target name"
// @Success 200
// @Router /target/{target}/set-default [patch]
//
// @id SetDefaultTarget
func SetDefaultTarget(ctx *gin.Context) {
targetName := ctx.Param("target")

server := server.GetInstance(nil)

target, err := server.ProviderTargetService.Find(&provider.TargetFilter{
Name: &targetName,
})
if err != nil {
ctx.AbortWithError(http.StatusNotFound, fmt.Errorf("failed to find target: %w", err))
return
}

err = server.ProviderTargetService.SetDefault(target)
if err != nil {
ctx.AbortWithError(http.StatusNotFound, fmt.Errorf("failed to set project config to default: %s", err.Error()))
return
}

ctx.Status(200)
}
49 changes: 48 additions & 1 deletion pkg/api/docs/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -1522,7 +1522,7 @@ const docTemplate = `{
"in": "body",
"required": true,
"schema": {
"$ref": "#/definitions/ProviderTarget"
"$ref": "#/definitions/CreateProviderTargetDTO"
}
}
],
Expand Down Expand Up @@ -1557,6 +1557,30 @@ const docTemplate = `{
}
}
},
"/target/{target}/set-default": {
"patch": {
"description": "Set target to default",
"tags": [
"target"
],
"summary": "Set target to default",
"operationId": "SetDefaultTarget",
"parameters": [
{
"type": "string",
"description": "Target name",
"name": "target",
"in": "path",
"required": true
}
],
"responses": {
"200": {
"description": "OK"
}
}
}
},
"/workspace": {
"get": {
"description": "List workspaces",
Expand Down Expand Up @@ -2107,6 +2131,25 @@ const docTemplate = `{
}
}
},
"CreateProviderTargetDTO": {
"type": "object",
"required": [
"name",
"options",
"providerInfo"
],
"properties": {
"name": {
"type": "string"
},
"options": {
"type": "string"
},
"providerInfo": {
"$ref": "#/definitions/provider.ProviderInfo"
}
}
},
"CreateWorkspaceDTO": {
"type": "object",
"required": [
Expand Down Expand Up @@ -2680,11 +2723,15 @@ const docTemplate = `{
"ProviderTarget": {
"type": "object",
"required": [
"isDefault",
"name",
"options",
"providerInfo"
],
"properties": {
"isDefault": {
"type": "boolean"
},
"name": {
"type": "string"
},
Expand Down
Loading

0 comments on commit 5130f09

Please sign in to comment.