Skip to content

Commit

Permalink
fix: Context updating, environment rate limiting, toggle styling (#437)
Browse files Browse the repository at this point in the history
* Fix context updating, fix environment rate limiting, fix toggle

* Fix limit not being respected

* Remove unnecessary filterEnvironments
  • Loading branch information
cdelst authored Oct 1, 2024
1 parent facf237 commit c7832d5
Show file tree
Hide file tree
Showing 15 changed files with 257 additions and 94 deletions.
3 changes: 1 addition & 2 deletions cmd/dev_server/dev_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@ import (
"github.com/spf13/viper"

cmdAnalytics "github.com/launchdarkly/ldcli/cmd/analytics"
"github.com/launchdarkly/ldcli/internal/analytics"

"github.com/launchdarkly/ldcli/cmd/cliflags"
resourcecmd "github.com/launchdarkly/ldcli/cmd/resources"
"github.com/launchdarkly/ldcli/internal/analytics"
"github.com/launchdarkly/ldcli/internal/dev_server"
"github.com/launchdarkly/ldcli/internal/resources"
)
Expand Down
40 changes: 24 additions & 16 deletions internal/dev_server/adapters/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package adapters

import (
"context"
"fmt"
"log"
"net/url"
"strconv"
Expand All @@ -25,7 +26,7 @@ func GetApi(ctx context.Context) Api {
type Api interface {
GetSdkKey(ctx context.Context, projectKey, environmentKey string) (string, error)
GetAllFlags(ctx context.Context, projectKey string) ([]ldapi.FeatureFlag, error)
GetProjectEnvironments(ctx context.Context, projectKey string) ([]ldapi.Environment, error)
GetProjectEnvironments(ctx context.Context, projectKey string, query string, limit *int) ([]ldapi.Environment, error)
}

type apiClientApi struct {
Expand Down Expand Up @@ -54,9 +55,9 @@ func (a apiClientApi) GetAllFlags(ctx context.Context, projectKey string) ([]lda
return flags, err
}

func (a apiClientApi) GetProjectEnvironments(ctx context.Context, projectKey string) ([]ldapi.Environment, error) {
func (a apiClientApi) GetProjectEnvironments(ctx context.Context, projectKey string, query string, limit *int) ([]ldapi.Environment, error) {
log.Printf("Fetching all environments for project '%s'", projectKey)
environments, err := a.getEnvironments(ctx, projectKey, nil)
environments, err := a.getEnvironments(ctx, projectKey, nil, query, limit)
if err != nil {
err = errors.Wrap(err, "unable to get environments from LD API")
}
Expand All @@ -81,21 +82,28 @@ func (a apiClientApi) getFlags(ctx context.Context, projectKey string, href *str
})
}

func (a apiClientApi) getEnvironments(ctx context.Context, projectKey string, href *string) ([]ldapi.Environment, error) {
return getPaginatedItems(ctx, projectKey, href, func(ctx context.Context, projectKey string, limit, offset *int64) (*ldapi.Environments, error) {
request := a.apiClient.EnvironmentsApi.GetEnvironmentsByProject(ctx, projectKey)
if limit != nil {
request = request.Limit(*limit)
}
func (a apiClientApi) getEnvironments(ctx context.Context, projectKey string, href *string, query string, limit *int) ([]ldapi.Environment, error) {
request := a.apiClient.EnvironmentsApi.GetEnvironmentsByProject(ctx, projectKey)

if offset != nil {
request = request.Offset(*offset)
}
if limit != nil {
request = request.Limit(int64(*limit))
}

envs, _, err := request.
Execute()
return envs, err
})
if query != "" {
request = request.Sort("name").Filter(fmt.Sprintf("query:%s", query))
}

envs, _, err := request.
Execute()
if err != nil {
return nil, err
}

if envs == nil {
return []ldapi.Environment{}, nil
}

return envs.Items, nil
}

func getPaginatedItems[T any, R interface {
Expand Down
8 changes: 4 additions & 4 deletions internal/dev_server/adapters/mocks/api.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions internal/dev_server/api/api.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,18 @@ paths:
summary: list all environments for the given project
parameters:
- $ref: "#/components/parameters/projectKey"
- name: name
in: query
description: filter by environment name
required: false
schema:
type: string
- name: limit
in: query
description: limit the number of environments returned
required: false
schema:
type: integer
responses:
200:
description: OK. List of environments
Expand Down
7 changes: 6 additions & 1 deletion internal/dev_server/api/get_environments.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,12 @@ func (s server) GetEnvironments(ctx context.Context, request GetEnvironmentsRequ
return GetEnvironments404JSONResponse{}, nil
}

environments, err := model.GetEnvironmentsForProject(ctx, project.Key)
var query string
if request.Params.Name != nil {
query = *request.Params.Name
}

environments, err := model.GetEnvironmentsForProject(ctx, project.Key, query, request.Params.Limit)
if err != nil {
return nil, err
}
Expand Down
36 changes: 33 additions & 3 deletions internal/dev_server/api/server.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions internal/dev_server/model/environments.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ type Environment struct {
Name string
}

func GetEnvironmentsForProject(ctx context.Context, projectKey string) ([]Environment, error) {
func GetEnvironmentsForProject(ctx context.Context, projectKey string, query string, limit *int) ([]Environment, error) {
apiAdapter := adapters.GetApi(ctx)
environments, err := apiAdapter.GetProjectEnvironments(ctx, projectKey)
environments, err := apiAdapter.GetProjectEnvironments(ctx, projectKey, query, limit)
if err != nil {
return nil, err
}
Expand Down
9 changes: 5 additions & 4 deletions internal/dev_server/model/project_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,18 @@ import (
"errors"
"testing"

"github.com/samber/lo"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.uber.org/mock/gomock"

ldapi "github.com/launchdarkly/api-client-go/v14"
"github.com/launchdarkly/go-sdk-common/v3/ldcontext"
"github.com/launchdarkly/go-sdk-common/v3/ldvalue"
"github.com/launchdarkly/go-server-sdk/v7/interfaces/flagstate"
adapters_mocks "github.com/launchdarkly/ldcli/internal/dev_server/adapters/mocks"
"github.com/launchdarkly/ldcli/internal/dev_server/model"
"github.com/launchdarkly/ldcli/internal/dev_server/model/mocks"
"github.com/samber/lo"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.uber.org/mock/gomock"
)

func TestCreateProject(t *testing.T) {
Expand Down
7 changes: 4 additions & 3 deletions internal/dev_server/sdk/go_sdk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ import (
"time"

"github.com/gorilla/mux"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.uber.org/mock/gomock"

"github.com/launchdarkly/go-sdk-common/v3/ldcontext"
"github.com/launchdarkly/go-sdk-common/v3/ldvalue"
ldclient "github.com/launchdarkly/go-server-sdk/v7"
Expand All @@ -17,9 +21,6 @@ import (
"github.com/launchdarkly/ldcli/internal/dev_server/adapters/mocks"
"github.com/launchdarkly/ldcli/internal/dev_server/db"
"github.com/launchdarkly/ldcli/internal/dev_server/model"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.uber.org/mock/gomock"
)

// TestSdkRoutesViaGoSDK is an integration test. It hooks up a real go SDK to our SDK routes and makes changes to the
Expand Down
Loading

0 comments on commit c7832d5

Please sign in to comment.