You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@openwhisk.apache.org by GitBox <gi...@apache.org> on 2018/02/08 20:25:00 UTC

[GitHub] pritidesai commented on a change in pull request #715: Adding support for export managed project back to manifest

pritidesai commented on a change in pull request #715: Adding support for export managed project back to manifest
URL: https://github.com/apache/incubator-openwhisk-wskdeploy/pull/715#discussion_r167052402
 
 

 ##########
 File path: cmd/export.go
 ##########
 @@ -0,0 +1,266 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package cmd
+
+import (
+	"errors"
+	"fmt"
+	"os"
+	"path/filepath"
+	"strings"
+
+	"github.com/apache/incubator-openwhisk-client-go/whisk"
+	"github.com/apache/incubator-openwhisk-wskdeploy/deployers"
+	"github.com/apache/incubator-openwhisk-wskdeploy/parsers"
+	"github.com/apache/incubator-openwhisk-wskdeploy/utils"
+	"github.com/apache/incubator-openwhisk-wskdeploy/wskderrors"
+	"github.com/spf13/cobra"
+)
+
+var exportCmd = &cobra.Command{
+	Use:        "export",
+	SuggestFor: []string{"capture"},
+	Short:      "Export project assets from OpenWhisk",
+	Long:       `Exports managed project assets from OpenWhisk to manifest and function files`,
+	RunE:       ExportCmdImp,
+}
+
+var config *whisk.Config
+
+func ExportAction(actionName string, packageName string, maniyaml *parsers.YAML) error {
+
+	pkg := maniyaml.Packages[packageName]
+	if pkg.Actions == nil {
+		pkg.Actions = make(map[string]parsers.Action)
+		maniyaml.Packages[packageName] = pkg
+	}
+
+	wskAction, _, err := client.Actions.Get(actionName)
+	if err != nil {
+		return err
+	}
+	if wskAction.Exec.Kind == "sequence" {
+		seq := new(parsers.Sequence)
+		for _, component := range wskAction.Exec.Components {
+			// must ommit namespace from seq component name
+			ExportAction(strings.SplitN(component, "/", 3)[2], packageName, maniyaml)
+			slices := strings.Split(component, "/")
+
+			// save in the seq list only action names
+			if len(seq.Actions) > 0 {
+				seq.Actions += ","
+			}
+
+			seq.Actions += slices[len(slices)-1]
+		}
+
+		pkg = maniyaml.Packages[packageName]
+		if pkg.Sequences == nil {
+			pkg.Sequences = make(map[string]parsers.Sequence)
+		}
+
+		pkg.Sequences[wskAction.Name] = *seq
+	} else {
+		parsedAction := *maniyaml.ComposeParsersAction(*wskAction)
+
+		// get the action file extension according to action kind (e.g. js for nodejs)
+		ext := utils.FileRuntimeExtensionsMap[wskAction.Exec.Kind]
+
+		manifestDir := filepath.Dir(utils.Flags.ManifestPath)
+
+		// store function file under action package name subdirectory in the specified manifest folder
+		functionDir := filepath.Join(manifestDir, packageName)
+		os.MkdirAll(functionDir, os.ModePerm)
+
+		// store function in manifest under path relative to manifest root
+		functionFile := filepath.Join(packageName, wskAction.Name) + "." + ext
+		parsedAction.Function = functionFile
+
+		// create function file at the full path
+		functionFile = filepath.Join(manifestDir, functionFile)
+		f, err := os.Create(functionFile)
+		if err != nil {
+			return wskderrors.NewFileReadError(functionFile, err.Error())
+		}
+
+		defer f.Close()
+
+		// store action function in the filesystem next to the manifest.yml
+		// TODO: consider to name files by namespace + action to make function file names uniqueue
+		f.Write([]byte(*wskAction.Exec.Code))
+		pkg.Actions[wskAction.Name] = parsedAction
+	}
+
+	maniyaml.Packages[packageName] = pkg
+	return nil
+}
+
+func ExportCmdImp(cmd *cobra.Command, args []string) error {
+
+	projectName := utils.Flags.ProjectPath
+	maniyaml := &parsers.YAML{}
+	maniyaml.Project.Name = projectName
+
+	config, _ = deployers.NewWhiskConfig(wskpropsPath, utils.Flags.DeploymentPath, utils.Flags.ManifestPath, false)
+	client, _ = deployers.CreateNewClient(config)
+
+	// Init supported runtimes and action files extensions maps
+	setSupportedRuntimes(config.Host)
+
+	// Get the list of packages in your namespace
+	packages, _, err := client.Packages.List(&whisk.PackageListOptions{})
+	if err != nil {
+		return err
+	}
+
+	// iterate over each package to find managed annotations
+	// check if "managed" annotation is attached to a package
+	// add to export when managed project name matches with the
+	// specified project name
+	for _, pkg := range packages {
+		if a := pkg.Annotations.GetValue(utils.MANAGED); a != nil {
+			// decode the JSON blob and retrieve __OW_PROJECT_NAME
+			pa := a.(map[string]interface{})
+
+			// we have found a package which is part of the current project
+			if pa[utils.OW_PROJECT_NAME] == projectName {
+
+				if maniyaml.Packages == nil {
+					maniyaml.Packages = make(map[string]parsers.Package)
+				}
+
+				maniyaml.Packages[pkg.Name] = *maniyaml.ComposeParsersPackage(pkg)
+				// TODO: throw if there more than single package managed by project
+				// currently will be a mess because triggers and rules managed under packages
+				// instead of the top level (similar to OW model)
+				if len(maniyaml.Packages) > 1 {
+					return errors.New("currently can't work with more than one package managed by one project")
 
 Review comment:
   we generally define errors under `wskderrors` with a translatable strings in `en_US.all.json` and `i18n_ids.go`, please open an issue if this PR is merged as is to change using `errors.new()` to `wskderrors`, we can take care of it.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services