Skip to content

Commit

Permalink
refactor: Move endpoints to their own files, rename (#429)
Browse files Browse the repository at this point in the history
* Rename operations to make more sense

* Move methods to separate files
  • Loading branch information
cdelst authored Sep 13, 2024
1 parent b492666 commit 15cbf77
Show file tree
Hide file tree
Showing 12 changed files with 623 additions and 553 deletions.
10 changes: 9 additions & 1 deletion internal/dev_server/api/api.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ paths:
/dev/projects:
get:
summary: lists all projects that have been configured for the dev server
operationId: getProjects
responses:
200:
description: OK. List of projects
Expand All @@ -26,6 +27,7 @@ paths:
/dev/projects/{projectKey}/sync:
patch:
summary: updates the flag state for the given project and source environment
operationId: patchSyncProject
parameters:
- $ref: "#/components/parameters/projectKey"
- $ref: "#/components/parameters/projectExpand"
Expand All @@ -37,6 +39,7 @@ paths:
/dev/projects/{projectKey}:
get:
summary: get the specified project and its configuration for syncing from the LaunchDarkly Service
operationId: getProject
parameters:
- $ref: "#/components/parameters/projectKey"
- $ref: "#/components/parameters/projectExpand"
Expand All @@ -47,6 +50,7 @@ paths:
description: No project found
patch:
summary: updates the project context or sourceEnvironmentKey
operationId: patchProject
parameters:
- $ref: "#/components/parameters/projectKey"
- $ref: "#/components/parameters/projectExpand"
Expand All @@ -68,6 +72,7 @@ paths:
description: No project found
delete:
summary: remove the specified project from the dev server
operationId: deleteProject
parameters:
- $ref: "#/components/parameters/projectKey"
responses:
Expand All @@ -77,6 +82,7 @@ paths:
$ref: "#/components/responses/ErrorResponse"
post:
summary: Add the project to the dev server
operationId: postAddProject
parameters:
- $ref: "#/components/parameters/projectKey"
- $ref: "#/components/parameters/projectExpand"
Expand All @@ -103,6 +109,7 @@ paths:
/dev/projects/{projectKey}/overrides/{flagKey}:
put:
summary: override flag value with value provided in the body
operationId: putOverrideFlag
parameters:
- $ref: "#/components/parameters/projectKey"
- $ref: "#/components/parameters/flagKey"
Expand All @@ -121,6 +128,7 @@ paths:

delete:
summary: remove override for flag
operationId: deleteFlagOverride
parameters:
- $ref: "#/components/parameters/projectKey"
- $ref: "#/components/parameters/flagKey"
Expand All @@ -131,7 +139,7 @@ paths:
description: no matching override found
/dev/projects/{projectKey}/environments:
get:
operationId: getProjectsEnvironments
operationId: getEnvironments
summary: list all environments for the given project
parameters:
- $ref: "#/components/parameters/projectKey"
Expand Down
21 changes: 21 additions & 0 deletions internal/dev_server/api/delete_flag_override.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package api

import (
"context"

"github.com/pkg/errors"

"github.com/launchdarkly/ldcli/internal/dev_server/model"
)

func (s server) DeleteFlagOverride(ctx context.Context, request DeleteFlagOverrideRequestObject) (DeleteFlagOverrideResponseObject, error) {
store := model.StoreFromContext(ctx)
err := store.DeactivateOverride(ctx, request.ProjectKey, request.FlagKey)
if err != nil {
if errors.Is(err, model.ErrNotFound) {
return DeleteFlagOverride404Response{}, nil
}
return nil, err
}
return DeleteFlagOverride204Response{}, nil
}
22 changes: 22 additions & 0 deletions internal/dev_server/api/delete_project.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package api

import (
"context"

"github.com/launchdarkly/ldcli/internal/dev_server/model"
)

func (s server) DeleteProject(ctx context.Context, request DeleteProjectRequestObject) (DeleteProjectResponseObject, error) {
store := model.StoreFromContext(ctx)
deleted, err := store.DeleteDevProject(ctx, request.ProjectKey)
if err != nil {
return nil, err
}
if !deleted {
return DeleteProject404JSONResponse{ErrorResponseJSONResponse{
Code: "not_found",
Message: "project not found",
}}, nil
}
return DeleteProject204Response{}, nil
}
33 changes: 33 additions & 0 deletions internal/dev_server/api/get_environments.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package api

import (
"context"

"github.com/launchdarkly/ldcli/internal/dev_server/model"
)

func (s server) GetEnvironments(ctx context.Context, request GetEnvironmentsRequestObject) (GetEnvironmentsResponseObject, error) {
store := model.StoreFromContext(ctx)
project, err := store.GetDevProject(ctx, request.ProjectKey)
if err != nil {
return nil, err
}
if project == nil {
return GetEnvironments404JSONResponse{}, nil
}

environments, err := model.GetEnvironmentsForProject(ctx, project.Key)
if err != nil {
return nil, err
}

var envReps []Environment
for _, env := range environments {
envReps = append(envReps, Environment{
Key: env.Key,
Name: env.Name,
})
}

return GetEnvironments200JSONResponse(envReps), nil
}
60 changes: 60 additions & 0 deletions internal/dev_server/api/get_project.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package api

import (
"context"

"github.com/launchdarkly/ldcli/internal/dev_server/model"
)

func (s server) GetProject(ctx context.Context, request GetProjectRequestObject) (GetProjectResponseObject, error) {
store := model.StoreFromContext(ctx)
project, err := store.GetDevProject(ctx, request.ProjectKey)
if err != nil {
return nil, err
}
if project == nil {
return GetProject404Response{}, nil
}

response := ProjectJSONResponse{
LastSyncedFromSource: project.LastSyncTime.Unix(),
Context: project.Context,
SourceEnvironmentKey: project.SourceEnvironmentKey,
FlagsState: &project.AllFlagsState,
}

if request.Params.Expand != nil {
for _, item := range *request.Params.Expand {
if item == "overrides" {
overrides, err := store.GetOverridesForProject(ctx, request.ProjectKey)
if err != nil {
return nil, err
}
respOverrides := make(model.FlagsState)
for _, override := range overrides {
if !override.Active {
continue
}
respOverrides[override.FlagKey] = model.FlagState{
Value: override.Value,
Version: override.Version,
}
}
response.Overrides = &respOverrides
}
if item == "availableVariations" {
availableVariations, err := store.GetAvailableVariationsForProject(ctx, request.ProjectKey)
if err != nil {
return nil, err
}
respAvailableVariations := availableVariationsToResponseFormat(availableVariations)
response.AvailableVariations = &respAvailableVariations
}
}

}

return GetProject200JSONResponse{
response,
}, nil
}
19 changes: 19 additions & 0 deletions internal/dev_server/api/get_projects.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package api

import (
"context"

"github.com/launchdarkly/ldcli/internal/dev_server/model"
)

func (s server) GetProjects(ctx context.Context, _ GetProjectsRequestObject) (GetProjectsResponseObject, error) {
store := model.StoreFromContext(ctx)
projectKeys, err := store.GetDevProjectKeys(ctx)
if err != nil {
return nil, err
}
if projectKeys == nil {
projectKeys = make([]string, 0) // HACK to make the json behavior compatible with go.
}
return GetProjects200JSONResponse(projectKeys), nil
}
60 changes: 60 additions & 0 deletions internal/dev_server/api/patch_project.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package api

import (
"context"

"github.com/launchdarkly/ldcli/internal/dev_server/model"
)

func (s server) PatchProject(ctx context.Context, request PatchProjectRequestObject) (PatchProjectResponseObject, error) {
store := model.StoreFromContext(ctx)
project, err := model.UpdateProject(ctx, request.ProjectKey, request.Body.Context, request.Body.SourceEnvironmentKey)
if err != nil {
return nil, err
}
if project.Key == "" && project.SourceEnvironmentKey == "" {
return PatchProject404Response{}, nil
}

response := ProjectJSONResponse{
LastSyncedFromSource: project.LastSyncTime.Unix(),
Context: project.Context,
SourceEnvironmentKey: project.SourceEnvironmentKey,
FlagsState: &project.AllFlagsState,
}

if request.Params.Expand != nil {
for _, item := range *request.Params.Expand {
if item == "overrides" {
overrides, err := store.GetOverridesForProject(ctx, request.ProjectKey)
if err != nil {
return nil, err
}
respOverrides := make(model.FlagsState)
for _, override := range overrides {
if !override.Active {
continue
}
respOverrides[override.FlagKey] = model.FlagState{
Value: override.Value,
Version: override.Version,
}
}
response.Overrides = &respOverrides
}
if item == "availableVariations" {
availableVariations, err := store.GetAvailableVariationsForProject(ctx, request.ProjectKey)
if err != nil {
return nil, err
}
respAvailableVariations := availableVariationsToResponseFormat(availableVariations)
response.AvailableVariations = &respAvailableVariations
}
}

}

return PatchProject200JSONResponse{
response,
}, nil
}
60 changes: 60 additions & 0 deletions internal/dev_server/api/patch_sync_project.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package api

import (
"context"

"github.com/launchdarkly/ldcli/internal/dev_server/model"
)

func (s server) PatchSyncProject(ctx context.Context, request PatchSyncProjectRequestObject) (PatchSyncProjectResponseObject, error) {
store := model.StoreFromContext(ctx)
project, err := model.UpdateProject(ctx, request.ProjectKey, nil, nil)
if err != nil {
return nil, err
}
if project.Key == "" && project.SourceEnvironmentKey == "" {
return PatchSyncProject404Response{}, nil
}

response := ProjectJSONResponse{
LastSyncedFromSource: project.LastSyncTime.Unix(),
Context: project.Context,
SourceEnvironmentKey: project.SourceEnvironmentKey,
FlagsState: &project.AllFlagsState,
}

if request.Params.Expand != nil {
for _, item := range *request.Params.Expand {
if item == "overrides" {
overrides, err := store.GetOverridesForProject(ctx, request.ProjectKey)
if err != nil {
return nil, err
}
respOverrides := make(model.FlagsState)
for _, override := range overrides {
if !override.Active {
continue
}
respOverrides[override.FlagKey] = model.FlagState{
Value: override.Value,
Version: override.Version,
}
}
response.Overrides = &respOverrides
}
if item == "availableVariations" {
availableVariations, err := store.GetAvailableVariationsForProject(ctx, request.ProjectKey)
if err != nil {
return nil, err
}
respAvailableVariations := availableVariationsToResponseFormat(availableVariations)
response.AvailableVariations = &respAvailableVariations
}
}

}

return PatchSyncProject200JSONResponse{
response,
}, nil
}
Loading

0 comments on commit 15cbf77

Please sign in to comment.