8000
Skip to content

Auth: Entity type refactor#13262

Closed
markylaing wants to merge 45 commits intocanonical:mainfrom
markylaing:entity-type-refactor
Closed

Auth: Entity type refactor#13262
markylaing wants to merge 45 commits intocanonical:mainfrom
markylaing:entity-type-refactor

Conversation

@markylaing
Copy link
Copy Markdown
Contributor
@markylaing markylaing commented Apr 5, 2024

Opening as a draft to get some feedback.

  • The type of entity.Type has been changed to an interface.
  • A new type has been created for each shared entity type that implements the interface. These types are empty structs for the purpose of embedding them into the database type. The types are defined as entity.Instance, entity.Network and so on. A variable is also defined for each entity type for ease of use (e.g. var TypeInstance = Instance{}).
  • Because each entity requires different arguments for creating a URL, these methods are defined on the concrete types and this is not part of the interface.
  • In the db/cluster package, the EntityType type is now a struct which embeds an entityType interface, the entityType interface embeds (shared/entity).Type and has methods for returning SQL for each entity type.
  • A consequence of using interfaces for entity types is that we need to be more careful when checking for equality. We need to always compare them by the Name() function (e.g. entityType1.Name() == entityType2.Name(). I've added an Equals method to the shared/entity package for this.

In order to add a new entity type, one needs to implement the interface in the shared package following the same pattern, and add the entity type to the list of all entity types in that package. Afterwards, the interface in the database package must be implemented (with the shared entity type being embedded in the database type) and this type must also be added to the list in the database package.

Closes #12928

@markylaing markylaing self-assigned this Apr 5, 2024
@markylaing markylaing force-pushed the entity-type-refactor branch 5 times, most recently from 829e518 to c62358f Compare April 10, 2024 07:35
@markylaing markylaing marked this pull request as ready for review April 10, 2024 08:46
@markylaing markylaing requested a review from tomponline as a code owner April 10, 2024 08:46
@markylaing
Copy link
Copy Markdown
Contributor Author

@tomponline this is ready for review when you have some time. It will likely conflict with #13298 so I'll need to rebase when that's merged.

@markylaing markylaing force-pushed the entity-type-refactor branch from c62358f to 550d572 Compare April 10, 2024 12:58
@markylaing
Copy link
Copy Markdown
Contributor Author

Rebased and ready for review. Thanks.

Comment thread shared/entity/type.go
Comment thread shared/entity/type.go Outdated
Comment thread shared/entity/type.go
Comment thread shared/entity/type.go Outdated
@markylaing markylaing force-pushed the entity-type-refactor branch from 550d572 to 7ee82c6 Compare April 11, 2024 11:57
@markylaing
Copy link
Copy Markdown
Contributor Author

Ready for round two :)

@markylaing markylaing requested a review from tomponline April 11, 2024 12:02
@markylaing markylaing force-pushed the entity-type-refactor branch from 7ee82c6 to ccf8b55 Compare April 11, 2024 13:50
@markylaing
Copy link
Copy Markdown
Contributor Author

I've just pushed again as I realised when working on #13072 that I had missed some equality checks that should now be using the entity.Equals method.

Comment thread lxd/db/cluster/entities.go
if len(entityTypesFilter) == 0 && projectName == "" {
for _, dbEntityType := range entityTypes {
stmt := dbEntityType.AllURLs()
if stmt != "" {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should an empty stmt be an error condition?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmmm... for this one perhaps but not for URLsByProject for example, as not all entities are project specific.

The implementation of the "server" entityType in the cluster package returns an empty string for AllURLs because there is no database query should be run for it (the caller should know that it's /1.0).

Comment thread lxd/storage/backend_lxd.go Outdated
Comment thread lxd/db/cluster/entities.go Outdated
Comment thread lxd/db/cluster/entity_type_auth_group.go Outdated
Comment thread lxd/db/cluster/entities.go
Additionally, remove the "nolint" directive as this function is now in
use.

Signed-off-by: Mark Laing <mark.laing@canonical.com>
Signed-off-by: Mark Laing <mark.laing@canonical.com>
Signed-off-by: Mark Laing <mark.laing@canonical.com>
Signed-off-by: Mark Laing <mark.laing@canonical.com>
Signed-off-by: Mark Laing <mark.laing@canonical.com>
The entity deletion triggers are now composed of a trigger name
and the SQL for creating it.

Signed-off-by: Mark Laing <mark.laing@canonical.com>
Create a map of `entity.TypeName` to `[]Entitlement` for
entitlement validation.

Signed-off-by: Mark Laing <mark.laing@canonical.com>
Signed-off-by: Mark Laing <mark.laing@canonical.com>
…ument.

Signed-off-by: Mark Laing <mark.laing@canonical.com>
Signed-off-by: Mark Laing <mark.laing@canonical.com>
Signed-off-by: Mark Laing <mark.laing@canonical.com>
Signed-off-by: Mark Laing <mark.laing@canonical.com>
Signed-off-by: Mark Laing <mark.laing@canonical.com>
Signed-off-by: Mark Laing <mark.laing@canonical.com>
Signed-off-by: Mark Laing <mark.laing@canonical.com>
Signed-off-by: Mark Laing <mark.laing@canonical.com>
Signed-off-by: Mark Laing <mark.laing@canonical.com>
Signed-off-by: Mark Laing <mark.laing@canonical.com>
Signed-off-by: Mark Laing <mark.laing@canonical.com>
Signed-off-by: Mark Laing <mark.laing@canonical.com>
Signed-off-by: Mark Laing <mark.laing@canonical.com>
Signed-off-by: Mark Laing <mark.laing@canonical.com>
Signed-off-by: Mark Laing <mark.laing@canonical.com>
Signed-off-by: Mark Laing <mark.laing@canonical.com>
@markylaing markylaing force-pushed the entity-type-refactor branch from b009f89 to d0b2b3d Compare June 13, 2024 08:41
@markylaing markylaing marked this pull request as ready for review June 13, 2024 08:44
@markylaing
Copy link
Copy Markdown
Contributor Author

@tomponline have rebased

tomponline added a commit that referenced this pull request Aug 2, 2024
@tomponline @hamistao this is a much more concise entity type refactor
that I think works much better. (Thanks Tom for the idea of keeping the
types as they are, but storing them in a map the returns a
struct/interface with relevant fields/methods).

With this change, I think it should be easy to add the URL prefix
classification by:
1. Adding a `PrefixMatch() []string` method to `typeImpl` that returns a
slice of URL prefixes to match the entity type against.
2. Adding a function `MatchURLPrefix` which iterates over the
`entityType` map, and iterates over the `PrefixMatch()` array for each
`typeImpl`, then returns the entity type if `strings.HasPrefix(u.Path,
prefix)`.

Closes #13262
Closes #12928
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Refactor entity types for maintainability

2 participants

0