Skip to content

Commit

Permalink
Enforcing uniqueness of AWSAccessPointUser for (user, aws).
Browse files Browse the repository at this point in the history
  • Loading branch information
Chrystinne committed Jan 22, 2025
1 parent 187fe98 commit a5892d1
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 11 deletions.
33 changes: 23 additions & 10 deletions physionet-django/project/cloud/s3.py
Original file line number Diff line number Diff line change
Expand Up @@ -1235,14 +1235,13 @@ def initialize_access_points(project):

def associate_aws_users_with_data_access_point(access_point, aws_accounts):
"""
Associates a list of `aws_accounts` with the
`AWSAccessPoint`.
Associates a list of `aws_accounts` with the `AWSAccessPoint`.
Args:
access_point (AWSAccessPoint): The access point to
which the accounts will be associated.
aws_accounts (list): List of dictionaries containing
`aws_id` and `aws_userid` to be associated.
access_point (AWSAccessPoint): The access point to which
the accounts will be associated.
aws_accounts (list): List of dictionaries containing `aws_id`
and `aws_userid` to be associated.
Returns:
bool: True if the association was successfully created,
Expand All @@ -1258,17 +1257,31 @@ def associate_aws_users_with_data_access_point(access_point, aws_accounts):
cloud_info = CloudInformation.objects.filter(
Q(aws_id=aws_id) | Q(aws_userid=aws_userid)
).first()

if not cloud_info:
print(f"User not found for aws_id: {aws_id} or aws_userid: {aws_userid}")
continue

user = cloud_info.user

# Get the AWS instance associated with the access point
aws_instance = access_point.aws

# Check if the user is already associated with the access_point
if not AWSAccessPointUser.objects.filter(access_point=access_point, user=user).exists():
AWSAccessPointUser.objects.create(access_point=access_point, user=user)
existing_association = AWSAccessPointUser.objects.filter(
access_point=access_point,
user=user,
aws=aws_instance
).first()

if not existing_association:
# Create the association with the required fields
AWSAccessPointUser.objects.create(
access_point=access_point,
user=user,
aws=aws_instance
)

# After iterating through all the AWS accounts, save the access_point
access_point.save()
return True
except Exception as e:
print(f"Error associating aws_accounts with access_point: {str(e)}")
Expand Down
7 changes: 6 additions & 1 deletion physionet-django/project/modelcomponents/storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,14 @@ class AWSAccessPointUser(models.Model):
related_name='aws_access_point_users',
on_delete=models.CASCADE
)
aws = models.ForeignKey(
AWS,
related_name='access_point_users',
on_delete=models.CASCADE
)

class Meta:
unique_together = [('access_point', 'user')]
unique_together = [('user', 'aws')]

def __str__(self):
return f"User: {self.user}, Access Point: {self.access_point}"

0 comments on commit a5892d1

Please sign in to comment.