mrutkows closed pull request #731: Adding rule annotations for managed project
URL: https://github.com/apache/incubator-openwhisk-wskdeploy/pull/731
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/Godeps/Godeps.json b/Godeps/Godeps.json
index 1758c5d9..d6613b32 100644
--- a/Godeps/Godeps.json
+++ b/Godeps/Godeps.json
@@ -105,11 +105,11 @@
                },
                {
                        "ImportPath": 
"github.com/apache/incubator-openwhisk-client-go/whisk",
-                       "Rev": "d7cab4297a25e2cc25492b85b64e7ec000da9ffb"
+                       "Rev": "a81a9be21faae877e858c6ab9122a40419577f95"
                },
                {
                        "ImportPath": 
"github.com/apache/incubator-openwhisk-client-go/wski18n",
-                       "Rev": "d7cab4297a25e2cc25492b85b64e7ec000da9ffb"
+                       "Rev": "a81a9be21faae877e858c6ab9122a40419577f95"
                },
                {
                        "ImportPath": "github.com/pelletier/go-buffruneio",
diff --git a/deployers/manifestreader.go b/deployers/manifestreader.go
index 0442abc4..6c433fb3 100644
--- a/deployers/manifestreader.go
+++ b/deployers/manifestreader.go
@@ -88,7 +88,7 @@ func (deployer *ManifestReader) HandleYaml(sdeployer 
*ServiceDeployer, manifestP
                return wskderrors.NewYAMLFileFormatError(manifestName, err)
        }
 
-       rules, err := manifestParser.ComposeRulesFromAllPackages(manifest)
+       rules, err := manifestParser.ComposeRulesFromAllPackages(manifest, ma)
        if err != nil {
                return wskderrors.NewYAMLFileFormatError(manifestName, err)
        }
diff --git a/deployers/servicedeployer.go b/deployers/servicedeployer.go
index 14fc1c8b..1bb2f5e4 100644
--- a/deployers/servicedeployer.go
+++ b/deployers/servicedeployer.go
@@ -603,9 +603,44 @@ func (deployer *ServiceDeployer) RefreshManagedTriggers(ma 
map[string]interface{
        return nil
 }
 
-// TODO() engage community to allow metadata (annotations) on Rules
 // TODO() display "update" | "synced" messages pre/post
 func (deployer *ServiceDeployer) RefreshManagedRules(ma 
map[string]interface{}) error {
+       options := whisk.RuleListOptions{}
+       // Get list of rules in your namespace
+       rules, _, err := deployer.Client.Rules.List(&options)
+       if err != nil {
+               return err
+       }
+       // iterate over the list of rules to determine whether any of them was 
part of managed project
+       // and now deleted from manifest file we can determine that from the 
managed annotation
+       // If a rule has attached managed annotation with the project name 
equals to the current project name
+       // but the project hash is different (project hash differs since the 
rule is deleted from the manifest file)
+       for _, rule := range rules {
+               // rule has attached managed annotation
+               if a := rule.Annotations.GetValue(utils.MANAGED); a != nil {
+                       // decode the JSON blob and retrieve __OW_PROJECT_NAME 
and __OW_PROJECT_HASH
+                       ta := a.(map[string]interface{})
+                       if ta[utils.OW_PROJECT_NAME] == 
ma[utils.OW_PROJECT_NAME] && ta[utils.OW_PROJECT_HASH] != 
ma[utils.OW_PROJECT_HASH] {
+                               // we have found a trigger which was earlier 
part of the current project
+                               output := 
wski18n.T(wski18n.ID_MSG_MANAGED_FOUND_DELETED_X_key_X_name_X_project_X,
+                                       map[string]interface{}{
+                                               wski18n.KEY_KEY:     
parsers.YAML_KEY_RULE,
+                                               wski18n.KEY_NAME:    rule.Name,
+                                               wski18n.KEY_PROJECT: 
ma[utils.OW_PROJECT_NAME]})
+                               wskprint.PrintOpenWhiskWarning(output)
+
+                               var err error
+                               err = retry(DEFAULT_ATTEMPTS, DEFAULT_INTERVAL, 
func() error {
+                                       _, err := 
deployer.Client.Rules.Delete(rule.Name)
+                                       return err
+                               })
+
+                               if err != nil {
+                                       return err
+                               }
+                       }
+               }
+       }
        return nil
 }
 
diff --git a/parsers/manifest_parser.go b/parsers/manifest_parser.go
index b8405b12..88a3b032 100644
--- a/parsers/manifest_parser.go
+++ b/parsers/manifest_parser.go
@@ -827,12 +827,12 @@ func (dm *YAMLParser) ComposeTriggers(filePath string, 
pkg Package, ma whisk.Key
        return t1, nil
 }
 
-func (dm *YAMLParser) ComposeRulesFromAllPackages(manifest *YAML) 
([]*whisk.Rule, error) {
+func (dm *YAMLParser) ComposeRulesFromAllPackages(manifest *YAML, ma 
whisk.KeyValue) ([]*whisk.Rule, error) {
        var rules []*whisk.Rule = make([]*whisk.Rule, 0)
        manifestPackages := make(map[string]Package)
 
        if manifest.Package.Packagename != "" {
-               return dm.ComposeRules(manifest.Package, 
manifest.Package.Packagename)
+               return dm.ComposeRules(manifest.Package, 
manifest.Package.Packagename, ma)
        } else {
                if len(manifest.Packages) != 0 {
                        manifestPackages = manifest.Packages
@@ -842,7 +842,7 @@ func (dm *YAMLParser) ComposeRulesFromAllPackages(manifest 
*YAML) ([]*whisk.Rule
        }
 
        for n, p := range manifestPackages {
-               r, err := dm.ComposeRules(p, n)
+               r, err := dm.ComposeRules(p, n, ma)
                if err == nil {
                        rules = append(rules, r...)
                } else {
@@ -852,16 +852,38 @@ func (dm *YAMLParser) 
ComposeRulesFromAllPackages(manifest *YAML) ([]*whisk.Rule
        return rules, nil
 }
 
-func (dm *YAMLParser) ComposeRules(pkg Package, packageName string) 
([]*whisk.Rule, error) {
+func (dm *YAMLParser) ComposeRules(pkg Package, packageName string, ma 
whisk.KeyValue) ([]*whisk.Rule, error) {
        var r1 []*whisk.Rule = make([]*whisk.Rule, 0)
 
        for _, rule := range pkg.GetRuleList() {
-               wskrule := rule.ComposeWskRule()
+               wskrule := new(whisk.Rule)
+               wskrule.Name = wskenv.ConvertSingleName(rule.Name)
+               //wskrule.Namespace = rule.Namespace
+               pub := false
+               wskrule.Publish = &pub
+               wskrule.Trigger = wskenv.ConvertSingleName(rule.Trigger)
+               wskrule.Action = wskenv.ConvertSingleName(rule.Action)
                act := strings.TrimSpace(wskrule.Action.(string))
                if !strings.ContainsRune(act, '/') && !strings.HasPrefix(act, 
packageName+"/") {
                        act = path.Join(packageName, act)
                }
                wskrule.Action = act
+               listOfAnnotations := make(whisk.KeyValueArr, 0)
+               for name, value := range rule.Annotations {
+                       var keyVal whisk.KeyValue
+                       keyVal.Key = name
+                       keyVal.Value = wskenv.GetEnvVar(value)
+                       listOfAnnotations = append(listOfAnnotations, keyVal)
+               }
+               if len(listOfAnnotations) > 0 {
+                       wskrule.Annotations = append(wskrule.Annotations, 
listOfAnnotations...)
+               }
+
+               // add managed annotations if its a managed deployment
+               if utils.Flags.Managed {
+                       wskrule.Annotations = append(wskrule.Annotations, ma)
+               }
+
                r1 = append(r1, wskrule)
        }
        return r1, nil
diff --git a/parsers/manifest_parser_test.go b/parsers/manifest_parser_test.go
index cd07c5cb..af906d1c 100644
--- a/parsers/manifest_parser_test.go
+++ b/parsers/manifest_parser_test.go
@@ -1327,7 +1327,7 @@ func TestComposeRules(t *testing.T) {
        // read and parse manifest.yaml file
        p := NewYAMLParser()
        m, _ := p.ParseManifest(tmpfile.Name())
-       ruleList, err := p.ComposeRulesFromAllPackages(m)
+       ruleList, err := p.ComposeRulesFromAllPackages(m, whisk.KeyValue{})
        if err != nil {
                assert.Fail(t, "Failed to compose rules")
        }
@@ -1800,7 +1800,7 @@ func TestRuleName_Env_Var(t *testing.T) {
        mm := NewYAMLParser()
        manifestfile := "../tests/dat/manifest_data_rule_env_var.yaml"
        manifest, _ := mm.ParseManifest(manifestfile)
-       rules, err := mm.ComposeRulesFromAllPackages(manifest)
+       rules, err := mm.ComposeRulesFromAllPackages(manifest, whisk.KeyValue{})
        if err != nil {
                assert.Fail(t, "Failed to compose rules")
        }
diff --git a/parsers/yamlparser.go b/parsers/yamlparser.go
index 7e517144..a43b6c76 100644
--- a/parsers/yamlparser.go
+++ b/parsers/yamlparser.go
@@ -173,7 +173,8 @@ type Rule struct {
        Action string `yaml:"action"` //used in manifest.yaml
        Rule   string `yaml:"rule"`   //used in manifest.yaml
        //mapping to wsk.Rule.Name
-       Name string
+       Name        string
+       Annotations map[string]interface{} `yaml:"annotations,omitempty"`
 }
 
 type Repository struct {


 

----------------------------------------------------------------
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:
us...@infra.apache.org


With regards,
Apache Git Services

Reply via email to