-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Ivan Zuev
committed
Dec 24, 2021
1 parent
5136bb5
commit 2875275
Showing
21 changed files
with
404 additions
and
199 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
16.13.1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { serviceClients, Session, cloudApi } from 'yandex-cloud'; | ||
import { getEnv } from './utils/get-env'; | ||
import { log } from './utils/logger'; | ||
|
||
const { ai: { translate_translation_service: { TranslateRequest, TranslateRequest_Format: Format } } } = cloudApi; | ||
|
||
const TEXTS = ['NodeJS SDK examples', 'Powerful, but easy to use library']; | ||
const AUTH_TOKEN = getEnv('YC_OAUTH_TOKEN'); | ||
const FOLDER_ID = getEnv('YC_FOLDER_ID'); | ||
|
||
(async () => { | ||
const session = new Session({ oauthToken: AUTH_TOKEN }); | ||
const client = session.client(serviceClients.TranslationServiceClient); | ||
|
||
const response = await client.translate(TranslateRequest.fromPartial({ | ||
targetLanguageCode: 'ru', | ||
format: Format.PLAIN_TEXT, | ||
folderId: FOLDER_ID, | ||
texts: TEXTS, | ||
})); | ||
|
||
for (const [idx, text] of response.translations.entries()) { | ||
log(`translated '${TEXTS[idx]}' => '${text.text}'`); | ||
} | ||
})(); |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
import { | ||
serviceClients, Session, cloudApi, waitForOperation, decodeMessage, | ||
} from 'yandex-cloud'; | ||
import { getEnv } from './utils/get-env'; | ||
import { log } from './utils/logger'; | ||
|
||
const AUTH_TOKEN = getEnv('YC_OAUTH_TOKEN'); | ||
const FOLDER_ID = getEnv('YC_FOLDER_ID'); | ||
const TARGET_ZONE_ID = 'ru-central1-a'; | ||
|
||
const { | ||
vpc: { | ||
network_service: { | ||
ListNetworksRequest, | ||
ListNetworkSubnetsRequest, | ||
}, | ||
}, | ||
compute: { | ||
image_service: { | ||
GetImageLatestByFamilyRequest, | ||
}, | ||
instance_service: { | ||
CreateInstanceRequest, | ||
DeleteInstanceRequest, | ||
}, | ||
instance: { | ||
IpVersion, | ||
}, | ||
}, | ||
} = cloudApi; | ||
|
||
(async () => { | ||
const session = new Session({ oauthToken: AUTH_TOKEN }); | ||
const imageClient = session.client(serviceClients.ComputeImageServiceClient); | ||
const instanceClient = session.client(serviceClients.InstanceServiceClient); | ||
const networkClient = session.client(serviceClients.NetworkServiceClient); | ||
|
||
const networkResponse = await networkClient.list(ListNetworksRequest.fromPartial({ | ||
folderId: FOLDER_ID, | ||
})); | ||
|
||
log(`Found ${networkResponse.networks.length} networks in folder ${FOLDER_ID}`); | ||
|
||
const network = networkResponse.networks.pop(); | ||
|
||
if (!network) { | ||
throw new Error(`There are no networks in folder ${FOLDER_ID}`); | ||
} | ||
|
||
const subnetsResponse = await networkClient.listSubnets(ListNetworkSubnetsRequest.fromPartial({ | ||
networkId: network.id, | ||
})); | ||
const subnet = subnetsResponse.subnets.find((s) => s.zoneId === TARGET_ZONE_ID); | ||
|
||
if (!subnet) { | ||
throw new Error(`There is no subnet in zone ${TARGET_ZONE_ID} in folder ${FOLDER_ID}`); | ||
} | ||
|
||
const image = await imageClient.getLatestByFamily(GetImageLatestByFamilyRequest.fromPartial({ | ||
family: 'ubuntu-1804-lts', | ||
folderId: 'standard-images', | ||
})); | ||
|
||
const createOp = await instanceClient.create(CreateInstanceRequest.fromPartial({ | ||
folderId: FOLDER_ID, | ||
zoneId: TARGET_ZONE_ID, | ||
platformId: 'standard-v2', | ||
labels: { 'nodejs-sdk': 'yes' }, | ||
resourcesSpec: { | ||
memory: 2 * 1024 * 1024 * 1024, | ||
cores: 2, | ||
}, | ||
bootDiskSpec: { | ||
autoDelete: true, | ||
diskSpec: { | ||
size: 10 * 1024 * 1024 * 1024, | ||
imageId: image.id, | ||
}, | ||
}, | ||
networkInterfaceSpecs: [ | ||
{ | ||
subnetId: subnet.id, | ||
primaryV4AddressSpec: { | ||
oneToOneNatSpec: { ipVersion: IpVersion.IPV4 }, | ||
}, | ||
}, | ||
], | ||
})); | ||
|
||
log(`Instance create operation id: ${createOp.id}`); | ||
|
||
const finishedCreateOp = await waitForOperation(createOp, session); | ||
|
||
if (finishedCreateOp.response) { | ||
const instance = decodeMessage<cloudApi.compute.instance.Instance>(finishedCreateOp.response); | ||
|
||
log(`Instance ${instance.id} created`); | ||
|
||
const removeOp = await instanceClient.delete(DeleteInstanceRequest.fromPartial({ | ||
instanceId: instance.id, | ||
})); | ||
|
||
const finishedRemoveOp = await waitForOperation(removeOp, session); | ||
|
||
if (finishedRemoveOp.error) { | ||
log(`Failed to remove instance ${instance.id}`); | ||
} else { | ||
log(`Successfully remove instance ${instance.id}`); | ||
} | ||
} | ||
})(); |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { serviceClients, Session, cloudApi } from 'yandex-cloud'; | ||
import { getEnv } from './utils/get-env'; | ||
import { log } from './utils/logger'; | ||
|
||
const { serverless: { functions_function_service: { ListFunctionsRequest } } } = cloudApi; | ||
const AUTH_TOKEN = getEnv('YC_OAUTH_TOKEN'); | ||
const FOLDER_ID = getEnv('YC_FOLDER_ID'); | ||
|
||
(async () => { | ||
const session = new Session({ oauthToken: AUTH_TOKEN }); | ||
const client = session.client(serviceClients.FunctionServiceClient); | ||
|
||
const response = await client.list(ListFunctionsRequest.fromPartial({ folderId: FOLDER_ID })); | ||
|
||
for (const func of response.functions) { | ||
log(`Function: ${func.name}, id: ${func.id}, invoke URL: ${func.httpInvokeUrl}`); | ||
} | ||
})(); |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { serviceClients, Session, cloudApi } from 'yandex-cloud'; | ||
import { getEnv } from './utils/get-env'; | ||
import { log } from './utils/logger'; | ||
|
||
const { | ||
iot: { | ||
devices_registry_service: { ListRegistriesRequest }, | ||
devices_registry_data_service: { PublishRegistryDataRequest }, | ||
}, | ||
} = cloudApi; | ||
const AUTH_TOKEN = getEnv('YC_OAUTH_TOKEN'); | ||
const FOLDER_ID = getEnv('YC_FOLDER_ID'); | ||
|
||
(async () => { | ||
const session = new Session({ oauthToken: AUTH_TOKEN }); | ||
const registryClient = session.client(serviceClients.IotRegistryServiceClient); | ||
const dataClient = session.client(serviceClients.RegistryDataServiceClient); | ||
|
||
const registries = await registryClient.list(ListRegistriesRequest.fromPartial({ | ||
folderId: FOLDER_ID, | ||
})); | ||
|
||
log(`Found ${registries.registries.length} registries in folder ${FOLDER_ID}`); | ||
|
||
for (const registry of registries.registries) { | ||
log(`Broadcasting to ${registry.id}`); | ||
|
||
dataClient.publish(PublishRegistryDataRequest.fromPartial({ | ||
registryId: registry.id, | ||
topic: `$registries/${registry.id}/commands/heartbeat`, | ||
data: Buffer.from('{"hello": "world"}'), | ||
})); | ||
} | ||
|
||
log('Broadcasted to all registries'); | ||
})(); |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.