Skip to content

Commit

Permalink
Added populateOrgName param in /v1/workspace api which adds `orgN…
Browse files Browse the repository at this point in the history
…ame` and `displayName` in result

Updated the CLI to use this new paramter to display organization name with project name with support for backward compatibility keeping original behaviour for older apis
  • Loading branch information
rhythmbhiwani committed Mar 3, 2024
1 parent 2192985 commit 756c1e5
Show file tree
Hide file tree
Showing 8 changed files with 69 additions and 37 deletions.
1 change: 1 addition & 0 deletions backend/src/server/routes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,7 @@ export const registerRoutes = async (
const projectService = projectServiceFactory({
permissionService,
projectDAL,
orgDAL,
projectQueue: projectQueueService,
secretBlindIndexDAL,
identityProjectDAL,
Expand Down
12 changes: 10 additions & 2 deletions backend/src/server/routes/v1/project-router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ 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()
environments: z.object({ name: z.string(), slug: z.string(), id: z.string() }).array(),
orgName: z.string().optional(),
displayName: z.string().optional()
})
);

Expand Down Expand Up @@ -91,6 +93,12 @@ 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()
Expand All @@ -99,7 +107,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);
const workspaces = await server.services.project.getProjects(req.permission.id, req.query.populateOrgName);
return { workspaces };
}
});
Expand Down
16 changes: 15 additions & 1 deletion backend/src/services/project/project-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ 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";
Expand Down Expand Up @@ -46,6 +47,7 @@ type TProjectServiceFactoryDep = {
projectDAL: TProjectDALFactory;
projectQueue: TProjectQueueFactory;
userDAL: TUserDALFactory;
orgDAL: TOrgDALFactory;
folderDAL: TSecretFolderDALFactory;
projectEnvDAL: Pick<TProjectEnvDALFactory, "insertMany" | "find">;
identityOrgMembershipDAL: TIdentityOrgDALFactory;
Expand All @@ -64,6 +66,7 @@ export type TProjectServiceFactory = ReturnType<typeof projectServiceFactory>;
export const projectServiceFactory = ({
projectDAL,
projectQueue,
orgDAL,
projectKeyDAL,
permissionService,
userDAL,
Expand Down Expand Up @@ -306,8 +309,19 @@ export const projectServiceFactory = ({
return deletedProject;
};

const getProjects = async (actorId: string) => {
const getProjects = async (actorId: string, populateOrgName?: boolean) => {
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;
};

Expand Down
17 changes: 1 addition & 16 deletions cli/packages/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ func CallGetAllWorkSpacesUserBelongsTo(httpClient *resty.Client) (GetWorkSpacesR
R().
SetResult(&workSpacesResponse).
SetHeader("User-Agent", USER_AGENT).
SetQueryParam("populateOrgName", "true").
Get(fmt.Sprintf("%v/v1/workspace", config.INFISICAL_URL))

if err != nil {
Expand All @@ -180,22 +181,6 @@ func CallGetAllWorkSpacesUserBelongsTo(httpClient *resty.Client) (GetWorkSpacesR
return GetWorkSpacesResponse{}, fmt.Errorf("CallGetAllWorkSpacesUserBelongsTo: Unsuccessful response: [response=%v]", response)
}

// Call the organization API
orgResponse, err := CallGetAllOrganizations(httpClient)
if err != nil {
return GetWorkSpacesResponse{}, err
}

// Update organization names in workspacesResponse
for i, workspace := range workSpacesResponse.Workspaces {
for _, organization := range orgResponse.Organizations {
if workspace.Organization == organization.ID {
workSpacesResponse.Workspaces[i].Organization = organization.Name
break
}
}
}

return workSpacesResponse, nil
}

Expand Down
11 changes: 6 additions & 5 deletions cli/packages/api/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,11 +120,12 @@ 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:"orgId,omitempty"`
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"`
} `json:"workspaces"`
}

Expand Down
11 changes: 3 additions & 8 deletions cli/packages/cmd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ package cmd

import (
"encoding/json"
"fmt"

"github.com/Infisical/infisical-merge/packages/api"
"github.com/Infisical/infisical-merge/packages/models"
Expand Down Expand Up @@ -58,14 +57,10 @@ var initCmd = &cobra.Command{
}

workspaces := workspaceResponse.Workspaces
if len(workspaces) == 0 {
message := fmt.Sprintf("You don't have any projects created in Infisical. You must first create a project at %s", util.INFISICAL_TOKEN_NAME)
util.PrintErrorMessageAndExit(message)
}

var workspaceNames []string
for _, workspace := range workspaces {
workspaceNames = append(workspaceNames, fmt.Sprintf("%s (%s)", workspace.Name, workspace.Organization))
workspaceNames, err := util.GetWorkspacesNameList(workspaceResponse)
if err != nil {
util.HandleError(err, "Error extracting workspace names")
}

prompt := promptui.Select{
Expand Down
11 changes: 6 additions & 5 deletions cli/packages/models/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,12 @@ 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:"orgId,omitempty"`
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"`
}

type WorkspaceConfigFile struct {
Expand Down
27 changes: 27 additions & 0 deletions cli/packages/util/init.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package util

import (
"fmt"

"github.com/Infisical/infisical-merge/packages/api"
)

func GetWorkspacesNameList(workspaceResponse api.GetWorkSpacesResponse) ([]string, error) {
workspaces := workspaceResponse.Workspaces

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)
PrintErrorMessageAndExit(message)
}

var workspaceNames []string
for _, workspace := range workspaces {
if workspace.DisplayName != nil {
workspaceNames = append(workspaceNames, *workspace.DisplayName)
} else {
workspaceNames = append(workspaceNames, workspace.Name)
}
}

return workspaceNames, nil
}

0 comments on commit 756c1e5

Please sign in to comment.