1717 ***********************************************************************/
1818
1919import * 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
2421import icon from '../icon.png' ;
2522import { 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' ;
3837import { SSOStatusBarItem } from './status-bar-item' ;
39- import { isRedHatRegistryConfigured , REGISTRY_REDHAT_IO , signIntoRedHatDeveloperAccount } from './subscription' ;
4038import { ExtensionTelemetryLogger as TelemetryLogger } from './telemetry' ;
41- import { isLinux } from './util' ;
39+ import { isLinux , signIntoRedHatDeveloperAccount } from './util' ;
4240
4341interface JwtToken {
4442 organization : {
@@ -101,24 +99,29 @@ async function createOrReuseRegistryServiceAccount(): Promise<void> {
10199 TOKEN : currentSession ! . accessToken ,
102100 } ) ;
103101 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 ) {
102+ await saApiV1 . removeServiceAccountUsingDelete1 ( 'podman-desktop' , accessTokenJson . organization . id ) ;
103+ let { data : serviceAccount } = await saApiV1 . serviceAccountByNameUsingGet1 (
104+ 'podman-desktop' ,
105+ accessTokenJson . organization . id ,
106+ ) ;
107+
108+ if ( ! serviceAccount ) {
111109 // ignore error when there is no podman-desktop service account yet
112- selectedServiceAccount = await saApiV1 . createServiceAccountUsingPost1 ( {
110+ const { data : createdServiceAccount } = await saApiV1 . createServiceAccountUsingPost1 ( {
113111 name : 'podman-desktop' ,
114112 description : 'Service account to use from Podman Desktop' ,
115113 redHatAccountId : accessTokenJson . organization . id ,
116114 } ) ;
115+ if ( createdServiceAccount ) {
116+ serviceAccount = createdServiceAccount ;
117+ } else {
118+ throw new Error ( `Can't create registry authorizer service account.` ) ;
119+ }
117120 }
118121
119122 await createRegistry (
120- selectedServiceAccount ! . credentials ! . username ! ,
121- selectedServiceAccount ! . credentials ! . password ! ,
123+ serviceAccount ! . credentials ! . username ! ,
124+ serviceAccount ! . credentials ! . password ! ,
122125 currentSession . account . label ,
123126 ) ;
124127}
@@ -131,20 +134,20 @@ async function createOrReuseActivationKey(connection: extensionApi.ProviderConta
131134 TOKEN : currentSession ! . accessToken ,
132135 } ) ;
133136
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 ( {
137+ const { error : showKeyErr } = await client . activationKey . showActivationKey ( 'podman-desktop' ) ;
138+
139+ if ( showKeyErr ) {
140+ // error is undefined when activation key already exists
141+ const { error : createKeyErr } = await client . activationKey . createActivationKeys ( {
141142 name : 'podman-desktop' ,
142- role : 'RHEL Server ' ,
143+ role : 'RHEl Workstation ' ,
143144 usage : 'Development/Test' ,
144145 serviceLevel : 'Self-Support' ,
145146 } ) ;
147+ if ( createKeyErr ) {
148+ throw new Error ( createKeyErr . error ?. message ) ;
149+ }
146150 }
147-
148151 await runSubscriptionManagerRegister ( connection , 'podman-desktop' , accessTokenJson . organization . id ) ;
149152}
150153
@@ -155,7 +158,7 @@ async function isSimpleContentAccessEnabled(): Promise<boolean> {
155158 TOKEN : currentSession ! . accessToken ,
156159 } ) ;
157160 const response = await client . organization . checkOrgScaCapability ( ) ;
158- return ! ! response . body && response . body . simpleContentAccess === 'enabled' ;
161+ return response . data ? .body ? .simpleContentAccess === 'enabled' ;
159162}
160163
161164async function isSubscriptionManagerInstalled ( connection : extensionApi . ProviderContainerConnection ) : Promise < boolean > {
0 commit comments