public async Task HandleAsync(AWSContextAccountCreatedDomainEvent domainEvent)
        {
            var addUserCmd =
                $"Get-ADUser \"CN=IT BuildSource DevEx,OU=DFDS AS,OU=Mailboxes,OU=Accounts,OU=DFDS,DC=dk,DC=dfds,DC=root\" | Set-ADUser -Add @{{proxyAddresses=\"smtp:{domainEvent.Payload.RoleEmail}\"}}";
            var installToolsCmd =
                $"Get-WindowsCapability -Online | ? {{$_.Name -like 'Rsat.ActiveDirectory.DS-LDS.Tools*'}} | Add-WindowsCapability -Online";
            var addDeployCredentials = $"ROOT_ID={domainEvent.Payload.CapabilityRootId} ACCOUNT_ID={domainEvent.Payload.AccountId} ./kube-config-generator.sh";

            var sb = new StringBuilder();

            sb.AppendLine($"An AWS Context account has been created for ContextId: {domainEvent.Payload.ContextId}");
            sb.AppendLine($"Please execute the following command:");
            sb.AppendLine(addUserCmd);
            sb.AppendLine($"Should you not have RSAT tools installed, please do so with command:");
            sb.AppendLine(installToolsCmd);
            sb.AppendLine("---");
            sb.AppendLine($"Please execute the following script in K8s root and AWS prime context for this repo https://github.com/dfds/ded-toolbox/tree/master/k8s-service-account-config-to-ssm:");
            sb.AppendLine(addDeployCredentials);

            var hardCodedDedChannelId = new ChannelId("GFYE9B99Q");
            await _slackFacade.SendNotificationToChannel(hardCodedDedChannelId.ToString(), sb.ToString());

            // Send message to Capability Slack channel
            var capabilities = await _capabilityRepository.GetById(domainEvent.Payload.CapabilityId);

            foreach (var capability in capabilities)
            {
                await _slackFacade.SendNotificationToChannel(capability.SlackChannelId.ToString(),
                                                             $"Status update\n{SlackContextAddedToCapabilityDomainEventHandler.CreateTaskTable(true, false, false)}");
            }
        }
Esempio n. 2
0
        public async Task HandleAsync(ContextAddedToCapabilityDomainEvent domainEvent)
        {
            var capabilities = await _capabilityRepository.GetById(domainEvent.Payload.CapabilityId);

            var message = CreateMessage(domainEvent, _externalEventMetaDataStore.XCorrelationId);

            var hardCodedDedChannelId = new ChannelId("GFYE9B99Q");
            await _slackFacade.SendNotificationToChannel(hardCodedDedChannelId.ToString(), message);

            // Send message to Capability Slack channels
            foreach (var capability in capabilities)
            {
                await _slackFacade.SendNotificationToChannel(
                    capability.SlackChannelId.ToString(),
                    $"We're working on setting up your environment. Currently the following resources are being provisioned and are awaiting status updates" +
                    $"\n" +
                    $"{CreateTaskTable(false, false, false)}");
            }
        }
