importResources method

Future<AutomationImportResult> importResources({
  1. String? message,
  2. List<AutomationImportResource>? resources,
  3. Map<String, String>? nameTable,
  4. bool? protect,
  5. bool generateCode = true,
  6. String? converter,
  7. List<String>? converterArgs,
  8. bool check = true,
  9. bool includeSummary = true,
  10. bool showSummarySecrets = false,
  11. bool captureEvents = false,
  12. AutomationEngineEventHandler? onEvent,
  13. List<String> extraArgs = const <String>[],
})

Imports resources into this stack and returns a typed operation result.

Implementation

Future<AutomationImportResult> importResources({
  String? message,
  List<AutomationImportResource>? resources,
  Map<String, String>? nameTable,
  bool? protect,
  bool generateCode = true,
  String? converter,
  List<String>? converterArgs,
  bool check = true,
  bool includeSummary = true,
  bool showSummarySecrets = false,
  bool captureEvents = false,
  AutomationEngineEventHandler? onEvent,
  List<String> extraArgs = const <String>[],
}) async {
  final args = <String>['import', '--stack', name, '--yes', '--skip-preview'];
  if (message != null && message.trim().isNotEmpty) {
    args.addAll(<String>['--message', message]);
  }
  args.addAll(extraArgs);

  Directory? tempDir;
  String generatedCode = '';

  try {
    tempDir = await Directory.systemTemp.createTemp('pulumi-import-');
    final generatedCodePath = p.join(tempDir.path, 'generated_code.txt');

    if (resources != null) {
      final importFilePath = p.join(tempDir.path, 'import.json');
      final payload = <String, dynamic>{
        'resources': resources
            .map((resource) => resource.toJson())
            .toList(growable: false),
      };
      if (nameTable != null) {
        payload['nameTable'] = nameTable;
      }
      await File(importFilePath).writeAsString(jsonEncode(payload));
      args.addAll(<String>['--file', importFilePath]);
    }

    if (protect != null) {
      args.add('--protect=${protect ? 'true' : 'false'}');
    }

    if (generateCode) {
      args.add('--out=$generatedCodePath');
    } else {
      args.add('--generate-code=false');
    }

    if (converter != null && converter.trim().isNotEmpty) {
      args.addAll(<String>['--from', converter]);
      if (converterArgs != null && converterArgs.isNotEmpty) {
        args.add('--');
        args.addAll(converterArgs);
      }
    }

    final (result, events) = await _runStackCommand(
      args,
      check: check,
      captureEvents: captureEvents,
      onEvent: onEvent,
    );

    if (generateCode && result.succeeded) {
      final generatedCodeFile = File(generatedCodePath);
      if (await generatedCodeFile.exists()) {
        generatedCode = await generatedCodeFile.readAsString();
      }
    }

    AutomationUpdateSummary? summary;
    if (includeSummary && result.succeeded) {
      final effectiveShowSummarySecrets = workspace.remote
          ? false
          : showSummarySecrets;
      summary = workspace.remote
          ? await _awaitTerminalRemoteSummary(
              showSecrets: effectiveShowSummarySecrets,
            )
          : await infoSummary(showSecrets: effectiveShowSummarySecrets);
    }

    return AutomationImportResult(
      commandResult: result,
      events: events,
      generatedCode: generatedCode,
      summary: summary,
    );
  } finally {
    if (tempDir != null && await tempDir.exists()) {
      await tempDir.delete(recursive: true);
    }
  }
}