Skip to content

Commit

Permalink
[EventGrid] Move to Core V2 Generator (Azure#16047)
Browse files Browse the repository at this point in the history
As part of the development of the new pipeline, event grid was hand
ported to use the new pipeline. Now that the code generator targets
the new pipeline, we can start using it to generate the code.

Since EventGrid includes the `/api/events` path segment in the
Endpoint, We need to do a small amount of post processing of the
generated code, to ensure `/api/events` is not appended to the
endpoint (we do this by setting an empty path in the operation spec,
which is as things were before moving over to the generator).

Fixes Azure#15823
  • Loading branch information
ellismg authored Jun 28, 2021
1 parent 21f28dc commit 5c5c2cf
Show file tree
Hide file tree
Showing 7 changed files with 261 additions and 204 deletions.
2 changes: 1 addition & 1 deletion sdk/eventgrid/eventgrid/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
},
"scripts": {
"audit": "node ../../../common/scripts/rush-audit.js && rimraf node_modules package-lock.json && npm i --package-lock-only 2>&1 && npm audit",
"build:autorest": "autorest ./swagger/README.md --typescript --v3",
"build:autorest": "autorest ./swagger/README.md --typescript --v3 && node ./scripts/setPathToEmpty.js",
"build:browser": "tsc -p . && cross-env ONLY_BROWSER=true rollup -c 2>&1",
"build:node": "tsc -p . && cross-env ONLY_NODE=true rollup -c 2>&1",
"build:samples": "echo Obsolete",
Expand Down
12 changes: 12 additions & 0 deletions sdk/eventgrid/eventgrid/scripts/setPathToEmpty.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
"use strict";
const fs = require("fs");

// The "endpoint" shown in the portal and CLI for an Azure Event Grid includes the "/api/events"
// path parameter. We need to ensure that we don't add another "/api/events" suffix, but there
// is not a way to express this in swagger. So, we post process the generated client to ensure
// that the operation spec we build has the empty string for the path to append.
console.log("Updating ./scr/generate/generateClient.ts path entries");
let data = fs.readFileSync("./src/generated/generatedClient.ts", "utf8");
data = data.replace(new RegExp('path: "/api/events"', "g"), 'path: ""');
fs.writeFileSync("./src/generated/generatedClient.ts", data, "utf8");
console.log("Done Updating ./scr/generate/generateClient.ts path entries");
34 changes: 20 additions & 14 deletions sdk/eventgrid/eventgrid/src/generated/generatedClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,20 @@
* Changes may cause incorrect behavior and will be lost if the code is regenerated.
*/

import { OperationOptions, OperationSpec, createSerializer } from "@azure/core-client";
import * as coreClient from "@azure/core-client";
import * as Parameters from "./models/parameters";
import * as Mappers from "./models/mappers";
import { GeneratedClientContext } from "./generatedClientContext";
import { GeneratedClientOptionalParams, EventGridEvent, CloudEvent } from "./models";
import {
GeneratedClientOptionalParams,
EventGridEvent,
GeneratedClientPublishEventsOptionalParams,
CloudEvent,
GeneratedClientPublishCloudEventEventsOptionalParams,
GeneratedClientPublishCustomEventEventsOptionalParams
} from "./models";

/** @hidden */
/** @internal */
export class GeneratedClient extends GeneratedClientContext {
/**
* Initializes a new instance of the GeneratedClient class.
Expand All @@ -31,9 +38,9 @@ export class GeneratedClient extends GeneratedClientContext {
publishEvents(
topicHostname: string,
events: EventGridEvent[],
options?: OperationOptions
options?: GeneratedClientPublishEventsOptionalParams
): Promise<void> {
return this.sendOperationRequest<void>(
return this.sendOperationRequest(
{ topicHostname, events, options },
publishEventsOperationSpec
);
Expand All @@ -48,9 +55,9 @@ export class GeneratedClient extends GeneratedClientContext {
publishCloudEventEvents(
topicHostname: string,
events: CloudEvent[],
options?: OperationOptions
options?: GeneratedClientPublishCloudEventEventsOptionalParams
): Promise<void> {
return this.sendOperationRequest<void>(
return this.sendOperationRequest(
{ topicHostname, events, options },
publishCloudEventEventsOperationSpec
);
Expand All @@ -65,19 +72,18 @@ export class GeneratedClient extends GeneratedClientContext {
publishCustomEventEvents(
topicHostname: string,
events: any[],
options?: OperationOptions
options?: GeneratedClientPublishCustomEventEventsOptionalParams
): Promise<void> {
return this.sendOperationRequest<void>(
return this.sendOperationRequest(
{ topicHostname, events, options },
publishCustomEventEventsOperationSpec
);
}
}
// Operation Specifications
const serializer = coreClient.createSerializer(Mappers, /* isXml */ false);

const serializer = createSerializer(Mappers, /* isXml */ false);

const publishEventsOperationSpec: OperationSpec = {
const publishEventsOperationSpec: coreClient.OperationSpec = {
path: "",
httpMethod: "POST",
responses: { 200: {}, default: {} },
Expand All @@ -88,7 +94,7 @@ const publishEventsOperationSpec: OperationSpec = {
mediaType: "json",
serializer
};
const publishCloudEventEventsOperationSpec: OperationSpec = {
const publishCloudEventEventsOperationSpec: coreClient.OperationSpec = {
path: "",
httpMethod: "POST",
responses: { 200: {}, default: {} },
Expand All @@ -99,7 +105,7 @@ const publishCloudEventEventsOperationSpec: OperationSpec = {
mediaType: "json",
serializer
};
const publishCustomEventEventsOperationSpec: OperationSpec = {
const publishCustomEventEventsOperationSpec: coreClient.OperationSpec = {
path: "",
httpMethod: "POST",
responses: { 200: {}, default: {} },
Expand Down
29 changes: 20 additions & 9 deletions sdk/eventgrid/eventgrid/src/generated/generatedClientContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,32 +6,43 @@
* Changes may cause incorrect behavior and will be lost if the code is regenerated.
*/

import { ServiceClient } from "@azure/core-client";
import * as coreClient from "@azure/core-client";
import { GeneratedClientOptionalParams } from "./models";

export class GeneratedClientContext extends ServiceClient {
/** @internal */
export class GeneratedClientContext extends coreClient.ServiceClient {
apiVersion: string;

/**
* Initializes a new instance of the GeneratedClientContext class.
* @param options The parameter options
*/
constructor(options: GeneratedClientOptionalParams = {}) {
constructor(options?: GeneratedClientOptionalParams) {
// Initializing default values for options
if (!options) {
options = {};
}
const defaults: GeneratedClientOptionalParams = {
requestContentType: "application/json; charset=utf-8"
};

const { endpoint, apiVersion, ...restOptions } = options;
const packageDetails = `azsdk-js-eventgrid/4.3.1`;
const userAgentPrefix =
options.userAgentOptions && options.userAgentOptions.userAgentPrefix
? `${options.userAgentOptions.userAgentPrefix} ${packageDetails}`
: `${packageDetails}`;

const optionsWithDefaults: GeneratedClientOptionalParams = {
const optionsWithDefaults = {
...defaults,
...restOptions,
baseUri: endpoint || "{topicHostname}"
...options,
userAgentOptions: {
userAgentPrefix
},
baseUri: options.endpoint || "{topicHostname}"
};

super(optionsWithDefaults);

// Assigning values to Constant parameters
this.apiVersion = apiVersion || "2018-01-01";
this.apiVersion = options.apiVersion || "2018-01-01";
}
}
28 changes: 21 additions & 7 deletions sdk/eventgrid/eventgrid/src/generated/models/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
* Code generated by Microsoft (R) AutoRest Code Generator.
* Changes may cause incorrect behavior and will be lost if the code is regenerated.
*/
import { ServiceClientOptions } from "@azure/core-client";

import * as coreClient from "@azure/core-client";

export type MediaJobOutputUnion = MediaJobOutput | MediaJobOutputAsset;

Expand Down Expand Up @@ -2369,7 +2370,7 @@ export const enum KnownAppAction {
* Defines values for AppAction. \
* {@link KnownAppAction} can be used interchangeably with AppAction,
* this enum contains the known values that the service supports.
* ### Know values supported by the service
* ### Known values supported by the service
* **Restarted**: Web app was restarted. \
* **Stopped**: Web app was stopped. \
* **ChangedAppSettings**: There was an operation to change app setting on the web app. \
Expand All @@ -2393,7 +2394,7 @@ export const enum KnownStampKind {
* Defines values for StampKind. \
* {@link KnownStampKind} can be used interchangeably with StampKind,
* this enum contains the known values that the service supports.
* ### Know values supported by the service
* ### Known values supported by the service
* **Public**: App Service Plan is running on a public stamp. \
* **AseV1**: App Service Plan is running on an App Service Environment V1. \
* **AseV2**: App Service Plan is running on an App Service Environment V2.
Expand All @@ -2410,7 +2411,7 @@ export const enum KnownAppServicePlanAction {
* Defines values for AppServicePlanAction. \
* {@link KnownAppServicePlanAction} can be used interchangeably with AppServicePlanAction,
* this enum contains the known values that the service supports.
* ### Know values supported by the service
* ### Known values supported by the service
* **Updated**: App Service plan is being updated.
*/
export type AppServicePlanAction = string;
Expand All @@ -2429,7 +2430,7 @@ export const enum KnownAsyncStatus {
* Defines values for AsyncStatus. \
* {@link KnownAsyncStatus} can be used interchangeably with AsyncStatus,
* this enum contains the known values that the service supports.
* ### Know values supported by the service
* ### Known values supported by the service
* **Started**: Async operation has started. \
* **Completed**: Async operation has completed. \
* **Failed**: Async operation failed to complete.
Expand All @@ -2447,7 +2448,7 @@ export const enum KnownCommunicationCloudEnvironmentModel {
* Defines values for CommunicationCloudEnvironmentModel. \
* {@link KnownCommunicationCloudEnvironmentModel} can be used interchangeably with CommunicationCloudEnvironmentModel,
* this enum contains the known values that the service supports.
* ### Know values supported by the service
* ### Known values supported by the service
* **public** \
* **dod** \
* **gcch**
Expand Down Expand Up @@ -2484,7 +2485,20 @@ export type MediaJobErrorCategory =
export type MediaJobRetry = "DoNotRetry" | "MayRetry";

/** Optional parameters. */
export interface GeneratedClientOptionalParams extends ServiceClientOptions {
export interface GeneratedClientPublishEventsOptionalParams
extends coreClient.OperationOptions {}

/** Optional parameters. */
export interface GeneratedClientPublishCloudEventEventsOptionalParams
extends coreClient.OperationOptions {}

/** Optional parameters. */
export interface GeneratedClientPublishCustomEventEventsOptionalParams
extends coreClient.OperationOptions {}

/** Optional parameters. */
export interface GeneratedClientOptionalParams
extends coreClient.ServiceClientOptions {
/** Api Version */
apiVersion?: string;
/** Overrides client endpoint. */
Expand Down
Loading

0 comments on commit 5c5c2cf

Please sign in to comment.