Skip to content
Permalink
Browse files
Add Types for Ideal State, fix Payload Format (#2168)
Add types for IdealState in helix-front. Fix an associated JSON serialization bug.
  • Loading branch information
micahstubbs committed Jun 28, 2022
1 parent dadaba1 commit 3b28b4e6db980533a1db937fbc740ace28e3b5ec
Showing 8 changed files with 49 additions and 16 deletions.
@@ -4,3 +4,4 @@ node
node_modules
target
package.json
coverage/
@@ -106,10 +106,7 @@
"node_modules/ace-builds/src-min/mode-json.js"
],
"styles": ["client/styles.scss", "client/theme.scss"],
"assets": [
"client/assets",
"client/favicon.ico"
]
"assets": ["client/assets", "client/favicon.ico"]
}
},
"lint": {
@@ -29,7 +29,7 @@ export class HelixService {
.pipe(catchError(this.errorHandler));
}

protected post(path: string, data: string): Observable<any> {
protected post(path: string, data: any): Observable<any> {
return this.http
.post(`${Settings.helixAPI}${this.getHelixKey()}${path}`, data, {
headers: this.getHeaders(),
@@ -3,10 +3,9 @@ import { Injectable } from '@angular/core';

import * as _ from 'lodash';

import { IdealState } from '../../shared/node-viewer/node-viewer.component';
import { HelixService } from '../../core/helix.service';
import { Resource } from './resource.model';
import { Cluster } from '../../cluster/shared/cluster.model';
import { Node } from '../../shared/models/node.model';

@Injectable()
export class ResourceService extends HelixService {
@@ -114,12 +113,11 @@ export class ResourceService extends HelixService {
public setIdealState(
clusterName: string,
resourceName: string,
idealState: Node
idealState: IdealState
) {
return this.post(
`/clusters/${clusterName}/resources/${resourceName}/idealState?command=update`,
JSON.stringify(idealState)
idealState
);
}
}

@@ -34,9 +34,15 @@
</mat-button-toggle-group>
<section class="viewer" [ngSwitch]="group.value" fxFlexFill>
<ngx-json-viewer *ngSwitchCase="'tree'" [json]="obj"></ngx-json-viewer>
<ace-editor *ngSwitchCase="'json'" [(text)]="objString" mode="json" theme="chrome"
[options]="{useWorker: false}" style="min-height:300px;"
#editor>
<ace-editor
*ngSwitchCase="'json'"
[(text)]="objString"
mode="json"
theme="chrome"
[options]="{ useWorker: false }"
style="min-height: 300px"
#editor
>
</ace-editor>
<section *ngSwitchCase="'table'">
<!-- TODO vxu: use mat-simple-table when it's available -->
@@ -25,6 +25,13 @@ import { Settings } from '../../core/settings';
import { InputDialogComponent } from '../dialog/input-dialog/input-dialog.component';
import { ConfirmDialogComponent } from '../dialog/confirm-dialog/confirm-dialog.component';

export type IdealState = {
id: string;
simpleFields?: { [key: string]: any };
listFields?: { [key: string]: any };
mapFields?: { [key: string]: any };
};

config.set(
'basePath',
'https://cdn.jsdelivr.net/npm/ace-builds@1.6.0/src-noconflict/'
@@ -374,10 +381,29 @@ export class NodeViewerComponent implements OnInit {

const path = this?.route?.snapshot?.data?.path;
if (path && path === 'idealState') {
const idealState: IdealState = {
id: this.resourceName,
};

// format the payload the way that helix-rest expects
// before: { simpleFields: [{ name: 'NUM_PARTITIONS', value: 2 }] };
// after: { simpleFields: { NUM_PARTITIONS: 2 } };
function appendIdealStateProperty(property: keyof Node) {
if (Array.isArray(newNode[property]) && newNode[property].length > 0) {
idealState[property] = {} as any;
(newNode[property] as any[]).forEach((field) => {
idealState[property][field.name] = field.value;
});
}
}
Object.keys(newNode).forEach((key) =>
appendIdealStateProperty(key as keyof Node)
);

const observer = this.resourceService.setIdealState(
this.clusterName,
this.resourceName,
newNode
idealState
);

if (observer) {
@@ -386,7 +412,10 @@ export class NodeViewerComponent implements OnInit {
() => {
this.helper.showSnackBar('Ideal State updated!');
},
(error) => this.helper.showError(error),
(error) => {
this.helper.showError(error);
this.isLoading = false;
},
() => (this.isLoading = false)
);
}
@@ -34,7 +34,7 @@ import { DisabledLabelComponent } from './disabled-label/disabled-label.componen
FormsModule,
NgxDatatableModule,
NgxJsonViewerModule,
AceEditorModule
AceEditorModule,
],
declarations: [
InputDialogComponent,
@@ -51,6 +51,8 @@ export class HelixCtrl {
request[method](options, (error, response, body) => {
if (error) {
res.status(500).send(error);
} else if (body?.error) {
res.status(500).send(body?.error);
} else {
res.status(response.statusCode).send(body);
}

0 comments on commit 3b28b4e

Please sign in to comment.