Move stored procedure operations to RP (#281)

- move read, delete, create, and update stored procedure calls to RP
- fixed a bug where the console message never clears when reading offers with SDK
This commit is contained in:
victor-meng 2020-10-15 11:10:20 -07:00 committed by GitHub
parent bd00e5eb9b
commit 9d50577800
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 124 additions and 27 deletions

View File

@ -1,28 +1,53 @@
import { AuthType } from "../../AuthType";
import { Resource, StoredProcedureDefinition } from "@azure/cosmos"; import { Resource, StoredProcedureDefinition } from "@azure/cosmos";
import { logConsoleError, logConsoleProgress } from "../../Utils/NotificationConsoleUtils"; import {
SqlStoredProcedureCreateUpdateParameters,
SqlStoredProcedureResource
} from "../../Utils/arm/generatedClients/2020-04-01/types";
import { client } from "../CosmosClient"; import { client } from "../CosmosClient";
import { createUpdateSqlStoredProcedure } from "../../Utils/arm/generatedClients/2020-04-01/sqlResources";
import { logConsoleError, logConsoleProgress } from "../../Utils/NotificationConsoleUtils";
import { logError } from "../Logger"; import { logError } from "../Logger";
import { sendNotificationForError } from "./sendNotificationForError"; import { sendNotificationForError } from "./sendNotificationForError";
import { userContext } from "../../UserContext";
export async function createStoredProcedure( export async function createStoredProcedure(
databaseId: string, databaseId: string,
collectionId: string, collectionId: string,
storedProcedure: StoredProcedureDefinition storedProcedure: StoredProcedureDefinition
): Promise<StoredProcedureDefinition & Resource> { ): Promise<StoredProcedureDefinition & Resource> {
let createdStoredProcedure: StoredProcedureDefinition & Resource;
const clearMessage = logConsoleProgress(`Creating stored procedure ${storedProcedure.id}`); const clearMessage = logConsoleProgress(`Creating stored procedure ${storedProcedure.id}`);
try { try {
if (window.authType === AuthType.AAD && !userContext.useSDKOperations) {
const createSprocParams: SqlStoredProcedureCreateUpdateParameters = {
properties: {
resource: storedProcedure as SqlStoredProcedureResource,
options: {}
}
};
const rpResponse = await createUpdateSqlStoredProcedure(
userContext.subscriptionId,
userContext.resourceGroup,
userContext.databaseAccount.name,
databaseId,
collectionId,
storedProcedure.id,
createSprocParams
);
return rpResponse && (rpResponse.properties?.resource as StoredProcedureDefinition & Resource);
}
const response = await client() const response = await client()
.database(databaseId) .database(databaseId)
.container(collectionId) .container(collectionId)
.scripts.storedProcedures.create(storedProcedure); .scripts.storedProcedures.create(storedProcedure);
createdStoredProcedure = response.resource; return response?.resource;
} catch (error) { } catch (error) {
logConsoleError(`Error while creating stored procedure ${storedProcedure.id}:\n ${JSON.stringify(error)}`); logConsoleError(`Error while creating stored procedure ${storedProcedure.id}:\n ${JSON.stringify(error)}`);
logError(JSON.stringify(error), "CreateStoredProcedure", error.code); logError(JSON.stringify(error), "CreateStoredProcedure", error.code);
sendNotificationForError(error); sendNotificationForError(error);
throw error;
} finally {
clearMessage();
} }
clearMessage();
return createdStoredProcedure;
} }

View File

@ -1,7 +1,10 @@
import { logConsoleError, logConsoleProgress } from "../../Utils/NotificationConsoleUtils"; import { AuthType } from "../../AuthType";
import { client } from "../CosmosClient"; import { client } from "../CosmosClient";
import { deleteSqlStoredProcedure } from "../../Utils/arm/generatedClients/2020-04-01/sqlResources";
import { logConsoleError, logConsoleProgress } from "../../Utils/NotificationConsoleUtils";
import { logError } from "../Logger"; import { logError } from "../Logger";
import { sendNotificationForError } from "./sendNotificationForError"; import { sendNotificationForError } from "./sendNotificationForError";
import { userContext } from "../../UserContext";
export async function deleteStoredProcedure( export async function deleteStoredProcedure(
databaseId: string, databaseId: string,
@ -10,17 +13,28 @@ export async function deleteStoredProcedure(
): Promise<void> { ): Promise<void> {
const clearMessage = logConsoleProgress(`Deleting stored procedure ${storedProcedureId}`); const clearMessage = logConsoleProgress(`Deleting stored procedure ${storedProcedureId}`);
try { try {
await client() if (window.authType === AuthType.AAD && !userContext.useSDKOperations) {
.database(databaseId) await deleteSqlStoredProcedure(
.container(collectionId) userContext.subscriptionId,
.scripts.storedProcedure(storedProcedureId) userContext.resourceGroup,
.delete(); userContext.databaseAccount.name,
databaseId,
collectionId,
storedProcedureId
);
} else {
await client()
.database(databaseId)
.container(collectionId)
.scripts.storedProcedure(storedProcedureId)
.delete();
}
} catch (error) { } catch (error) {
logConsoleError(`Error while deleting stored procedure ${storedProcedureId}:\n ${JSON.stringify(error)}`); logConsoleError(`Error while deleting stored procedure ${storedProcedureId}:\n ${JSON.stringify(error)}`);
logError(JSON.stringify(error), "DeleteStoredProcedure", error.code); logError(JSON.stringify(error), "DeleteStoredProcedure", error.code);
sendNotificationForError(error); sendNotificationForError(error);
throw error;
} finally {
clearMessage();
} }
clearMessage();
return undefined;
} }

View File

@ -13,10 +13,13 @@ export const readOffers = async (): Promise<Offer[]> => {
const clearMessage = logConsoleProgress(`Querying offers`); const clearMessage = logConsoleProgress(`Querying offers`);
try { try {
if (configContext.platform === Platform.Portal) { if (configContext.platform === Platform.Portal) {
return sendCachedDataMessage<Offer[]>(MessageTypes.AllOffers, [ const offers = sendCachedDataMessage<Offer[]>(MessageTypes.AllOffers, [
userContext.databaseAccount.id, userContext.databaseAccount.id,
ClientDefaults.portalCacheTimeoutMs ClientDefaults.portalCacheTimeoutMs
]); ]);
clearMessage();
return offers;
} }
} catch (error) { } catch (error) {
// If error getting cached Offers, continue on and read via SDK // If error getting cached Offers, continue on and read via SDK

View File

@ -1,27 +1,41 @@
import { AuthType } from "../../AuthType";
import { Resource, StoredProcedureDefinition } from "@azure/cosmos"; import { Resource, StoredProcedureDefinition } from "@azure/cosmos";
import { logConsoleError, logConsoleProgress } from "../../Utils/NotificationConsoleUtils";
import { client } from "../CosmosClient"; import { client } from "../CosmosClient";
import { listSqlStoredProcedures } from "../../Utils/arm/generatedClients/2020-04-01/sqlResources";
import { logConsoleError, logConsoleProgress } from "../../Utils/NotificationConsoleUtils";
import { logError } from "../Logger"; import { logError } from "../Logger";
import { sendNotificationForError } from "./sendNotificationForError"; import { sendNotificationForError } from "./sendNotificationForError";
import { userContext } from "../../UserContext";
export async function readStoredProcedures( export async function readStoredProcedures(
databaseId: string, databaseId: string,
collectionId: string collectionId: string
): Promise<(StoredProcedureDefinition & Resource)[]> { ): Promise<(StoredProcedureDefinition & Resource)[]> {
let sprocs: (StoredProcedureDefinition & Resource)[];
const clearMessage = logConsoleProgress(`Querying stored procedures for container ${collectionId}`); const clearMessage = logConsoleProgress(`Querying stored procedures for container ${collectionId}`);
try { try {
if (window.authType === AuthType.AAD && !userContext.useSDKOperations) {
const rpResponse = await listSqlStoredProcedures(
userContext.subscriptionId,
userContext.resourceGroup,
userContext.databaseAccount.name,
databaseId,
collectionId
);
return rpResponse?.value?.map(sproc => sproc.properties?.resource as StoredProcedureDefinition & Resource);
}
const response = await client() const response = await client()
.database(databaseId) .database(databaseId)
.container(collectionId) .container(collectionId)
.scripts.storedProcedures.readAll() .scripts.storedProcedures.readAll()
.fetchAll(); .fetchAll();
sprocs = response.resources; return response?.resources;
} catch (error) { } catch (error) {
logConsoleError(`Failed to query stored procedures for container ${collectionId}: ${JSON.stringify(error)}`); logConsoleError(`Failed to query stored procedures for container ${collectionId}: ${JSON.stringify(error)}`);
logError(JSON.stringify(error), "ReadStoredProcedures", error.code); logError(JSON.stringify(error), "ReadStoredProcedures", error.code);
sendNotificationForError(error); sendNotificationForError(error);
throw error;
} finally {
clearMessage();
} }
clearMessage();
return sprocs;
} }

View File

@ -1,29 +1,70 @@
import { AuthType } from "../../AuthType";
import { Resource, StoredProcedureDefinition } from "@azure/cosmos"; import { Resource, StoredProcedureDefinition } from "@azure/cosmos";
import { logConsoleError, logConsoleProgress } from "../../Utils/NotificationConsoleUtils"; import {
SqlStoredProcedureCreateUpdateParameters,
SqlStoredProcedureResource
} from "../../Utils/arm/generatedClients/2020-04-01/types";
import { client } from "../CosmosClient"; import { client } from "../CosmosClient";
import { logConsoleError, logConsoleProgress } from "../../Utils/NotificationConsoleUtils";
import {
createUpdateSqlStoredProcedure,
getSqlStoredProcedure
} from "../../Utils/arm/generatedClients/2020-04-01/sqlResources";
import { logError } from "../Logger"; import { logError } from "../Logger";
import { sendNotificationForError } from "./sendNotificationForError"; import { sendNotificationForError } from "./sendNotificationForError";
import { userContext } from "../../UserContext";
export async function updateStoredProcedure( export async function updateStoredProcedure(
databaseId: string, databaseId: string,
collectionId: string, collectionId: string,
storedProcedure: StoredProcedureDefinition storedProcedure: StoredProcedureDefinition
): Promise<StoredProcedureDefinition & Resource> { ): Promise<StoredProcedureDefinition & Resource> {
let updatedStoredProcedure: StoredProcedureDefinition & Resource;
const clearMessage = logConsoleProgress(`Updating stored procedure ${storedProcedure.id}`); const clearMessage = logConsoleProgress(`Updating stored procedure ${storedProcedure.id}`);
try { try {
if (window.authType === AuthType.AAD && !userContext.useSDKOperations) {
const getResponse = await getSqlStoredProcedure(
userContext.subscriptionId,
userContext.resourceGroup,
userContext.databaseAccount.name,
databaseId,
collectionId,
storedProcedure.id
);
if (getResponse?.properties?.resource) {
const createSprocParams: SqlStoredProcedureCreateUpdateParameters = {
properties: {
resource: storedProcedure as SqlStoredProcedureResource,
options: {}
}
};
const rpResponse = await createUpdateSqlStoredProcedure(
userContext.subscriptionId,
userContext.resourceGroup,
userContext.databaseAccount.name,
databaseId,
collectionId,
storedProcedure.id,
createSprocParams
);
return rpResponse && (rpResponse.properties?.resource as StoredProcedureDefinition & Resource);
}
throw new Error(`Failed to update stored procedure: ${storedProcedure.id} does not exist.`);
}
const response = await client() const response = await client()
.database(databaseId) .database(databaseId)
.container(collectionId) .container(collectionId)
.scripts.storedProcedure(storedProcedure.id) .scripts.storedProcedure(storedProcedure.id)
.replace(storedProcedure); .replace(storedProcedure);
updatedStoredProcedure = response.resource; return response?.resource;
} catch (error) { } catch (error) {
logConsoleError(`Error while updating stored procedure ${storedProcedure.id}:\n ${JSON.stringify(error)}`); logConsoleError(`Error while updating stored procedure ${storedProcedure.id}:\n ${JSON.stringify(error)}`);
logError(JSON.stringify(error), "UpdateStoredProcedure", error.code); logError(JSON.stringify(error), "UpdateStoredProcedure", error.code);
sendNotificationForError(error); sendNotificationForError(error);
throw error;
} finally {
clearMessage();
} }
clearMessage();
return updatedStoredProcedure;
} }