Exemplo n.º 1
0
 public TriggerBinding(GooglePubSubTriggerAttribute googlePubSubTriggerAttribute, ParameterInfo parameter, Microsoft.Extensions.Logging.ILogger logger)
 {
     this.googlePubSubTriggerAttribute = googlePubSubTriggerAttribute;
     this.parameter  = parameter;
     this.logger     = logger;
     bindingContract = CreateBindingDataContract();
 }
Exemplo n.º 2
0
        private Task CreateSubscription(Topics topicsClient, GooglePubSubTriggerAttribute triggerAttribute, CancellationToken cancellationToken)
        {
            var topicName = $"projects/{triggerAttribute.ProjectId}/topics/{triggerAttribute.TopicId}";

            var topic = new TransparentApiClient.Google.PubSub.V1.Schema.Topic()
            {
                name = topicName
                       //ackDeadlineSeconds= triggerAttribute.AcknowledgeDeadline,
                       //name = triggerAttribute.SubscriptionId,
                       //topic = $"projects/{triggerAttribute.ProjectId}/topics/{triggerAttribute.TopicId}"
            };

            return(topicsClient.CreateAsync(topicName, topic, null, cancellationToken));
        }
        internal static GooglePubSubTriggerAttribute GetAttributeByConfiguration(GooglePubSubTriggerAttribute googlePubSubTriggerAttribute)
        {
            if (string.IsNullOrWhiteSpace(googlePubSubTriggerAttribute.ConfigurationNodeName))
            {
                return(googlePubSubTriggerAttribute);
            }

            var credentialsString   = Environment.GetEnvironmentVariable($"{googlePubSubTriggerAttribute.ConfigurationNodeName}.{nameof(Credentials)}", EnvironmentVariableTarget.Process);
            var credentialsFileName = Environment.GetEnvironmentVariable($"{googlePubSubTriggerAttribute.ConfigurationNodeName}.{nameof(CredentialsFileName)}", EnvironmentVariableTarget.Process);
            var projectId           = Environment.GetEnvironmentVariable($"{googlePubSubTriggerAttribute.ConfigurationNodeName}.{nameof(ProjectId)}", EnvironmentVariableTarget.Process);
            var topicId             = Environment.GetEnvironmentVariable($"{googlePubSubTriggerAttribute.ConfigurationNodeName}.{nameof(TopicId)}", EnvironmentVariableTarget.Process);
            var subscriptionId      = Environment.GetEnvironmentVariable($"{googlePubSubTriggerAttribute.ConfigurationNodeName}.{nameof(SubscriptionId)}", EnvironmentVariableTarget.Process);

            GooglePubSubTriggerAttribute newGooglePubSubTriggerAttribute = null;

            if (string.IsNullOrWhiteSpace(credentialsString) && string.IsNullOrEmpty(credentialsFileName))
            {
                newGooglePubSubTriggerAttribute = new GooglePubSubTriggerAttribute(projectId, topicId, subscriptionId);
            }
            else
            {
                if (string.IsNullOrWhiteSpace(credentialsString))
                {
                    newGooglePubSubTriggerAttribute = new GooglePubSubTriggerAttribute(credentialsFileName, projectId, topicId, subscriptionId);
                }
                else
                {
                    var credentials = System.Text.Encoding.UTF8.GetBytes(credentialsString);
                    newGooglePubSubTriggerAttribute = new GooglePubSubTriggerAttribute(credentials, projectId, topicId, subscriptionId);
                }
            }

            var createSubscriptionIfDoesntExist = Environment.GetEnvironmentVariable($"{googlePubSubTriggerAttribute.ConfigurationNodeName}.{nameof(CreateSubscriptionIfDoesntExist)}", EnvironmentVariableTarget.Process);
            var maxBatchSize = Environment.GetEnvironmentVariable($"{googlePubSubTriggerAttribute.ConfigurationNodeName}.{nameof(MaxBatchSize)}", EnvironmentVariableTarget.Process);

            if (createSubscriptionIfDoesntExist != null)
            {
                newGooglePubSubTriggerAttribute.CreateSubscriptionIfDoesntExist = bool.Parse(createSubscriptionIfDoesntExist);
            }

            if (maxBatchSize != null && System.Text.RegularExpressions.Regex.IsMatch(maxBatchSize, "^\\d+$"))
            {
                newGooglePubSubTriggerAttribute.MaxBatchSize = int.Parse(maxBatchSize);
            }

            return(newGooglePubSubTriggerAttribute);
        }
Exemplo n.º 4
0
        public Task <ITriggerBinding> TryCreateAsync(TriggerBindingProviderContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            ParameterInfo parameter = context.Parameter;
            GooglePubSubTriggerAttribute attribute = parameter.GetCustomAttribute <GooglePubSubTriggerAttribute>(inherit: false);

            if (attribute == null)
            {
                return(Task.FromResult <ITriggerBinding>(null));
            }

            // TODO: Define the types your binding supports here
            if (parameter.ParameterType != typeof(IEnumerable <string>) &&
                parameter.ParameterType != typeof(string[]))
            {
                throw new InvalidOperationException($"Can't bind {nameof(GooglePubSubTriggerAttribute)} to type '{parameter.ParameterType}'.");
            }

            return(Task.FromResult <ITriggerBinding>(new TriggerBinding(attribute, context.Parameter, logger)));
        }
Exemplo n.º 5
0
 public Listener(ITriggeredFunctionExecutor executor, GooglePubSubTriggerAttribute triggerAttribute, Microsoft.Extensions.Logging.ILogger logger)
 {
     this.executor         = executor;
     this.logger           = logger;
     this.triggerAttribute = GooglePubSubTriggerAttribute.GetAttributeByConfiguration(triggerAttribute);
 }