n8n-nodes-awx
Version:
n8n node to interact with Ansible AWX/Tower with improved type safety
69 lines • 2.53 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.HostResources = void 0;
class HostResources {
constructor(apiClient) {
this.apiClient = apiClient;
}
async list(params) {
const url = this.buildUrl('/api/v2/hosts/', params);
const response = await this.apiClient.get(url);
return response.data.results;
}
async get(id) {
if (!id) {
throw new Error('Host ID is required');
}
const response = await this.apiClient.get(`/api/v2/hosts/${id}/`);
return response.data;
}
async getByName(name, inventoryId) {
const params = { name };
if (inventoryId) {
params.inventory = inventoryId;
}
const url = this.buildUrl('/api/v2/hosts/', params);
const response = await this.apiClient.get(url);
if (response.data.count === 0) {
return null;
}
if (response.data.count > 1) {
throw new Error(`Multiple hosts found with name: ${name}`);
}
return response.data.results[0];
}
async create(data) {
const requiredFields = ['name', 'inventory'];
const missingFields = requiredFields.filter(field => data[field] === undefined || data[field] === null);
if (missingFields.length > 0) {
throw new Error(`Missing required fields: ${missingFields.join(', ')}`);
}
const response = await this.apiClient.post('/api/v2/hosts/', data);
return response.data;
}
async update(id, data) {
if (!id) {
throw new Error('Host ID is required for update');
}
const response = await this.apiClient.put(`/api/v2/hosts/${id}/`, data);
return response.data;
}
async delete(id) {
if (!id) {
throw new Error('Host ID is required for deletion');
}
await this.apiClient.delete(`/api/v2/hosts/${id}/`);
}
buildUrl(baseUrl, params) {
if (!params || Object.keys(params).length === 0) {
return baseUrl;
}
const queryString = Object.entries(params)
.filter(([_, value]) => value !== undefined && value !== null && value !== '')
.map(([key, value]) => `${encodeURIComponent(key)}=${encodeURIComponent(String(value))}`)
.join('&');
return queryString ? `${baseUrl}?${queryString}` : baseUrl;
}
}
exports.HostResources = HostResources;
//# sourceMappingURL=HostResources.js.map