All files / src/commands/ng ng.create.external.command.js

72.72% Statements 32/44
100% Branches 1/1
0% Functions 0/1
72.72% Lines 32/44

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x                         193x 193x  
import { z } from 'zod';
import { defineArgument } from '../../lib/define-argument.js';
import { defineCommand } from '../../lib/define-command.js';
import { styleText } from '../../lib/style-text.js';
import { Logger } from '../../logger.js';
import * as networkGroupResources from '../../models/ng-resources.js';
import { ngResourceType } from '../../parsers.js';
import { orgaIdOrNameOption } from '../global.options.js';
import { ngIdOrLabelArg } from './ng.args.js';
 
export const ngCreateExternalCommand = defineCommand({
  description: 'Create an external peer in a Network Group',
  since: '3.12.0',
  options: {
    org: orgaIdOrNameOption,
  },
  args: [
    defineArgument({
      schema: z.string().transform(ngResourceType),
      description: 'External peer label',
      placeholder: 'external-peer-label',
    }),
    ngIdOrLabelArg,
    defineArgument({
      schema: z.string(),
      description: 'WireGuard public key of the external peer to link to a Network Group',
      placeholder: 'public-key',
    }),
  ],
  async handler(options, peerIdOrLabel, ngIdOrLabel, publicKey) {
    const { org } = options;

    await networkGroupResources.createExternalPeerWithParent(
      ngIdOrLabel,
      peerIdOrLabel.ngResourceLabel,
      publicKey,
      org,
    );
    const ngText = ngIdOrLabel.ngResourceLabel ?? ngIdOrLabel.ngId;
    Logger.printSuccess(
      `External peer ${styleText('green', peerIdOrLabel.ngResourceLabel)} successfully created in Network Group ${styleText('green', ngText)}`,
    );
  },
});