Skip to content
This repository has been archived by the owner on Jul 12, 2020. It is now read-only.

Commit

Permalink
Configuration changes for the CosmosStoreTrigger attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
Elfocrash committed Nov 30, 2018
1 parent 8ae4879 commit d72b89a
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ public static class CosmosStoreTriggerFunction
public static void Run(
[CosmosStoreTrigger(
"localtest",
ServiceEndpoint = "CosmosEndpoint",
AuthKey = "CosmosAuthKey",
ConnectionStringSetting = "CosmosConnectionString",
LeaseConnectionStringSetting = "LeaseSettings",
LeaseCollectionName = "leases",
CreateLeaseCollectionIfNotExists = true)]IReadOnlyList<Llama> llamaInput,
Expand Down
5 changes: 2 additions & 3 deletions samples/Cosmonaut.AzureFunction/local.settings.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
{
"IsEncrypted": false,
"IsEncrypted": false,
"Values": {
"AzureWebJobsStorage": "UseDevelopmentStorage=true",
"FUNCTIONS_WORKER_RUNTIME": "dotnet",
"CosmosEndpoint": "https://localhost:8081",
"CosmosAuthKey": "C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw==",
"CosmosConnectionString": "AccountEndpoint=https://localhost:8081/;AccountKey=C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw==",
"LeaseSettings": "AccountEndpoint=https://localhost:8081/;AccountKey=C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw=="
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,8 @@ public CosmosStoreTriggerAttribute(string databaseName, string overridenCollecti
/// Connection string for the service containing the collection to monitor
/// </summary>
[AppSetting]
public string ServiceEndpoint { get; set; }

[AppSetting]
public string AuthKey { get; set; }

public string ConnectionStringSetting { get; set; }

/// <summary>
/// Name of the collection to monitor for changes
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ internal class CosmosStoreTriggerAttributeBindingProvider<T> : ITriggerBindingPr
_configuration = configuration;
_nameResolver = nameResolver;
_bindingOptions = bindingOptions;
_logger = loggerFactory.CreateLogger(LogCategories.CreateTriggerCategory("CosmosDB"));
_logger = loggerFactory.CreateLogger(LogCategories.CreateTriggerCategory("CosmosStore"));
}

public async Task<ITriggerBinding> TryCreateAsync(TriggerBindingProviderContext context)
Expand Down Expand Up @@ -61,11 +61,14 @@ public async Task<ITriggerBinding> TryCreateAsync(TriggerBindingProviderContext

try
{
if (attribute.ServiceEndpoint == null)
var monitoredConnectionString = ResolveConnectionString(attribute.ConnectionStringSetting, nameof(CosmosStoreTriggerAttribute.ConnectionStringSetting));
if (string.IsNullOrEmpty(monitoredConnectionString))
{
throw new InvalidOperationException("The connection string for the monitored collection is in an invalid format, please use AccountEndpoint=XXXXXX;AccountKey=XXXXXX;.");
}

var monitoredCosmosDbConnectionString = new CosmosDBConnectionString(monitoredConnectionString);

var leasesConnectionString = ResolveAttributeLeasesConnectionString(attribute);
var leasesConnection = new CosmosDBConnectionString(leasesConnectionString);
if (leasesConnection.ServiceEndpoint == null)
Expand All @@ -77,14 +80,13 @@ public async Task<ITriggerBinding> TryCreateAsync(TriggerBindingProviderContext

documentCollectionLocation = new DocumentCollectionInfo
{
Uri = new Uri(_configuration.GetConnectionStringOrSetting(attribute.ServiceEndpoint)),
MasterKey = _configuration.GetConnectionStringOrSetting(attribute.AuthKey),
Uri = monitoredCosmosDbConnectionString.ServiceEndpoint,
MasterKey = monitoredCosmosDbConnectionString.AuthKey,
DatabaseName = ResolveAttributeValue(attribute.DatabaseName),
CollectionName = monitoredCollectionName
CollectionName = monitoredCollectionName,
ConnectionPolicy = {UserAgentSuffix = CosmosStoreTriggerUserAgentSuffix}
};

documentCollectionLocation.ConnectionPolicy.UserAgentSuffix = CosmosStoreTriggerUserAgentSuffix;

if (desiredConnectionMode.HasValue)
{
documentCollectionLocation.ConnectionPolicy.ConnectionMode = desiredConnectionMode.Value;
Expand Down Expand Up @@ -185,13 +187,10 @@ internal static TimeSpan ResolveTimeSpanFromMilliseconds(string nameOfProperty,
private string ResolveAttributeLeasesConnectionString(CosmosStoreTriggerAttribute attribute)
{
// If the lease connection string is not set, use the trigger's
var keyToResolve = attribute.LeaseConnectionStringSetting;
if (string.IsNullOrEmpty(keyToResolve))
{
keyToResolve = $"AccountEndpoint={attribute.ServiceEndpoint};AccountKey={attribute.AuthKey}";
}

var connectionString = ResolveConnectionString(keyToResolve, nameof(CosmosStoreTriggerAttribute.LeaseConnectionStringSetting));
var connectionString = !string.IsNullOrEmpty(attribute.LeaseConnectionStringSetting) ?
ResolveConnectionString(attribute.LeaseConnectionStringSetting, nameof(CosmosStoreTriggerAttribute.LeaseConnectionStringSetting)) :
ResolveConnectionString(attribute.ConnectionStringSetting, nameof(CosmosStoreTriggerAttribute.ConnectionStringSetting));

if (string.IsNullOrEmpty(connectionString))
{
Expand All @@ -211,7 +210,7 @@ private void ThrowMissingConnectionStringExceptionForLease()
var leaseString = "lease ";

throw new InvalidOperationException(
$"The CosmosDBTrigger {leaseString}connection string must be set either via a '{Constants.DefaultConnectionStringName}' configuration connection string, via the {attributeProperty} property or via {optionsProperty}.");
$"The CosmosStoreTrigger {leaseString}connection string must be set either via a '{Constants.DefaultConnectionStringName}' configuration connection string, via the {attributeProperty} property or via {optionsProperty}.");
}

internal string ResolveConnectionString(string unresolvedConnectionString, string propertyName)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ internal static class CosmosStoreTriggerConstants
{
public const string DefaultLeaseCollectionName = "leases";

public const string TriggerName = "CosmosDBTrigger";
public const string TriggerName = "CosmosStoreTrigger";

public const string TriggerDescription = "New changes on collection {0} at {1}";

Expand Down

0 comments on commit d72b89a

Please sign in to comment.