Skip to content

Commit

Permalink
TOAZ-230 Delete Azure App (#2986)
Browse files Browse the repository at this point in the history
* TOAZ-230 Delete Azure App

* TOAZ-230 Delete Azure App

* TOAZ-230 Delete Azure App

* TOAZ-230 Delete Azure App

* TOAZ-230 Delete Azure App

* TOAZ-230 Delete Azure App

* TOAZ-230 Delete Azure App

* TOAZ-230 Delete Azure App

* TOAZ-230 Delete Azure App

* TOAZ-230 Delete Azure App

* TOAZ-230 Delete Azure App

* TOAZ-230 Delete Azure App

* TOAZ-230 Delete Azure App

* TOAZ-230 Delete Azure App

* TOAZ-230 Delete Azure App

* TOAZ-230 Delete Azure App

* TOAZ-230 Delete Azure App

* TOAZ-230 Delete Azure App

* TOAZ-230 Delete Azure App

* TOAZ-230 Delete Azure App

* TOAZ-230 Delete Azure App

* TOAZ-230 Delete Azure App

* TOAZ-230 Delete Azure App

* TOAZ-230 Delete Azure App

* TOAZ-230 Delete Azure App

* TOAZ-230 Delete Azure App

* TOAZ-230 Delete Azure App

* TOAZ-230 Delete Azure App

* TOAZ-230 Delete Azure App

* TOAZ-230 Delete Azure App

* TOAZ-230 Delete Azure App

* TOAZ-230 Delete Azure App

* TOAZ-230 Delete Azure App

* TOAZ-230 Delete Azure App

* TOAZ-230 Delete Azure App

* TOAZ-230 Delete Azure App
  • Loading branch information
Ghost-in-a-Jar authored Nov 30, 2022
1 parent d3b1748 commit 7d4ecba
Show file tree
Hide file tree
Showing 16 changed files with 489 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,10 @@ final class LeoPublisher[F[_]](
KubernetesServiceDbQueries
.markPendingCreating(m.appId, None, None, None)
.transaction
case _: LeoPubsubMessage.DeleteAppV2Message =>
// TODO: TOAZ-230
F.unit
case m: LeoPubsubMessage.DeleteAppV2Message =>
KubernetesServiceDbQueries
.markPendingAppDeletion(m.appId, m.diskId, now)
.transaction
}
} yield ()
}
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ class HttpSamDAO[F[_]](httpClient: Client[F],
): F[Unit] =
deleteResourceInternal(resource, Authorization(Credentials.Token(AuthScheme.Bearer, userInfo.accessToken.token)))

