Browse Source

Cdp/parser fix [v3.1] (#3771)

Signed-off-by: Christian Petersen <Christian.Petersen2@ibm.com>
Co-authored-by: Christian Petersen <Christian.Petersen2@ibm.com>
Niko Kovacevic 1 week ago
parent
commit
a4f852c893

+ 5 - 1
core/pkg/filter/resourcequota/parser.go

@@ -1,6 +1,9 @@
 package resourcequota
 
-import "github.com/opencost/opencost/core/pkg/filter/ast"
+import (
+	"github.com/opencost/opencost/core/pkg/filter/ast"
+	"github.com/opencost/opencost/core/pkg/filter/ops"
+)
 
 var resourceQuotaFilterFields []*ast.Field = []*ast.Field{
 	ast.NewField(FieldClusterID),
@@ -19,6 +22,7 @@ func init() {
 		ff := *f
 		fieldMap[ResourceQuotaField(ff.Name)] = &ff
 	}
+	ops.RegisterDefaultFieldLookup[ResourceQuotaField](DefaultFieldByName)
 }
 
 // DefaultFieldByName returns only default resource quota filter fields by name.

+ 26 - 0
core/pkg/filter/resourcequota/parser_test.go

@@ -4,6 +4,7 @@ import (
 	"testing"
 
 	"github.com/opencost/opencost/core/pkg/filter/ast"
+	"github.com/opencost/opencost/core/pkg/filter/ops"
 )
 
 func TestDefaultFieldByName(t *testing.T) {
@@ -41,3 +42,28 @@ func TestDefaultFieldByName(t *testing.T) {
 	}
 
 }
+
+func TestOpsEqWithResourceQuotaField(t *testing.T) {
+	clusterFilter := ops.Eq(FieldClusterID, "test-cluster")
+
+	equalOp, ok := clusterFilter.(*ast.EqualOp)
+	if !ok {
+		t.Fatalf("expected *ast.EqualOp, got %T", clusterFilter)
+	}
+
+	if equalOp.Left.Field == nil {
+		t.Fatal("expected Field to be non-nil, got nil")
+	}
+
+	if equalOp.Left.Field.Name == "" {
+		t.Fatal("expected Field.Name to be non-empty, got empty string")
+	}
+
+	if equalOp.Left.Field.Name != "cluster" {
+		t.Errorf("expected Field.Name to be 'cluster', got '%s'", equalOp.Left.Field.Name)
+	}
+
+	if equalOp.Right != "test-cluster" {
+		t.Errorf("expected Right to be 'test-cluster', got '%s'", equalOp.Right)
+	}
+}