Skip to content

Commit 6e4bb18

Browse files
committed
fix: migrate ot openapi-typescript and openapi-fetch
Signed-off-by: Denis Golovin <[email protected]>
1 parent 5f22ef9 commit 6e4bb18

11 files changed

+3707
-217
lines changed

.gitignore

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@ redhat-authentication.cdix
77
tests/**/output
88
test-resources
99
test-results
10-
10+
src/rh-api/gen

package.json

+4-3
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,9 @@
3434
}
3535
},
3636
"scripts": {
37-
"build": "vite build && node ./scripts/build.cjs",
37+
"generate:registry-authorizer": "npx openapi-typescript src/rh-api/registry-authorizer-schema.json -o src/rh-api/gen/registry-authorizer.d.ts",
38+
"generate:subscription": "npx openapi-typescript src/rh-api/subscription-schema.json -o src/rh-api/gen/subscription.d.ts",
39+
"build": "pnpm generate:subscription && pnpm generate:registry-authorizer && vite build && node ./scripts/build.cjs",
3840
"watch": "vite build -w",
3941
"format:check": "prettier --end-of-line auto --cache --check \"{src,types,scripts}/**/*.{ts,js}\"",
4042
"format:fix": "prettier --cache --write \"{src,types,scripts}/**/*.{ts,js}\"",
@@ -49,11 +51,10 @@
4951
"dependencies": {
5052
"@podman-desktop/api": "^1.14.1",
5153
"@podman-desktop/podman-extension-api": "^1.14.2",
52-
"@redhat-developer/rhcra-client": "^0.0.1",
53-
"@redhat-developer/rhsm-client": "^0.0.4",
5454
"axios": "^1.8.2",
5555
"js-yaml": "^4.1.0",
5656
"object-hash": "3.0.0",
57+
"openapi-fetch": "^0.13.4",
5758
"openid-client": "^5.7.0"
5859
},
5960
"devDependencies": {

pnpm-lock.yaml

+144-165
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/extension.spec.ts

+19-5
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,18 @@
1818
/* eslint-disable @typescript-eslint/no-explicit-any */
1919
/* eslint-disable @typescript-eslint/explicit-function-return-type */
2020

21-
import type { AuthenticationGetSessionOptions, AuthenticationSession, ExtensionContext } from '@podman-desktop/api';
21+
import type {
22+
AuthenticationGetSessionOptions,
23+
AuthenticationSession,
24+
ExtensionContext,
25+
} from '@podman-desktop/api';
2226
import { authentication, commands } from '@podman-desktop/api';
23-
import { OrganizationService } from '@redhat-developer/rhsm-client';
2427
import { afterEach, beforeEach, expect, suite, test, vi } from 'vitest';
2528

2629
import * as extension from './extension';
2730
import * as podmanCli from './podman-cli';
28-
import * as subscription from './subscription';
31+
import { Organization } from './rh-api/subscription';
32+
import * as subscription from './rh-api/subscription';
2933
import { ExtensionTelemetryLogger } from './telemetry';
3034
import * as util from './util';
3135

@@ -180,8 +184,18 @@ suite('signin command telemetry reports', () => {
180184
},
181185
);
182186
vi.spyOn(subscription, 'isRedHatRegistryConfigured').mockReturnValue(true);
183-
vi.spyOn(podmanCli, 'getConnectionForRunningPodmanMachine').mockReturnValue('machine1');
184-
vi.spyOn(OrganizationService.prototype, 'checkOrgScaCapability').mockResolvedValue({ body: {} });
187+
vi.spyOn(podmanCli, 'getConnectionForRunningPodmanMachine').mockReturnValue({
188+
providerId: '1',
189+
connection: {
190+
name: 'machine1',
191+
type: 'podman',
192+
endpoint: {
193+
socketPath: '/path/to/the/socket',
194+
},
195+
status: () => 'started',
196+
},
197+
});
198+
vi.spyOn(Organization.prototype, 'checkOrgScaCapability').mockResolvedValue({ body: {} });
185199
await extension.activate(createExtContext());
186200
expect(commandFunctionCopy!).toBeDefined();
187201
await commandFunctionCopy!();

src/extension.ts

+28-27
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,6 @@
1717
***********************************************************************/
1818

1919
import * as extensionApi from '@podman-desktop/api';
20-
import type { ServiceAccountV1 } from '@redhat-developer/rhcra-client';
21-
import { ContainerRegistryAuthorizerClient } from '@redhat-developer/rhcra-client';
22-
import { SubscriptionManagerClient } from '@redhat-developer/rhsm-client';
2320

2421
import icon from '../icon.png';
2522
import { onDidChangeSessions, RedHatAuthenticationService } from './authentication-service';
@@ -35,10 +32,11 @@ import {
3532
runSubscriptionManagerRegister,
3633
runSubscriptionManagerUnregister,
3734
} from './podman-cli';
35+
import { ContainerRegistryAuthorizerClient } from './rh-api/registry-authorizer';
36+
import { isRedHatRegistryConfigured, REGISTRY_REDHAT_IO, SubscriptionManagerClient } from './rh-api/subscription';
3837
import { SSOStatusBarItem } from './status-bar-item';
39-
import { isRedHatRegistryConfigured, REGISTRY_REDHAT_IO, signIntoRedHatDeveloperAccount } from './subscription';
4038
import { ExtensionTelemetryLogger as TelemetryLogger } from './telemetry';
41-
import { isLinux } from './util';
39+
import { isLinux, signIntoRedHatDeveloperAccount } from './util';
4240

4341
interface JwtToken {
4442
organization: {
@@ -96,29 +94,32 @@ function removeRegistry(serverUrl: string = REGISTRY_REDHAT_IO): void {
9694
async function createOrReuseRegistryServiceAccount(): Promise<void> {
9795
const currentSession = await signIntoRedHatDeveloperAccount();
9896
const accessTokenJson = parseJwt(currentSession!.accessToken);
99-
const client = new ContainerRegistryAuthorizerClient({
97+
const { serviceAccountsApiV1: saApiV1 } = new ContainerRegistryAuthorizerClient({
10098
BASE: 'https://access.redhat.com/hydra/rest/terms-based-registry',
10199
TOKEN: currentSession!.accessToken,
102100
});
103-
const saApiV1 = client.serviceAccountsApiV1;
104-
let selectedServiceAccount: ServiceAccountV1 | undefined;
105-
try {
106-
selectedServiceAccount = await saApiV1.serviceAccountByNameUsingGet1(
107-
'podman-desktop',
108-
accessTokenJson.organization.id,
109-
);
110-
} catch (err) {
101+
let { data: serviceAccount } = await saApiV1.serviceAccountByNameUsingGet1(
102+
'podman-desktop',
103+
accessTokenJson.organization.id,
104+
);
105+
106+
if (!serviceAccount) {
111107
// ignore error when there is no podman-desktop service account yet
112-
selectedServiceAccount = await saApiV1.createServiceAccountUsingPost1({
108+
const { data: createdServiceAccount } = await saApiV1.createServiceAccountUsingPost1({
113109
name: 'podman-desktop',
114110
description: 'Service account to use from Podman Desktop',
115111
redHatAccountId: accessTokenJson.organization.id,
116112
});
113+
if (createdServiceAccount) {
114+
serviceAccount = createdServiceAccount;
115+
} else {
116+
throw new Error(`Can't create registry authorizer service account.`);
117+
}
117118
}
118119

119120
await createRegistry(
120-
selectedServiceAccount!.credentials!.username!,
121-
selectedServiceAccount!.credentials!.password!,
121+
serviceAccount!.credentials!.username!,
122+
serviceAccount!.credentials!.password!,
122123
currentSession.account.label,
123124
);
124125
}
@@ -131,20 +132,20 @@ async function createOrReuseActivationKey(connection: extensionApi.ProviderConta
131132
TOKEN: currentSession!.accessToken,
132133
});
133134

134-
try {
135-
await client.activationKey.showActivationKey('podman-desktop');
136-
// podman-desktop activation key exists
137-
} catch (err) {
138-
// ignore and continue with activation key creation
139-
// TODO: add check that used role and usage exists in organization
140-
await client.activationKey.createActivationKeys({
135+
const { error: showKeyErr } = await client.activationKey.showActivationKey('podman-desktop');
136+
137+
if (showKeyErr) {
138+
// error is undefined when activation key already exists
139+
const { error: createKeyErr } = await client.activationKey.createActivationKeys({
141140
name: 'podman-desktop',
142141
role: 'RHEl Workstation',
143142
usage: 'Development/Test',
144143
serviceLevel: 'Self-Support',
145144
});
145+
if (createKeyErr) {
146+
throw new Error(createKeyErr.error?.message);
147+
}
146148
}
147-
148149
await runSubscriptionManagerRegister(connection, 'podman-desktop', accessTokenJson.organization.id);
149150
}
150151

@@ -154,8 +155,8 @@ async function isSimpleContentAccessEnabled(): Promise<boolean> {
154155
BASE: 'https://console.redhat.com/api/rhsm/v2',
155156
TOKEN: currentSession!.accessToken,
156157
});
157-
const response = await client.organization.checkOrgScaCapability();
158-
return !!response.body && response.body.simpleContentAccess === 'enabled';
158+
const data = await client.organization.checkOrgScaCapability();
159+
return data?.body?.simpleContentAccess === 'enabled';
159160
}
160161

161162
async function isSubscriptionManagerInstalled(connection: extensionApi.ProviderContainerConnection): Promise<boolean> {

0 commit comments

Comments
 (0)