Esempio n. 3
0
        public async Task HandleAsync(CapabilityCreatedDomainEvent domainEvent)
        {
            var createChannelResponse = await _slackFacade.CreateChannel(domainEvent.Payload.CapabilityName);

            UserGroupDto userGroup = null;

            try
            {
                userGroup = await _slackService.EnsureUserGroupExists(domainEvent.Payload.CapabilityName);
            }
            catch (SlackFacadeException ex)
            {
                _logger.LogError($"Issue with Slack API during CreateUserGroup: {ex} : {ex.Message}");
            }


            var channelName = createChannelResponse?.Channel?.Name;


            if (createChannelResponse.Ok)
            {
                var channelId = new ChannelId(createChannelResponse?.Channel?.Id);
                _logger.LogInformation($"Slack channel '{channelName}' for capability '{domainEvent.Payload.CapabilityName}' created with ID: {channelId}");

                var userGroupId = userGroup?.Id;
                // Save even without user group.
                var capability = Capability.Create(
                    id: domainEvent.Payload.CapabilityId,
                    name: domainEvent.Payload.CapabilityName,
                    slackChannelId: channelId,
                    slackUserGroupId: userGroupId);
                _logger.LogInformation($"Capability id: '{capability.Id}'  name: '{capability.Name}' slackChannelId: '{capability.SlackChannelId}', userGroupId: '{capability.SlackUserGroupId}'");

                await _capabilityRepository.Add(capability);

                // Notify channel about handle.
                var sendNotificationResponse = await _slackFacade.SendNotificationToChannel(
                    channelIdentifier : channelId.ToString(),
                    message :
                    $"Thank you for creating capability '{capability.Name}'.\n" +
                    $"This channel along with handle @{userGroup.Handle} has been created.\n" +
                    "Use the handle to notify capability members.\n" +
                    $"If you want to define a better handle, you can do this in the '{userGroup.Name}'");

                // Pin message.
                await _slackFacade.PinMessageToChannel(channelId.ToString(), sendNotificationResponse.TimeStamp);
            }
            else
            {
                _logger.LogError($"Error creating Slack channel '{channelName}', Error: '{createChannelResponse.Error}'");
            }
        }
        public async Task HandleAsync(AWSContextAccountCreatedDomainEvent domainEvent)
        {
            var addUserCmd =
                $"Get-ADUser \"CN=IT BuildSource DevEx,OU=DFDS AS,OU=Mailboxes,OU=Accounts,OU=DFDS,DC=dk,DC=dfds,DC=root\" | Set-ADUser -Add @{{proxyAddresses=\"smtp:{domainEvent.Payload.RoleEmail}\"}}";
            var installToolsCmd =
                $"Get-WindowsCapability -Online | ? {{$_.Name -like 'Rsat.ActiveDirectory.DS-LDS.Tools*'}} | Add-WindowsCapability -Online";
            var addDeployCredentialsBash = $"AWS_ROLE_CLOUD_ADMIN=\"[Cloud Administrator Role ARN]\"\\\n" +
                                           $"AWS_ROLE_ADFS_ADMIN=\"[ADFS Administrator Role ARN]\"\\\n" +
                                           $"poetry run python ./kube_config_generator.py -r {domainEvent.Payload.CapabilityRootId} -a {domainEvent.Payload.AccountId}";
            var addDeployCredentialsPS = $"$ENV:AWS_ROLE_CLOUD_ADMIN=\"[Cloud Administrator Role ARN]\"\\\n" +
                                         $"$ENV:AWS_ROLE_ADFS_ADMIN=\"[ADFS Administrator Role ARN]\"\\\n" +
                                         $"poetry run python .\\kube_config_generator.py -r {domainEvent.Payload.CapabilityRootId} -a {domainEvent.Payload.AccountId}";

            // poetry run python kube_config_generator.py -r {domainEvent.Payload.CapabilityRootId} -a {domainEvent.Payload.AccountId}
            var sb = new StringBuilder();

            sb.AppendLine($"*An AWS Context account has been created for ContextId: {domainEvent.Payload.ContextId}*");
            sb.AppendLine("\n_Add email address to shared mailbox_");
            sb.AppendLine("Execute the following Powershell command:");
            sb.AppendLine($"`{addUserCmd}`");
            // sb.AppendLine($"Should you not have RSAT tools installed, please do so with command:");
            // sb.AppendLine(installToolsCmd);
            sb.AppendLine($"\n_Generate k8s service account_");
            sb.AppendLine($"Execute the Python script from github.com/dfds/ce-toolbox/k8s-service-account-config-to-ssm.  Please ensure that the two environment variables are modified to include the correct Role ARNs.");
            sb.AppendLine($"Bash:\n```{addDeployCredentialsBash}```");
            sb.AppendLine($"Powershell:\n```{addDeployCredentialsPS}```");

            var hardCodedDedChannelId = new ChannelId("GFYE9B99Q");
            await _slackFacade.SendNotificationToChannel(hardCodedDedChannelId.ToString(), sb.ToString());

            // Send message to Capability Slack channel
            var capabilities = await _capabilityRepository.GetById(domainEvent.Payload.CapabilityId);

            foreach (var capability in capabilities)
            {
                await _slackFacade.SendNotificationToChannel(capability.SlackChannelId.ToString(),
                                                             $"Status update\n{SlackContextAddedToCapabilityDomainEventHandler.CreateTaskTable(true, false, false)}");
            }
        }
        public async Task HandleAsync(CapabilityDeletedDomainEvent domainEvent)
        {
            var hardCodedDedChannelId = new ChannelId("GFYE9B99Q");

            var messageForDed =
                $":x: Capability with Id: '{domainEvent.Payload.CapabilityId}' & name : '{domainEvent.Payload.CapabilityName}' have been deleted" + Environment.NewLine +
                "Please Do the needfull";

            await _slackFacade.SendNotificationToChannel(
                hardCodedDedChannelId.ToString(),
                messageForDed
                );
        }
        public async Task HandleAsync(K8sNamespaceCreatedAndAwsArnConnectedDomainEvent domainEvent)
        {
            var capabilities = await _capabilityRepository.GetById(domainEvent.Payload.CapabilityId);

            // 1st Message, instant.
            var missingAdsyncTaskTable = SlackContextAddedToCapabilityDomainEventHandler.CreateTaskTable(
                awsAccDone: true,
                k8sCreatedDone: true,
                adsyncDone: false
                );

            foreach (var capability in capabilities)
            {
                await _slackFacade.SendNotificationToChannel(
                    capability.SlackChannelId.ToString(),
                    $"Nearly there... time to grab a coffee?\n{missingAdsyncTaskTable}"
                    );
            }

            var timeToWait = (60 * 15); // 15 Minutes
            var timestamp  = DateTimeOffset.UtcNow.ToUnixTimeSeconds() + timeToWait;

            // 2nd Message, delayed.
            var allDoneTaskTable = SlackContextAddedToCapabilityDomainEventHandler.CreateTaskTable(
                awsAccDone: true,
                k8sCreatedDone: true,
                adsyncDone: true
                );

            foreach (var capability in capabilities)
            {
                await _slackFacade.SendDelayedNotificationToChannel(
                    capability.SlackChannelId.ToString(),
                    $"All done:\n{allDoneTaskTable}",
                    timestamp
                    );
            }
        }
        public async Task <IActionResult> DispatchMessage(DispatchMessageInput input)
        {
            if (!input.CapabilityId.HasValue)
            {
                return(BadRequest("capabilityId is required."));
            }

            var capabilities = await _capabilityRepository.GetById(input.CapabilityId.Value);

            if (capabilities.Any() == false)
            {
                return(UnprocessableEntity($"No channel for capability ID '{input.CapabilityId.Value}' Found"));
            }

            foreach (var capability in capabilities)
            {
                try
                {
                    var sendNotificationToChannelResponse = await _slackFacade.SendNotificationToChannel(capability.SlackChannelId.ToString(), input.Message);

                    if (!sendNotificationToChannelResponse.Ok)
                    {
                        return(StatusCode(
                                   StatusCodes.Status503ServiceUnavailable,
                                   $"An error occured trying to send notification: {sendNotificationToChannelResponse.Error}"));
                    }
                }
                catch (Exception exp)
                {
                    return(StatusCode(
                               StatusCodes.Status500InternalServerError,
                               $"An error occured trying to send notification: {exp.Message}"));
                }
            }

            return(Accepted());
        }