Skip to content

Commit

Permalink
MEDIUM: api consolidation - Move child resources as nested resources
Browse files Browse the repository at this point in the history
  • Loading branch information
hdurand0710 committed Jul 22, 2024
1 parent 42c82e2 commit 169188f
Show file tree
Hide file tree
Showing 38 changed files with 23,981 additions and 22,141 deletions.
2 changes: 1 addition & 1 deletion .gitlab-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ build-specification:
tags:
- go
script:
- go run specification/build/build.go -file specification/haproxy-spec.yaml > specification/build/haproxy_spec_to_compare.yaml
- go run cmd/specification/*.go -file specification/haproxy-spec.yaml > specification/build/haproxy_spec_to_compare.yaml
- diff -u specification/build/haproxy_spec_to_compare.yaml specification/build/haproxy_spec.yaml
rules:
- if: $CI_PIPELINE_SOURCE == 'merge_request_event'
Expand Down
2 changes: 1 addition & 1 deletion .yamllint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
extends: default

ignore: |
build/
specification/haproxy-spec.yaml
.github/
rules:
Expand Down
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ e2e-docker:

.PHONY: spec
spec:
go run specification/build/build.go -file specification/haproxy-spec.yaml > specification/build/haproxy_spec.yaml
go run cmd/specification/*.go -file specification/haproxy-spec.yaml > specification/build/haproxy_spec.yaml

.PHONY: models
models: spec swagger-check
Expand All @@ -42,7 +42,7 @@ lint:

.PHONY: lint-yaml
lint-yaml:
docker run --rm -v $(pwd):/data cytopia/yamllint .
docker run --rm -v $(PWD):/data cytopia/yamllint .

.PHONY: gofumpt
gofumpt:
Expand Down
76 changes: 57 additions & 19 deletions specification/build/build.go → cmd/specification/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,24 @@ package main

import (
"bufio"
"bytes"
"flag"
"fmt"
"html/template"
"os"
"path"
"path/filepath"
"sort"
"strings"

"github.com/go-openapi/swag"
"github.com/haproxytech/client-native/v6/configuration/parents"
"gopkg.in/yaml.v3"
)

var cache map[string]interface{}
var cache map[string]interface{} //nolint:gochecknoglobals

func error(msg string) {
func errorExit(msg string) {
fmt.Fprintf(os.Stderr, "ERROR: %v\n", msg)
os.Exit(1)
}
Expand All @@ -39,7 +43,7 @@ func expandRef(refValue string, absPath string, prefix string) string {
if !ok {
fileHandle, err := os.Open(filePath)
if err != nil {
error(err.Error())
errorExit(err.Error())
}
defer fileHandle.Close()

Expand All @@ -51,8 +55,8 @@ func expandRef(refValue string, absPath string, prefix string) string {

err = yaml.Unmarshal([]byte(value), &m)
if err != nil {
fmt.Println(refValue)
fmt.Println("WARNING: ", err)
fmt.Println(refValue) //nolint:forbidigo
fmt.Println("WARNING: ", err) //nolint:forbidigo
return refValue
}
cache[filePath] = m
Expand All @@ -61,48 +65,53 @@ func expandRef(refValue string, absPath string, prefix string) string {
if m[keyPath[1:]] != nil {
retVal = m[keyPath[1:]].(map[string]interface{})
} else {
fmt.Println(refValue)
fmt.Println(keyPath)
fmt.Println(m[keyPath[1:]])
fmt.Println(refValue) //nolint:forbidigo
fmt.Println(keyPath) //nolint:forbidigo
fmt.Println(m[keyPath[1:]]) //nolint:forbidigo
}

retValByte, err := yaml.Marshal(retVal)
buf := bytes.Buffer{}
enc := yaml.NewEncoder(&buf)
enc.SetIndent(2)
// Can set default indent here on the encoder
err := enc.Encode(&retVal)
if err != nil {
warn("Error encoding YAML")
return refValue
}
retValStr := string(retValByte)
retValStr := buf.String()

indentedRetValStr := ""
indentedLine := ""
for _, line := range strings.Split(retValStr, "\n") {
if strings.TrimSpace(line) != "" {
indentedLine = prefix + " " + line + "\n"
indentedLine = prefix + "" + line + "\n"
indentedRetValStr += indentedLine
}
}

return indentedRetValStr[:len(indentedRetValStr)-1]
}

func main() {
func main() { //nolint:gocognit
inputFilePtr := flag.String("file", "", "Source file")

flag.Parse()

if *inputFilePtr == "" {
error("Input file not specified, please specify")
errorExit("Input file not specified, please specify")
}
// sanity checks
if _, err := os.Stat(strings.TrimSpace(*inputFilePtr)); os.IsNotExist(err) {
error("File " + *inputFilePtr + " does not exist")
errorExit("File " + *inputFilePtr + " does not exist")
}

cache = make(map[string]interface{})

absPath := filepath.Dir(*inputFilePtr)
fileHandle, err := os.Open(*inputFilePtr)
if err != nil {
error(err.Error())
errorExit(err.Error())
}
defer fileHandle.Close()

Expand All @@ -114,7 +123,7 @@ func main() {
Description string `yaml:"description,omitempty"`
}
type tags []tag
var ts tags = tags{}
ts := tags{}
var tagResult strings.Builder
for fileScanner.Scan() {
line := fileScanner.Text()
Expand Down Expand Up @@ -147,7 +156,7 @@ func main() {
str := tagResult.String()
err = yaml.Unmarshal([]byte(str), &ts)
if err != nil {
error(err.Error())
errorExit(err.Error())
}
sort.Slice(ts, func(i, j int) bool {
return ts[i].Name < ts[j].Name
Expand All @@ -156,7 +165,10 @@ func main() {
result.WriteString("\n")

b, _ := yaml.Marshal(&ts)
result.WriteString(string(b))

for _, line := range strings.Split(strings.TrimRight(string(b), "\n"), "\n") {
result.WriteString(" " + line + "\n")
}
result.WriteString("security:")
result.WriteString("\n")
break
Expand All @@ -167,5 +179,31 @@ func main() {
result.WriteString("\n")
}
}
fmt.Println(result.String())

tmplRes := expandChildren(result.String())
res := tmplRes.String()
res = strings.TrimSuffix(res, "\n")

fmt.Println(res) //nolint:forbidigo
}

// to expand models for nested children
// update:
// - configuration/parents/constants.go
// - congiuration/parents/parents.go
// and specification/haproxy_spec.yaml (template)

func expandChildren(src string) bytes.Buffer {
funcMap := template.FuncMap{
"parents": parents.Parents,
"toGoName": swag.ToGoName,
}

tmpl := template.Must(template.New("").Funcs(funcMap).Parse(src))
var result bytes.Buffer
err := tmpl.Execute(&result, nil)
if err != nil {
errorExit(err.Error())
}
return result
}
46 changes: 46 additions & 0 deletions configuration/parents/constants.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Copyright 2019 HAProxy Technologies
//
// Licensed 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 parents

const (
ServerChildType = "server"
HTTPAfterResponseRuleChildType = "http_after_response_rule"
HTTPCheckChildType = "http_check"
HTTPErrorRuleChildType = "http_error_rule"
HTTPRequestRuleChildType = "http_request_rule"
HTTPResponseRuleChildType = "http_response_rule"
TCPCheckChildType = "tcp_check"
TCPRequestRuleChildType = "tcp_request_rule"
TCPResponseRuleChildType = "tcp_response_rule"
ACLChildType = "acl"
BindChildType = "bind"
FilterChildType = "filter"
LogTargetChildType = "log_target"
)

type CnParentType string

const (
BackendParentType CnParentType = "backend"
FrontendParentType CnParentType = "frontend"
DefaultsParentType CnParentType = "defaults"
LogForwardParentType CnParentType = "log_forward"
PeerParentType CnParentType = "peers"
RingParentType CnParentType = "ring"
GlobalParentType CnParentType = "global"
FCGIAppParentType CnParentType = "fcgi-app"
ResolverParentType CnParentType = "resolvers"
)
101 changes: 101 additions & 0 deletions configuration/parents/parents.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
// Copyright 2019 HAProxy Technologies
//
// Licensed 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 parents

type Parent struct {
PathParentType string
ParentType string
IsGenericParent bool // only one of the parents for a child type can be set as Geenric - used in Dapi
GenericParentType string
IsUnnamedParent bool
}

func Parents(childType string) []Parent {
switch childType {
case ServerChildType:
return []Parent{
{PathParentType: "backends", ParentType: "Backend", IsGenericParent: true},
{PathParentType: "peers", ParentType: "Peer", GenericParentType: "Backend"},
{PathParentType: "rings", ParentType: "Ring", GenericParentType: "Backend"},
}
case HTTPAfterResponseRuleChildType:
return []Parent{
{PathParentType: "backends", ParentType: "Backend", IsGenericParent: true},
{PathParentType: "frontends", ParentType: "Frontend", GenericParentType: "Backend"},
}
case HTTPCheckChildType:
return []Parent{
{PathParentType: "backends", ParentType: "Backend", IsGenericParent: true},
{PathParentType: "defaults", ParentType: "Defaults", GenericParentType: "Backend"},
}
case HTTPErrorRuleChildType:
return []Parent{
{PathParentType: "backends", ParentType: "Backend", IsGenericParent: true},
{PathParentType: "frontends", ParentType: "Frontend", GenericParentType: "Backend"},
{PathParentType: "defaults", ParentType: "Defaults", GenericParentType: "Backend"},
}
case HTTPRequestRuleChildType:
return []Parent{
{PathParentType: "backends", ParentType: "Backend", IsGenericParent: true},
{PathParentType: "frontends", ParentType: "Frontend", GenericParentType: "Backend"},
}
case HTTPResponseRuleChildType:
return []Parent{
{PathParentType: "backends", ParentType: "Backend", IsGenericParent: true},
{PathParentType: "frontends", ParentType: "Frontend", GenericParentType: "Backend"},
}
case TCPCheckChildType:
return []Parent{
{PathParentType: "backends", ParentType: "Backend", IsGenericParent: true},
{PathParentType: "defaults", ParentType: "Defaults", GenericParentType: "Backend"},
}
case TCPRequestRuleChildType:
return []Parent{
{PathParentType: "backends", ParentType: "Backend", IsGenericParent: true},
{PathParentType: "frontends", ParentType: "Frontend", GenericParentType: "Backend"},
}
case TCPResponseRuleChildType:
return []Parent{
{PathParentType: "backends", ParentType: "Backend", IsGenericParent: true},
}
case ACLChildType:
return []Parent{
{PathParentType: "backends", ParentType: "Backend", IsGenericParent: true},
{PathParentType: "frontends", ParentType: "Frontend", GenericParentType: "Backend"},
{PathParentType: "fcgi_apps", ParentType: "FCGIApp", GenericParentType: "Backend"},
}
case BindChildType:
return []Parent{
{PathParentType: "frontends", ParentType: "Frontend", IsGenericParent: true},
{PathParentType: "log_forwards", ParentType: "LogForward", GenericParentType: "Frontend"},
{PathParentType: "peers", ParentType: "Peer", GenericParentType: "Frontend"},
}
case FilterChildType:
return []Parent{
{PathParentType: "backends", ParentType: "Backend", IsGenericParent: true},
{PathParentType: "frontends", ParentType: "Frontend", GenericParentType: "Backend"},
}
case LogTargetChildType:
return []Parent{
{PathParentType: "backends", ParentType: "Backend", IsGenericParent: true},
{PathParentType: "frontends", ParentType: "Frontend", GenericParentType: "Backend"},
{PathParentType: "defaults", ParentType: "Defaults", GenericParentType: "Backend"},
{PathParentType: "peers", ParentType: "Peer", GenericParentType: "Backend"},
{PathParentType: "log_forwards", ParentType: "LogForward", GenericParentType: "Backend"},
}
}
return nil
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ require (
github.com/stretchr/testify v1.9.0
golang.org/x/text v0.16.0
golang.org/x/tools v0.23.0
gopkg.in/yaml.v3 v3.0.1
)

require (
Expand All @@ -42,5 +43,4 @@ require (
golang.org/x/mod v0.19.0 // indirect
golang.org/x/sync v0.7.0 // indirect
golang.org/x/sys v0.22.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
Loading

0 comments on commit 169188f

Please sign in to comment.