Skip to content

Commit

Permalink
feat: add functions_config parameter to deploy
Browse files Browse the repository at this point in the history
  • Loading branch information
khendrikse committed Jan 30, 2023
1 parent a025a78 commit 92c921d
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 20 deletions.
44 changes: 29 additions & 15 deletions go/porcelain/deploy.go
Expand Up @@ -95,6 +95,7 @@ type DeployOptions struct {
files *deployFiles
functions *deployFiles
functionSchedules []*models.FunctionSchedule
functionsConfig map[string]models.FunctionConfig
}

type uploadError struct {
Expand Down Expand Up @@ -225,7 +226,7 @@ func (n *Netlify) DoDeploy(ctx context.Context, options *DeployOptions, deploy *

options.files = files

functions, schedules, err := bundle(ctx, options.FunctionsDir, options.Observer)
functions, schedules, functionsConfig, err := bundle(ctx, options.FunctionsDir, options.Observer)
if err != nil {
if options.Observer != nil {
options.Observer.OnFailedWalk()
Expand All @@ -234,6 +235,7 @@ func (n *Netlify) DoDeploy(ctx context.Context, options *DeployOptions, deploy *
}
options.functions = functions
options.functionSchedules = schedules
options.functionsConfig = functionsConfig

deployFiles := &models.DeployFiles{
Files: options.files.Sums,
Expand All @@ -255,11 +257,16 @@ func (n *Netlify) DoDeploy(ctx context.Context, options *DeployOptions, deploy *
deployFiles.FunctionSchedules = schedules
}

if options.functionsConfig != nil {
deployFiles.FunctionsConfig = options.functionsConfig
}

l := context.GetLogger(ctx)
l.WithFields(logrus.Fields{
"site_id": options.SiteID,
"deploy_files": len(options.files.Sums),
"scheduled_functions": len(schedules),
"functions_config": functionsConfig,
}).Debug("Starting to deploy files")
authInfo := context.GetAuthInfo(ctx)

Expand Down Expand Up @@ -649,9 +656,9 @@ func addEdgeFunctionsToDeployFiles(dir string, files *deployFiles, observer Depl
})
}

func bundle(ctx context.Context, functionDir string, observer DeployObserver) (*deployFiles, []*models.FunctionSchedule, error) {
func bundle(ctx context.Context, functionDir string, observer DeployObserver) (*deployFiles, []*models.FunctionSchedule, map[string]models.FunctionConfig, error) {
if functionDir == "" {
return nil, nil, nil
return nil, nil, nil, nil
}

manifestFile, err := os.Open(filepath.Join(functionDir, "manifest.json"))
Expand All @@ -668,7 +675,7 @@ func bundle(ctx context.Context, functionDir string, observer DeployObserver) (*

info, err := ioutil.ReadDir(functionDir)
if err != nil {
return nil, nil, err
return nil, nil, nil, err
}

for _, i := range info {
Expand All @@ -678,23 +685,23 @@ func bundle(ctx context.Context, functionDir string, observer DeployObserver) (*
case zipFile(i):
runtime, err := readZipRuntime(filePath)
if err != nil {
return nil, nil, err
return nil, nil, nil, err
}
file, err := newFunctionFile(filePath, i, runtime, observer)
if err != nil {
return nil, nil, err
return nil, nil, nil, err
}
functions.Add(file.Name, file)
case jsFile(i):
file, err := newFunctionFile(filePath, i, jsRuntime, observer)
if err != nil {
return nil, nil, err
return nil, nil, nil, err
}
functions.Add(file.Name, file)
case goFile(filePath, i, observer):
file, err := newFunctionFile(filePath, i, goRuntime, observer)
if err != nil {
return nil, nil, err
return nil, nil, nil, err
}
functions.Add(file.Name, file)
default:
Expand All @@ -704,14 +711,14 @@ func bundle(ctx context.Context, functionDir string, observer DeployObserver) (*
}
}

return functions, nil, nil
return functions, nil, nil, nil
}

func bundleFromManifest(ctx context.Context, manifestFile *os.File, observer DeployObserver) (*deployFiles, []*models.FunctionSchedule, error) {
func bundleFromManifest(ctx context.Context, manifestFile *os.File, observer DeployObserver) (*deployFiles, []*models.FunctionSchedule, map[string]models.FunctionConfig, error) {
manifestBytes, err := ioutil.ReadAll(manifestFile)

if err != nil {
return nil, nil, err
return nil, nil, nil, err
}

logger := context.GetLogger(ctx)
Expand All @@ -722,23 +729,24 @@ func bundleFromManifest(ctx context.Context, manifestFile *os.File, observer Dep
err = json.Unmarshal(manifestBytes, &manifest)

if err != nil {
return nil, nil, fmt.Errorf("malformed functions manifest file: %w", err)
return nil, nil, nil, fmt.Errorf("malformed functions manifest file: %w", err)
}

schedules := make([]*models.FunctionSchedule, 0, len(manifest.Functions))
functions := newDeployFiles()
functionsConfig := make(map[string]models.FunctionConfig)

for _, function := range manifest.Functions {
fileInfo, err := os.Stat(function.Path)

if err != nil {
return nil, nil, fmt.Errorf("manifest file specifies a function path that cannot be found: %s", function.Path)
return nil, nil, nil, fmt.Errorf("manifest file specifies a function path that cannot be found: %s", function.Path)
}

file, err := newFunctionFile(function.Path, fileInfo, function.Runtime, observer)

if err != nil {
return nil, nil, err
return nil, nil, nil, err
}

if function.Schedule != "" {
Expand All @@ -748,10 +756,16 @@ func bundleFromManifest(ctx context.Context, manifestFile *os.File, observer Dep
})
}

if function.DisplayName != "" {
functionsConfig[file.Name] = models.FunctionConfig{
DisplayName: function.DisplayName,
}
}

functions.Add(file.Name, file)
}

return functions, schedules, nil
return functions, schedules, functionsConfig, nil
}

func readZipRuntime(filePath string) (string, error) {
Expand Down
14 changes: 9 additions & 5 deletions go/porcelain/deploy_test.go
Expand Up @@ -306,7 +306,7 @@ func TestUploadFunctions_RetryCountHeader(t *testing.T) {
defer os.RemoveAll(dir)
require.NoError(t, ioutil.WriteFile(filepath.Join(functionsPath, "foo.js"), []byte("module.exports = () => {}"), 0644))

files, _, err := bundle(ctx, functionsPath, mockObserver{})
files, _, _, err := bundle(ctx, functionsPath, mockObserver{})
require.NoError(t, err)
d := &models.Deploy{}
for _, bundle := range files.Files {
Expand All @@ -317,11 +317,12 @@ func TestUploadFunctions_RetryCountHeader(t *testing.T) {
}

func TestBundle(t *testing.T) {
functions, schedules, err := bundle(gocontext.Background(), "../internal/data", mockObserver{})
functions, schedules, functionsConfig, err := bundle(gocontext.Background(), "../internal/data", mockObserver{})

assert.Nil(t, err)
assert.Equal(t, 3, len(functions.Files))
assert.Empty(t, schedules)
assert.Nil(t, functionsConfig)

jsFunction := functions.Files["hello-js-function-test"]
pyFunction := functions.Files["hello-py-function-test"]
Expand All @@ -344,6 +345,7 @@ func TestBundleWithManifest(t *testing.T) {
"path": "%s",
"runtime": "a-runtime",
"mainFile": "/some/path/hello-js-function-test.js",
"displayName": "Hello Javascript Function",
"name": "hello-js-function-test",
"schedule": "* * * * *"
},
Expand All @@ -352,7 +354,7 @@ func TestBundleWithManifest(t *testing.T) {
"runtime": "some-other-runtime",
"mainFile": "/some/path/hello-py-function-test",
"name": "hello-py-function-test"
}
}
],
"version": 1
}`, jsFunctionPath, pyFunctionPath)
Expand All @@ -361,8 +363,7 @@ func TestBundleWithManifest(t *testing.T) {
defer os.Remove(manifestPath)
assert.Nil(t, err)

functions, schedules, err := bundle(gocontext.Background(), "../internal/data", mockObserver{})

functions, schedules, functionsConfig, err := bundle(gocontext.Background(), "../internal/data", mockObserver{})
assert.Nil(t, err)

assert.Equal(t, 1, len(schedules))
Expand All @@ -372,6 +373,9 @@ func TestBundleWithManifest(t *testing.T) {
assert.Equal(t, 2, len(functions.Files))
assert.Equal(t, "a-runtime", functions.Files["hello-js-function-test"].Runtime)
assert.Equal(t, "some-other-runtime", functions.Files["hello-py-function-test"].Runtime)

assert.Equal(t, 1, len(functionsConfig))
assert.Equal(t, "Hello Javascript Function", functionsConfig["hello-js-function-test"].DisplayName)
}

func TestReadZipRuntime(t *testing.T) {
Expand Down
1 change: 1 addition & 0 deletions go/porcelain/functions_manifest.go
Expand Up @@ -12,4 +12,5 @@ type functionsManifestEntry struct {
Path string `json:"path"`
Runtime string `json:"runtime"`
Schedule string `json:"schedule"`
DisplayName string `json:"displayName"`
}

0 comments on commit 92c921d

Please sign in to comment.