Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found
Select Git revision
Loading items

Target

Select target project
  • coscine/backend/libraries/database
1 result
Select Git revision
Loading items
Show changes
Commits on Source (6)
......@@ -4,7 +4,7 @@
<RootNamespace>Coscine.Database</RootNamespace>
<AssemblyName>Coscine.Database</AssemblyName>
<TargetFrameworks>net5.0;net461</TargetFrameworks>
<Version>2.1.1</Version>
<Version>2.2.0</Version>
</PropertyGroup>
<PropertyGroup>
<Authors>RWTH Aachen University</Authors>
......
using Coscine.Database.DataModel;
using Coscine.Database.ReturnObjects;
using Coscine.Database.Util;
using LinqKit;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text.RegularExpressions;
using LinqKit;
namespace Coscine.Database.Models
{
public class ProjectModel : DatabaseModel<Project>
......@@ -119,7 +117,7 @@ namespace Coscine.Database.Models
{
return DatabaseConnection.ConnectToDatabase((db) =>
{
return db.Delete(databaseObject);
return (int) db.Remove(databaseObject).State;
});
}
......@@ -127,15 +125,15 @@ namespace Coscine.Database.Models
{
return DatabaseConnection.ConnectToDatabase((db) =>
{
return db.Delete(from tableEntry in GetITableFromDatabase(db).AsExpandable()
return (int)db.Remove(from tableEntry in GetITableFromDatabase(db).AsExpandable()
where whereClause.Invoke(tableEntry)
select tableEntry);
select tableEntry).State;
});
}
public Project StoreFromObject(ProjectObject projectObject, User user, bool isRWTHMember = false)
{
if (projectObject.Disciplines.Count() == 0 || projectObject.Organizations.Count() == 0)
if (!projectObject.Disciplines.Any() || !projectObject.Organizations.Any())
{
throw new ArgumentException("Discipline and Institute are necessary!");
}
......@@ -162,10 +160,10 @@ namespace Coscine.Database.Models
SetOrganizations(project, projectObject.Organizations);
SetQuotas(project, isRWTHMember);
}
catch (Exception e)
catch (Exception)
{
HardDelete(project);
throw e;
throw;
}
SetOwner(project, user);
return project;
......@@ -185,7 +183,7 @@ namespace Coscine.Database.Models
});
}
private String GenerateSlug(ProjectObject projectObject)
private string GenerateSlug(ProjectObject projectObject)
{
// create slug for project
var slug = projectObject.DisplayName;
......@@ -360,7 +358,7 @@ namespace Coscine.Database.Models
public int UpdateByObject(Project project, ProjectObject projectObject)
{
if (projectObject.Disciplines.Count() == 0 || projectObject.Organizations.Count() == 0)
if (!projectObject.Disciplines.Any() || !projectObject.Organizations.Any())
{
throw new ArgumentException("Discipline and Institute are necessary!");
}
......@@ -433,7 +431,7 @@ namespace Coscine.Database.Models
&& subProject.Project.Deleted == false
select subProject).Any());
if (list.Count() > 0)
if (list.Any())
{
currentProject = list.First();
bool authorized = true;
......@@ -476,5 +474,54 @@ namespace Coscine.Database.Models
databaseObject.Id = id;
}
public string GetMetadataCompleteness(ProjectObject projectObject)
{
var maxCount = 0;
var counted = 0;
var projectProperties = typeof(ProjectObject).GetProperties();
foreach (var property in projectProperties)
{
if (property == null
|| property.PropertyType == typeof(Guid)
|| property.Name == "Slug")
{
continue;
}
maxCount++;
if (property.PropertyType == typeof(string)
&& property.GetValue(projectObject) != null
&& !string.IsNullOrEmpty(property.GetValue(projectObject).ToString()))
{
counted++;
}
else if (property.PropertyType == typeof(DateTime)
&& property.GetValue(projectObject) != null)
{
counted++;
}
else if (property.PropertyType == typeof(IEnumerable<DisciplineObject>)
&& property.GetValue(projectObject) != null
&& ((IEnumerable<DisciplineObject>)property.GetValue(projectObject)).Any())
{
counted++;
}
else if (property.PropertyType == typeof(IEnumerable<OrganizationObject>)
&& property.GetValue(projectObject) != null
&& ((IEnumerable<OrganizationObject>)property.GetValue(projectObject)).Any())
{
counted++;
}
else if (property.PropertyType == typeof(VisibilityObject)
&& property.GetValue(projectObject) != null)
{
counted++;
}
}
return $"{counted}/{maxCount}";
}
}
}
......@@ -28,6 +28,7 @@ namespace Coscine.Database.Models
var dictionary = new Dictionary<string, string>();
var resourceType = GetById(id);
dictionary.Add("bucketname", resourceType.BucketName);
dictionary.Add("size", $"{resourceType.Size}");
return dictionary;
}
}
......
using Coscine.Database.DataModel;
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
namespace Coscine.Database.Models
{
public class RdsS3ResourceTypeModel : DatabaseModel<RdsS3resourceType>
{
public override Expression<Func<RdsS3resourceType, Guid>> GetIdFromObject()
{
return (rdsResourceType) => rdsResourceType.Id;
}
public override Microsoft.EntityFrameworkCore.DbSet<RdsS3resourceType> GetITableFromDatabase(CoscineDB db)
{
return db.RdsS3resourceTypes;
}
public override void SetObjectId(RdsS3resourceType databaseObject, Guid id)
{
databaseObject.Id = id;
}
public Dictionary<string, string> GetResourceTypeOptions(Guid id)
{
var dictionary = new Dictionary<string, string>();
var resourceType = GetById(id);
dictionary.Add("accessKey", resourceType.AccessKey);
dictionary.Add("secretKey", resourceType.SecretKey);
dictionary.Add("accessKeyRead", resourceType.AccessKeyRead);
dictionary.Add("secretKeyRead", resourceType.SecretKeyRead);
dictionary.Add("accessKeyWrite", resourceType.AccessKeyWrite);
dictionary.Add("secretKeyWrite", resourceType.SecretKeyWrite);
dictionary.Add("bucketname", resourceType.BucketName);
dictionary.Add("endpoint", resourceType.Endpoint);
dictionary.Add("size", $"{resourceType.Size}");
return dictionary;
}
}
}
using Coscine.Database.DataModel;
using Coscine.Configuration;
using Coscine.Database.DataModel;
using Coscine.Database.ReturnObjects;
using Coscine.Database.Util;
......@@ -12,6 +13,7 @@ namespace Coscine.Database.Models
{
public class ResourceModel : DatabaseModel<Resource>
{
private readonly IConfiguration _configuration = new ConsulConfiguration();
public Resource StoreFromObject(ResourceObject resourceObject)
{
if (!resourceObject.Disciplines.Any() || resourceObject.ResourceTypeOption == null)
......@@ -44,10 +46,11 @@ namespace Coscine.Database.Models
SetDisciplines(resource, resourceObject.Disciplines);
SetResourceTypeObject(resource, resourceObject.ResourceTypeOption);
}
catch (Exception e)
catch (Exception)
{
Delete(resource);
throw e;
// Makes sure to delete all FK refrences, otherwise a delete is not possible
DeleteResource(resource);
throw;
}
return resource;
......@@ -192,6 +195,34 @@ namespace Coscine.Database.Models
Update(resource);
}
}
else if (resource.Type.DisplayName == "rdss3")
{
var rdsS3ResourceTypeObject = resourceTypeOption.ToObject<RdsS3ResourceTypeObject>();
var rdsS3ResourceTypeModel = new RdsS3ResourceTypeModel();
if (resource.ResourceTypeOptionId != null)
{
var rdsS3ResourceType = rdsS3ResourceTypeModel.GetById(resource.ResourceTypeOptionId.Value);
rdsS3ResourceTypeModel.Update(rdsS3ResourceType);
}
else
{
var rdsS3ResourceType = new RdsS3resourceType()
{
BucketName = resource.Id.ToString(),
AccessKey = _configuration.GetString("coscine/global/rds/ecs-rwth/rds-s3/object_user_name"),
SecretKey = _configuration.GetString("coscine/global/rds/ecs-rwth/rds-s3/object_user_secretkey"),
AccessKeyRead = $"read_{resource.Id}",
SecretKeyRead = RandomHelper.GenerateRandomChunk(32),
AccessKeyWrite = $"write_{resource.Id}",
SecretKeyWrite = RandomHelper.GenerateRandomChunk(32),
Endpoint = _configuration.GetString("coscine/global/rds/ecs-rwth/rds-s3/s3_endpoint"),
Size = rdsS3ResourceTypeObject.Size,
};
rdsS3ResourceTypeModel.Insert(rdsS3ResourceType);
resource.ResourceTypeOptionId = rdsS3ResourceType.Id;
Update(resource);
}
}
else
{
throw new ArgumentException("Not supported resource type!");
......@@ -219,6 +250,10 @@ namespace Coscine.Database.Models
{
return new LinkedResourceTypeModel().GetResourceTypeOptions(resourceTypeOptionId);
}
else if (resourceType.DisplayName == "rdss3")
{
return new RdsS3ResourceTypeModel().GetResourceTypeOptions(resourceTypeOptionId);
}
else
{
return new Dictionary<string, string>();
......@@ -256,7 +291,7 @@ namespace Coscine.Database.Models
public int UpdateByObject(Resource resource, ResourceObject resourceObject)
{
if (resourceObject.Disciplines.Count() == 0 || resourceObject.ResourceTypeOption == null)
if (!resourceObject.Disciplines.Any() || resourceObject.ResourceTypeOption == null)
{
throw new ArgumentException("Discipline and ResourceTypeOption are necessary!");
}
......@@ -321,6 +356,11 @@ namespace Coscine.Database.Models
GitlabResourceTypeModel gitlabResourceTypeModel = new GitlabResourceTypeModel();
gitlabResourceTypeModel.Delete(gitlabResourceTypeModel.GetById(resource.ResourceTypeOptionId.Value));
}
else if (resource.Type.DisplayName == "rdss3" && resource.ResourceTypeOptionId != null)
{
var rdsS3ResourceTypeModel = new RdsS3ResourceTypeModel();
rdsS3ResourceTypeModel.Delete(rdsS3ResourceTypeModel.GetById(resource.ResourceTypeOptionId.Value));
}
}
public ResourceObject CreateReturnObjectFromDatabaseObject(Resource resource)
......@@ -370,6 +410,23 @@ namespace Coscine.Database.Models
LinkedResourceTypeModel linkedResourceTypeModel = new LinkedResourceTypeModel();
var linkedResourceType = linkedResourceTypeModel.GetById(resource.ResourceTypeOptionId.Value);
resourceTypeOptionObject = new LinkedResourceTypeObject(linkedResourceType.Id);
}
else if (resource.Type.DisplayName == "rdss3" && resource.ResourceTypeOptionId != null)
{
var rdsS3ResourceTypeModel = new RdsS3ResourceTypeModel();
var rdsS3ResourceType = rdsS3ResourceTypeModel.GetById(resource.ResourceTypeOptionId.Value);
resourceTypeOptionObject = new RdsS3ResourceTypeObject
{
Id = rdsS3ResourceType.Id,
BucketName = rdsS3ResourceType.BucketName,
Endpoint = rdsS3ResourceType.Endpoint,
ReadAccessKey = rdsS3ResourceType.AccessKeyRead,
ReadSecretKey = rdsS3ResourceType.SecretKeyRead,
Size = rdsS3ResourceType.Size,
WriteAccessKey = rdsS3ResourceType.AccessKeyWrite,
WriteSecretKey = rdsS3ResourceType.SecretKeyWrite,
};
}
return new ResourceObject(
......@@ -383,7 +440,7 @@ namespace Coscine.Database.Models
disciplines,
(resource.Visibility != null) ? new VisibilityObject(resource.Visibility.Id, resource.Visibility.DisplayName) : null,
(resource.License != null) ? new LicenseObject(resource.License.Id, resource.License.DisplayName) : null,
JObject.FromObject(resourceTypeOptionObject),
resourceTypeOptionObject == null ? new JObject() : JObject.FromObject(resourceTypeOptionObject),
resource.ApplicationProfile,
JToken.Parse(resource.FixedValues ?? "{}"),
(resource.Creator != null) ? resource.Creator : null
......@@ -404,5 +461,73 @@ namespace Coscine.Database.Models
{
databaseObject.Id = id;
}
public string GetMetadataCompleteness(ResourceObject resourceObject)
{
var maxCount = 0;
var counted = 0;
var projectProperties = typeof(ResourceObject).GetProperties();
foreach (var property in projectProperties)
{
if (property == null
|| property.PropertyType == typeof(Guid)
|| property.Name == "Type"
|| property.Name == "ResourceTypeOption"
|| property.Name == "ApplicationProfile"
|| property.Name == "FixedValues"
|| property.Name == "Creator")
{
continue;
}
maxCount++;
if (property.PropertyType == typeof(string)
&& property.GetValue(resourceObject) != null
&& !string.IsNullOrEmpty(property.GetValue(resourceObject).ToString()))
{
counted++;
}
else if (property.PropertyType == typeof(DateTime)
&& property.GetValue(resourceObject) != null)
{
counted++;
}
else if (property.PropertyType == typeof(ResourceTypeObject)
&& property.GetValue(resourceObject) != null)
{
counted++;
}
else if (property.PropertyType == typeof(IEnumerable<DisciplineObject>)
&& property.GetValue(resourceObject) != null
&& ((IEnumerable<DisciplineObject>)property.GetValue(resourceObject)).Count() > 0)
{
counted++;
}
else if (property.PropertyType == typeof(VisibilityObject)
&& property.GetValue(resourceObject) != null)
{
counted++;
}
else if (property.PropertyType == typeof(LicenseObject)
&& property.GetValue(resourceObject) != null)
{
counted++;
}
else if (property.PropertyType == typeof(JObject)
&& property.GetValue(resourceObject) != null)
{
counted++;
}
else if (property.PropertyType == typeof(JToken)
&& property.GetValue(resourceObject) != null)
{
counted++;
}
}
return $"{counted}/{maxCount}";
}
}
}
using System;
namespace Coscine.Database.ReturnObjects
{
[Serializable]
public class RdsS3ResourceTypeObject : ResourceTypeOptionObject
{
public Guid Id { get; set; }
public string BucketName { get; set; }
public string ReadAccessKey { get; set; }
public string ReadSecretKey { get; set; }
public string WriteAccessKey { get; set; }
public string WriteSecretKey { get; set; }
public string Endpoint { get; set; }
public int Size { get; set; }
}
}
\ No newline at end of file
using System.Security.Cryptography;
using System.Text;
namespace Coscine.Database.Util
{
public class RandomHelper
{
const string humanSafeChars = "ABCDEFGHJKLMNPQRSTUVWXYZ23456789";
const string urlSafeChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
const char separatorChar = '-';
static readonly RandomNumberGenerator rng = RandomNumberGenerator.Create();
public static string GenerateRandomChunk(int length, int chunks = 1, bool humanSafe = false)
{
char[] chars = (humanSafe ? humanSafeChars : urlSafeChars).ToCharArray();
byte[] randomBytes = new byte[chunks * length];
lock (rng)
{
rng.GetBytes(randomBytes);
}
StringBuilder code = new StringBuilder();
for (int i = 0; i < randomBytes.Length; i++)
{
if (i % length == 0)
{
code.Append(separatorChar);
}
code.Append(chars[randomBytes[i] % chars.Length]);
}
code.Remove(0, 1);
return code.ToString();
}
}
}
......@@ -40,6 +40,7 @@ namespace Coscine.Database.DataModel
public virtual DbSet<ProjectQuota> ProjectQuotas { get; set; }
public virtual DbSet<ProjectResource> ProjectResources { get; set; }
public virtual DbSet<ProjectRole> ProjectRoles { get; set; }
public virtual DbSet<RdsS3resourceType> RdsS3resourceTypes { get; set; }
public virtual DbSet<RdsresourceType> RdsresourceTypes { get; set; }
public virtual DbSet<Resource> Resources { get; set; }
public virtual DbSet<ResourceDiscipline> ResourceDisciplines { get; set; }
......@@ -473,6 +474,45 @@ namespace Coscine.Database.DataModel
.HasConstraintName("FK_ProjectRoles_UserId_Users_Id");
});
modelBuilder.Entity<RdsS3resourceType>(entity =>
{
entity.ToTable("RdsS3ResourceType");
entity.Property(e => e.Id).HasDefaultValueSql("(newid())");
entity.Property(e => e.AccessKey)
.IsRequired()
.HasMaxLength(200);
entity.Property(e => e.AccessKeyRead)
.IsRequired()
.HasMaxLength(200);
entity.Property(e => e.AccessKeyWrite)
.IsRequired()
.HasMaxLength(200);
entity.Property(e => e.BucketName)
.IsRequired()
.HasMaxLength(63);
entity.Property(e => e.Endpoint)
.IsRequired()
.HasMaxLength(200);
entity.Property(e => e.SecretKey)
.IsRequired()
.HasMaxLength(200);
entity.Property(e => e.SecretKeyRead)
.IsRequired()
.HasMaxLength(200);
entity.Property(e => e.SecretKeyWrite)
.IsRequired()
.HasMaxLength(200);
});
modelBuilder.Entity<RdsresourceType>(entity =>
{
entity.ToTable("RDSResourceType");
......
using System;
using System.Collections.Generic;
// Code scaffolded by EF Core assumes nullable reference types (NRTs) are not used or disabled.
// If you have enabled NRTs for your project, then un-comment the following line:
// #nullable disable
namespace Coscine.Database.DataModel
{
public partial class RdsS3resourceType
{
public Guid Id { get; set; }
public string BucketName { get; set; }
public string AccessKey { get; set; }
public string SecretKey { get; set; }
public string AccessKeyRead { get; set; }
public string SecretKeyRead { get; set; }
public string AccessKeyWrite { get; set; }
public string SecretKeyWrite { get; set; }
public string Endpoint { get; set; }
public int Size { get; set; }
}
}