Skip to content

Commit

Permalink
WIP: handling of non-JSON plan output
Browse files Browse the repository at this point in the history
  • Loading branch information
radeksimko committed Nov 9, 2023
1 parent 8bcbe85 commit b0e703e
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 19 deletions.
40 changes: 23 additions & 17 deletions src/providers/tfc/planProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import * as vscode from 'vscode';
import * as readline from 'readline';
import { Writable } from 'stream';
import axios from 'axios';
import semver from 'semver';
import TelemetryReporter from '@vscode/extension-telemetry';

import { apiClient } from '../../terraformCloud';
Expand Down Expand Up @@ -53,14 +54,17 @@ export class PlanTreeDataProvider implements vscode.TreeDataProvider<vscode.Tree
return [];
}

const jsonExpected =
this.plan.attributes['structured-run-output-enabled'] && semver.satisfies(this.plan.terraformVersion, '> 0.15.2');

try {
return this.getPlanFromUrl(this.plan.logReadUrl);
return this.getPlanFromUrl(this.plan.logReadUrl, jsonExpected);
} catch (error) {
return [];
}
}

private async getPlanFromUrl(planLogUrl: string): Promise<vscode.TreeItem[]> {
private async getPlanFromUrl(planLogUrl: string, jsonExpected: boolean): Promise<vscode.TreeItem[]> {
const session = await vscode.authentication.getSession(TerraformCloudAuthenticationProvider.providerID, [], {
createIfNone: false,
});
Expand All @@ -86,22 +90,24 @@ export class PlanTreeDataProvider implements vscode.TreeDataProvider<vscode.Tree
const items: PlannedChange[] = [];

for await (const line of lineStream) {
try {
const logLine: LogLine = JSON.parse(line);

// TODO: non-JSON output?

// TODO: resource_drift
// TODO: import?
// TODO: test_*

if (logLine.type === 'planned_change' && logLine.change) {
const runItem = new PlannedChange(logLine.change.action, logLine.change.resource);
items.push(runItem);
if (jsonExpected) {
try {
const logLine: LogLine = JSON.parse(line);

// TODO: resource_drift
// TODO: import?
// TODO: test_*

if (logLine.type === 'planned_change' && logLine.change) {
const runItem = new PlannedChange(logLine.change.action, logLine.change.resource);
items.push(runItem);
}
} catch (e) {
// skip any non-JSON lines, like Terraform version output
continue;
}
} catch (e) {
// skip any non-JSON lines, like Terraform version output
continue;
} else {
// TODO: handle non-JSON output
}
}

Expand Down
9 changes: 7 additions & 2 deletions src/providers/tfc/runProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ export class RunTreeDataProvider implements vscode.TreeDataProvider<TFCRunTreeIt
if (plan) {
const status = plan.data.attributes.status;
const label = status === 'unreachable' ? 'Plan will not run' : `Plan ${status}`;
const item = new PlanTreeItem(root.planId, label, plan.data.attributes);
const item = new PlanTreeItem(root.planId, label, plan.data.attributes, root.attributes['terraform-version']);
switch (status) {
case 'unreachable':
item.label = 'Plan will not run';
Expand Down Expand Up @@ -283,7 +283,12 @@ export class RunTreeItem extends vscode.TreeItem {
export class PlanTreeItem extends vscode.TreeItem {
public logReadUrl = '';

constructor(public id: string, public label: string, public attributes: PlanAttributes) {
constructor(
public id: string,
public label: string,
public attributes: PlanAttributes,
public terraformVersion: ,

Check failure on line 290 in src/providers/tfc/runProvider.ts

View workflow job for this annotation

GitHub Actions / test (1.75.1, windows-latest)

Type expected.

Check failure on line 290 in src/providers/tfc/runProvider.ts

View workflow job for this annotation

GitHub Actions / test (1.75.1, macos-latest)

Type expected.

Check failure on line 290 in src/providers/tfc/runProvider.ts

View workflow job for this annotation

GitHub Actions / test (1.75.1, macos-11.0)

Type expected.

Check failure on line 290 in src/providers/tfc/runProvider.ts

View workflow job for this annotation

GitHub Actions / test (1.75.1, ubuntu-latest)

Type expected.

Check failure on line 290 in src/providers/tfc/runProvider.ts

View workflow job for this annotation

GitHub Actions / test (insiders, windows-latest)

Type expected.

Check failure on line 290 in src/providers/tfc/runProvider.ts

View workflow job for this annotation

GitHub Actions / test (insiders, macos-latest)

Type expected.

Check failure on line 290 in src/providers/tfc/runProvider.ts

View workflow job for this annotation

GitHub Actions / test (insiders, macos-11.0)

Type expected.

Check failure on line 290 in src/providers/tfc/runProvider.ts

View workflow job for this annotation

GitHub Actions / test (insiders, ubuntu-latest)

Type expected.

Check failure on line 290 in src/providers/tfc/runProvider.ts

View workflow job for this annotation

GitHub Actions / test (stable, windows-latest)

Type expected.

Check failure on line 290 in src/providers/tfc/runProvider.ts

View workflow job for this annotation

GitHub Actions / test (stable, macos-latest)

Type expected.

Check failure on line 290 in src/providers/tfc/runProvider.ts

View workflow job for this annotation

GitHub Actions / test (stable, macos-11.0)

Type expected.

Check failure on line 290 in src/providers/tfc/runProvider.ts

View workflow job for this annotation

GitHub Actions / test (stable, ubuntu-latest)

Type expected.
) {
super(label);
this.iconPath = GetPlanApplyStatusIcon(attributes.status);
if (attributes) {
Expand Down

0 comments on commit b0e703e

Please sign in to comment.