@@ -153,14 +153,14 @@ func TestRunFunction(t *testing.T) {
153153 Desired : & fnv1.State {
154154 Composite : & fnv1.Resource {
155155 Resource : resource .MustStructJSON (`{
156- "apiVersion": "example.org/v1",
157- "kind": "XR",
158- "status": {
159- "azResourceGraphQueryResult":
160- {
161- "resource": "mock-resource"
162- }
163- }}` ),
156+ "apiVersion": "example.org/v1",
157+ "kind": "XR",
158+ "status": {
159+ "azResourceGraphQueryResult":
160+ {
161+ "resource": "mock-resource"
162+ }
163+ }}` ),
164164 },
165165 },
166166 },
@@ -454,6 +454,168 @@ func TestRunFunction(t *testing.T) {
454454 },
455455 },
456456 },
457+ "CanGetQueryFromXRStatusKey" : {
458+ reason : "The Function should be able to get Query from the XR status field" ,
459+ args : args {
460+ ctx : context .Background (),
461+ req : & fnv1.RunFunctionRequest {
462+ Meta : & fnv1.RequestMeta {Tag : "hello" },
463+ Input : resource .MustStructJSON (`{
464+ "apiVersion": "azresourcegraph.fn.crossplane.io/v1alpha1",
465+ "kind": "Input",
466+ "queryRef": "status.azResourceGraphQuery",
467+ "target": "context.azResourceGraphQueryResult"
468+ }` ),
469+ Observed : & fnv1.State {
470+ Composite : & fnv1.Resource {
471+ Resource : resource .MustStructJSON (`{
472+ "apiVersion": "example.org/v1",
473+ "kind": "XR",
474+ "status": {
475+ "azResourceGraphQuery": "QueryFromXRStatus"
476+ }}` ),
477+ },
478+ },
479+ Credentials : map [string ]* fnv1.Credentials {
480+ "azure-creds" : {
481+ Source : & fnv1.Credentials_CredentialData {CredentialData : creds },
482+ },
483+ },
484+ },
485+ },
486+ want : want {
487+ rsp : & fnv1.RunFunctionResponse {
488+ Meta : & fnv1.ResponseMeta {Tag : "hello" , Ttl : durationpb .New (response .DefaultTTL )},
489+ Conditions : []* fnv1.Condition {
490+ {
491+ Type : "FunctionSuccess" ,
492+ Status : fnv1 .Status_STATUS_CONDITION_TRUE ,
493+ Reason : "Success" ,
494+ Target : fnv1 .Target_TARGET_COMPOSITE_AND_CLAIM .Enum (),
495+ },
496+ },
497+ Results : []* fnv1.Result {
498+ {
499+ Severity : fnv1 .Severity_SEVERITY_NORMAL ,
500+ Message : `Query: "QueryFromXRStatus"` ,
501+ Target : fnv1 .Target_TARGET_COMPOSITE .Enum (),
502+ },
503+ },
504+ Context : resource .MustStructJSON (
505+ `{
506+ "azResourceGraphQueryResult":
507+ {
508+ "resource": "mock-resource"
509+ }
510+ }` ,
511+ ),
512+ },
513+ },
514+ },
515+ "CanGetQueryFromNestedXRStatusKey" : {
516+ reason : "The Function should be able to get Query from the nested XR status field" ,
517+ args : args {
518+ ctx : context .Background (),
519+ req : & fnv1.RunFunctionRequest {
520+ Meta : & fnv1.RequestMeta {Tag : "hello" },
521+ Input : resource .MustStructJSON (`{
522+ "apiVersion": "azresourcegraph.fn.crossplane.io/v1alpha1",
523+ "kind": "Input",
524+ "queryRef": "status.testKey.azResourceGraphQuery",
525+ "target": "context.azResourceGraphQueryResult"
526+ }` ),
527+ Observed : & fnv1.State {
528+ Composite : & fnv1.Resource {
529+ Resource : resource .MustStructJSON (`{
530+ "apiVersion": "example.org/v1",
531+ "kind": "XR",
532+ "status": {
533+ "testKey": {
534+ "azResourceGraphQuery": "QueryFromNestedXRStatus"
535+ }
536+ }}` ),
537+ },
538+ },
539+ Credentials : map [string ]* fnv1.Credentials {
540+ "azure-creds" : {
541+ Source : & fnv1.Credentials_CredentialData {CredentialData : creds },
542+ },
543+ },
544+ },
545+ },
546+ want : want {
547+ rsp : & fnv1.RunFunctionResponse {
548+ Meta : & fnv1.ResponseMeta {Tag : "hello" , Ttl : durationpb .New (response .DefaultTTL )},
549+ Conditions : []* fnv1.Condition {
550+ {
551+ Type : "FunctionSuccess" ,
552+ Status : fnv1 .Status_STATUS_CONDITION_TRUE ,
553+ Reason : "Success" ,
554+ Target : fnv1 .Target_TARGET_COMPOSITE_AND_CLAIM .Enum (),
555+ },
556+ },
557+ Results : []* fnv1.Result {
558+ {
559+ Severity : fnv1 .Severity_SEVERITY_NORMAL ,
560+ Message : `Query: "QueryFromNestedXRStatus"` ,
561+ Target : fnv1 .Target_TARGET_COMPOSITE .Enum (),
562+ },
563+ },
564+ Context : resource .MustStructJSON (
565+ `{
566+ "azResourceGraphQueryResult":
567+ {
568+ "resource": "mock-resource"
569+ }
570+ }` ,
571+ ),
572+ },
573+ },
574+ },
575+ "FailIfQueryIsEmpty" : {
576+ reason : "The Function should fail if Query is empty" ,
577+ args : args {
578+ ctx : context .Background (),
579+ req : & fnv1.RunFunctionRequest {
580+ Meta : & fnv1.RequestMeta {Tag : "hello" },
581+ Input : resource .MustStructJSON (`{
582+ "apiVersion": "azresourcegraph.fn.crossplane.io/v1alpha1",
583+ "kind": "Input",
584+ "queryRef": "status.nonExistingKey.azResourceGraphQuery",
585+ "target": "context.azResourceGraphQueryResult"
586+ }` ),
587+ Observed : & fnv1.State {
588+ Composite : & fnv1.Resource {
589+ Resource : resource .MustStructJSON (`{
590+ "apiVersion": "example.org/v1",
591+ "kind": "XR",
592+ "status": {
593+ "testKey": {
594+ "azResourceGraphQuery": "QueryFromNestedXRStatus"
595+ }
596+ }}` ),
597+ },
598+ },
599+ Credentials : map [string ]* fnv1.Credentials {
600+ "azure-creds" : {
601+ Source : & fnv1.Credentials_CredentialData {CredentialData : creds },
602+ },
603+ },
604+ },
605+ },
606+ want : want {
607+ rsp : & fnv1.RunFunctionResponse {
608+ Meta : & fnv1.ResponseMeta {Tag : "hello" , Ttl : durationpb .New (response .DefaultTTL )},
609+ Results : []* fnv1.Result {
610+ {
611+ Severity : fnv1 .Severity_SEVERITY_FATAL ,
612+ Message : `Query is empty` ,
613+ Target : fnv1 .Target_TARGET_COMPOSITE .Enum (),
614+ },
615+ },
616+ },
617+ },
618+ },
457619 }
458620
459621 for name , tc := range cases {
0 commit comments