17
17
***********************************************************************/
18
18
19
19
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' ;
23
20
24
21
import icon from '../icon.png' ;
25
22
import { onDidChangeSessions , RedHatAuthenticationService } from './authentication-service' ;
@@ -35,10 +32,11 @@ import {
35
32
runSubscriptionManagerRegister ,
36
33
runSubscriptionManagerUnregister ,
37
34
} from './podman-cli' ;
35
+ import { ContainerRegistryAuthorizerClient } from './rh-api/registry-authorizer' ;
36
+ import { isRedHatRegistryConfigured , REGISTRY_REDHAT_IO , SubscriptionManagerClient } from './rh-api/subscription' ;
38
37
import { SSOStatusBarItem } from './status-bar-item' ;
39
- import { isRedHatRegistryConfigured , REGISTRY_REDHAT_IO , signIntoRedHatDeveloperAccount } from './subscription' ;
40
38
import { ExtensionTelemetryLogger as TelemetryLogger } from './telemetry' ;
41
- import { isLinux } from './util' ;
39
+ import { isLinux , signIntoRedHatDeveloperAccount } from './util' ;
42
40
43
41
interface JwtToken {
44
42
organization : {
@@ -96,29 +94,32 @@ function removeRegistry(serverUrl: string = REGISTRY_REDHAT_IO): void {
96
94
async function createOrReuseRegistryServiceAccount ( ) : Promise < void > {
97
95
const currentSession = await signIntoRedHatDeveloperAccount ( ) ;
98
96
const accessTokenJson = parseJwt ( currentSession ! . accessToken ) ;
99
- const client = new ContainerRegistryAuthorizerClient ( {
97
+ const { serviceAccountsApiV1 : saApiV1 } = new ContainerRegistryAuthorizerClient ( {
100
98
BASE : 'https://access.redhat.com/hydra/rest/terms-based-registry' ,
101
99
TOKEN : currentSession ! . accessToken ,
102
100
} ) ;
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 ) {
111
107
// ignore error when there is no podman-desktop service account yet
112
- selectedServiceAccount = await saApiV1 . createServiceAccountUsingPost1 ( {
108
+ const { data : createdServiceAccount } = await saApiV1 . createServiceAccountUsingPost1 ( {
113
109
name : 'podman-desktop' ,
114
110
description : 'Service account to use from Podman Desktop' ,
115
111
redHatAccountId : accessTokenJson . organization . id ,
116
112
} ) ;
113
+ if ( createdServiceAccount ) {
114
+ serviceAccount = createdServiceAccount ;
115
+ } else {
116
+ throw new Error ( `Can't create registry authorizer service account.` ) ;
117
+ }
117
118
}
118
119
119
120
await createRegistry (
120
- selectedServiceAccount ! . credentials ! . username ! ,
121
- selectedServiceAccount ! . credentials ! . password ! ,
121
+ serviceAccount ! . credentials ! . username ! ,
122
+ serviceAccount ! . credentials ! . password ! ,
122
123
currentSession . account . label ,
123
124
) ;
124
125
}
@@ -131,20 +132,20 @@ async function createOrReuseActivationKey(connection: extensionApi.ProviderConta
131
132
TOKEN : currentSession ! . accessToken ,
132
133
} ) ;
133
134
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 ( {
141
140
name : 'podman-desktop' ,
142
141
role : 'RHEl Workstation' ,
143
142
usage : 'Development/Test' ,
144
143
serviceLevel : 'Self-Support' ,
145
144
} ) ;
145
+ if ( createKeyErr ) {
146
+ throw new Error ( createKeyErr . error ?. message ) ;
147
+ }
146
148
}
147
-
148
149
await runSubscriptionManagerRegister ( connection , 'podman-desktop' , accessTokenJson . organization . id ) ;
149
150
}
150
151
@@ -154,8 +155,8 @@ async function isSimpleContentAccessEnabled(): Promise<boolean> {
154
155
BASE : 'https://console.redhat.com/api/rhsm/v2' ,
155
156
TOKEN : currentSession ! . accessToken ,
156
157
} ) ;
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' ;
159
160
}
160
161
161
162
async function isSubscriptionManagerInstalled ( connection : extensionApi . ProviderContainerConnection ) : Promise < boolean > {
0 commit comments