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

View File

@@ -341,7 +341,6 @@ export const registerRoutes = async (
const projectService = projectServiceFactory({
permissionService,
projectDAL,
orgDAL,
projectQueue: projectQueueService,
secretBlindIndexDAL,
identityProjectDAL,

View File

@@ -16,9 +16,7 @@ import { sanitizedServiceTokenSchema } from "../v2/service-token-router";
const projectWithEnv = ProjectsSchema.merge(
z.object({
_id: z.string(),
environments: z.object({ name: z.string(), slug: z.string(), id: z.string() }).array(),
orgName: z.string().optional(),
displayName: z.string().optional()
environments: z.object({ name: z.string(), slug: z.string(), id: z.string() }).array()
})
);
@@ -93,12 +91,6 @@ export const registerProjectRouter = async (server: FastifyZodProvider) => {
url: "/",
method: "GET",
schema: {
querystring: z.object({
populateOrgName: z
.enum(["true", "false"])
.default("false")
.transform((value) => value === "true")
}),
response: {
200: z.object({
workspaces: projectWithEnv.array()
@@ -107,7 +99,7 @@ export const registerProjectRouter = async (server: FastifyZodProvider) => {
},
onRequest: verifyAuth([AuthMode.JWT, AuthMode.API_KEY]),
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 };
}
});

View File

@@ -17,7 +17,6 @@ import { TProjectPermission } from "@app/lib/types";
import { ActorType } from "../auth/auth-type";
import { TIdentityOrgDALFactory } from "../identity/identity-org-dal";
import { TIdentityProjectDALFactory } from "../identity-project/identity-project-dal";
import { TOrgDALFactory } from "../org/org-dal";
import { TOrgServiceFactory } from "../org/org-service";
import { TProjectBotDALFactory } from "../project-bot/project-bot-dal";
import { TProjectEnvDALFactory } from "../project-env/project-env-dal";
@@ -47,7 +46,6 @@ type TProjectServiceFactoryDep = {
projectDAL: TProjectDALFactory;
projectQueue: TProjectQueueFactory;
userDAL: TUserDALFactory;
orgDAL: TOrgDALFactory;
folderDAL: TSecretFolderDALFactory;
projectEnvDAL: Pick<TProjectEnvDALFactory, "insertMany" | "find">;
identityOrgMembershipDAL: TIdentityOrgDALFactory;
@@ -66,7 +64,6 @@ export type TProjectServiceFactory = ReturnType<typeof projectServiceFactory>;
export const projectServiceFactory = ({
projectDAL,
projectQueue,
orgDAL,
projectKeyDAL,
permissionService,
userDAL,
@@ -309,19 +306,8 @@ export const projectServiceFactory = ({
return deletedProject;
};
const getProjects = async (actorId: string, populateOrgName?: boolean) => {
const getProjects = async (actorId: string) => {
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;
};

View File

@@ -120,12 +120,11 @@ type PullSecretsByInfisicalTokenResponse struct {
type GetWorkSpacesResponse struct {
Workspaces []struct {
ID string `json:"_id"`
Name string `json:"name"`
Plan string `json:"plan,omitempty"`
V int `json:"__v"`
Organization *string `json:"orgName,omitempty"`
DisplayName *string `json:"displayName,omitempty"`
ID string `json:"_id"`
Name string `json:"name"`
Plan string `json:"plan,omitempty"`
V int `json:"__v"`
OrganizationId string `json:"orgId"`
} `json:"workspaces"`
}

View File

@@ -51,21 +51,19 @@ var initCmd = &cobra.Command{
httpClient := resty.New()
httpClient.SetAuthToken(userCreds.UserCredentials.JTWToken)
workspaceResponse, err := api.CallGetAllWorkSpacesUserBelongsTo(httpClient)
organizationResponse, err := api.CallGetAllOrganizations(httpClient)
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)
if err != nil {
util.HandleError(err, "Error extracting workspace names")
}
organizationNames := util.GetOrganizationsNameList(organizationResponse)
prompt := promptui.Select{
Label: "Which of your Infisical projects would you like to connect this project to?",
Items: workspaceNames,
Label: "Which of your Infisical organization would you like to get projects from?",
Items: organizationNames,
Size: 7,
}
@@ -74,7 +72,27 @@ var initCmd = &cobra.Command{
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 {
util.HandleError(err)
}

View File

@@ -45,12 +45,11 @@ type SingleFolder struct {
}
type Workspace struct {
ID string `json:"_id"`
Name string `json:"name"`
Plan string `json:"plan,omitempty"`
V int `json:"__v"`
Organization *string `json:"orgName,omitempty"`
DisplayName *string `json:"displayName,omitempty"`
ID string `json:"_id"`
Name string `json:"name"`
Plan string `json:"plan,omitempty"`
V int `json:"__v"`
OrganizationId string `json:"orgId"`
}
type WorkspaceConfigFile struct {

View File

@@ -4,24 +4,42 @@ import (
"fmt"
"github.com/Infisical/infisical-merge/packages/api"
"github.com/Infisical/infisical-merge/packages/models"
)
func GetWorkspacesNameList(workspaceResponse api.GetWorkSpacesResponse) ([]string, error) {
workspaces := workspaceResponse.Workspaces
func GetOrganizationsNameList(organizationResponse api.GetOrganizationsResponse) []string {
organizations := organizationResponse.Organizations
if len(workspaces) == 0 {
message := fmt.Sprintf("You don't have any projects created in Infisical. You must first create a project at %s", INFISICAL_TOKEN_NAME)
if len(organizations) == 0 {
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)
}
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
for _, workspace := range workspaces {
if workspace.DisplayName != nil {
workspaceNames = append(workspaceNames, *workspace.DisplayName)
} else {
if workspace.OrganizationId == orgId {
filteredWorkspaces = append(filteredWorkspaces, workspace)
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
}