Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Simplify the secrets API via an enum for the id's entity #271

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions core/src/main/kotlin/api/OrganizationsRoute.kt
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,8 @@ fun Route.organizations() = route("organizations") {
val orgId = call.requireIdParameter("organizationId")
val pagingOptions = call.pagingOptions(SortProperty("name", SortDirection.ASCENDING))

val secretsForOrganization = secretService.listForOrganization(orgId, pagingOptions.mapToModel())
val secretsForOrganization = secretService
.listSecrets(Entity.ORGANIZATION, orgId, pagingOptions.mapToModel())
val pagedResponse = PagedResponse(
secretsForOrganization.map { it.mapToApi() },
pagingOptions
Expand All @@ -192,7 +193,7 @@ fun Route.organizations() = route("organizations") {
val organizationId = call.requireIdParameter("organizationId")
val secretName = call.requireParameter("secretName")

secretService.getSecretByOrganizationIdAndName(organizationId, secretName)
secretService.getSecret(Entity.ORGANIZATION, organizationId, secretName)
?.let { call.respond(HttpStatusCode.OK, it.mapToApi()) }
?: call.respond(HttpStatusCode.NotFound)
}
Expand All @@ -206,7 +207,8 @@ fun Route.organizations() = route("organizations") {

call.respond(
HttpStatusCode.OK,
secretService.updateSecretByOrganizationAndName(
secretService.updateSecret(
Entity.ORGANIZATION,
organizationId,
secretName,
updateSecret.value.mapToModel(),
Expand All @@ -221,7 +223,7 @@ fun Route.organizations() = route("organizations") {
val organizationId = call.requireIdParameter("organizationId")
val secretName = call.requireParameter("secretName")

secretService.deleteSecretByOrganizationAndName(organizationId, secretName)
secretService.deleteSecret(Entity.ORGANIZATION, organizationId, secretName)
Copy link
Contributor

Choose a reason for hiding this comment

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

Similar to the comment on the first commit, it would be nice to have convenience functions like getRepositorySecret, deleteRepositorySecret, or listRepositorySecrets.


call.respond(HttpStatusCode.NoContent)
}
Expand Down
9 changes: 5 additions & 4 deletions core/src/main/kotlin/api/ProductsRoute.kt
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ fun Route.products() = route("products/{productId}") {
val productId = call.requireIdParameter("productId")
val pagingOptions = call.pagingOptions(SortProperty("name", SortDirection.ASCENDING))

val secretsForProduct = secretService.listForProduct(productId, pagingOptions.mapToModel())
val secretsForProduct = secretService.listSecrets(Entity.PRODUCT, productId, pagingOptions.mapToModel())
val pagedResponse = PagedResponse(
secretsForProduct.map { it.mapToApi() },
pagingOptions
Expand All @@ -155,7 +155,7 @@ fun Route.products() = route("products/{productId}") {
val productId = call.requireIdParameter("productId")
val secretName = call.requireParameter("secretName")

secretService.getSecretByProductIdAndName(productId, secretName)
secretService.getSecret(Entity.PRODUCT, productId, secretName)
?.let { call.respond(HttpStatusCode.OK, it.mapToApi()) }
?: call.respond(HttpStatusCode.NotFound)
}
Expand All @@ -169,7 +169,8 @@ fun Route.products() = route("products/{productId}") {

call.respond(
HttpStatusCode.OK,
secretService.updateSecretByProductAndName(
secretService.updateSecret(
Entity.PRODUCT,
productId,
secretName,
updateSecret.value.mapToModel(),
Expand All @@ -184,7 +185,7 @@ fun Route.products() = route("products/{productId}") {
val productId = call.requireIdParameter("productId")
val secretName = call.requireParameter("secretName")

secretService.deleteSecretByProductAndName(productId, secretName)
secretService.deleteSecret(Entity.PRODUCT, productId, secretName)

call.respond(HttpStatusCode.NoContent)
}
Expand Down
10 changes: 6 additions & 4 deletions core/src/main/kotlin/api/RepositoriesRoute.kt
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,8 @@ fun Route.repositories() = route("repositories/{repositoryId}") {
val repositoryId = call.requireIdParameter("repositoryId")
val pagingOptions = call.pagingOptions(SortProperty("name", SortDirection.ASCENDING))

val secretsForRepository = secretService.listForRepository(repositoryId, pagingOptions.mapToModel())
val secretsForRepository = secretService
.listSecrets(Entity.REPOSITORY, repositoryId, pagingOptions.mapToModel())
val pagedResponse = PagedResponse(
secretsForRepository.map { it.mapToApi() },
pagingOptions
Expand All @@ -184,7 +185,7 @@ fun Route.repositories() = route("repositories/{repositoryId}") {
val repositoryId = call.requireIdParameter("repositoryId")
val secretName = call.requireParameter("secretName")

secretService.getSecretByRepositoryIdAndName(repositoryId, secretName)
secretService.getSecret(Entity.REPOSITORY, repositoryId, secretName)
?.let { call.respond(HttpStatusCode.OK, it.mapToApi()) }
?: call.respond(HttpStatusCode.NotFound)
}
Expand All @@ -198,7 +199,8 @@ fun Route.repositories() = route("repositories/{repositoryId}") {

call.respond(
HttpStatusCode.OK,
secretService.updateSecretByRepositoryAndName(
secretService.updateSecret(
Entity.REPOSITORY,
repositoryId,
secretName,
updateSecret.value.mapToModel(),
Expand All @@ -213,7 +215,7 @@ fun Route.repositories() = route("repositories/{repositoryId}") {
val repositoryId = call.requireIdParameter("repositoryId")
val secretName = call.requireParameter("secretName")

secretService.deleteSecretByRepositoryAndName(repositoryId, secretName)
secretService.deleteSecret(Entity.REPOSITORY, repositoryId, secretName)

call.respond(HttpStatusCode.NoContent)
}
Expand Down
12 changes: 6 additions & 6 deletions core/src/test/kotlin/api/OrganizationsRouteIntegrationTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -694,7 +694,7 @@ class OrganizationsRouteIntegrationTest : AbstractIntegrationTest({
response shouldHaveStatus HttpStatusCode.Created
response shouldHaveBody Secret(secret.name, secret.description)

secretRepository.getByOrganizationIdAndName(organizationId, secret.name)?.mapToApi() shouldBe
secretRepository.get(Entity.ORGANIZATION, organizationId, secret.name)?.mapToApi() shouldBe
Secret(secret.name, secret.description)

val provider = SecretsProviderFactoryForTesting.instance()
Expand Down Expand Up @@ -732,7 +732,7 @@ class OrganizationsRouteIntegrationTest : AbstractIntegrationTest({
body.message shouldBe "Request validation has failed."
body.cause shouldContain "Validation failed for CreateSecret"

secretRepository.getByOrganizationIdAndName(organizationId, secret.name)?.mapToApi().shouldBeNull()
secretRepository.get(Entity.ORGANIZATION, organizationId, secret.name)?.mapToApi().shouldBeNull()

val provider = SecretsProviderFactoryForTesting.instance()
provider.readSecret(Path("organization_${organizationId}_${secret.name}"))?.value.shouldBeNull()
Expand Down Expand Up @@ -767,7 +767,7 @@ class OrganizationsRouteIntegrationTest : AbstractIntegrationTest({
response shouldHaveStatus HttpStatusCode.OK
response shouldHaveBody Secret(secret.name, updatedDescription)

secretRepository.getByOrganizationIdAndName(organizationId, updateSecret.name.valueOrThrow)
secretRepository.get(Entity.ORGANIZATION, organizationId, updateSecret.name.valueOrThrow)
?.mapToApi() shouldBe Secret(secret.name, updatedDescription)
}
}
Expand Down Expand Up @@ -800,7 +800,7 @@ class OrganizationsRouteIntegrationTest : AbstractIntegrationTest({
setBody(updateSecret)
} shouldHaveStatus HttpStatusCode.InternalServerError

secretRepository.getByOrganizationIdAndName(organizationId, secret.name) shouldBe secret
secretRepository.get(Entity.ORGANIZATION, organizationId, secret.name) shouldBe secret
}
}

Expand All @@ -825,7 +825,7 @@ class OrganizationsRouteIntegrationTest : AbstractIntegrationTest({
superuserClient.delete("/api/v1/organizations/$organizationId/secrets/${secret.name}") shouldHaveStatus
HttpStatusCode.NoContent

secretRepository.listForOrganization(organizationId) shouldBe emptyList()
secretRepository.list(Entity.ORGANIZATION, organizationId) shouldBe emptyList()

val provider = SecretsProviderFactoryForTesting.instance()
provider.readSecret(Path(secret.path)) should beNull()
Expand Down Expand Up @@ -868,7 +868,7 @@ class OrganizationsRouteIntegrationTest : AbstractIntegrationTest({
superuserClient.delete("/api/v1/organizations/$organizationId/secrets/${secret.name}") shouldHaveStatus
HttpStatusCode.InternalServerError

secretRepository.getByOrganizationIdAndName(organizationId, secret.name) shouldBe secret
secretRepository.get(Entity.ORGANIZATION, organizationId, secret.name) shouldBe secret
}
}

Expand Down
12 changes: 6 additions & 6 deletions core/src/test/kotlin/api/ProductsRouteIntegrationTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -482,7 +482,7 @@ class ProductsRouteIntegrationTest : AbstractIntegrationTest({
response shouldHaveStatus HttpStatusCode.Created
response shouldHaveBody Secret(secret.name, secret.description)

secretRepository.getByProductIdAndName(productId, secret.name)?.mapToApi() shouldBe
secretRepository.get(Entity.PRODUCT, productId, secret.name)?.mapToApi() shouldBe
Secret(secret.name, secret.description)

val provider = SecretsProviderFactoryForTesting.instance()
Expand Down Expand Up @@ -520,7 +520,7 @@ class ProductsRouteIntegrationTest : AbstractIntegrationTest({
body.message shouldBe "Request validation has failed."
body.cause shouldContain "Validation failed for CreateSecret"

secretRepository.getByProductIdAndName(productId, secret.name)?.mapToApi().shouldBeNull()
secretRepository.get(Entity.PRODUCT, productId, secret.name)?.mapToApi().shouldBeNull()

val provider = SecretsProviderFactoryForTesting.instance()
provider.readSecret(Path("product_${productId}_${secret.name}"))?.value shouldBe null
Expand Down Expand Up @@ -555,7 +555,7 @@ class ProductsRouteIntegrationTest : AbstractIntegrationTest({
response shouldHaveStatus HttpStatusCode.OK
response shouldHaveBody Secret(secret.name, updatedDescription)

secretRepository.getByProductIdAndName(productId, updateSecret.name.valueOrThrow)?.mapToApi() shouldBe
secretRepository.get(Entity.PRODUCT, productId, updateSecret.name.valueOrThrow)?.mapToApi() shouldBe
Secret(secret.name, updatedDescription)
}
}
Expand Down Expand Up @@ -588,7 +588,7 @@ class ProductsRouteIntegrationTest : AbstractIntegrationTest({
setBody(updateSecret)
} shouldHaveStatus HttpStatusCode.InternalServerError

secretRepository.getByProductIdAndName(productId, secret.name) shouldBe secret
secretRepository.get(Entity.PRODUCT, productId, secret.name) shouldBe secret
}
}

Expand All @@ -613,7 +613,7 @@ class ProductsRouteIntegrationTest : AbstractIntegrationTest({
superuserClient.delete("/api/v1/products/$productId/secrets/${secret.name}") shouldHaveStatus
HttpStatusCode.NoContent

secretRepository.listForProduct(productId) shouldBe emptyList()
secretRepository.list(Entity.PRODUCT, productId) shouldBe emptyList()

val provider = SecretsProviderFactoryForTesting.instance()
provider.readSecret(Path(secret.path)) should beNull()
Expand Down Expand Up @@ -655,7 +655,7 @@ class ProductsRouteIntegrationTest : AbstractIntegrationTest({
superuserClient.delete("/api/v1/products/$productId/secrets/${secret.name}") shouldHaveStatus
HttpStatusCode.InternalServerError

secretRepository.getByProductIdAndName(productId, secret.name) shouldBe secret
secretRepository.get(Entity.PRODUCT, productId, secret.name) shouldBe secret
}
}

Expand Down
12 changes: 6 additions & 6 deletions core/src/test/kotlin/api/RepositoriesRouteIntegrationTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -654,7 +654,7 @@ class RepositoriesRouteIntegrationTest : AbstractIntegrationTest({
response shouldHaveStatus HttpStatusCode.Created
response shouldHaveBody Secret(secret.name, secret.description)

secretRepository.getByRepositoryIdAndName(repositoryId, secret.name)?.mapToApi() shouldBe
secretRepository.get(Entity.REPOSITORY, repositoryId, secret.name)?.mapToApi() shouldBe
Secret(secret.name, secret.description)

val provider = SecretsProviderFactoryForTesting.instance()
Expand Down Expand Up @@ -703,7 +703,7 @@ class RepositoriesRouteIntegrationTest : AbstractIntegrationTest({
body.message shouldBe "Request validation has failed."
body.cause shouldContain "Validation failed for CreateSecret"

secretRepository.getByRepositoryIdAndName(repositoryId, secret.name)?.mapToApi().shouldBeNull()
secretRepository.get(Entity.REPOSITORY, repositoryId, secret.name)?.mapToApi().shouldBeNull()

val provider = SecretsProviderFactoryForTesting.instance()
provider.readSecret(Path("repository_${repositoryId}_${secret.name}"))?.value.shouldBeNull()
Expand All @@ -727,7 +727,7 @@ class RepositoriesRouteIntegrationTest : AbstractIntegrationTest({
response shouldHaveStatus HttpStatusCode.OK
response shouldHaveBody Secret(secret.name, updatedDescription)

secretRepository.getByRepositoryIdAndName(repositoryId, updateSecret.name.valueOrThrow)
secretRepository.get(Entity.REPOSITORY, repositoryId, updateSecret.name.valueOrThrow)
?.mapToApi() shouldBe Secret(secret.name, updatedDescription)
}
}
Expand Down Expand Up @@ -760,7 +760,7 @@ class RepositoriesRouteIntegrationTest : AbstractIntegrationTest({
setBody(updateSecret)
} shouldHaveStatus HttpStatusCode.InternalServerError

secretRepository.getByRepositoryIdAndName(repositoryId, secret.name) shouldBe secret
secretRepository.get(Entity.REPOSITORY, repositoryId, secret.name) shouldBe secret
}
}

Expand All @@ -785,7 +785,7 @@ class RepositoriesRouteIntegrationTest : AbstractIntegrationTest({
superuserClient.delete("/api/v1/repositories/$repositoryId/secrets/${secret.name}") shouldHaveStatus
HttpStatusCode.NoContent

secretRepository.listForRepository(repositoryId) shouldBe emptyList()
secretRepository.list(Entity.REPOSITORY, repositoryId) shouldBe emptyList()

val provider = SecretsProviderFactoryForTesting.instance()
provider.readSecret(Path(secret.path)) should beNull()
Expand All @@ -800,7 +800,7 @@ class RepositoriesRouteIntegrationTest : AbstractIntegrationTest({
superuserClient.delete("/api/v1/repositories/$repositoryId/secrets/${secret.name}") shouldHaveStatus
HttpStatusCode.InternalServerError

secretRepository.getByRepositoryIdAndName(repositoryId, secret.name) shouldBe secret
secretRepository.get(Entity.REPOSITORY, repositoryId, secret.name) shouldBe secret
}
}

Expand Down
Loading
Loading