Skip to content
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: 1 addition & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ linters-settings:

gocyclo:
# minimal code complexity to report, 30 by default (but we recommend 10-20)
min-complexity: 15
min-complexity: 20

maligned:
# print struct with more effective memory layout or not, false by default
Expand Down
24 changes: 24 additions & 0 deletions example/composition-with-mgmt-groups.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
apiVersion: apiextensions.crossplane.io/v1
kind: Composition
metadata:
name: function-azresourcegraph
spec:
compositeTypeRef:
apiVersion: example.crossplane.io/v1
kind: XR
mode: Pipeline
pipeline:
- step: query-azresourcegraph
functionRef:
name: function-azresourcegraph
input:
apiVersion: azresourcegraph.fn.crossplane.io/v1alpha1
kind: Input
query: "Resources | project name, location, type, id| where type =~ 'Microsoft.Compute/virtualMachines' | order by name desc"
managementGroups: ["example"]
credentials:
- name: azure-creds
source: Secret
secretRef:
namespace: upbound-system
name: azure-account-creds
22 changes: 14 additions & 8 deletions fn.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,14 +92,20 @@ func (f *Function) RunFunction(ctx context.Context, req *fnv1.RunFunctionRequest
return rsp, nil
}

// Create the query request, Run the query and get the results. Update the VM and subscriptionID details below.
results, err := client.Resources(ctx,
armresourcegraph.QueryRequest{
Query: to.Ptr(in.Query),
Subscriptions: []*string{
to.Ptr(subscriptionID)},
},
nil)
queryRequest := armresourcegraph.QueryRequest{
Query: to.Ptr(in.Query),
}

if len(subscriptionID) > 0 {
queryRequest.Subscriptions = []*string{to.Ptr(subscriptionID)}
}

if len(in.ManagementGroups) > 0 {
queryRequest.ManagementGroups = in.ManagementGroups
}

// Create the query request, Run the query and get the results.
results, err := client.Resources(ctx, queryRequest, nil)
if err != nil {
response.Fatal(rsp, errors.Wrap(err, "failed to finish the request"))
f.log.Info("FAILURE: ", "failure", fmt.Sprint(err))
Expand Down
26 changes: 26 additions & 0 deletions fn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,32 @@ func TestRunFunction(t *testing.T) {
},
},
},
"ResponseIsReturnedWithOptionalManagementGroups": {
reason: "The Function should accept optional managmenetGroups input",
args: args{
req: &fnv1.RunFunctionRequest{
Meta: &fnv1.RequestMeta{Tag: "hello"},
Input: resource.MustStructJSON(`{
"apiVersion": "azresourcegraph.fn.crossplane.io/v1alpha1",
"kind": "Input",
"query": "Resources| count",
"managementGroups": ["test"]
}`),
},
},
want: want{
rsp: &fnv1.RunFunctionResponse{
Meta: &fnv1.ResponseMeta{Tag: "hello", Ttl: durationpb.New(response.DefaultTTL)},
Results: []*fnv1.Result{
{
Severity: fnv1.Severity_SEVERITY_FATAL,
Message: "failed to get azure-creds credentials",
Target: fnv1.Target_TARGET_COMPOSITE.Enum(),
},
},
},
},
},
}

for name, tc := range cases {
Expand Down
3 changes: 3 additions & 0 deletions input/v1beta1/input.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,7 @@ type Input struct {

// Query to Azure Resource Graph API
Query string `json:"query"`
// Azure management groups against which to execute the query. Example: [ 'mg1', 'mg2' ]
// +optional
ManagementGroups []*string `json:"managementGroups,omitempty"`
}
11 changes: 11 additions & 0 deletions input/v1beta1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions package/input/azresourcegraph.fn.crossplane.io_inputs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ spec:
In CamelCase.
More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds
type: string
managementGroups:
description: 'Azure management groups against which to execute the query.
Example: [ ''mg1'', ''mg2'' ]'
items:
type: string
type: array
metadata:
type: object
query:
Expand Down
Loading