private def deleteResourceInternal[R](resource: R, authHeader: Authorization)(implicit
def deleteResourceInternal[R](resource: R, authHeader: Authorization)(implicit
sr: SamResource[R],
ev: Ask[F, TraceId]
): F[Unit] =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,18 @@ trait SamDAO[F[_]] {
ev: Ask[F, TraceId]
): F[Unit]

/** Deletes a Sam resource R using the provided user token. */
/** Deletes a Sam resource R using the provided user info. */
def deleteResourceWithUserInfo[R](resource: R, userInfo: UserInfo)(implicit
sr: SamResource[R],
ev: Ask[F, TraceId]
): F[Unit]

/** Deletes a Sam resource R using the provided bearer token. */
def deleteResourceInternal[R](resource: R, authHeader: Authorization)(implicit
sr: SamResource[R],
ev: Ask[F, TraceId]
): F[Unit]

/** Gets a pet GCP service account for the calling user. */
def getPetServiceAccount(authorization: Authorization, googleProject: GoogleProject)(implicit
ev: Ask[F, TraceId]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import org.broadinstitute.dsde.workbench.google2.{
}
import org.broadinstitute.dsde.workbench.leonardo.AppRestore.{CromwellRestore, GalaxyRestore}
import org.broadinstitute.dsde.workbench.leonardo.AppType._
import org.broadinstitute.dsde.workbench.leonardo.CloudContext
import org.broadinstitute.dsde.workbench.leonardo.JsonCodec._
import org.broadinstitute.dsde.workbench.leonardo.SamResourceId._
import org.broadinstitute.dsde.workbench.leonardo.config._
Expand Down Expand Up @@ -663,6 +664,27 @@ final class LeoAppServiceInterp[F[_]: Parallel](config: AppServiceConfig,
// Get the disk to delete if specified
diskOpt = if (deleteDisk) appResult.app.appResources.disk.map(_.id) else None

// Resolve the workspace in WSM to get the cloud context
userToken = org.http4s.headers.Authorization(
org.http4s.Credentials.Token(AuthScheme.Bearer, userInfo.accessToken.token)
)
workspaceDescOpt <- wsmDao.getWorkspace(workspaceId, userToken)
workspaceDesc <- F.fromOption(workspaceDescOpt, WorkspaceNotFoundException(workspaceId, ctx.traceId))
cloudContext <- (workspaceDesc.azureContext, workspaceDesc.gcpContext) match {
case (Some(azureContext), _) => F.pure[CloudContext](CloudContext.Azure(azureContext))
case (_, Some(gcpContext)) => F.pure[CloudContext](CloudContext.Gcp(gcpContext))
case (None, None) => F.raiseError[CloudContext](CloudContextNotFoundException(workspaceId, ctx.traceId))
}

// Get the Landing Zone Resources for the app for Azure
landingZoneResourcesOpt <- cloudContext.cloudProvider match {
case CloudProvider.Gcp => F.pure(None)
case CloudProvider.Azure =>
for {
landingZoneResources <- getLandingZoneResources(workspaceDesc.spendProfile, userToken)
} yield Some(landingZoneResources)
}

_ <-
if (appResult.app.status == AppStatus.Error) {
for {
Expand All @@ -677,7 +699,9 @@ final class LeoAppServiceInterp[F[_]: Parallel](config: AppServiceConfig,
appResult.app.id,
appResult.app.appName,
workspaceId,
cloudContext,
diskOpt,
landingZoneResourcesOpt,
Some(ctx.traceId)
)
_ <- publisherQueue.offer(deleteMessage)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,7 @@ class LeoPubsubMessageSubscriber[F[_]](
)
}
case msg: CreateAppV2Message => handleCreateAppV2Message(msg)
case _: DeleteAppV2Message =>
// TODO: TOAZ-230
F.unit
case msg: DeleteAppV2Message => handleDeleteAppV2Message(msg)
}
} yield resp

Expand Down Expand Up @@ -1447,4 +1445,37 @@ class LeoPubsubMessageSubscriber[F[_]](
)
}
} yield ()

private[monitor] def handleDeleteAppV2Message(
msg: DeleteAppV2Message
)(implicit ev: Ask[F, AppContext]): F[Unit] =
for {
ctx <- ev.ask
_ <- msg.cloudContext match {
case CloudContext.Azure(c) =>
val task =
azurePubsubHandler.deleteApp(msg.appId, msg.appName, msg.workspaceId, msg.landingZoneResourcesOpt, c)
asyncTasks.offer(
Task(ctx.traceId, task, Some(handleKubernetesError), ctx.now, "deleteAppV2")
)
case CloudContext.Gcp(c) =>
F.raiseError(
PubsubKubernetesError(
AppError(
s"Error creating GCP app with id ${msg.appId} and cloudContext ${c.value}: DeleteAppV2 not supported for GCP",
ctx.now,
ErrorAction.CreateApp,
ErrorSource.App,
None,
Some(ctx.traceId)
),
Some(msg.appId),
false,
None,
None,
None
)
)
}
} yield ()
}
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,10 @@ object LeoPubsubMessageType extends Enum[LeoPubsubMessageType] {
final case object CreateAppV2 extends LeoPubsubMessageType {
val asString = "createAppV2"
}

final case object DeleteAppV2 extends LeoPubsubMessageType {
val asString = "deleteAppV2"
}
}

