generated from TBD54566975/tbd-project-template
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
serverside no longer uses modulecontext directly
- Loading branch information
Showing
5 changed files
with
115 additions
and
252 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
package controller | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"os" | ||
"strings" | ||
|
||
"connectrpc.com/connect" | ||
ftlv1 "github.com/TBD54566975/ftl/backend/protos/xyz/block/ftl/v1" | ||
"github.com/TBD54566975/ftl/backend/schema" | ||
"github.com/TBD54566975/ftl/common/configuration" | ||
cf "github.com/TBD54566975/ftl/common/configuration" | ||
"github.com/TBD54566975/ftl/internal/slices" | ||
) | ||
|
||
func moduleContextToProto(ctx context.Context, name string, schemas []*schema.Module) (*connect.Response[ftlv1.ModuleContextResponse], error) { | ||
schemas = slices.Filter(schemas, func(s *schema.Module) bool { | ||
return s.Name == name | ||
}) | ||
if len(schemas) == 0 { | ||
return nil, connect.NewError(connect.CodeNotFound, fmt.Errorf("no schema found for module %q", name)) | ||
} else if len(schemas) > 1 { | ||
return nil, connect.NewError(connect.CodeInternal, fmt.Errorf("multiple schemas found for module %q", name)) | ||
} | ||
|
||
// configs | ||
configManager := configuration.ConfigFromContext(ctx) | ||
configList, err := configManager.List(ctx) | ||
if err != nil { | ||
return nil, err | ||
} | ||
configProtos := []*ftlv1.ModuleContextResponse_Config{} | ||
for _, entry := range configList { | ||
data, err := configManager.GetData(ctx, entry.Ref) | ||
if err != nil { | ||
return nil, err | ||
} | ||
configProtos = append(configProtos, &ftlv1.ModuleContextResponse_Config{ | ||
Ref: configRefToProto(entry.Ref), | ||
Data: data, | ||
}) | ||
} | ||
|
||
// secrets | ||
secretsManager := configuration.SecretsFromContext(ctx) | ||
secretsList, err := secretsManager.List(ctx) | ||
if err != nil { | ||
return nil, err | ||
} | ||
secretProtos := []*ftlv1.ModuleContextResponse_Secret{} | ||
for _, entry := range secretsList { | ||
data, err := secretsManager.GetData(ctx, entry.Ref) | ||
if err != nil { | ||
return nil, err | ||
} | ||
secretProtos = append(secretProtos, &ftlv1.ModuleContextResponse_Secret{ | ||
Ref: configRefToProto(entry.Ref), | ||
Data: data, | ||
}) | ||
} | ||
|
||
// DSNs | ||
dsnProtos := []*ftlv1.ModuleContextResponse_DSN{} | ||
for _, decl := range schemas[0].Decls { | ||
dbDecl, ok := decl.(*schema.Database) | ||
if !ok { | ||
continue | ||
} | ||
key := fmt.Sprintf("FTL_POSTGRES_DSN_%s_%s", strings.ToUpper(name), strings.ToUpper(dbDecl.Name)) | ||
dsn, ok := os.LookupEnv(key) | ||
if !ok { | ||
return nil, fmt.Errorf("missing environment variable %q", key) | ||
} | ||
dsnProtos = append(dsnProtos, &ftlv1.ModuleContextResponse_DSN{ | ||
Name: dbDecl.Name, | ||
Type: ftlv1.ModuleContextResponse_POSTGRES, | ||
Dsn: dsn, | ||
}) | ||
} | ||
|
||
return connect.NewResponse(&ftlv1.ModuleContextResponse{ | ||
Configs: configProtos, | ||
Secrets: secretProtos, | ||
Databases: dsnProtos, | ||
}), nil | ||
} | ||
|
||
func configRefToProto(r cf.Ref) *ftlv1.ModuleContextResponse_Ref { | ||
protoRef := &ftlv1.ModuleContextResponse_Ref{ | ||
Name: r.Name, | ||
} | ||
if module, ok := r.Module.Get(); ok { | ||
protoRef.Module = &module | ||
} | ||
return protoRef | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,48 +1 @@ | ||
package modulecontext | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
cf "github.com/TBD54566975/ftl/common/configuration" | ||
"github.com/TBD54566975/ftl/internal/log" | ||
"github.com/alecthomas/assert/v2" | ||
"github.com/alecthomas/types/optional" | ||
) | ||
|
||
type jsonableStruct struct { | ||
Str string `json:"string"` | ||
TestStruct *jsonableStruct `json:"struct,omitempty"` | ||
} | ||
|
||
func TestConfigManagerInAndOut(t *testing.T) { | ||
ctx := log.ContextWithNewDefaultLogger(context.Background()) | ||
|
||
cm, err := newInMemoryConfigManager[cf.Configuration](ctx) | ||
assert.NoError(t, err) | ||
|
||
moduleName := "test" | ||
strValue := "HelloWorld" | ||
intValue := 42 | ||
structValue := jsonableStruct{Str: "HelloWorld", TestStruct: &jsonableStruct{Str: "HelloWorld"}} | ||
cm.Set(ctx, cf.Ref{Module: optional.Some(moduleName), Name: "str"}, strValue) | ||
cm.Set(ctx, cf.Ref{Module: optional.Some(moduleName), Name: "int"}, intValue) | ||
cm.Set(ctx, cf.Ref{Module: optional.Some(moduleName), Name: "struct"}, structValue) | ||
|
||
builder := NewBuilder(moduleName).AddConfigFromManager(cm) | ||
|
||
moduleCtx, err := builder.Build(ctx) | ||
assert.NoError(t, err) | ||
|
||
var outStr string | ||
moduleCtx.configManager.Get(ctx, cf.Ref{Module: optional.Some(moduleName), Name: "str"}, &outStr) | ||
assert.Equal(t, strValue, outStr, "expected string value to be set and retrieved correctly") | ||
|
||
var outInt int | ||
moduleCtx.configManager.Get(ctx, cf.Ref{Module: optional.Some(moduleName), Name: "int"}, &outInt) | ||
assert.Equal(t, intValue, outInt, "expected int value to be set and retrieved correctly") | ||
|
||
var outStruct jsonableStruct | ||
moduleCtx.configManager.Get(ctx, cf.Ref{Module: optional.Some(moduleName), Name: "struct"}, &outStruct) | ||
assert.Equal(t, structValue, outStruct, "expected struct value to be set and retrieved correctly") | ||
} |
Oops, something went wrong.