@@ -3,93 +3,154 @@ import { getAzureSubscriptionsStaticDropdown } from '@openops/common';
33import { runCommand } from './azure-cli' ;
44import { getAzureErrorMessage } from './error-helper' ;
55
6- export const subDropdown = Property . DynamicProperties ( {
7- displayName : '' ,
6+ export interface SubscriptionDropdownConfig {
7+ displayName : string ;
8+ description : string ;
9+ required : boolean ;
10+ multiSelect : boolean ;
11+ preselectAll ?: boolean ;
12+ }
13+
14+ const SINGLE_SELECT_CONFIG : SubscriptionDropdownConfig = {
15+ displayName : 'Subscriptions' ,
16+ description : 'Select a single subscription from the list' ,
817 required : true ,
9- refreshers : [
10- 'auth' ,
11- 'useHostSession' ,
12- 'useHostSession.useHostSessionCheckbox' ,
13- ] ,
14- props : async ( { auth, useHostSession } ) => {
15- let subDropdown ;
16- try {
17- if ( useHostSession ?. [ 'useHostSessionCheckbox' ] as unknown as boolean ) {
18- subDropdown = await getSubscriptionsDropdownForHostSession ( auth ) ;
19- } else {
20- if ( ! auth ) {
18+ multiSelect : false ,
19+ } ;
20+
21+ const MULTI_SELECT_CONFIG : SubscriptionDropdownConfig = {
22+ displayName : 'Subscriptions' ,
23+ description : 'Select subscriptions to perform the action on' ,
24+ required : false ,
25+ multiSelect : true ,
26+ } ;
27+
28+ async function fetchSubscriptionsFromHostSession ( auth : any ) {
29+ const result = await runCommand (
30+ 'account list --only-show-errors' ,
31+ auth ,
32+ true ,
33+ undefined ,
34+ ) ;
35+ return JSON . parse ( result ) ;
36+ }
37+
38+ function createSubscriptionDropdown (
39+ config : SubscriptionDropdownConfig ,
40+ options : any ,
41+ ) {
42+ const PropertyType = config . multiSelect
43+ ? Property . StaticMultiSelectDropdown
44+ : Property . StaticDropdown ;
45+
46+ const base = {
47+ displayName : config . displayName ,
48+ description : config . description ,
49+ required : config . required ,
50+ options,
51+ } as any ;
52+
53+ if ( config . multiSelect && config . preselectAll && options ?. options ?. length ) {
54+ return PropertyType ( {
55+ ...base ,
56+ defaultValue : options . options . map ( ( o : any ) => o . value ) ,
57+ } ) ;
58+ }
59+
60+ return PropertyType ( base ) ;
61+ }
62+
63+ async function getSubscriptionsDropdown (
64+ auth : any ,
65+ config : SubscriptionDropdownConfig ,
66+ ) {
67+ try {
68+ const parsedSubscriptions = await fetchSubscriptionsFromHostSession ( auth ) ;
69+
70+ return createSubscriptionDropdown ( config , {
71+ disabled : false ,
72+ options : parsedSubscriptions . map ( ( obj : { id : string ; name : string } ) => ( {
73+ label : obj . name ,
74+ value : obj . id ,
75+ } ) ) ,
76+ } ) ;
77+ } catch ( error ) {
78+ return createSubscriptionDropdown ( config , {
79+ disabled : true ,
80+ options : [ ] ,
81+ placeholder : 'Something went wrong fetching subscriptions' ,
82+ error : `${ error } ` ,
83+ } ) ;
84+ }
85+ }
86+
87+ export function createSubscriptionDynamicProperty (
88+ config : SubscriptionDropdownConfig ,
89+ propertyKey : string ,
90+ ) {
91+ return Property . DynamicProperties ( {
92+ displayName : '' ,
93+ required : true ,
94+ refreshers : [
95+ 'auth' ,
96+ 'useHostSession' ,
97+ 'useHostSession.useHostSessionCheckbox' ,
98+ ] ,
99+ props : async ( { auth, useHostSession } ) => {
100+ try {
101+ const useHost = useHostSession ?. [ 'useHostSessionCheckbox' ] as
102+ | boolean
103+ | undefined ;
104+
105+ if ( ! auth && ! useHost ) {
21106 return {
22- subDropdown : Property . StaticDropdown ( {
23- displayName : 'Subscriptions' ,
24- description : 'Select a single subscription from the list' ,
25- required : true ,
26- options : {
27- disabled : true ,
28- options : [ ] ,
29- placeholder : 'Please authenticate first' ,
30- } ,
107+ [ propertyKey ] : createSubscriptionDropdown ( config , {
108+ disabled : true ,
109+ options : [ ] ,
110+ placeholder : 'Please authenticate first' ,
31111 } ) ,
32112 } ;
33113 }
34114
35- subDropdown = await getAzureSubscriptionsStaticDropdown ( auth ) ;
115+ let dropdown ;
116+ if ( useHost ) {
117+ dropdown = await getSubscriptionsDropdown ( auth , config ) ;
118+ } else {
119+ const staticDropdown = await getAzureSubscriptionsStaticDropdown (
120+ auth ,
121+ ) ;
122+ dropdown = createSubscriptionDropdown ( config , staticDropdown . options ) ;
123+ }
124+
125+ return { [ propertyKey ] : dropdown } ;
126+ } catch ( error ) {
127+ return {
128+ [ propertyKey ] : createSubscriptionDropdown ( config , {
129+ disabled : true ,
130+ options : [ ] ,
131+ placeholder : 'Something went wrong fetching subscriptions' ,
132+ error : getAzureErrorMessage ( error ) ,
133+ } ) ,
134+ } ;
36135 }
37- } catch ( error ) {
38- subDropdown = Property . StaticDropdown ( {
39- displayName : 'Subscriptions' ,
40- description : 'Select a single subscription from the list' ,
41- required : true ,
42- options : {
43- disabled : true ,
44- options : [ ] ,
45- placeholder : `Something went wrong fetching subscriptions` ,
46- error : getAzureErrorMessage ( error ) ,
47- } ,
48- } ) ;
49- }
50-
51- return {
52- subDropdown : subDropdown ,
53- } ;
54- } ,
55- } ) ;
136+ } ,
137+ } ) ;
138+ }
139+
140+ export const subDropdown = createSubscriptionDynamicProperty (
141+ SINGLE_SELECT_CONFIG ,
142+ 'subDropdown' ,
143+ ) ;
144+
145+ export const subMultiSelectDropdown = createSubscriptionDynamicProperty (
146+ MULTI_SELECT_CONFIG ,
147+ 'subMultiSelect' ,
148+ ) ;
56149
57150export async function getSubscriptionsDropdownForHostSession ( auth : any ) {
58- try {
59- const result = await runCommand (
60- 'account list --only-show-errors' ,
61- auth ,
62- true ,
63- undefined ,
64- ) ;
65-
66- const parsedSubscriptions = JSON . parse ( result ) ;
67-
68- return Property . StaticDropdown ( {
69- displayName : 'Subscriptions' ,
70- description : 'Select a single subscription from the list' ,
71- required : true ,
72- options : {
73- disabled : false ,
74- options : parsedSubscriptions . map (
75- ( obj : { id : string ; name : string } ) => ( {
76- label : obj . name ,
77- value : obj . id ,
78- } ) ,
79- ) ,
80- } ,
81- } ) ;
82- } catch ( error ) {
83- return Property . StaticDropdown ( {
84- displayName : 'Subscriptions' ,
85- description : 'Select a single subscription from the list' ,
86- required : true ,
87- options : {
88- disabled : true ,
89- options : [ ] ,
90- placeholder : `Something went wrong fetching subscriptions` ,
91- error : `${ error } ` ,
92- } ,
93- } ) ;
94- }
151+ return getSubscriptionsDropdown ( auth , SINGLE_SELECT_CONFIG ) ;
152+ }
153+
154+ export async function getSubscriptionsMultiSelectForHostSession ( auth : any ) {
155+ return getSubscriptionsDropdown ( auth , MULTI_SELECT_CONFIG ) ;
95156}
0 commit comments