Skip to content

Instantly share code, notes, and snippets.

@Eccenux
Last active August 25, 2023 15:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Eccenux/7aa2a4289d1e174f6d8d64aab34a045c to your computer and use it in GitHub Desktop.
Save Eccenux/7aa2a4289d1e174f6d8d64aab34a045c to your computer and use it in GitHub Desktop.
Proxmox multitags
// rename tag (in all nodes that have the tag)
ProxTagger.renameTag("priority0", '0-priority')
nodes = await ProxTagger.readNodes();
// filter
data = nodes
.filter(n=>n.name.search(/^(postgre00[0-9]|sm-)/) >= 0)
.filter(n=>n.name.search(/^(sm-rel)/) < 0)
.map(n=>ProxTagger.mapData(n))
;
// append tag
ProxTagger.tagMany(data, "postgres")
// replace all tags with one
//ProxTagger.tagMany(data, "postgres", true)
// copy this token from any api call
reqToken = '6a4E...iDeLw'
/**
Tagging helper for Proxmox 7+.
Filter network in FF:
-method:GET
Append tags:
```
nodes = await ProxTagger.readNodes();
// filter
data = nodes
.filter(n=>n.name.search(/^(db00[0-9]|sm-)/) >= 0)
.filter(n=>n.name.search(/^(sm-rel)/) < 0)
.map(n=>ProxTagger.mapData(n))
;
ProxTagger.tagMany(data, "postgres")
```
*/
ProxTagger = class {
/** Tag multiple nodes (defaults to appending tag(s)). */
static async tagMany(data, tagsRaw, replace) {
let res = [];
let tags = Array.isArray(tagsRaw) ? tagsRaw : [tagsRaw];
for (let d of data) {
console.log(d);
let allTags = new Set(replace ? tags : [...tags, ...d.tags]);
let re = this.tagNode(d.path, Array.from(allTags));
res.push(re);
}
await Promise.all(res);
}
static async tagRemove(data, tagsDel_, tagsAdd_) {
let res = [];
let tagsDel = Array.isArray(tagsDel_) ? tagsDel_ : [tagsDel_];
let tagsAdd = Array.isArray(tagsAdd_) ? tagsAdd_ : (tagsAdd_ ? [tagsAdd_] : []);
for (let d of data) {
console.log(d, d.tags.join());
let allTags = d.tags.filter(t => tagsDel.indexOf(t) < 0);
if (tagsAdd.length) {
allTags = Array.from(new Set([...allTags, ...tagsAdd]));
}
//console.log({was:d.tags.join(), is:allTags.join()})
let re = this.tagNode(d.path, Array.from(allTags));
//let re = Promise.resolve('done');
res.push(re);
}
await Promise.all(res);
}
/** Tag given node. */
static async tagNode(nodePath, tagsArray) {
// nodePath = 'prox001/lxc/123'
// tags = 'abc,def'
let tags = tagsArray.join(",");
await fetch(`/api2/extjs/nodes/${nodePath}/config`, {
"credentials": "include",
"headers": {
"Accept": "*/*",
"Accept-Language": "pl,en-US;q=0.7,en;q=0.3",
"CSRFPreventionToken": reqToken,
"Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
"X-Requested-With": "XMLHttpRequest",
"Sec-Fetch-Dest": "empty",
"Sec-Fetch-Mode": "cors",
"Sec-Fetch-Site": "same-origin",
"Pragma": "no-cache",
"Cache-Control": "no-cache"
},
"body": `tags=${encodeURIComponent(tags)}`,
"method": "PUT",
"mode": "cors"
});
}
/** Read all nodes data. */
static async readNodes() {
let re = await fetch("/api2/json/cluster/resources", {
"credentials": "include",
"headers": {
"Pragma": "no-cache",
"Cache-Control": "no-cache"
},
"method": "GET",
});
let d = await re.json();
// normalize
let data = d.data
.filter(n => 'name' in n)
.map(n => {
let tags = ('tags' in n) ? n.tags : '';
tags = tags.split(';');
n.tags = tags;
return n;
});
return data;
}
/** Map node data for tagging (after filters) */
static mapData(n) {
return ({
path: `${n.node}/${n.id}`,
name: n.name,
tags: n.tags
});
}
/** rename tag to a different tag */
static async renameTag(from, to) {
var nodes = await ProxTagger.readNodes();
console.log(nodes);
// preapre paths
var data = nodes
.filter(n => n.tags.indexOf(from) >= 0)
.map(n => ProxTagger.mapData(n));
console.log(data, from, to);
await ProxTagger.tagRemove(data, from, to)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment