Exemplo n.º 1
0
        public override async Task <Response <RegistrationResourceAttributes> > ProcessCreateResourceAsync(Request <RegistrationResourceProperties> request)
        {
            var properties = request.ResourceProperties;

            // determine the kind of registration that is requested
            switch (request.ResourceProperties.ResourceType)
            {
            case "LambdaSharp::Registration::Module": {
                LogInfo($"Adding Module: Id={properties.ModuleId}, Info={properties.Module}");
                var owner = PopulateOwnerMetaData(properties);

                // create new rollbar project
                if (_rollbarClient.HasTokens)
                {
                    var name    = _rollbarProjectPrefix + request.ResourceProperties.GetModuleFullName();
                    var project = await _rollbarClient.FindProjectByName(name)
                                  ?? await _rollbarClient.CreateProject(name);

                    var tokens = await _rollbarClient.ListProjectTokens(project.Id);

                    var token = tokens.First(t => t.Name == "post_server_item").AccessToken;
                    owner.RollbarProjectId   = project.Id;
                    owner.RollbarAccessToken = await EncryptSecretAsync(token);
                }

                // create owner record
                await _registrations.PutOwnerMetaDataAsync($"M:{owner.ModuleId}", owner);

                return(Respond($"registration:module:{properties.ModuleId}"));
            }

            case "LambdaSharp::Registration::Function": {
                LogInfo($"Adding Function: Id={properties.FunctionId}, Name={properties.FunctionName}");
                var owner = await _registrations.GetOwnerMetaDataAsync($"M:{properties.ModuleId}");

                if (owner == null)
                {
                    throw new RegistrarException("no registration found for module {0}", properties.ModuleId);
                }
                owner = PopulateOwnerMetaData(properties, owner);
                await _registrations.PutOwnerMetaDataAsync($"F:{owner.FunctionId}", owner);

                return(Respond($"registration:function:{properties.FunctionId}"));
            }

            default:
                throw new RegistrarException("unrecognized resource type: {0}", request.ResourceType);
            }
        }
Exemplo n.º 2
0
        public override async Task <Response <RegistrationResourceAttributes> > ProcessCreateResourceAsync(Request <RegistrationResourceProperties> request)
        {
            var properties = request.ResourceProperties;

            // determine the kind of registration that is requested
            switch (request.ResourceProperties.ResourceType)
            {
            case "LambdaSharp::Registration::Module": {
                LogInfo($"Adding Module: Id={properties.ModuleId}, Info={properties.Module}");
                var owner = PopulateOwnerMetaData(properties);

                // create new rollbar project
                if (_rollbarClient.HasTokens)
                {
                    var name = Regex.Replace(_rollbarProjectPattern, @"\{(?!\!)[^\}]+\}", match => {
                            var value = match.ToString();
                            switch (value)
                            {
                            case "{ModuleFullName}":
                                return(request.ResourceProperties.GetModuleFullName());

                            case "{ModuleNamespace}":
                                return(request.ResourceProperties.GetModuleNamespace());

                            case "{ModuleName}":
                                return(request.ResourceProperties.GetModuleName());

                            default:

                                // remove curly braces for unrecognized placeholders
                                return(value.Substring(1, value.Length - 2));
                            }
                        });

                    // NOTE (2020-02-19, bjorg): Rollbar projects cannot exceed 32 characters
                    if (name.Length > 32)
                    {
                        using (var crypto = new SHA256Managed()) {
                            var hash = string.Concat(crypto.ComputeHash(Encoding.UTF8.GetBytes(name)).Select(x => x.ToString("X2")));

                            // keep first X characters for original project name, append (32-X) characters from the hash
                            name = name.Substring(0, 32 - PROJECT_HASH_LENGTH) + hash.Substring(0, PROJECT_HASH_LENGTH);
                        }
                    }
                    var project = await _rollbarClient.FindProjectByName(name)
                                  ?? await _rollbarClient.CreateProject(name);

                    var tokens = await _rollbarClient.ListProjectTokens(project.Id);

                    var token = tokens.First(t => t.Name == "post_server_item").AccessToken;
                    owner.RollbarProjectId   = project.Id;
                    owner.RollbarAccessToken = await EncryptSecretAsync(token, _coreSecretsKey);
                }

                // create owner record
                await _registrations.PutOwnerMetaDataAsync($"M:{owner.ModuleId}", owner);

                return(Respond($"registration:module:{properties.ModuleId}"));
            }

            case "LambdaSharp::Registration::Function": {
                LogInfo($"Adding Function: Id={properties.FunctionId}, Name={properties.FunctionName}");
                var owner = await _registrations.GetOwnerMetaDataAsync($"M:{properties.ModuleId}");

                if (owner == null)
                {
                    throw new RegistrarException("no registration found for module {0}", properties.ModuleId);
                }
                owner = PopulateOwnerMetaData(properties, owner);
                await _registrations.PutOwnerMetaDataAsync($"F:{owner.FunctionId}", owner);

                return(Respond($"registration:function:{properties.FunctionId}"));
            }

            default:
                throw new RegistrarException("unrecognized resource type: {0}", request.ResourceType);
            }
        }