Skip to content

Commit

Permalink
Add change api provider and get all apis rest api.
Browse files Browse the repository at this point in the history
  • Loading branch information
shnrndk committed Nov 28, 2023
1 parent 7aa2c32 commit e3cb8aa
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 63 deletions.
9 changes: 5 additions & 4 deletions portals/admin/src/main/webapp/site/public/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -88,14 +88,14 @@
"Apis.Details.Scopes.CreateScope.roles.help": "Enter a valid role and press `Enter`.",
"Apis.Details.Scopes.Roles.Invalid": "A Role is invalid",
"Apis.Listing.Listing.apis.search": "Search",
"Apis.Listing.Listing.apis.search.label": "Search Api by Api Owner",
"Apis.Listing.Listing.apis.search.label": "Search Api by Api Provider",
"Apis.Listing.Listing.apis.searching": "Searching",
"Apis.Listing.Listing.clear.search": "Clear Search",
"Apis.Listing.Listing.empty.message": "No Data to Display",
"Apis.Listing.Listing.search.placeholder": "Api Owner",
"Apis.Listing.Listing.search.placeholder": "Api Provider",
"Apis.Listing.apiTableHead.actions": "Actions",
"Apis.Listing.apiTableHead.name": "Name",
"Apis.Listing.apiTableHead.owner": "Owner",
"Apis.Listing.apiTableHead.provider": "Provider",
"Apis.Shared.AdminRootErrorBoundary.refresh": "Refresh",
"Apis.Shared.AdminRootErrorBoundary.refresh.or.try.again.message": "You may refresh the page now or try again later",
"Apis.Shared.AdminRootErrorBoundary.something.went.wrong.while.rendering.button": "Something went wrong while rendering the",
Expand Down Expand Up @@ -130,7 +130,8 @@
"Base.RouteMenuMapping.application.creation": "Application Creation",
"Base.RouteMenuMapping.application.deletion": "Application Deletion",
"Base.RouteMenuMapping.application.throttling.policies": "Application Policies",
"Base.RouteMenuMapping.applications": "Applications",
"Base.RouteMenuMapping.applications": "Change Application Owner",
"Base.RouteMenuMapping.apis": "Change Api Provder",
"Base.RouteMenuMapping.apps": "Apps",
"Base.RouteMenuMapping.blacklisted.items": "Deny Policies",
"Base.RouteMenuMapping.custom.throttling.policies": "Custom Policies",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ class ApisTableContent extends Component {
<StyledTableCell align='left'>
{app.name}
</StyledTableCell>
<StyledTableCell align='left'>{app.owner}</StyledTableCell>
<StyledTableCell align='left'>{app.provider}</StyledTableCell>
<StyledTableCell align='left'>
<EditApi
dataRow={app}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,12 @@ const apisTableHead = (props) => {
sorting: true,
},
{
id: 'owner',
id: 'provider',
numeric: false,
disablePadding: false,
label: (<FormattedMessage
id='Apis.Listing.apiTableHead.owner'
defaultMessage='Owner'
id='Apis.Listing.apiTableHead.provider'
defaultMessage='Provider'
/>),
sorting: true,
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ const useStyles = makeStyles((theme) => ({
function reducer(state, { field, value }) {
switch (field) {
case 'name':
case 'owner':
case 'provider':
return { ...state, [field]: value };
case 'editDetails':
return value;
Expand All @@ -47,7 +47,7 @@ function reducer(state, { field, value }) {
}
}
/**
* Render a pop-up dialog to change ownership of an Api
* Render a pop-up dialog to change providership of an Api
* @param {JSON} props props passed from parent
* @returns {JSX}.
*/
Expand All @@ -59,45 +59,45 @@ function EditApi(props) {
} = props;
const [initialState, setInitialState] = useState({
name: '',
owner: '',
provider: '',
});

const [state, dispatch] = useReducer(reducer, initialState);
const { name, owner } = state;
const { name, provider } = state;

useEffect(() => {
setInitialState({
name: '',
owner: '',
provider: '',
});
}, []);
const onChange = (e) => {
dispatch({ field: e.target.name, value: e.target.value });
};

const validateOwner = () => {
const validateProvider = () => {

Check failure on line 78 in portals/admin/src/main/webapp/source/src/app/components/APISettings/EditApi.jsx

View workflow job for this annotation

GitHub Actions / build-product

'validateProvider' is assigned a value but never used
let validationError = 'Something went wrong when validating user';

const apisWithSameName = apiList.filter(
(app) => app.name === name && app.owner === owner,
(app) => app.name === name && app.provider === provider,
);

const promiseValidation = new Promise((resolve, reject) => {
if (apisWithSameName.length > 0) {
validationError = `${owner} already has an api with name: ${name}`;
validationError = `${provider} already has an api with name: ${name}`;
reject(validationError);
}
const basicScope = 'apim:subscribe';
restApi.getUserScope(owner, basicScope)
restApi.getUserScope(provider, basicScope)
.then(() => {
// This api returns 200 when only the $owner has the $basicScope.
// This api returns 200 when only the $provider has the $basicScope.
resolve();
}).catch((error) => {
const { response } = error;
// This api returns 404 when the $owner is not found.
// This api returns 404 when the $provider is not found.
// error codes: 901502, 901500 for user not found and scope not found
if (response?.body?.code === 901502 || response?.body?.code === 901500) {
validationError = `${owner} is not a valid Subscriber`;
validationError = `${provider} is not a valid Subscriber`;
}
}).finally(() => {
if (validationError) {
Expand All @@ -110,35 +110,33 @@ function EditApi(props) {
};

const formSaveCallback = () => {
return validateOwner().then(() => {
return restApi.updateApiOwner(dataRow.apiId, owner)
.then(() => {
return (
<FormattedMessage
id='AdminPages.ApiSettings.EditApi.form.edit.successful'
defaultMessage='Api owner changed successfully'
/>
);
})
.catch((error) => {
const { response } = error;
if (response?.body?.code === 500) {
const notValidSubscriber = 'Error while updating ownership to ' + owner;
throw notValidSubscriber;
} else {
const updateError = 'Something went wrong when updating owner';
throw updateError;
}
})
.finally(() => {
updateList();
});
});
return restApi.updateApiProvider(dataRow.id, provider)
.then(() => {
return (
<FormattedMessage
id='AdminPages.ApiSettings.EditApi.form.edit.successful'
defaultMessage='Api provider changed successfully'
/>
);
})
.catch((error) => {
const { response } = error;
if (response?.body?.code === 500) {
const notValidSubscriber = 'Error while updating providership to ' + provider;
throw notValidSubscriber;
} else {
const updateError = 'Something went wrong when updating provider';
throw updateError;
}
})
.finally(() => {
updateList();
});
};
const dialogOpenCallback = () => {
if (dataRow) {
const { name: originalName, owner: originalOwner } = dataRow;
dispatch({ field: 'editDetails', value: { name: originalName, owner: originalOwner } });
const { name: originalName, provider: originalProvider } = dataRow;
dispatch({ field: 'editDetails', value: { name: originalName, provider: originalProvider } });
}
};
return (
Expand Down Expand Up @@ -170,16 +168,16 @@ function EditApi(props) {
<TextField
autoFocus
margin='dense'
name='owner'
value={owner}
name='provider'
value={provider}
onChange={onChange}
label='Owner'
label='Provider'
fullWidth
helperText={(
<FormattedMessage
id='AdminPages.ApiSettings.EditApi.form.helperText'
defaultMessage={'Enter a new Owner. '
+ 'Make sure the new owner has logged into the Developer Portal at least once'}
defaultMessage={'Enter a new Provider. '
+ 'Make sure the new provider has logged into the Developer Portal at least once'}
/>
)}
variant='outlined'
Expand All @@ -195,8 +193,8 @@ EditApi.defaultProps = {
EditApi.propTypes = {
updateList: PropTypes.func.isRequired,
dataRow: PropTypes.shape({
apiId: PropTypes.string.isRequired,
owner: PropTypes.string.isRequired,
id: PropTypes.string.isRequired,
provider: PropTypes.string.isRequired,
name: PropTypes.string.isRequired,
}).isRequired,
icon: PropTypes.element,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,17 +72,17 @@ export default function ListApis() {
const [totalApps, setTotalApps] = useState(0);
const [rowsPerPage, setRowsPerPage] = useState(10);
const [page, setPage] = useState(0);
const [owner, setOwner] = useState('');
const [provider, setProvider] = useState('');

/**
* API call to get api list
* @returns {Promise}.
*/
function apiCall(pageNo, user = owner) {
function apiCall(pageNo, user = provider) {
setLoading(true);
const restApi = new API();
return restApi
.getApplicationList({ limit: rowsPerPage, offset: pageNo * rowsPerPage, user })
.getApiList({ limit: rowsPerPage, offset: pageNo * rowsPerPage, user })
.then((result) => {
setApiList(result.body.list);
const { pagination: { total } } = result.body;
Expand Down Expand Up @@ -129,7 +129,7 @@ export default function ListApis() {

function clearSearch() {
setPage(0);
setOwner('');
setProvider('');
apiCall(page, '').then((result) => {
setApiList(result);
});
Expand All @@ -140,7 +140,7 @@ export default function ListApis() {
if (newQuery === '') {
clearSearch();
} else {
setOwner(newQuery);
setProvider(newQuery);
}
}

Expand All @@ -166,22 +166,22 @@ export default function ListApis() {
fullWidth
id='search-label'
label={intl.formatMessage({
defaultMessage: 'Search Api by Api Owner',
defaultMessage: 'Search Api by Api Provider',
id: 'Apis.Listing.Listing.apis.search.label',
})}
placeholder={intl.formatMessage({
defaultMessage: 'Api Owner',
defaultMessage: 'Api Provider',
id: 'Apis.Listing.Listing.search.placeholder',
})}
InputProps={{
disableUnderline: true,
className: classes.searchInput,
}}
value={owner}
value={provider}
onChange={setQuery}
// onKeyPress={this.handleSearchKeyPress}
/>
{ owner.length > 0
{ provider.length > 0
&& (
<Tooltip
title={
Expand Down Expand Up @@ -236,7 +236,7 @@ export default function ListApis() {
rowsPerPage={rowsPerPage}
editComponentProps={{
icon: <EditIcon aria-label='edit-api-settings' />,
title: 'Change Api Owner',
title: 'Change Api Provider',
apiList,
}}
EditComponent={EditApi}
Expand Down
23 changes: 23 additions & 0 deletions portals/admin/src/main/webapp/source/src/app/data/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,17 @@ class API extends Resource {
});
}

/**
* Get a list of apis from all users
*/
getApiList(params) {
return this.client.then((client) => {
return client.apis['APIs'].getAllAPIs(
params, this._requestMetaData(),
);
});
}

/**
* Get Subscription Throttling Policies
*/
Expand All @@ -445,6 +456,18 @@ class API extends Resource {
});
}

/**
* Update an api's owner
*/
updateApiProvider(apiId, provider) {
return this.client.then((client) => {
return client.apis["Api Provider Change"].providerProviderNameApisApiIdPut(
{ providerName: provider, apiId: apiId },
this._requestMetaData(),
);
});
}

/**
* Get a list of available Gateway Environments
*/
Expand Down

0 comments on commit e3cb8aa

Please sign in to comment.