Reverted backend api changes

In CLI, now first asking org, then projects
This commit is contained in:
Rhythm Bhiwani
2024-03-05 03:08:13 +05:30
parent 756c1e5098
commit c7bbe82f4a
7 changed files with 67 additions and 56 deletions
-1
View File
@@ -341,7 +341,6 @@ export const registerRoutes = async (
const projectService = projectServiceFactory({ const projectService = projectServiceFactory({
permissionService, permissionService,
projectDAL, projectDAL,
orgDAL,
projectQueue: projectQueueService, projectQueue: projectQueueService,
secretBlindIndexDAL, secretBlindIndexDAL,
identityProjectDAL, identityProjectDAL,
+2 -10
View File
@@ -16,9 +16,7 @@ import { sanitizedServiceTokenSchema } from "../v2/service-token-router";
const projectWithEnv = ProjectsSchema.merge( const projectWithEnv = ProjectsSchema.merge(
z.object({ z.object({
_id: z.string(), _id: z.string(),
environments: z.object({ name: z.string(), slug: z.string(), id: z.string() }).array(), environments: z.object({ name: z.string(), slug: z.string(), id: z.string() }).array()
orgName: z.string().optional(),
displayName: z.string().optional()
}) })
); );
@@ -93,12 +91,6 @@ export const registerProjectRouter = async (server: FastifyZodProvider) => {
url: "/", url: "/",
method: "GET", method: "GET",
schema: { schema: {
querystring: z.object({
populateOrgName: z
.enum(["true", "false"])
.default("false")
.transform((value) => value === "true")
}),
response: { response: {
200: z.object({ 200: z.object({
workspaces: projectWithEnv.array() workspaces: projectWithEnv.array()
@@ -107,7 +99,7 @@ export const registerProjectRouter = async (server: FastifyZodProvider) => {
}, },
onRequest: verifyAuth([AuthMode.JWT, AuthMode.API_KEY]), onRequest: verifyAuth([AuthMode.JWT, AuthMode.API_KEY]),
handler: async (req) => { handler: async (req) => {
const workspaces = await server.services.project.getProjects(req.permission.id, req.query.populateOrgName); const workspaces = await server.services.project.getProjects(req.permission.id);
return { workspaces }; return { workspaces };
} }
}); });
@@ -17,7 +17,6 @@ import { TProjectPermission } from "@app/lib/types";
import { ActorType } from "../auth/auth-type"; import { ActorType } from "../auth/auth-type";
import { TIdentityOrgDALFactory } from "../identity/identity-org-dal"; import { TIdentityOrgDALFactory } from "../identity/identity-org-dal";
import { TIdentityProjectDALFactory } from "../identity-project/identity-project-dal"; import { TIdentityProjectDALFactory } from "../identity-project/identity-project-dal";
import { TOrgDALFactory } from "../org/org-dal";
import { TOrgServiceFactory } from "../org/org-service"; import { TOrgServiceFactory } from "../org/org-service";
import { TProjectBotDALFactory } from "../project-bot/project-bot-dal"; import { TProjectBotDALFactory } from "../project-bot/project-bot-dal";
import { TProjectEnvDALFactory } from "../project-env/project-env-dal"; import { TProjectEnvDALFactory } from "../project-env/project-env-dal";
@@ -47,7 +46,6 @@ type TProjectServiceFactoryDep = {
projectDAL: TProjectDALFactory; projectDAL: TProjectDALFactory;
projectQueue: TProjectQueueFactory; projectQueue: TProjectQueueFactory;
userDAL: TUserDALFactory; userDAL: TUserDALFactory;
orgDAL: TOrgDALFactory;
folderDAL: TSecretFolderDALFactory; folderDAL: TSecretFolderDALFactory;
projectEnvDAL: Pick<TProjectEnvDALFactory, "insertMany" | "find">; projectEnvDAL: Pick<TProjectEnvDALFactory, "insertMany" | "find">;
identityOrgMembershipDAL: TIdentityOrgDALFactory; identityOrgMembershipDAL: TIdentityOrgDALFactory;
@@ -66,7 +64,6 @@ export type TProjectServiceFactory = ReturnType<typeof projectServiceFactory>;
export const projectServiceFactory = ({ export const projectServiceFactory = ({
projectDAL, projectDAL,
projectQueue, projectQueue,
orgDAL,
projectKeyDAL, projectKeyDAL,
permissionService, permissionService,
userDAL, userDAL,
@@ -309,19 +306,8 @@ export const projectServiceFactory = ({
return deletedProject; return deletedProject;
}; };
const getProjects = async (actorId: string, populateOrgName?: boolean) => { const getProjects = async (actorId: string) => {
const workspaces = await projectDAL.findAllProjects(actorId); const workspaces = await projectDAL.findAllProjects(actorId);
if (populateOrgName) {
const orgs = await orgDAL.findAllOrgsByUserId(actorId);
return workspaces.map((workspace) => {
const orgName = orgs.find((org) => org.id === workspace.orgId)?.name || "";
return {
...workspace,
orgName,
displayName: `${workspace.name} (${orgName})`
};
});
}
return workspaces; return workspaces;
}; };
+5 -6
View File
@@ -120,12 +120,11 @@ type PullSecretsByInfisicalTokenResponse struct {
type GetWorkSpacesResponse struct { type GetWorkSpacesResponse struct {
Workspaces []struct { Workspaces []struct {
ID string `json:"_id"` ID string `json:"_id"`
Name string `json:"name"` Name string `json:"name"`
Plan string `json:"plan,omitempty"` Plan string `json:"plan,omitempty"`
V int `json:"__v"` V int `json:"__v"`
Organization *string `json:"orgName,omitempty"` OrganizationId string `json:"orgId"`
DisplayName *string `json:"displayName,omitempty"`
} `json:"workspaces"` } `json:"workspaces"`
} }
+28 -10
View File
@@ -51,21 +51,19 @@ var initCmd = &cobra.Command{
httpClient := resty.New() httpClient := resty.New()
httpClient.SetAuthToken(userCreds.UserCredentials.JTWToken) httpClient.SetAuthToken(userCreds.UserCredentials.JTWToken)
workspaceResponse, err := api.CallGetAllWorkSpacesUserBelongsTo(httpClient)
organizationResponse, err := api.CallGetAllOrganizations(httpClient)
if err != nil { if err != nil {
util.HandleError(err, "Unable to pull projects that belong to you") util.HandleError(err, "Unable to pull organizations that belong to you")
} }
workspaces := workspaceResponse.Workspaces organizations := organizationResponse.Organizations
workspaceNames, err := util.GetWorkspacesNameList(workspaceResponse) organizationNames := util.GetOrganizationsNameList(organizationResponse)
if err != nil {
util.HandleError(err, "Error extracting workspace names")
}
prompt := promptui.Select{ prompt := promptui.Select{
Label: "Which of your Infisical projects would you like to connect this project to?", Label: "Which of your Infisical organization would you like to get projects from?",
Items: workspaceNames, Items: organizationNames,
Size: 7, Size: 7,
} }
@@ -74,7 +72,27 @@ var initCmd = &cobra.Command{
util.HandleError(err) util.HandleError(err)
} }
err = writeWorkspaceFile(workspaces[index]) selectedOrganization := organizations[index]
workspaceResponse, err := api.CallGetAllWorkSpacesUserBelongsTo(httpClient)
if err != nil {
util.HandleError(err, "Unable to pull projects that belong to you")
}
filteredWorkspaces, workspaceNames := util.GetWorkspacesInOrganization(workspaceResponse, selectedOrganization.ID)
prompt = promptui.Select{
Label: "Which of your Infisical projects would you like to connect this project to?",
Items: workspaceNames,
Size: 7,
}
index, _, err = prompt.Run()
if err != nil {
util.HandleError(err)
}
err = writeWorkspaceFile(filteredWorkspaces[index])
if err != nil { if err != nil {
util.HandleError(err) util.HandleError(err)
} }
+5 -6
View File
@@ -45,12 +45,11 @@ type SingleFolder struct {
} }
type Workspace struct { type Workspace struct {
ID string `json:"_id"` ID string `json:"_id"`
Name string `json:"name"` Name string `json:"name"`
Plan string `json:"plan,omitempty"` Plan string `json:"plan,omitempty"`
V int `json:"__v"` V int `json:"__v"`
Organization *string `json:"orgName,omitempty"` OrganizationId string `json:"orgId"`
DisplayName *string `json:"displayName,omitempty"`
} }
type WorkspaceConfigFile struct { type WorkspaceConfigFile struct {
+26 -8
View File
@@ -4,24 +4,42 @@ import (
"fmt" "fmt"
"github.com/Infisical/infisical-merge/packages/api" "github.com/Infisical/infisical-merge/packages/api"
"github.com/Infisical/infisical-merge/packages/models"
) )
func GetWorkspacesNameList(workspaceResponse api.GetWorkSpacesResponse) ([]string, error) { func GetOrganizationsNameList(organizationResponse api.GetOrganizationsResponse) []string {
workspaces := workspaceResponse.Workspaces organizations := organizationResponse.Organizations
if len(workspaces) == 0 { if len(organizations) == 0 {
message := fmt.Sprintf("You don't have any projects created in Infisical. You must first create a project at %s", INFISICAL_TOKEN_NAME) message := fmt.Sprintf("You don't have any organization created in Infisical. You must first create a organization at %s", INFISICAL_TOKEN_NAME)
PrintErrorMessageAndExit(message) PrintErrorMessageAndExit(message)
} }
var organizationNames []string
for _, workspace := range organizations {
organizationNames = append(organizationNames, workspace.Name)
}
return organizationNames
}
func GetWorkspacesInOrganization(workspaceResponse api.GetWorkSpacesResponse, orgId string) ([]models.Workspace, []string) {
workspaces := workspaceResponse.Workspaces
var filteredWorkspaces []models.Workspace
var workspaceNames []string var workspaceNames []string
for _, workspace := range workspaces { for _, workspace := range workspaces {
if workspace.DisplayName != nil { if workspace.OrganizationId == orgId {
workspaceNames = append(workspaceNames, *workspace.DisplayName) filteredWorkspaces = append(filteredWorkspaces, workspace)
} else {
workspaceNames = append(workspaceNames, workspace.Name) workspaceNames = append(workspaceNames, workspace.Name)
} }
} }
return workspaceNames, nil if len(filteredWorkspaces) == 0 {
message := fmt.Sprintf("You don't have any projects created in Infisical organization. You must first create a project at %s", INFISICAL_TOKEN_NAME)
PrintErrorMessageAndExit(message)
}
return filteredWorkspaces, workspaceNames
} }