Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Show organization name in infisical init command #1521

Merged
merged 6 commits into from
Mar 5, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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})`
rhythmbhiwani marked this conversation as resolved.
Show resolved Hide resolved
};
});
}
return workspaces;
};

Expand Down
20 changes: 20 additions & 0 deletions cli/packages/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,12 +145,32 @@ func CallLogin2V2(httpClient *resty.Client, request GetLoginTwoV2Request) (GetLo
return loginTwoV2Response, nil
}

func CallGetAllOrganizations(httpClient *resty.Client) (GetOrganizationsResponse, error) {
var orgResponse GetOrganizationsResponse
response, err := httpClient.
R().
SetResult(&orgResponse).
SetHeader("User-Agent", USER_AGENT).
Get(fmt.Sprintf("%v/v1/organization", config.INFISICAL_URL))

if err != nil {
return GetOrganizationsResponse{}, err
}

if response.IsError() {
return GetOrganizationsResponse{}, fmt.Errorf("CallGetAllOrganizations: Unsuccessful response: [response=%v]", response)
}

return orgResponse, nil
}

func CallGetAllWorkSpacesUserBelongsTo(httpClient *resty.Client) (GetWorkSpacesResponse, error) {
var workSpacesResponse GetWorkSpacesResponse
response, err := httpClient.
R().
SetResult(&workSpacesResponse).
SetHeader("User-Agent", USER_AGENT).
SetQueryParam("populateOrgName", "true").
rhythmbhiwani marked this conversation as resolved.
Show resolved Hide resolved
Get(fmt.Sprintf("%v/v1/workspace", config.INFISICAL_URL))

if err != nil {
Expand Down
20 changes: 14 additions & 6 deletions cli/packages/api/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,14 +120,22 @@ 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:"organization,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"`
}

type GetOrganizationsResponse struct {
Organizations []struct {
ID string `json:"id"`
Name string `json:"name"`
} `json:"organizations"`
}

type Secret struct {
SecretKeyCiphertext string `json:"secretKeyCiphertext,omitempty"`
SecretKeyIV string `json:"secretKeyIV,omitempty"`
Expand Down Expand Up @@ -505,5 +513,5 @@ type GetRawSecretsV3Response struct {
SecretComment string `json:"secretComment"`
} `json:"secrets"`
Imports []any `json:"imports"`
ETag string
ETag string
}
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)
maidul98 marked this conversation as resolved.
Show resolved Hide resolved
util.PrintErrorMessageAndExit(message)
}

var workspaceNames []string
for _, workspace := range workspaces {
workspaceNames = append(workspaceNames, workspace.Name)
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:"organization,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
}
Loading