Skip to content

Commit 425cbcc

Browse files
authored
Merge pull request #1 from UpboundCare/mgmt_groups
Add support for managementGroups
2 parents a533620 + 977483a commit 425cbcc

File tree

7 files changed

+85
-9
lines changed

7 files changed

+85
-9
lines changed

.golangci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ linters-settings:
4343

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

4848
maligned:
4949
# print struct with more effective memory layout or not, false by default
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
apiVersion: apiextensions.crossplane.io/v1
2+
kind: Composition
3+
metadata:
4+
name: function-azresourcegraph
5+
spec:
6+
compositeTypeRef:
7+
apiVersion: example.crossplane.io/v1
8+
kind: XR
9+
mode: Pipeline
10+
pipeline:
11+
- step: query-azresourcegraph
12+
functionRef:
13+
name: function-azresourcegraph
14+
input:
15+
apiVersion: azresourcegraph.fn.crossplane.io/v1alpha1
16+
kind: Input
17+
query: "Resources | project name, location, type, id| where type =~ 'Microsoft.Compute/virtualMachines' | order by name desc"
18+
managementGroups: ["example"]
19+
credentials:
20+
- name: azure-creds
21+
source: Secret
22+
secretRef:
23+
namespace: upbound-system
24+
name: azure-account-creds

fn.go

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -92,14 +92,20 @@ func (f *Function) RunFunction(ctx context.Context, req *fnv1.RunFunctionRequest
9292
return rsp, nil
9393
}
9494

95-
// Create the query request, Run the query and get the results. Update the VM and subscriptionID details below.
96-
results, err := client.Resources(ctx,
97-
armresourcegraph.QueryRequest{
98-
Query: to.Ptr(in.Query),
99-
Subscriptions: []*string{
100-
to.Ptr(subscriptionID)},
101-
},
102-
nil)
95+
queryRequest := armresourcegraph.QueryRequest{
96+
Query: to.Ptr(in.Query),
97+
}
98+
99+
if len(subscriptionID) > 0 {
100+
queryRequest.Subscriptions = []*string{to.Ptr(subscriptionID)}
101+
}
102+
103+
if len(in.ManagementGroups) > 0 {
104+
queryRequest.ManagementGroups = in.ManagementGroups
105+
}
106+
107+
// Create the query request, Run the query and get the results.
108+
results, err := client.Resources(ctx, queryRequest, nil)
103109
if err != nil {
104110
response.Fatal(rsp, errors.Wrap(err, "failed to finish the request"))
105111
f.log.Info("FAILURE: ", "failure", fmt.Sprint(err))

fn_test.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,32 @@ func TestRunFunction(t *testing.T) {
5656
},
5757
},
5858
},
59+
"ResponseIsReturnedWithOptionalManagementGroups": {
60+
reason: "The Function should accept optional managmenetGroups input",
61+
args: args{
62+
req: &fnv1.RunFunctionRequest{
63+
Meta: &fnv1.RequestMeta{Tag: "hello"},
64+
Input: resource.MustStructJSON(`{
65+
"apiVersion": "azresourcegraph.fn.crossplane.io/v1alpha1",
66+
"kind": "Input",
67+
"query": "Resources| count",
68+
"managementGroups": ["test"]
69+
}`),
70+
},
71+
},
72+
want: want{
73+
rsp: &fnv1.RunFunctionResponse{
74+
Meta: &fnv1.ResponseMeta{Tag: "hello", Ttl: durationpb.New(response.DefaultTTL)},
75+
Results: []*fnv1.Result{
76+
{
77+
Severity: fnv1.Severity_SEVERITY_FATAL,
78+
Message: "failed to get azure-creds credentials",
79+
Target: fnv1.Target_TARGET_COMPOSITE.Enum(),
80+
},
81+
},
82+
},
83+
},
84+
},
5985
}
6086

6187
for name, tc := range cases {

input/v1beta1/input.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,7 @@ type Input struct {
2424

2525
// Query to Azure Resource Graph API
2626
Query string `json:"query"`
27+
// Azure management groups against which to execute the query. Example: [ 'mg1', 'mg2' ]
28+
// +optional
29+
ManagementGroups []*string `json:"managementGroups,omitempty"`
2730
}

input/v1beta1/zz_generated.deepcopy.go

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package/input/azresourcegraph.fn.crossplane.io_inputs.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,12 @@ spec:
3636
In CamelCase.
3737
More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds
3838
type: string
39+
managementGroups:
40+
description: 'Azure management groups against which to execute the query.
41+
Example: [ ''mg1'', ''mg2'' ]'
42+
items:
43+
type: string
44+
type: array
3945
metadata:
4046
type: object
4147
query:

0 commit comments

Comments
 (0)