Skip to content

Commit

Permalink
Merge pull request #5194 from systeminit/chore(sdf)-Migrate-module-re…
Browse files Browse the repository at this point in the history
…mote-spec-endpoints-to-v2-api

chore(sdf): Migrate module remote spec endpoints to v2 api
  • Loading branch information
stack72 authored Jan 3, 2025
2 parents 5558101 + f698e60 commit 3849bf5
Show file tree
Hide file tree
Showing 5 changed files with 127 additions and 116 deletions.
153 changes: 78 additions & 75 deletions app/web/src/store/module.store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,7 @@ export const useModuleStore = () => {
},
},
actions: {
// Modules API V2
async SYNC() {
return new ApiRequest<
{
Expand Down Expand Up @@ -278,14 +279,88 @@ export const useModuleStore = () => {
async GET_LOCAL_MODULE_DETAILS(hash: ModuleHash) {
return new ApiRequest<LocalModuleDetails>({
method: "get",
url: "/module/get_module_by_hash",
params: { hash, ...getVisibilityParams() },
url: API_PREFIX.concat(["module_by_hash"]),
params: { hash },
onSuccess: (response) => {
this.localModuleDetailsByName[response.name] = response;
},
});
},

async GET_REMOTE_MODULE_SPEC(id: ModuleId) {
return new ApiRequest<ModuleSpec>({
method: "get",
url: API_PREFIX.concat(["module_by_id"]),
keyRequestStatusBy: id,
params: { id },
onSuccess: (response) => {
this.remoteModuleSpecsById[id] = response;
},
});
},

async INSTALL_REMOTE_MODULE(moduleIds: ModuleId[]) {
if (changeSetsStore.creatingChangeSet)
throw new Error("race, wait until the change set is created");
if (changeSetId === changeSetsStore.headChangeSetId)
changeSetsStore.creatingChangeSet = true;

return new ApiRequest<{ id: string }>({
method: "post",
url: "/module/install_module",
keyRequestStatusBy: moduleIds,
params: {
ids: moduleIds,
...getVisibilityParams(),
},
onFail: () => {
changeSetsStore.creatingChangeSet = false;
},
onSuccess: () => {
// reset installed list
this.SYNC();
},
});
},

async REJECT_REMOTE_MODULE(moduleId: ModuleId) {
return new ApiRequest<{ success: true }>({
method: "post",
url: API_PREFIX.concat([{ moduleId }, "builtins", "reject"]),
params: { id: moduleId, ...getVisibilityParams() },
optimistic: () => {
// remove selection from URL
router.replace({
name: "workspace-lab-packages",
});
},
onSuccess: (_response) => {
// response is just success, so we have to reload the remote modules
this.LOAD_LOCAL_MODULES();
this.GET_REMOTE_MODULES_LIST({ su: true });
},
});
},

async PROMOTE_TO_BUILTIN(moduleId: ModuleId) {
return new ApiRequest<{ success: true }>({
method: "post",
url: API_PREFIX.concat([{ moduleId }, "builtins", "promote"]),
optimistic: () => {
// remove selection from URL
router.replace({
name: "workspace-lab-packages",
});
},
onSuccess: (_response) => {
// response is just success, so we have to reload the remote modules
this.LOAD_LOCAL_MODULES();
this.GET_REMOTE_MODULES_LIST({ su: true });
},
});
},

// Module Index API
async LIST_WORKSPACE_EXPORTS() {
return new ModuleIndexApiRequest<{
modules: (RemoteModuleSummary & {
Expand Down Expand Up @@ -360,79 +435,7 @@ export const useModuleStore = () => {
});
},

async GET_REMOTE_MODULE_SPEC(id: ModuleId) {
return new ApiRequest<ModuleSpec>({
method: "get",
url: "/module/remote_module_spec",
keyRequestStatusBy: id,
params: { id, ...getVisibilityParams() },
onSuccess: (response) => {
this.remoteModuleSpecsById[id] = response;
},
});
},

async INSTALL_REMOTE_MODULE(moduleIds: ModuleId[]) {
if (changeSetsStore.creatingChangeSet)
throw new Error("race, wait until the change set is created");
if (changeSetId === changeSetsStore.headChangeSetId)
changeSetsStore.creatingChangeSet = true;

return new ApiRequest<{ id: string }>({
method: "post",
url: "/module/install_module",
keyRequestStatusBy: moduleIds,
params: {
ids: moduleIds,
...getVisibilityParams(),
},
onFail: () => {
changeSetsStore.creatingChangeSet = false;
},
onSuccess: () => {
// reset installed list
this.SYNC();
},
});
},

async REJECT_REMOTE_MODULE(moduleId: ModuleId) {
return new ApiRequest<{ success: true }>({
method: "post",
url: API_PREFIX.concat([{ moduleId }, "builtins", "reject"]),
params: { id: moduleId, ...getVisibilityParams() },
optimistic: () => {
// remove selection from URL
router.replace({
name: "workspace-lab-packages",
});
},
onSuccess: (_response) => {
// response is just success, so we have to reload the remote modules
this.LOAD_LOCAL_MODULES();
this.GET_REMOTE_MODULES_LIST({ su: true });
},
});
},

async PROMOTE_TO_BUILTIN(moduleId: ModuleId) {
return new ApiRequest<{ success: true }>({
method: "post",
url: API_PREFIX.concat([{ moduleId }, "builtins", "promote"]),
optimistic: () => {
// remove selection from URL
router.replace({
name: "workspace-lab-packages",
});
},
onSuccess: (_response) => {
// response is just success, so we have to reload the remote modules
this.LOAD_LOCAL_MODULES();
this.GET_REMOTE_MODULES_LIST({ su: true });
},
});
},

// Workspace API V2
async EXPORT_WORKSPACE() {
this.exportingWorkspaceOperationId = null;
this.exportingWorkspaceOperationError = undefined;
Expand Down
9 changes: 1 addition & 8 deletions lib/sdf-server/src/service/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::path::{Path, PathBuf};
use axum::{
http::StatusCode,
response::{IntoResponse, Response},
routing::{get, post},
routing::post,
Router,
};
use convert_case::{Case, Casing};
Expand Down Expand Up @@ -31,10 +31,8 @@ const PKG_EXTENSION: &str = "sipkg";
const MAX_NAME_SEARCH_ATTEMPTS: usize = 100;

pub mod approval_process;
pub mod get_module;
pub mod import_workspace_vote;
pub mod install_module;
pub mod remote_module_spec;

#[remain::sorted]
#[derive(Error, Debug)]
Expand Down Expand Up @@ -239,12 +237,7 @@ pub async fn pkg_open(builder: &DalContextBuilder, file_name: &str) -> ModuleRes

pub fn routes() -> Router<AppState> {
Router::new()
.route("/get_module_by_hash", get(get_module::get_module_by_hash))
.route("/install_module", post(install_module::install_module))
.route(
"/remote_module_spec",
get(remote_module_spec::remote_module_spec),
)
.route(
"/begin_approval_process",
post(approval_process::begin_approval_process),
Expand Down
10 changes: 10 additions & 0 deletions lib/sdf-server/src/service/v2/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use axum::{
};
use dal::UserError;
use si_frontend_types as frontend_types;
use si_pkg::SiPkgError;
use telemetry::prelude::*;
use thiserror::Error;

Expand All @@ -14,6 +15,8 @@ use crate::{service::ApiError, AppState};
mod builtins;
mod contribute;
mod list;
mod module_by_hash;
mod module_by_id;
mod sync;

pub type ModuleAPIResult<T> = Result<T, ModulesAPIError>;
Expand All @@ -27,12 +30,16 @@ pub enum ModulesAPIError {
ContributionFailure(frontend_types::ModuleContributeRequest),
#[error("module error: {0}")]
Module(#[from] dal::module::ModuleError),
#[error("Module hash not be found: {0}")]
ModuleHashNotFound(String),
#[error("module index client error: {0}")]
ModuleIndexClient(#[from] module_index_client::ModuleIndexClientError),
#[error("module index not configured")]
ModuleIndexNotConfigured,
#[error("schema error: {0}")]
SchemaVariant(#[from] dal::SchemaVariantError),
#[error("si pkg error: {0}")]
SiPkg(#[from] SiPkgError),
#[error("transactions error: {0}")]
Transactions(#[from] dal::TransactionsError),
#[error("url parse error: {0}")]
Expand All @@ -53,6 +60,7 @@ impl IntoResponse for ModulesAPIError {
}
Self::Module(dal::module::ModuleError::EmptyMetadata(_, _)) => StatusCode::BAD_REQUEST,
Self::ContributionFailure(_) => StatusCode::BAD_REQUEST,
Self::ModuleHashNotFound(_) => StatusCode::NOT_FOUND,
_ => ApiError::DEFAULT_ERROR_STATUS_CODE,
};

Expand All @@ -67,4 +75,6 @@ pub fn v2_routes() -> Router<AppState> {
.route("/", get(list::list))
.route("/:module_id/builtins/reject", post(builtins::reject))
.route("/:module_id/builtins/promote", post(builtins::promote))
.route("/module_by_hash", get(module_by_hash::module_by_hash))
.route("/module_by_id", get(module_by_id::remote_module_by_id))
}
Original file line number Diff line number Diff line change
@@ -1,26 +1,22 @@
use std::cmp::{Ord, PartialOrd};

use axum::{
extract::{Host, OriginalUri, Query},
extract::{Host, OriginalUri, Path, Query},
Json,
};
use chrono::{DateTime, Utc};
use dal::{module::Module, Visibility};
use dal::{module::Module, ChangeSetId, WorkspacePk};
use serde::{Deserialize, Serialize};

use crate::{
extract::{AccessBuilder, HandlerContext, PosthogClient},
track,
};

use super::{ModuleError, ModuleResult};
use super::{ModuleAPIResult, ModulesAPIError};

#[derive(Deserialize, Serialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct PkgGetRequest {
pub struct GetModuleByHashRequest {
pub hash: String,
#[serde(flatten)]
pub visibility: Visibility,
}

#[derive(Deserialize, Serialize, Debug, PartialEq, Eq)]
Expand All @@ -45,7 +41,7 @@ impl PartialOrd for PkgFuncView {

#[derive(Deserialize, Serialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct PkgGetResponse {
pub struct GetModuleByHashResponse {
pub name: String,
pub hash: String,
pub version: String,
Expand All @@ -57,19 +53,26 @@ pub struct PkgGetResponse {
pub installed: bool,
}

pub async fn get_module_by_hash(
pub async fn module_by_hash(
HandlerContext(builder): HandlerContext,
AccessBuilder(request_ctx): AccessBuilder,
AccessBuilder(access_builder): AccessBuilder,
PosthogClient(posthog_client): PosthogClient,
OriginalUri(original_uri): OriginalUri,
Host(host_name): Host,
Query(request): Query<PkgGetRequest>,
) -> ModuleResult<Json<PkgGetResponse>> {
let ctx = builder.build(request_ctx.build(request.visibility)).await?;
Path((_workspace_pk, change_set_id)): Path<(WorkspacePk, ChangeSetId)>,
Query(request): Query<GetModuleByHashRequest>,
) -> ModuleAPIResult<Json<GetModuleByHashResponse>> {
let ctx = builder
.build(access_builder.build(change_set_id.into()))
.await?;

let installed_pkg = match Module::find_by_root_hash(&ctx, &request.hash).await? {
Some(m) => m,
None => return Err(ModuleError::ModuleHashNotFound(request.hash.to_string())),
None => {
return Err(ModulesAPIError::ModuleHashNotFound(
request.hash.to_string(),
))
}
};

let mut pkg_schemas: Vec<String> = installed_pkg
Expand Down Expand Up @@ -99,14 +102,14 @@ pub async fn get_module_by_hash(
&host_name,
"get_module",
serde_json::json!({
"pkg_name": installed_pkg.clone().name(),
"pkg_version": installed_pkg.clone().version(),
"pkg_schema_count": pkg_schemas.len(),
"pkg_funcs_count": pkg_funcs.len(),
"pkg_name": installed_pkg.clone().name(),
"pkg_version": installed_pkg.clone().version(),
"pkg_schema_count": pkg_schemas.len(),
"pkg_funcs_count": pkg_funcs.len(),
}),
);

Ok(Json(PkgGetResponse {
Ok(Json(GetModuleByHashResponse {
hash: installed_pkg.root_hash().to_string(),
name: installed_pkg.name().to_string(),
version: installed_pkg.version().to_string(),
Expand Down
Loading

0 comments on commit 3849bf5

Please sign in to comment.