Skip to content

Commit

Permalink
WIP: PR comments addressed, still unable to set config
Browse files Browse the repository at this point in the history
  • Loading branch information
safeer committed May 29, 2024
1 parent b5f53bd commit 678853f
Show file tree
Hide file tree
Showing 9 changed files with 90 additions and 105 deletions.
54 changes: 19 additions & 35 deletions backend/controller/admin.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package controller

import (
"bytes"
"context"
"encoding/json"
"fmt"
Expand All @@ -11,7 +10,6 @@ import (
ftlv1 "github.com/TBD54566975/ftl/backend/protos/xyz/block/ftl/v1"
"github.com/TBD54566975/ftl/backend/protos/xyz/block/ftl/v1/ftlv1connect"
cf "github.com/TBD54566975/ftl/common/configuration"
"github.com/TBD54566975/ftl/internal/log"
)

type AdminService struct {
Expand All @@ -32,7 +30,7 @@ func (s *AdminService) Ping(ctx context.Context, req *connect.Request[ftlv1.Ping
return connect.NewResponse(&ftlv1.PingResponse{}), nil
}

// List configuration.
// ConfigList returns the list of configuration values, optionally filtered by module.
func (s *AdminService) ConfigList(ctx context.Context, req *connect.Request[ftlv1.ListConfigRequest]) (*connect.Response[ftlv1.ListConfigResponse], error) {
listing, err := s.cm.List(ctx)
if err != nil {
Expand All @@ -51,77 +49,63 @@ func (s *AdminService) ConfigList(ctx context.Context, req *connect.Request[ftlv
ref = fmt.Sprintf("%s.%s", module, config.Name)
}

cv := ""
var cv []byte
if *req.Msg.IncludeValues {
var value any
err := s.cm.Get(ctx, config.Ref, &value)
if err != nil {
return nil, connect.NewError(connect.CodeInternal, fmt.Errorf("failed to get value: %w", err))
} else {

Check warning on line 58 in backend/controller/admin.go

View workflow job for this annotation

GitHub Actions / Lint

indent-error-flow: if block ends with a return statement, so drop this else and outdent its block (revive)
data, _ := json.Marshal(value)
cv = string(data)
cv, _ = json.Marshal(value)
}
}

configs = append(configs, &ftlv1.ListConfigResponse_Config{
RefPath: ref,
Value: &cv,
Value: cv,
})
}
return connect.NewResponse(&ftlv1.ListConfigResponse{Configs: configs}), nil
}

// Get a config value.
// ConfigGet returns the configuration value for a given ref string.
func (s *AdminService) ConfigGet(ctx context.Context, req *connect.Request[ftlv1.GetConfigRequest]) (*connect.Response[ftlv1.GetConfigResponse], error) {
var value any
err := s.cm.Get(ctx, cf.NewRef(*req.Msg.Ref.Module, req.Msg.Ref.Name), &value)
if err != nil {
return nil, connect.NewError(connect.CodeInternal, fmt.Errorf("failed to get value: %w", err))
}

buf := new(bytes.Buffer)
enc := json.NewEncoder(buf)
enc.SetIndent("", " ")
err = enc.Encode(value)
vb, err := json.MarshalIndent(value, "", " ")
if err != nil {
return nil, connect.NewError(connect.CodeInternal, fmt.Errorf("failed to encode value: %w", err))
}

return connect.NewResponse(&ftlv1.GetConfigResponse{Value: buf.String()}), nil
return connect.NewResponse(&ftlv1.GetConfigResponse{Value: vb}), nil
}

// Set a config value.
// ConfigSet sets the configuration at the given ref to the provided value.
func (s *AdminService) ConfigSet(ctx context.Context, req *connect.Request[ftlv1.SetConfigRequest]) (*connect.Response[ftlv1.SetConfigResponse], error) {
// cm := cf.ConfigFromContext(ctx) // TODO(saf): use cf.New to create a cm with the appropriate provider/writer

var err error

// var providerKey string
// switch *req.Msg.Provider {
// case ftlv1.ConfigProvider_CONFIG_ENVAR:
// providerKey = "envar"
// case ftlv1.ConfigProvider_CONFIG_INLINE:
// providerKey = "inline"
// }

logger := log.FromContext(ctx)
logger.Warnf("cm pre-mutable %+v", s.cm)
if err := s.cm.Mutable(); err != nil {
// TODO(saf): use req.Msg.Provider to update / create a manager with the correct provider
cm := s.cm

if err := cm.Mutable(); err != nil {
return nil, connect.NewError(connect.CodeInternal, err)
}
logger.Warnf("cm post-mutable %+v", s.cm)

err = s.cm.Set(ctx, cf.NewRef(*req.Msg.Ref.Module, req.Msg.Ref.Name), req.Msg.Value)
err = cm.Set(ctx, cf.NewRef(*req.Msg.Ref.Module, req.Msg.Ref.Name), req.Msg.Value)
if err != nil {
return nil, connect.NewError(connect.CodeInternal, fmt.Errorf("failed to set value: %w", err))
}

// TODO save the updated config
// TODO(saf) save the updated config

return connect.NewResponse(&ftlv1.SetConfigResponse{}), nil
}

// Unset a config value.
// ConfigUnset unsets the config value at the given ref.
func (s *AdminService) ConfigUnset(ctx context.Context, req *connect.Request[ftlv1.UnsetConfigRequest]) (*connect.Response[ftlv1.UnsetConfigResponse], error) {
err := s.cm.Unset(ctx, cf.NewRef(*req.Msg.Ref.Module, req.Msg.Ref.Name))
if err != nil {
Expand All @@ -130,22 +114,22 @@ func (s *AdminService) ConfigUnset(ctx context.Context, req *connect.Request[ftl
return connect.NewResponse(&ftlv1.UnsetConfigResponse{}), nil
}

// List secrets.
// SecretsList returns the list of secrets, optionally filtered by module.
func (s *AdminService) SecretsList(ctx context.Context, req *connect.Request[ftlv1.ListSecretsRequest]) (*connect.Response[ftlv1.ListSecretsResponse], error) {
return connect.NewResponse(&ftlv1.ListSecretsResponse{}), nil
}

// Get a secret.
// SecretGet returns the secret value for a given ref string.
func (s *AdminService) SecretGet(ctx context.Context, req *connect.Request[ftlv1.GetSecretRequest]) (*connect.Response[ftlv1.GetSecretResponse], error) {
return connect.NewResponse(&ftlv1.GetSecretResponse{}), nil
}

// Set a secret.
// SecretSet sets the secret at the given ref to the provided value.
func (s *AdminService) SecretSet(ctx context.Context, req *connect.Request[ftlv1.SetSecretRequest]) (*connect.Response[ftlv1.SetSecretResponse], error) {
return connect.NewResponse(&ftlv1.SetSecretResponse{}), nil
}

// Unset a secret.
// SecretUnset unsets the secret value at the given ref.
func (s *AdminService) SecretUnset(ctx context.Context, req *connect.Request[ftlv1.UnsetSecretRequest]) (*connect.Response[ftlv1.UnsetSecretResponse], error) {
return connect.NewResponse(&ftlv1.UnsetSecretResponse{}), nil
}
3 changes: 0 additions & 3 deletions backend/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,6 @@ func Start(ctx context.Context, config Config, runnerScaling scaling.RunnerScali
cm := cf.ConfigFromContext(ctx)
sm := cf.SecretsFromContext(ctx)

logger.Warnf("config manager: %+v", cm)
logger.Warnf("secrets manager: %+v", sm)

admin := NewAdminService(cm, sm)
console := NewConsoleService(dal)

Expand Down
60 changes: 30 additions & 30 deletions backend/protos/xyz/block/ftl/v1/ftl.pb.go

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

Loading

0 comments on commit 678853f

Please sign in to comment.