Skip to content

Commit 2795a23

Browse files
committed
Add account selection dropdown
1 parent dd9f03c commit 2795a23

2 files changed

Lines changed: 51 additions & 13 deletions

File tree

packages/blocks/aws/src/lib/actions/get-price-action.ts

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import { Property, createAction } from '@openops/blocks-framework';
33
import {
44
amazonAuth,
55
getAttributeValues,
6+
getAwsAccountsSingleSelectDropdown,
7+
getCredentialsForAccount,
68
getCredentialsFromAuth,
79
getPriceListWithCache,
810
getServices,
@@ -17,21 +19,25 @@ export const getPriceAction = createAction({
1719
displayName: 'Get Price from Price Catalog',
1820
isWriteAction: false,
1921
props: {
22+
account: getAwsAccountsSingleSelectDropdown().accounts,
2023
service: Property.Dropdown({
2124
displayName: 'Service Code',
2225
description: 'Service code for which to fetch the price',
23-
refreshers: ['auth'],
26+
refreshers: ['auth', 'account', 'account.accounts'],
2427
required: true,
25-
options: async ({ auth }: any) => {
26-
if (!auth) {
28+
options: async ({ auth, account }: any) => {
29+
if (!auth || !account) {
2730
return {
2831
disabled: true,
2932
options: [],
30-
placeholder: 'Please authenticate first',
33+
placeholder: 'Please authenticate first and select account',
3134
};
3235
}
3336

34-
const credentials = await getCredentialsFromAuth(auth);
37+
const credentials = await getCredentialsForAccount(
38+
auth,
39+
account?.['accounts'],
40+
);
3541
try {
3642
const services = await getServices(credentials, PRICING_REGION);
3743

@@ -66,9 +72,9 @@ export const getPriceAction = createAction({
6672
displayName: '',
6773
description: '',
6874
required: true,
69-
refreshers: ['auth', 'service'],
70-
props: async ({ auth, service }, { input }) => {
71-
if (!auth || !service) {
75+
refreshers: ['auth', 'account', 'account.accounts', 'service'],
76+
props: async ({ auth, account, service }, { input }) => {
77+
if (!auth || !account || !service) {
7278
return {};
7379
}
7480

@@ -101,7 +107,10 @@ export const getPriceAction = createAction({
101107
};
102108
}
103109

104-
const credentials = await getCredentialsFromAuth(auth);
110+
const credentials = await getCredentialsForAccount(
111+
auth,
112+
account?.['accounts'],
113+
);
105114
try {
106115
const attributeValues: AttributeValue[] =
107116
await getAttributeValues(
@@ -139,15 +148,17 @@ export const getPriceAction = createAction({
139148
},
140149
async run(context) {
141150
try {
142-
const { service, queryFilters } = context.propsValue;
151+
const { account, service, queryFilters } = context.propsValue;
143152
const filters = queryFilters['queryFilters'].map((filter: any) => {
144153
return {
145154
Field: filter.attributeName,
146155
Type: FilterType.TERM_MATCH,
147156
Value: filter.attributeValue,
148157
};
149158
});
150-
const credentials = await getCredentialsFromAuth(context.auth);
159+
const credentials = account?.['accounts']
160+
? await getCredentialsForAccount(context.auth, account['accounts'])
161+
: await getCredentialsFromAuth(context.auth);
151162

152163
const priceList = getPriceListWithCache(
153164
credentials,

packages/blocks/aws/test/get-price-action.test.ts

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
const openopsCommonMock = {
22
...jest.requireActual('@openops/common'),
33
getCredentialsFromAuth: jest.fn(),
4+
getCredentialsForAccount: jest.fn(),
45
getAttributeValues: jest.fn(),
56
getServices: jest.fn(),
67
getPriceListWithCache: jest.fn(),
@@ -18,6 +19,9 @@ describe('getPriceAction', () => {
1819
openopsCommonMock.getCredentialsFromAuth.mockResolvedValue({
1920
someCreds: 'some value',
2021
});
22+
openopsCommonMock.getCredentialsForAccount.mockResolvedValue({
23+
someCreds: 'some value',
24+
});
2125
});
2226

2327
const auth = {
@@ -34,12 +38,16 @@ describe('getPriceAction', () => {
3438
},
3539
auth: auth,
3640
propsValue: {
37-
accountId: 'some account id',
41+
account: { accounts: 'some account id' },
3842
},
3943
};
4044

4145
test('should create action with correct properties', () => {
4246
expect(getPriceAction.props).toMatchObject({
47+
account: {
48+
type: 'DYNAMIC',
49+
required: true,
50+
},
4351
service: {
4452
type: 'DROPDOWN',
4553
required: true,
@@ -85,17 +93,35 @@ describe('getPriceAction', () => {
8593
expectedFilters,
8694
'us-east-1',
8795
);
96+
expect(openopsCommonMock.getCredentialsForAccount).toHaveBeenCalledWith(
97+
auth,
98+
'some account id',
99+
);
88100
},
89101
);
90102

103+
test('should fallback to getCredentialsFromAuth when account is missing in run', async () => {
104+
openopsCommonMock.getPriceListWithCache.mockResolvedValue('mockResult');
105+
context.propsValue = {
106+
service: { ServiceCode: 'some service' },
107+
queryFilters: { queryFilters: [] },
108+
};
109+
110+
const result = (await getPriceAction.run(context)) as any;
111+
112+
expect(result).toEqual('mockResult');
113+
expect(openopsCommonMock.getCredentialsFromAuth).toHaveBeenCalledWith(auth);
114+
expect(openopsCommonMock.getCredentialsForAccount).not.toHaveBeenCalled();
115+
});
116+
91117
test('should return the list of services in property service', async () => {
92118
openopsCommonMock.getServices.mockResolvedValue([
93119
{ ServiceCode: 'service1' },
94120
{ ServiceCode: 'service2' },
95121
]);
96122

97123
const result = await getPriceAction.props['service'].options(
98-
{ auth },
124+
{ auth, account: { accounts: 'some account id' } },
99125
context,
100126
);
101127

@@ -118,6 +144,7 @@ describe('getPriceAction', () => {
118144
ServiceCode: 'some service',
119145
AttributeNames: ['some attibute'],
120146
};
147+
context.propsValue.account = { accounts: 'some account id' };
121148
context.propsValue.auth = auth;
122149
context.input = { attributeName: 'some attibute' };
123150

0 commit comments

Comments
 (0)