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)}");
            }
        }
        public async Task HandleAsync(MemberJoinedCapabilityDomainEvent domainEvent)
        {
            var capabilities = await _capabilityRepository.GetById(domainEvent.Payload.CapabilityId);

            foreach (var capability in capabilities)
            {
                if (capability == null)
                {
                    _logger.LogError(
                        $"Couldn't get capability with ID {domainEvent.Payload.CapabilityId}. Can't add member {domainEvent.Payload.MemberEmail} to Slack.");

                    return;
                }

                try
                {
                    await _slackFacade.InviteToChannel(
                        email : domainEvent.Payload.MemberEmail,
                        channelIdentifier : capability.SlackChannelId.ToString());
                }
                catch (SlackFacadeException ex)
                {
                    _logger.LogError($"Issue with Slack API during InviteToChannel: {ex} : {ex.Message}");
                }

                try
                {
                    //TODO: Should we remove this?
                    if (string.IsNullOrEmpty(capability.SlackUserGroupId))
                    {
                        var userGroup = await _slackService.EnsureUserGroupExists(capability.Name);

                        capability.SetUserGroupID(userGroup.Id);
                    }

                    await capability.AddMember(domainEvent.Payload.MemberEmail);

                    await _capabilityRepository.Update(capability);

                    // Add user to Slack user group:
                    await _slackFacade.AddUserGroupUser(email : domainEvent.Payload.MemberEmail,
                                                        userGroupId : capability.SlackUserGroupId);
                }
                catch (SlackFacadeException ex)
                {
                    _logger.LogError($"Issue with Slack API during AddUserGroupUser: {ex} : {ex.Message}");
                }
            }
        }
Ejemplo n.º 3
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)}");
            }
        }
        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(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
                    );
            }
        }
Ejemplo n.º 6
0
        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());
        }