forked from josephjacks/aci-connector-k8s
-
Notifications
You must be signed in to change notification settings - Fork 0
/
resource.ts
73 lines (69 loc) · 2.49 KB
/
resource.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import azureResource = require('azure-arm-resource');
import rest = require('ms-rest');
export async function getResource(client: azureResource.ResourceManagementClient, id: string, version: string): Promise<Object> {
let promise = new Promise((resolve, reject) => {
client.resources.getById(id, version, function (err, result) {
if (err) {
if ((err as rest.ServiceError).statusCode != 404) {
reject(err);
} else {
resolve(null);
}
} else {
resolve(result);
}
});
});
return promise;
}
export async function getMatchingResources(client: azureResource.ResourceManagementClient, type:string, version: string): Promise<Array<Object>> {
let result = new Promise<Array<Object>>((resolve, reject) => {
client.resources.list(async function (err, result, request, response) {
if (err) {
reject(err);
} else {
let arr = [];
for (let rsrc of result) {
if (rsrc.type == type) {
try {
let obj = await getResource(client, rsrc.id, version);
if (obj != null) {
arr.push(obj);
}
} catch (Exception) {
console.log(Exception);
reject(Exception);
return;
}
}
}
resolve(arr);
}
});
});
return result;
};
export function createResource(client: azureResource.ResourceManagementClient, group: string, namespace: string, type: string, name: string, params: Object, version: string) {
let result = new Promise((resolve, reject) => {
client.resources.createOrUpdate(group, namespace, '', type, name, version, params, (err, result) => {
if (err) {
reject(err);
return;
}
resolve(result);
});
});
return result;
}
export function deleteResource(client, id, version) {
let promise = new Promise((resolve, reject) => {
client.resources.deleteById(id, version, function (err, result) {
if (err) {
reject(err);
} else {
resolve(result);
}
});
});
return promise;
}