Skip to content

Commit

Permalink
chore: cloudApi serviceClients
Browse files Browse the repository at this point in the history
  • Loading branch information
GermanVor committed Oct 30, 2024
1 parent edf5c70 commit 0079ed6
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 25 deletions.
19 changes: 9 additions & 10 deletions src/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,12 @@ import {
import { IamTokenService } from './token-service/iam-token-service';
import { MetadataTokenService } from './token-service/metadata-token-service';
import { clientFactory } from './utils/client-factory';
import { cloudApi, serviceClients } from '.';

import { getServiceClientEndpoint } from './service-endpoints';
import {
CreateIamTokenRequest,
IamTokenServiceClient,
} from './generated/yandex/cloud/iam/v1/iam_token_service';

const isOAuth = (config: SessionConfig): config is OAuthCredentialsConfig => 'oauthToken' in config;

Expand All @@ -24,23 +28,18 @@ const isIamToken = (config: SessionConfig): config is IamTokenCredentialsConfig
const isServiceAccount = (config: SessionConfig): config is ServiceAccountCredentialsConfig =>
'serviceAccountJson' in config;

const createIamToken = async (
iamEndpoint: string,
req: Partial<cloudApi.iam.iam_token_service.CreateIamTokenRequest>,
) => {
const createIamToken = async (iamEndpoint: string, req: Partial<CreateIamTokenRequest>) => {
const channel = createChannel(iamEndpoint, credentials.createSsl());
const client = clientFactory.create(serviceClients.IamTokenServiceClient.service, channel);
const resp = await client.create(
cloudApi.iam.iam_token_service.CreateIamTokenRequest.fromPartial(req),
);
const client = clientFactory.create(IamTokenServiceClient.service, channel);
const resp = await client.create(CreateIamTokenRequest.fromPartial(req));

return resp.iamToken;
};

const newTokenCreator = (config: SessionConfig): (() => Promise<string>) => {
if (isOAuth(config)) {
return () => {
const iamEndpoint = getServiceClientEndpoint(serviceClients.IamTokenServiceClient);
const iamEndpoint = getServiceClientEndpoint(IamTokenServiceClient);

return createIamToken(iamEndpoint, {
yandexPassportOauthToken: config.oauthToken,
Expand Down
12 changes: 7 additions & 5 deletions src/token-service/iam-token-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ import { credentials } from '@grpc/grpc-js';
import * as jwt from 'jsonwebtoken';
import { DateTime } from 'luxon';
import { createChannel } from 'nice-grpc';
import { cloudApi, serviceClients } from '..';
import { getServiceClientEndpoint } from '../service-endpoints';
import { IIAmCredentials, ISslCredentials, TokenService } from '../types';
import { clientFactory } from '../utils/client-factory';

const { IamTokenServiceClient } = serviceClients;
import {
CreateIamTokenRequest,
CreateIamTokenResponse,
IamTokenServiceClient,
} from '../generated/yandex/cloud/iam/v1/iam_token_service';

export class IamTokenService implements TokenService {
public readonly sslCredentials?: ISslCredentials;
Expand Down Expand Up @@ -76,11 +78,11 @@ export class IamTokenService implements TokenService {
}
}

private async requestToken(): Promise<cloudApi.iam.iam_token_service.CreateIamTokenResponse> {
private async requestToken(): Promise<CreateIamTokenResponse> {
const deadline = DateTime.now().plus({ millisecond: this.tokenRequestTimeout }).toJSDate();

return this.client().create(
cloudApi.iam.iam_token_service.CreateIamTokenRequest.fromPartial({
CreateIamTokenRequest.fromPartial({
jwt: this.getJwtRequest(),
}),
{ deadline },
Expand Down
5 changes: 3 additions & 2 deletions src/utils/operation/time-spent.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Operation } from '../../generated/yandex/cloud/operation/operation';

export const timeSpentOperation = (op: Operation): number =>
Date.now() - (op.createdAt?.getTime() ?? 0);
export const timeSpentOperation = (op: Operation): number => {
return Date.now() - (op.createdAt?.getTime() ?? 0);
};
13 changes: 5 additions & 8 deletions src/utils/operation/wait-for.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
import { cloudApi, serviceClients } from '../..';
import { Operation } from '../../generated/yandex/cloud/operation/operation';
import {
GetOperationRequest,
OperationServiceClient,
} from '../../generated/yandex/cloud/operation/operation_service';
import { Session } from '../../session';

const {
operation: {
operation_service: { GetOperationRequest },
},
} = cloudApi;

const DEFAULT_TIMEOUT_MS = 10 * 60 * 1000; // 10 minutes

export const waitForOperation = (
Expand All @@ -16,7 +13,7 @@ export const waitForOperation = (
timeoutMs: number = DEFAULT_TIMEOUT_MS,
operationServiceEndpoint?: string,
): Promise<Operation> => {
const client = session.client(serviceClients.OperationServiceClient, operationServiceEndpoint);
const client = session.client(OperationServiceClient, operationServiceEndpoint);
const maxChecksCount = Math.ceil(timeoutMs / session.pollInterval);

let checksCount = 0;
Expand Down

0 comments on commit 0079ed6

Please sign in to comment.