Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify deployment manager field mapping #30

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.21
require (
github.com/PaesslerAG/jsonpath v0.1.1
github.com/gorilla/mux v1.8.0
github.com/itchyny/gojq v0.12.13
github.com/json-iterator/go v1.1.12
github.com/onsi/ginkgo/v2 v2.13.0
github.com/onsi/gomega v1.29.0
Expand All @@ -15,6 +16,7 @@ require (
github.com/PaesslerAG/gval v1.0.0 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/itchyny/timefmt-go v0.1.5 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/stretchr/testify v1.8.2 // indirect
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB7
github.com/ianlancetaylor/demangle v0.0.0-20200824232613-28f6c0f3b639/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/itchyny/gojq v0.12.13 h1:IxyYlHYIlspQHHTE0f3cJF0NKDMfajxViuhBLnHd/QU=
github.com/itchyny/gojq v0.12.13/go.mod h1:JzwzAqenfhrPUuwbmEz3nu3JQmFLlQTQMUcOdnu/Sf4=
github.com/itchyny/timefmt-go v0.1.5 h1:G0INE2la8S6ru/ZI5JecgyzbbJNs5lG1RcBqa7Jm6GE=
github.com/itchyny/timefmt-go v0.1.5/go.mod h1:nEP7L+2YmAbT2kZ2HfSs1d8Xtw9LY8D2stDBckWakZ8=
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
Expand Down
79 changes: 61 additions & 18 deletions internal/service/deployment_manager_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"log/slog"
"net/http"

"github.com/itchyny/gojq"
jsoniter "github.com/json-iterator/go"
"github.com/openshift-kni/oran-o2ims/internal/data"
"github.com/openshift-kni/oran-o2ims/internal/k8s"
Expand Down Expand Up @@ -50,6 +51,7 @@ type DeploymentManagerHandler struct {
backendClient *http.Client
jsonAPI jsoniter.API
selectorEvaluator *search.SelectorEvaluator
fieldMappers map[string]*gojq.Code
}

// NewDeploymentManagerHandler creates a builder that can then be used to configure and create a
Expand Down Expand Up @@ -151,6 +153,12 @@ func (b *DeploymentManagerHandlerBuilder) Build() (
return
}

// Compile the field mappers:
fieldMappers, err := b.compileFieldMappers()
if err != nil {
return
}

// Create and populate the object:
result = &DeploymentManagerHandler{
logger: b.logger,
Expand All @@ -160,6 +168,41 @@ func (b *DeploymentManagerHandlerBuilder) Build() (
backendClient: backendClient,
selectorEvaluator: selectorEvaluator,
jsonAPI: jsonAPI,
fieldMappers: fieldMappers,
}
return
}

// compileFieldMappers compiles the jq queries that are used to calculate the values of fields. This
// compilation happens when the object is created so that we don't need to compile those queries
// with every request.
func (b *DeploymentManagerHandlerBuilder) compileFieldMappers() (result map[string]*gojq.Code,
err error) {
result = map[string]*gojq.Code{}
for field, source := range deploymentManagerFieldMappers {
var query *gojq.Query
query, err = gojq.Parse(source)
if err != nil {
b.logger.Error(
"Failed to parse mapping",
slog.String("field", field),
slog.String("source", source),
slog.String("error", err.Error()),
)
return
}
var code *gojq.Code
code, err = gojq.Compile(query, gojq.WithVariables([]string{"$cloud_id"}))
if err != nil {
b.logger.Error(
"Failed to compile mapping",
slog.String("field", field),
slog.String("source", source),
slog.String("error", err.Error()),
)
return
}
result[field] = code
}
return
}
Expand Down Expand Up @@ -304,24 +347,24 @@ func (h *DeploymentManagerHandler) fetchItem(ctx context.Context, id string) (re

func (h *DeploymentManagerHandler) mapItem(ctx context.Context,
from data.Object) (to data.Object, err error) {
fromID, err := data.GetString(from, `$.metadata.labels["clusterID"]`)
if err != nil {
return
}
fromName, err := data.GetString(from, `$.metadata.name`)
if err != nil {
return
}
fromURL, err := data.GetString(from, `$.spec.managedClusterClientConfigs[0].url`)
if err != nil {
return
}
to = data.Object{
"deploymentManagerId": fromID,
"name": fromName,
"description": fromName,
"oCloudId": h.cloudID,
"serviceUri": fromURL,
to = data.Object{}
for name, code := range h.fieldMappers {
iterator := code.RunWithContext(ctx, from, h.cloudID)
value, ok := iterator.Next()
if !ok {
continue
}
to[name] = value
}
return
}

// deploymentManagerFieldMappers contains the correspondence between fields of the output objects
// and the jq queries that are used to extract their value from the input object.
var deploymentManagerFieldMappers = map[string]string{
"deploymentManagerId": `.metadata.labels["clusterID"]`,
"name": `.metadata.name`,
"description": `.metadata.name`,
"serviceUri": `.spec.managedClusterClientConfigs[0].url`,
"oCloudId": `$cloud_id`,
}
Loading