Skip to content

Commit

Permalink
first version
Browse files Browse the repository at this point in the history
  • Loading branch information
PK85 committed Feb 3, 2025
0 parents commit 42732c6
Show file tree
Hide file tree
Showing 6 changed files with 423 additions and 0 deletions.
61 changes: 61 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# hap-parser

Implementation of the [HAP Rule Specification](https://github.com/kyma-project/kyma-environment-broker/blob/06542126d7a9db9eec488092f19beee3b7dacf57/docs/contributor/03-11-hap-rules.md)

# Features
- Provide the rules in a YAML file :white_check_mark:
- Rule parsing and validation :white_check_mark:
- See what search labels will be used for every rule :white_check_mark:
- Rules convertion summary :white_check_mark:
- Uniqnuess & Priority :construction_worker:
- Provide example request and see which rule will be assigned :construction_worker:


# HowTo

1) Input rules:
```
cat resources/rules.yaml
rule:
- aws # pool: hyperscalerType: aws
- aws(PR=cf-eu11) -> EU # pool: hyperscalerType: aws_cf-eu11; euAccess: true
- azure # pool: hyperscalerType: azure
- azure(PR=cf-ch20) -> EU # pool: hyperscalerType: azure_cf-ch20; euAccess: true
- gcp # pool: hyperscalerType: gcp
- gcp(PR=cf-sa30) # pool: hyperscalerType: gcp_cf-sa30
- trial -> S # pool: hyperscalerType: azure; shared: true - TRIAL POOL
- sap-converged-cloud(HR=*) -> S # pool: hyperscalerType: openstack_<HYPERSCALER_REGION>; shared: true
- azure_lite # pool: hyperscalerType: azure
- preview # pool: hyperscalerType: aws
- free # pool: hyperscalerType: aws%
```

2) Run rules check:
```
goo run cmd/parser/main.go
Rule No. 1 is OK, value: [aws] - searchLabels: hyperscalerType: aws
Rule No. 2 is OK, value: [aws(PR=cf-eu11) -> EU] - searchLabels: hyperscalerType: aws_cf-eu11; euAccess: true
Rule No. 3 is OK, value: [azure] - searchLabels: hyperscalerType: azure
Rule No. 4 is OK, value: [azure(PR=cf-ch20) -> EU] - searchLabels: hyperscalerType: azure_cf-ch20; euAccess: true
Rule No. 5 is OK, value: [gcp] - searchLabels: hyperscalerType: gcp
Rule No. 6 is OK, value: [gcp(PR=cf-sa30)] - searchLabels: hyperscalerType: gcp_cf-sa30
Rule No. 7 is OK, value: [trial -> S] - searchLabels: hyperscalerType: aws; shared: true
Rule No. 8 is OK, value: [sap-converged-cloud(HR=*) -> S] - searchLabels: hyperscalerType: openstack_*; shared: true
Rule No. 9 is OK, value: [azure_lite] - searchLabels: hyperscalerType: azure_lite
Rule No. 10 is OK, value: [preview] - searchLabels: hyperscalerType: aws
Rule No. 11 is OK, value: [free] - searchLabels: hyperscalerType: aws/azure
```

3) Modify input rules and check results:
```
rule:
- aws
- aws(PR=cf-eu11, PR=cf-eu12) -> EU
- azure
```
```
Rule No. 1 is OK, value: [aws] - searchLabels: hyperscalerType: aws
Rule No. 2 is Invalid, value: [aws(PR=cf-eu11, PR=cf-eu12) -> EU] - reason: Input Attributes could be specified once. Attribute No. 2 is duplicated, value: PR
Rul No. 3 is OK, value: [azure] - searchLabels: hyperscalerType: azure
```
81 changes: 81 additions & 0 deletions cmd/parser/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package main

import (
"fmt"
"hap-parser/internal/rule"
"log"
"os"

"gopkg.in/yaml.v2"
)

