Skip to content

Commit

Permalink
[Service Bus] ATOM API: Allow configuring the rule with createSubscri…
Browse files Browse the repository at this point in the history
…ption (Azure#12495)

### Issue Azure#12345 

Adding support to allow configuring the rule with createSubscription by taking the [XML request](https://github.com/Azure/azure-sdk-for-net/blob/master/sdk/servicebus/Azure.Messaging.ServiceBus/tests/SessionRecords/ServiceBusManagementClientLiveTests/BasicRuleCrudOperationsAsync.json#L66-L71) from .NET SDK as reference.
(Thanks @JoshLove-msft)
  • Loading branch information
HarshaNalluru authored Nov 17, 2020
1 parent 17a3aed commit 21969bb
Show file tree
Hide file tree
Showing 8 changed files with 236 additions and 151 deletions.
4 changes: 3 additions & 1 deletion sdk/servicebus/service-bus/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@

- The `ServiceBusError.reason` field has been renamed `ServiceBusError.code`.
The `code` field can be used to differentiate what caused a `ServiceBusError` to be thrown.
- Numbers passed in `applicationProperties` of the correlation rule filter and `sqlParameters` under SQLRuleFilter will now be serialized as "double"(used to be "int") while sending the requests. The "double" and "int" values in the response will now be deserialized as "number"("double" wasn't supported before).
- Numbers passed in `applicationProperties` of the correlation rule filter and `sqlParameters` under SQLRuleFilter will now be serialized as "double"(used to be "int") while sending the requests. The "double" and "int" values in the response will now be deserialized as "number"("double" wasn't supported before).
[PR 12349](https://github.com/Azure/azure-sdk-for-js/pull/12349)
- `ServiceBusAdministrationClient.createSubscription` now supports configuring default rule at the time of creating the subscription.
[PR 12495](https://github.com/Azure/azure-sdk-for-js/pull/12495)

## 7.0.0-preview.8 (2020-11-04)

Expand Down
7 changes: 6 additions & 1 deletion sdk/servicebus/service-bus/review/service-bus.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,11 @@ export interface CreateSubscriptionOptions extends OperationOptions {
deadLetteringOnFilterEvaluationExceptions?: boolean;
deadLetteringOnMessageExpiration?: boolean;
defaultMessageTimeToLive?: string;
defaultRuleOptions?: {
name: string;
filter?: SqlRuleFilter | CorrelationRuleFilter;
action?: SqlRuleAction;
};
enableBatchedOperations?: boolean;
forwardDeadLetteredMessagesTo?: string;
forwardTo?: string;
Expand Down Expand Up @@ -508,7 +513,7 @@ export interface SubscriptionProperties {
status: EntityStatus;
readonly subscriptionName: string;
readonly topicName: string;
userMetadata: string;
userMetadata?: string;
}

// @public
Expand Down
18 changes: 9 additions & 9 deletions sdk/servicebus/service-bus/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ export { ServiceBusClientOptions } from "./constructorHelpers";
export { CorrelationRuleFilter } from "./core/managementClient";
export {
CreateMessageBatchOptions,
ServiceBusReceiverOptions,
ServiceBusSessionReceiverOptions,
GetMessageIteratorOptions,
MessageHandlers,
ProcessErrorArgs,
PeekMessagesOptions,
ProcessErrorArgs,
ReceiveMessagesOptions,
ServiceBusReceiverOptions,
ServiceBusSessionReceiverOptions,
SubscribeOptions
} from "./models";
export { OperationOptionsBase, TryAddOptions } from "./modelsToBeSharedWithEventHubs";
Expand All @@ -44,19 +44,19 @@ export {
} from "./serializers/topicResourceSerializer";
export {
EntitiesResponse,
WithResponse,
ServiceBusAdministrationClient
ServiceBusAdministrationClient,
WithResponse
} from "./serviceBusAtomManagementClient";
export { ServiceBusClient } from "./serviceBusClient";
export { isServiceBusError, ServiceBusError, ServiceBusErrorCode } from "./serviceBusError";
export {
DeadLetterOptions,
ServiceBusReceivedMessage,
ServiceBusMessage
ServiceBusMessage,
ServiceBusReceivedMessage
} from "./serviceBusMessage";
export { ServiceBusMessageBatch } from "./serviceBusMessageBatch";
export { AuthorizationRule, EntityStatus, EntityAvailabilityStatus } from "./util/utils";
export {
parseServiceBusConnectionString,
ServiceBusConnectionStringProperties
} from "./util/connectionStringUtils";
export { isServiceBusError, ServiceBusError, ServiceBusErrorCode } from "./serviceBusError";
export { AuthorizationRule, EntityAvailabilityStatus, EntityStatus } from "./util/utils";
138 changes: 80 additions & 58 deletions sdk/servicebus/service-bus/src/serializers/ruleResourceSerializer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,79 +157,101 @@ export interface SqlRuleFilter {
/**
* @internal
* @ignore
* RuleResourceSerializer for serializing / deserializing Rule entities
*
* @interface InternalRuleOptions
*/
export class RuleResourceSerializer implements AtomXmlSerializer {
serialize(rule: RuleProperties): object {
const resource: { Name: any; Filter: any; Action: any } = {
Filter: {},
Action: {},
Name: rule.name
};
export interface InternalRuleOptions {
Name: string;
Filter: any;
Action: any;
}

/**
* @internal
* @ignore
*
* @param {CreateRuleOptions} rule
*/
export function buildInternalRuleResource(rule: CreateRuleOptions): InternalRuleOptions {
const resource: InternalRuleOptions = {
Filter: {},
Action: {},
Name: rule.name
};

if (rule.filter == undefined) {
// Defaults to creating a true filter if none specified
if (rule.filter == undefined) {
// Defaults to creating a true filter if none specified
resource.Filter = {
SqlExpression: "1=1"
};
resource.Filter[Constants.XML_METADATA_MARKER] = {
"p4:type": "SqlFilter",
"xmlns:p4": "http://www.w3.org/2001/XMLSchema-instance"
};
} else {
if (rule.filter.hasOwnProperty("sqlExpression")) {
const sqlFilter: SqlRuleFilter = rule.filter as SqlRuleFilter;
resource.Filter = {
SqlExpression: "1=1"
SqlExpression: sqlFilter.sqlExpression,
Parameters: buildInternalRawKeyValuePairs(sqlFilter.sqlParameters, "sqlParameters")
};
resource.Filter[Constants.XML_METADATA_MARKER] = {
"p4:type": "SqlFilter",
"xmlns:p4": "http://www.w3.org/2001/XMLSchema-instance"
};
} else {
if (rule.filter.hasOwnProperty("sqlExpression")) {
const sqlFilter: SqlRuleFilter = rule.filter as SqlRuleFilter;
resource.Filter = {
SqlExpression: sqlFilter.sqlExpression,
Parameters: buildInternalRawKeyValuePairs(sqlFilter.sqlParameters, "sqlParameters")
};
resource.Filter[Constants.XML_METADATA_MARKER] = {
"p4:type": "SqlFilter",
"xmlns:p4": "http://www.w3.org/2001/XMLSchema-instance"
};
} else {
const correlationFilter: CorrelationRuleFilter = rule.filter as CorrelationRuleFilter;

resource.Filter = {
CorrelationId: correlationFilter.correlationId,
Label: correlationFilter.subject,
To: correlationFilter.to,
ReplyTo: correlationFilter.replyTo,
ReplyToSessionId: correlationFilter.replyToSessionId,
ContentType: correlationFilter.contentType,
SessionId: correlationFilter.sessionId,
MessageId: correlationFilter.messageId,
Properties: buildInternalRawKeyValuePairs(
correlationFilter.applicationProperties,
"applicationProperties"
)
};
resource.Filter[Constants.XML_METADATA_MARKER] = {
"p4:type": "CorrelationFilter",
"xmlns:p4": "http://www.w3.org/2001/XMLSchema-instance"
};
}
}
const correlationFilter: CorrelationRuleFilter = rule.filter as CorrelationRuleFilter;

if (rule.action == undefined || rule.action.sqlExpression == undefined) {
// Defaults to creating an empty rule action instance if none specified
resource.Action = {};
resource.Action[Constants.XML_METADATA_MARKER] = {
"p4:type": "EmptyRuleAction",
"xmlns:p4": "http://www.w3.org/2001/XMLSchema-instance"
};
} else {
resource.Action = {
SqlExpression: rule.action.sqlExpression,
Parameters: buildInternalRawKeyValuePairs(rule.action.sqlParameters, "sqlParameters")
resource.Filter = {
CorrelationId: correlationFilter.correlationId,
Label: correlationFilter.subject,
To: correlationFilter.to,
ReplyTo: correlationFilter.replyTo,
ReplyToSessionId: correlationFilter.replyToSessionId,
ContentType: correlationFilter.contentType,
SessionId: correlationFilter.sessionId,
MessageId: correlationFilter.messageId,
Properties: buildInternalRawKeyValuePairs(
correlationFilter.applicationProperties,
"applicationProperties"
)
};
resource.Action[Constants.XML_METADATA_MARKER] = {
"p4:type": "SqlRuleAction",
resource.Filter[Constants.XML_METADATA_MARKER] = {
"p4:type": "CorrelationFilter",
"xmlns:p4": "http://www.w3.org/2001/XMLSchema-instance"
};
}
}

if (rule.action == undefined || rule.action.sqlExpression == undefined) {
// Defaults to creating an empty rule action instance if none specified
resource.Action = {};
resource.Action[Constants.XML_METADATA_MARKER] = {
"p4:type": "EmptyRuleAction",
"xmlns:p4": "http://www.w3.org/2001/XMLSchema-instance"
};
} else {
resource.Action = {
SqlExpression: rule.action.sqlExpression,
Parameters: buildInternalRawKeyValuePairs(rule.action.sqlParameters, "sqlParameters")
};
resource.Action[Constants.XML_METADATA_MARKER] = {
"p4:type": "SqlRuleAction",
"xmlns:p4": "http://www.w3.org/2001/XMLSchema-instance"
};
}

return resource;
}

return serializeToAtomXmlRequest("RuleDescription", resource);
/**
* @internal
* @ignore
* RuleResourceSerializer for serializing / deserializing Rule entities
*/
export class RuleResourceSerializer implements AtomXmlSerializer {
serialize(rule: RuleProperties): object {
return serializeToAtomXmlRequest("RuleDescription", buildInternalRuleResource(rule));
}

async deserialize(response: HttpOperationResponse): Promise<HttpOperationResponse> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the MIT license.

import { HttpOperationResponse, OperationOptions } from "@azure/core-http";
import { CorrelationRuleFilter } from "..";
import {
AtomXmlSerializer,
deserializeAtomXmlResponse,
Expand All @@ -18,6 +19,12 @@ import {
getStringOrUndefined,
getDate
} from "../util/utils";
import {
buildInternalRuleResource,
InternalRuleOptions,
SqlRuleAction,
SqlRuleFilter
} from "./ruleResourceSerializer";

/**
* @internal
Expand All @@ -40,6 +47,9 @@ export function buildSubscriptionOptions(
DeadLetteringOnFilterEvaluationExceptions: getStringOrUndefined(
subscription.deadLetteringOnFilterEvaluationExceptions
),
DefaultRuleDescription: subscription.defaultRuleOptions
? buildInternalRuleResource(subscription.defaultRuleOptions)
: undefined,
MaxDeliveryCount: getStringOrUndefined(subscription.maxDeliveryCount),
EnableBatchedOperations: getStringOrUndefined(subscription.enableBatchedOperations),
Status: getStringOrUndefined(subscription.status),
Expand Down Expand Up @@ -178,6 +188,30 @@ export interface CreateSubscriptionOptions extends OperationOptions {
*/
deadLetteringOnFilterEvaluationExceptions?: boolean;

/**
* Represents the options to create the default rule for the subscription.
*/
defaultRuleOptions?: {
/**
* Name of the rule
*/
name: string;

/**
* Defines the filter expression that the rule evaluates. For `SqlRuleFilter` input,
* the expression string is interpreted as a SQL92 expression which must
* evaluate to True or False. Only one between a `CorrelationRuleFilter` or
* a `SqlRuleFilter` can be defined.
*/
filter?: SqlRuleFilter | CorrelationRuleFilter;

/**
* The SQL like expression that can be executed on the message should the
* associated filter apply.
*/
action?: SqlRuleAction;
};

/**
* The maximum delivery count of messages after which if it is still not settled,
* gets moved to the dead-letter sub-queue.
Expand Down Expand Up @@ -327,7 +361,7 @@ export interface SubscriptionProperties {
* Used to specify textual content such as tags, labels, etc.
* Value must not exceed 1024 bytes encoded in utf-8.
*/
userMetadata: string;
userMetadata?: string;

/**
* Absolute URL or the name of the queue or topic the dead-lettered
Expand Down Expand Up @@ -458,6 +492,8 @@ export interface InternalSubscriptionOptions {
* Availability status of the messaging entity.
*/
EntityAvailabilityStatus?: string;

DefaultRuleDescription?: InternalRuleOptions;
}

/**
Expand Down
8 changes: 0 additions & 8 deletions sdk/servicebus/service-bus/src/util/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,14 +120,6 @@ export const ENABLE_SUBSCRIPTION_PARTITIONING = "EnableSubscriptionPartitioning"
*/
export const FILTER_MESSAGES_BEFORE_PUBLISHING = "FilteringMessagesBeforePublishing";

/**
* Indicates the default rule description.
*
* @internal
* @ignore
*/
export const DEFAULT_RULE_DESCRIPTION = "DefaultRuleDescription";

/**
* The entity's size in bytes.
*
Expand Down
Loading

0 comments on commit 21969bb

Please sign in to comment.