Skip to content

Commit

Permalink
support editing metadata information for service instances
Browse files Browse the repository at this point in the history
  • Loading branch information
Ahoo-Wang committed May 15, 2021
1 parent 9339e76 commit f86ca99
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 6 deletions.
21 changes: 20 additions & 1 deletion dashboard/src/app/api/service/Instances.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import {ServiceInstanceDto} from './ServiceInstanceDto';
import {KeyValuePair} from '../../model/KeyValuePair';


export class Instances {
static of(): ServiceInstanceDto {
Expand All @@ -11,7 +13,24 @@ export class Instances {
weight: 1,
ephemeral: true,
ttlAt: -1,
metadata: new Map<string, string>()
metadata: new Object()
};
}

static metadataAsKeyValueArray(metadata: Map<string, string>): KeyValuePair<string, string> [] {
const keyValueArray: KeyValuePair<string, string>[] = [];
metadata.forEach(((value, key) => {
keyValueArray.push({key, value});
}));
return keyValueArray;
}

static keyValueArrayAsMetadata(keyValueArray: KeyValuePair<string, string> []): Map<string, string> {
const metadata = new Map<string, string>();
keyValueArray.forEach(keyValuePair => {
metadata.set(keyValuePair.key, keyValuePair.value);
});
return metadata;
}

}
2 changes: 1 addition & 1 deletion dashboard/src/app/api/service/ServiceInstanceDto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ export interface ServiceInstanceDto {
weight: number;
ephemeral: boolean;
ttlAt?: number;
metadata: Map<string, string>;
metadata: object;

}
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,13 @@
nzCheckedChildren="ephemeral"></nz-switch>
</nz-form-control>
</nz-form-item>
<!-- <nz-divider nzText="Metadata"></nz-divider>-->
<nz-form-item>
<nz-form-label [nzSpan]="6" nzRequired>Metadata (JSON)</nz-form-label>
<nz-form-control [nzSpan]="18">
<textarea rows="6" formControlName="metadata" nz-input [(ngModel)]="metadata"
placeholder="Please enter the metadata!"></textarea>
</nz-form-control>
</nz-form-item>
<nz-divider></nz-divider>
<nz-form-item>
<nz-form-control>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export class InstanceEditorComponent implements OnInit {
validateForm!: FormGroup;
@Input() instance!: ServiceInstanceDto;
@Output() afterRegister: EventEmitter<boolean> = new EventEmitter<boolean>();
metadata!: string;

constructor(private namespaceContext: NamespaceContext,
private serviceClient: ServiceClient,
Expand All @@ -23,25 +24,38 @@ export class InstanceEditorComponent implements OnInit {
}

ngOnInit(): void {
if (this.instance) {
this.metadata = JSON.stringify(this.instance.metadata);
}

this.validateForm = this.formBuilder.group({
serviceId: [this.instance.serviceId],
instanceId: [this.instance.instanceId],
schema: [this.instance.schema, [Validators.required]],
ip: [this.instance.ip, [Validators.required]],
port: [this.instance.port, [Validators.required]],
weight: [this.instance.weight, [Validators.required]],
ephemeral: [this.instance.ephemeral, [Validators.required]]
ephemeral: [this.instance.ephemeral, [Validators.required]],
metadata: [this.metadata]
});
}


register(): void {
try {
const objMetadata = JSON.parse(this.metadata);
this.instance.metadata = objMetadata;
} catch (e) {
this.messageService.error(`Metadata format error.\n${e}`);
return;
}

this.serviceClient.register(this.namespaceContext.ensureCurrentNamespace(), this.instance.serviceId, this.instance)
.subscribe(result => {
this.messageService.success('Instance added successfull!');
this.messageService.success('Instance saved successfull!');
this.afterRegister.emit(result);
}, error => {
this.messageService.error(`Instance add failed!\n${error}`);
this.messageService.error(`Instance saved failed!\n${error}`);
this.afterRegister.emit(false);
});
}
Expand Down
4 changes: 4 additions & 0 deletions dashboard/src/app/model/KeyValuePair.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export interface KeyValuePair<K, V> {
key: K;
value: V;
}

0 comments on commit f86ca99

Please sign in to comment.