Skip to content

Commit

Permalink
fix(communityEdition): fix unprocessable entity error
Browse files Browse the repository at this point in the history
Fix an issue where garden failed with the error "unprocessable entity"
some times when user has more than 24 projects.

Previously core listed all projects and if it didn't find project with
given name in the first list of results, it would attempt creating the
project. Creating the project fails then with "unprocessable entity"
because a project with the given name already exists.
  • Loading branch information
stefreak committed Apr 15, 2024
1 parent c4e6982 commit 6845c44
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 24 deletions.
37 changes: 17 additions & 20 deletions core/src/cloud/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -359,41 +359,38 @@ export class CloudApi {
return this.registeredSessions.has(id)
}

async getAllProjects(): Promise<CloudProject[]> {
async getProjectByName(projectName: string): Promise<CloudProject | undefined> {
let response: ListProjectsResponse

try {
response = await this.get<ListProjectsResponse>(`/projects`)
response = await this.get<ListProjectsResponse>(`/projects?name=${encodeURIComponent(projectName)}`)
} catch (err) {
this.log.debug(`Attempt to list all projects failed with ${err}`)
throw err
throw new CloudApiError({
message: `Failed to find Garden Cloud project by name: ${err}`,
})
}

const projectList: ListProjectsResponse["data"] = response.data

return projectList.map((p) => {
const project = toCloudProject(p)
// Cache the entry by ID
this.projects.set(project.id, project)
return project
})
}

async getProjectByName(projectName: string): Promise<CloudProject | undefined> {
const allProjects = await this.getAllProjects()

const projects = allProjects.filter((p) => p.name === projectName)
const projectList = response.data

// Expect a single project, otherwise we fail with an error
if (projects.length > 1) {
if (projectList.length > 1) {
throw new CloudApiDuplicateProjectsError({
message: deline`Found an unexpected state with multiple projects using the same name, ${projectName}.
Please make sure there is only one project with the given name.
Projects can be deleted through the Garden Cloud UI at ${this.domain}`,
})
}

return projects[0]
if (projectList.length === 0) {
return undefined
}

const project = toCloudProject(projectList[0])

// Cache the entry by ID
this.projects.set(project.id, project)

return project
}

async createProject(projectName: string): Promise<CloudProject> {
Expand Down
4 changes: 0 additions & 4 deletions core/test/helpers/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,6 @@ export class FakeCloudApi extends CloudApi {
}
}

override async getAllProjects(): Promise<CloudProject[]> {
return [(await this.getProjectById(apiProjectId))!]
}

override async createProject(name: string): Promise<CloudProject> {
return {
id: apiProjectId,
Expand Down

0 comments on commit 6845c44

Please sign in to comment.