sealed trait LeoPubsubMessage {
Expand Down Expand Up @@ -286,10 +290,12 @@ object LeoPubsubMessage {
final case class DeleteAppV2Message(appId: AppId,
appName: AppName,
workspaceId: WorkspaceId,
cloudContext: CloudContext,
diskId: Option[DiskId],
landingZoneResourcesOpt: Option[LandingZoneResources],
traceId: Option[TraceId]
) extends LeoPubsubMessage {
val messageType: LeoPubsubMessageType = LeoPubsubMessageType.DeleteApp
val messageType: LeoPubsubMessageType = LeoPubsubMessageType.DeleteAppV2
}

final case class StopAppMessage(appId: AppId, appName: AppName, project: GoogleProject, traceId: Option[TraceId])
Expand Down Expand Up @@ -530,6 +536,18 @@ object LeoPubsubCodec {
CreateAppV2Message.apply
)

implicit val deleteAppV2Decoder: Decoder[DeleteAppV2Message] =
Decoder.forProduct7("appId",
"appName",
"workspaceId",
"cloudContext",
"diskId",
"landingZoneResourcesOpt",
"tradeId"
)(
DeleteAppV2Message.apply
)

implicit val leoPubsubMessageDecoder: Decoder[LeoPubsubMessage] = Decoder.instance { message =>
for {
messageType <- message.downField("messageType").as[LeoPubsubMessageType]
Expand All @@ -549,6 +567,7 @@ object LeoPubsubCodec {
case LeoPubsubMessageType.CreateAzureRuntime => message.as[CreateAzureRuntimeMessage]
case LeoPubsubMessageType.DeleteAzureRuntime => message.as[DeleteAzureRuntimeMessage]
case LeoPubsubMessageType.CreateAppV2 => message.as[CreateAppV2Message]
case LeoPubsubMessageType.DeleteAppV2 => message.as[DeleteAppV2Message]
}
} yield value
}
Expand Down Expand Up @@ -882,8 +901,16 @@ object LeoPubsubCodec {
)(x => (x.messageType, x.appId, x.appName, x.workspaceId, x.cloudContext, x.landingZoneResourcesOpt, x.traceId))

implicit val deleteAppV2MessageEncoder: Encoder[DeleteAppV2Message] =
Encoder.forProduct6("messageType", "appId", "appName", "workspaceId", "diskId", "traceId")(x =>
(x.messageType, x.appId, x.appName, x.workspaceId, x.diskId, x.traceId)
Encoder.forProduct8("messageType",
"appId",
"appName",
"workspaceId",
"cloudContext",
"diskId",
"landingZoneResourcesOpt",
"traceId"
)(x =>
(x.messageType, x.appId, x.appName, x.workspaceId, x.cloudContext, x.diskId, x.landingZoneResourcesOpt, x.traceId)
)

implicit val leoPubsubMessageEncoder: Encoder[LeoPubsubMessage] = Encoder.instance {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ trait AKSAlgebra[F[_]] {
/** Creates an app and polls it for completion */
def createAndPollApp(params: CreateAKSAppParams)(implicit ev: Ask[F, AppContext]): F[Unit]

// TODO (TOAZ-230): delete, stop, start
def deleteApp(params: DeleteAKSAppParams)(implicit ev: Ask[F, AppContext]): F[Unit]

}

Expand All @@ -19,3 +19,11 @@ final case class CreateAKSAppParams(appId: AppId,
landingZoneResourcesOpt: Option[LandingZoneResources],
cloudContext: AzureCloudContext
)

final case class DeleteAKSAppParams(
appName: AppName,
workspaceId: WorkspaceId,
landingZoneResourcesOpt: Option[LandingZoneResources],
cloudContext: AzureCloudContext,
keepHistory: Boolean = false
)
Loading

0 comments on commit 7d4ecba

Please sign in to comment.