const (
RULE_OK1 = "aws"
RULE_OK2 = "aws -> EU, S"
RULE_OK3 = "aws(PR=cf-eu11) -> EU"
RULE_OK4 = "aws(PR=cf-eu11) -> EU"

RULE_OUT_NOT_OK1 = "aws(PR=cf-eu11) -> S -> EU" // "Separator -> is optional and max 1 occurance. Current count: 2"
RULE_OUT_NOT_OK2 = "aws(PR=cf-eu11) ->,S" // "Output Attributes cannot be empty. Attribute No. 1 is empty"
RULE_OUT_NOT_OK3 = "aws(PR=cf-eu11) -> ,," // "Output Attributes cannot be empty. Attribute No. 1 is empty"
RULE_OUT_NOT_OK4 = "aws(PR=cf-eu11) -> S," // "Output Attributes cannot be empty. Attribute No. 2 is empty"
RULE_OUT_NOT_OK5 = "aws(PR=cf-eu11) -> S,AA" // "Allowed Output Attributes are [S EU]. Attribute No. 2 has not supported value: AA"
RULE_OUT_NOT_OK6 = "aws(PR=cf-eu11) ->AA" // "Allowed Output Attributes are [S EU]. Attribute No. 1 has not supported value: AA"
RULE_OUT_NOT_OK7 = "aws(PR=cf-eu11) -> EU, S, S" // "Max number of Output Attributes equals number of supported Output Attributes. Supported: 2, current: 3"
RULE_OUT_NOT_OK8 = "aws(PR=cf-eu11) -> EU, EU" // "Allowed Output Attributes could be specified once. Attribute No. 2 is duplicated, value: EU"
RULE_OUT_NOT_OK9 = "aws(PR=cf-eu11) -> S, S" // "Allowed Output Attributes could be specified once. Attribute No. 2 is duplicated, value: S"}

RULE_IN_NOT_OK1 = "aaws" // "Allowed Plans: [aws azure gcp]. Plan and/or Input Attributes has incorrect syntax: aaws"
RULE_IN_NOT_OK2 = "awss" // "Allowed Plans: [aws azure gcp]. Plan and/or Input Attributes has incorrect syntax: awss"
RULE_IN_NOT_OK3 = "aws()" // "Allowed Plans: [aws azure gcp]. Plan and/or Input Attributes has incorrect syntax: aws()"
RULE_IN_NOT_OK4 = "aws(" // "Allowed Plans: [aws azure gcp]. Plan and/or Input Attributes has incorrect syntax: aws("
RULE_IN_NOT_OK5 = "aws(PR=cf-eu11" // "Allowed Plans: [aws azure gcp]. Plan and/or Input Attributes has incorrect syntax: aws(PR=cf-eu11"
RULE_IN_NOT_OK6 = "awsPR=cf-eu11)" // "Allowed Plans: [aws azure gcp]. Plan and/or Input Attributes has incorrect syntax: awsPR=cf-eu11)"
RULE_IN_NOT_OK7 = "aws(X)" // "Input Attributes must contain `=` character. Attribute No. 1 is invalid, value: X"
RULE_IN_NOT_OK8 = "aws(, )" // "Input Attributes cannot be empty. Attribute No. 1 is empty"
RULE_IN_NOT_OK9 = "aws(PR)" // "Input Attributes must contain `=` character.Attribute No. 1 is invalid, value: PR"
RULE_IN_NOT_OK10 = "aws(PR=cf-eu11, PR) " // "Input Attributes must contain `=` character. Attribute No. 2 is ivalid, value: PR"
RULE_IN_NOT_OK11 = "aws(PR=cf-=eu11)" // "Input Attributes must contain only one `=` character. Attribute No. 1 is invalid, value: PR=cf-=eu11"
RULE_IN_NOT_OK12 = "aws(PR=cf.PR=eu11)" // "Input Attributes must contain only one `=` character. Attribute No. 1 is invalid, value: PR=cf.PR=eu11"
RULE_IN_NOT_OK13 = "aws(HR=westeu, PR=cf-=eu11)" // "Input Attributes must contain only one `=` character. Attribute No. 2 is invalid, value: PR=cf-=eu11"}
RULE_IN_NOT_OK14 = "aws(PR=cf-eu11, PR=)" // "Input Attributes could be specified once. Attribute No. 2 is duplicated, value: PR"
RULE_IN_NOT_OK15 = "aws( PR=)" // "Input Attribute PR cannot be empty. Attribute No. 1, value: PR="
RULE_IN_NOT_OK16 = "aws(PR=cf-eu11, PR=cf-eu11)" // "Input Attributes could be specified once. Attribute No. 2 is duplicated, value: PR"
RULE_IN_NOT_OK17 = "aws(HR=cf-eu11, PR=)" // "Input Attribute PR cannot be empty. Attribute No. 2, value: PR="}
RULE_IN_NOT_OK18 = "aws( HR=)" // "Input Attribute HR cannot be empty. Attribute No. 1, value: HR="
RULE_IN_NOT_OK19 = "aws(HR=cf-eu11, HR=cf-eu11)" // "Input Attributes could be specified once. Attribute No. 2 is duplicated, value: HR"
RULE_IN_NOT_OK20 = "aws(PR=cf-eu11, HR=12, PR=88) " // "Max number of Input Attributes equals number of supported Input Attributes. Supported: 2, current: 3"
RULE_IN_NOT_OK21 = "aws(KK=cf-eu11) " // "Allowed Input Attributes are [PR HR]. Attribute No. 1 has not supported value: KK"
RULE_IN_NOT_OK22 = "aws(PR=cf-eu11, KK=cf-eu11) " // "Allowed Input Attributes are [PR HR]. Attribute No. 2 has not supported value: KK"
RULE_IN_NOT_OK23 = "aws(KK=cf-eu11, PR=cf-eu11 ) " // "Allowed Input Attributes are [PR HR]. Attribute No. 1 has not supported value: KK"
)

func main() {
var c conf
for index, ruleRaw := range c.getConf().Rules {
rule := rule.ConvertToRule(ruleRaw)
if !rule.IsInvalid {
fmt.Printf("Rule No. %d is OK, value: [%s] - searchLabels: %s\n", index+1, rule.Raw, *rule.SearchLabels)
} else {
fmt.Printf("Rule No. %d is Invalid, value: [%s] - reason: %s\n\n", index+1, rule.Raw, rule.InvalidMessage)
}
}
}

type conf struct {
Rules []string `yaml:"rule"`
}

func (c *conf) getConf() *conf {

yamlFile, err := os.ReadFile("resources/rules.yaml")
if err != nil {
log.Printf("yamlFile.Get err #%v ", err)
}
err = yaml.Unmarshal(yamlFile, c)
if err != nil {
log.Fatalf("Unmarshal: %v", err)
}

return c
}
5 changes: 5 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module hap-parser

go 1.23.2

require gopkg.in/yaml.v2 v2.4.0 // indirect
3 changes: 3 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
Loading

0 comments on commit 42732c6

Please sign in to comment.