Skip to content

Commit

Permalink
added new tests
Browse files Browse the repository at this point in the history
  • Loading branch information
raffis committed Feb 13, 2019
1 parent de34332 commit 076daad
Show file tree
Hide file tree
Showing 8 changed files with 385 additions and 45 deletions.
58 changes: 33 additions & 25 deletions src/icinga.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export default class Icinga {
/**
* Create host group
*/
public applyHostGroup(name: string): Promise<any> {
public applyHostGroup(name: string): Promise<boolean> {
return new Promise((resolve, reject) => {
this.logger.info(`apply host group ${name} aka kubernetes namespace`);

Expand All @@ -48,19 +48,19 @@ export default class Icinga {
this.logger.info(`host group ${name} on monitoring was not found, create one`, {error: err});

this.icingaClient.createHostGroup(name, name, [], (err, result) => {
resolve();

if (err) {
this.logger.error(`failed create host group ${name}`, {error: err});
reject(err);
} else {
this.logger.info(`host group ${name} was created successfully`, {result: result});
resolve(true);
}
});
} else {
reject();
reject(err);
}
} else {
resolve();
resolve(true);
}
});
});
Expand All @@ -79,16 +79,16 @@ export default class Icinga {
this.logger.info(`service group ${name} on monitoring was not found, create one`, {error: err});

this.icingaClient.createServiceGroup(name, name, [], (err, result) => {
resolve(true);

if (err) {
this.logger.error(`failed create service group ${name}`, {error: err});
reject(err);
} else {
this.logger.info(`service group ${name} was created successfully`, {result: result});
resolve(true);
}
});
} else {
reject();
reject(err);
}
} else {
resolve(true);
Expand All @@ -100,7 +100,7 @@ export default class Icinga {
/**
* Create or update a new icinga host object
*/
public applyHost(name: string, address: string, definition, templates: string[]=[]): Promise<boolean> {
public applyHost(name: string, definition, templates: string[]=[]): Promise<boolean> {
let host = {
attrs: definition,
templates: templates,
Expand All @@ -115,12 +115,12 @@ export default class Icinga {
this.logger.info(`host ${name} on monitoring was not found, create one`, {error: err});

this.icingaClient.createHostCustom(JSON.stringify(host), name, (err, result) => {
resolve(true);

if (err) {
this.logger.error(`failed create host ${name}`, {error: err});
reject(err);
} else {
this.logger.info(`host ${name} was created successfully`, {result: result});
resolve(true);
}
});
} else {
Expand All @@ -136,28 +136,36 @@ export default class Icinga {
/**
* Create or update an icinga service object
*/
public applyService(host: string, name: string, definition, templates: string[]=[]) {
public applyService(host: string, name: string, definition, templates: string[]=[]): Promise<boolean> {
let service = {
attrs: definition,
templates: templates,
};

this.logger.info(`apply service ${name} to host ${host}`, {service: definition});
return new Promise((resolve, reject) => {
this.logger.info(`apply service ${name} to host ${host}`, {service: definition});

this.icingaClient.getService(host, name, (err, result) => {
if (err) {
if (err.Statuscode == '404') {
this.logger.info(`service ${name} on host ${host} was not found, create one`, {error: err});
this.icingaClient.getService(host, name, (err, result) => {
if (err) {
if (err.Statuscode == '404') {
this.logger.info(`service ${name} on host ${host} was not found, create one`, {error: err});

this.icingaClient.createServiceCustom(JSON.stringify(service), host, name, (err, result) => {
if (err) {
this.logger.error(`failed create service ${name} on host ${host}`, {error: err});
} else {
this.logger.info(`service ${name} on host ${host} was created successfully`, {result: result});
}
});
this.icingaClient.createServiceCustom(JSON.stringify(service), host, name, (err, result) => {
if (err) {
this.logger.error(`failed create service ${name} on host ${host}`, {error: err});
reject(err);
} else {
this.logger.info(`service ${name} on host ${host} was created successfully`, {result: result});
resolve(true);
}
});
} else {
reject(err);
}
} else {
resolve(true);
}
}
});
});
}

Expand Down
4 changes: 2 additions & 2 deletions src/kube/ingress.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export default class Ingress extends Resource {
/**
* Apply host
*/
protected async applyHost(name: string, address: string, metadata, templates: string[]) {
protected async applyHost(name: string, address: string, metadata, templates: string[]): Promise<boolean> {
let definition = {
'display_name': name,
'address': address,
Expand All @@ -49,7 +49,7 @@ export default class Ingress extends Resource {
};

Object.assign(definition, this.options.hostDefinition);
return this.icinga.applyHost(name, address, definition, this.options.hostTemplates);
return this.icinga.applyHost(name, definition, this.options.hostTemplates);
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/kube/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export default class Node extends Resource {
/**
* Preapre icinga object and apply
*/
protected async prepareObject(definition: any): Promise<any> {
protected async prepareObject(definition: any): Promise<boolean> {
let host = {
'display_name': definition.metadata.name,
'host_name': definition.metadata.name,
Expand All @@ -48,7 +48,7 @@ export default class Node extends Resource {
}

host = Object.assign(host, this.options.hostDefinition);
return this.icinga.applyHost(host.host_name, host.host_name, host, this.options.hostTemplates);
return this.icinga.applyHost(host.host_name, host, this.options.hostTemplates);
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/kube/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ export default class Service extends Resource {
/**
* Apply host
*/
protected async applyHost(name: string, address: string, type: string, metadata, templates: string[]) {
protected async applyHost(name: string, address: string, type: string, metadata, templates: string[]): Promise<boolean> {
let definition = {
'display_name': name,
'address': address,
Expand All @@ -92,7 +92,7 @@ export default class Service extends Resource {
};

Object.assign(definition, this.options[type].hostDefinition);
return this.icinga.applyHost(name, address, definition, templates);
return this.icinga.applyHost(name, definition, templates);
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/kube/volume.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export default class Volume extends Resource {
/**
* Apply host
*/
protected async applyHost(name: string, address: string, metadata, templates: string[]) {
protected async applyHost(name: string, address: string, metadata, templates: string[]): Promise<boolean> {
let definition = {
'display_name': name,
'address': address,
Expand All @@ -49,7 +49,7 @@ export default class Volume extends Resource {
};

Object.assign(definition, this.options.hostDefinition);
return this.icinga.applyHost(name, address, definition, this.options.hostTemplates);
return this.icinga.applyHost(name, definition, this.options.hostTemplates);
}

/**
Expand Down
Loading

0 comments on commit 076daad

Please sign in to comment.