Skip to content

Commit d55a8ab

Browse files
committed
Merge remote-tracking branch 'origin/main' into add-fmodel-endpoint
2 parents 2e85875 + c2ea23b commit d55a8ab

File tree

3 files changed

+59
-12
lines changed

3 files changed

+59
-12
lines changed

src/otomi-stack.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { CoreV1Api, KubeConfig, User as k8sUser, V1ObjectReference } from '@kubernetes/client-node'
1+
import { CoreV1Api, User as k8sUser, KubeConfig, V1ObjectReference } from '@kubernetes/client-node'
22
import Debug from 'debug'
33

44
import { getRegions, ObjectStorageKeyRegions } from '@linode/api-v4'
@@ -131,7 +131,7 @@ import {
131131
import { getAplObjectFromV1, getV1MergeObject, getV1ObjectFromApl } from './utils/manifests'
132132
import { getSealedSecretsPEM, sealedSecretManifest } from './utils/sealedSecretUtils'
133133
import { getKeycloakUsers, isValidUsername } from './utils/userUtils'
134-
import { ObjectStorageClient } from './utils/wizardUtils'
134+
import { defineClusterId, ObjectStorageClient } from './utils/wizardUtils'
135135
import { fetchChartYaml, fetchWorkloadCatalog, NewHelmChartValues, sparseCloneChart } from './utils/workloadUtils'
136136

137137
interface ExcludedApp extends App {
@@ -301,12 +301,11 @@ export default class OtomiStack {
301301
const createdBuckets = [] as Array<string>
302302
if (data?.apiToken && data?.regionId) {
303303
const { cluster } = this.getSettings(['cluster'])
304-
let lkeClusterId: null | number = null
305-
if (cluster?.name?.includes('aplinstall')) {
306-
lkeClusterId = Number(cluster?.name?.replace('aplinstall', ''))
307-
} else if (lkeClusterId === null) {
308-
return { status: 'error', errorMessage: 'Cluster ID is not found in the cluster name.' }
304+
let lkeClusterId: undefined | string = defineClusterId(cluster?.name)
305+
if (lkeClusterId === undefined) {
306+
return { status: 'error', errorMessage: 'Cluster name is not found.' }
309307
}
308+
310309
const bucketNames = {
311310
cnpg: `lke${lkeClusterId}-cnpg`,
312311
harbor: `lke${lkeClusterId}-harbor`,

src/utils/wizardUtils.test.ts

Lines changed: 47 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,13 @@ jest.mock('@linode/api-v4', () => ({
1515

1616
import { createBucket, createObjectStorageKeys, ObjectStorageKey, setToken } from '@linode/api-v4'
1717
import { OtomiError } from 'src/error'
18-
import { ObjectStorageClient } from './wizardUtils'
18+
import { ObjWizard } from 'src/otomi-models'
19+
import OtomiStack from 'src/otomi-stack'
20+
import { defineClusterId, ObjectStorageClient } from './wizardUtils'
1921

2022
describe('ObjectStorageClient', () => {
2123
let client: ObjectStorageClient
22-
const clusterId = 12345
24+
let clusterId: string | undefined = '12345'
2325

2426
beforeEach(() => {
2527
client = new ObjectStorageClient('test-token')
@@ -35,7 +37,16 @@ describe('ObjectStorageClient', () => {
3537
describe('createObjectStorageBucket', () => {
3638
const label = 'test-bucket'
3739
const region = 'us-east'
40+
let otomiStack: OtomiStack
41+
const domainSuffix = 'dev.linode-apl.net'
42+
beforeEach(async () => {
43+
otomiStack = new OtomiStack()
44+
await otomiStack.init()
45+
})
3846

47+
afterEach(() => {
48+
jest.restoreAllMocks()
49+
})
3950
it('should successfully create bucket', async () => {
4051
const mockResponse = { label: 'test-bucket' }
4152
;(createBucket as jest.Mock).mockResolvedValue(mockResponse)
@@ -81,6 +92,38 @@ describe('ObjectStorageClient', () => {
8192
// @ts-ignore
8293
expect(result.code).toBe(500)
8394
})
95+
96+
test('should return lkeClusterId', () => {
97+
const settings = { cluster: { name: 'cluster-123' } }
98+
clusterId = defineClusterId(settings.cluster.name)
99+
100+
expect(clusterId).toBe('cluster-123')
101+
})
102+
103+
test('should return stripped down clusterId when name does not include prefix', () => {
104+
const settings = { cluster: { name: 'aplinstall123' } }
105+
clusterId = defineClusterId(settings.cluster.name)
106+
107+
expect(clusterId).toBe('123')
108+
})
109+
110+
test('should return undefined when cluster name is undefined', () => {
111+
const settings: any = { cluster: {} }
112+
clusterId = defineClusterId(settings.cluster?.name)
113+
114+
expect(clusterId).toBe(undefined)
115+
})
116+
117+
test('should return error when cluster name is undefined', async () => {
118+
const data = { apiToken: 'some-token', regionId: 'us-east', label: 'my-cluster' }
119+
jest.spyOn(otomiStack, 'getSettings').mockReturnValue({
120+
cluster: { domainSuffix, provider: 'linode' },
121+
} as any)
122+
const result: ObjWizard = await otomiStack.createObjWizard(data)
123+
124+
expect(result.status).toBe('error')
125+
expect(result.errorMessage).toBe('Cluster name is not found.')
126+
})
84127
})
85128

86129
describe('createObjectStorageKey', () => {
@@ -103,7 +146,7 @@ describe('ObjectStorageClient', () => {
103146
}
104147
;(createObjectStorageKeys as jest.Mock).mockResolvedValue(mockResponse)
105148

106-
const result = await client.createObjectStorageKey(clusterId, region, bucketNames)
149+
const result = await client.createObjectStorageKey(clusterId!, region, bucketNames)
107150

108151
expect(createObjectStorageKeys).toHaveBeenCalledWith({
109152
label: `lke${clusterId}-key-1704110400000`,
@@ -125,7 +168,7 @@ describe('ObjectStorageClient', () => {
125168
}
126169
;(createObjectStorageKeys as jest.Mock).mockRejectedValue(mockError)
127170

128-
const result = await client.createObjectStorageKey(clusterId, region, bucketNames)
171+
const result = await client.createObjectStorageKey(clusterId!, region, bucketNames)
129172
expect(result).toBeInstanceOf(OtomiError)
130173
// @ts-ignore
131174
expect(result.publicMessage).toBe('Your OAuth token is not authorized to use this endpoint')

src/utils/wizardUtils.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ export class ObjectStorageClient {
2828
}
2929

3030
public async createObjectStorageKey(
31-
lkeClusterId: number,
31+
lkeClusterId: string,
3232
region: string,
3333
bucketNames: string[],
3434
): Promise<Pick<ObjectStorageKey, 'access_key' | 'secret_key' | 'regions'> | OtomiError> {
@@ -56,3 +56,8 @@ export class ObjectStorageClient {
5656
}
5757
}
5858
}
59+
// define cluster id based on cluster name
60+
export function defineClusterId(clusterName: string | undefined): string | undefined {
61+
if (!clusterName) return undefined
62+
return clusterName.replace('aplinstall', '')
63+
}

0 commit comments

Comments
 (0)