-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathactivation.ts
124 lines (109 loc) · 4.58 KB
/
activation.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/* tslint:disable no-var-requires */
import { flags, SfdxCommand } from '@salesforce/command';
import chalk from 'chalk';
import request = require('request-promise-native');
export default class Activation extends SfdxCommand {
protected static deprecated = {
version: 48,
message: `This command is no longer used. Use flows instead of orchestrations for IoT use cases`
};
public static description = 'Activate an iot orchestration by name';
public static examples = [
`sfdx shane:iot:activate -n orchName -r
// activates the orchestration, including the context if necessary, optionally resetting all the instances
`,
`sfdx shane:iot:activate -n orchName -d
// deactivates the orchestration, without resetting all the instances
`
];
protected static flagsConfig = {
name: flags.string({ char: 'n', required: true, description: 'API name of the orchestration' }),
reset: flags.boolean({ char: 'r', description: 'reset all instances of the orchestration' }),
deactivate: flags.boolean({ char: 'd', description: 'deactivate the orchestration' })
};
protected static requiresUsername = true;
public async run(): Promise<any> {
// hardcoded because I've been burned on this one before
const version = this.flags.apiversion ?? '44.0';
const conn = this.org.getConnection();
const auth = {
bearer: conn.accessToken
};
const options = {
resetInstancesOnActivation: Boolean(this.flags.reset)
};
// first, get the orchestration Id
const { orchestrations } = JSON.parse(
await request.get(`${conn.instanceUrl}/services/data/v${version}/iot/orchestrations`, {
auth
})
);
// console.log(orchestrations);
// find the matching orchestration by name
const thisOrch = orchestrations.find(i => i.name === this.flags.name);
// didn't find a match. Tell the user what's in there.
if (!thisOrch) {
throw new Error(`No orchestration found matching that name. Orchestrations found: ${orchestrations.map(x => x.name).join(', ')}`);
}
// this.ux.logJson(thisOrch);
this.ux.log(`found orchestration with id ${thisOrch.id}. Will activate its context ${thisOrch.orchestrationContext.label}`);
const contextActivationResult = await request.post({
url: `${conn.instanceUrl}/services/data/v${version}/iot/contexts/${thisOrch.orchestrationContext.id}/activations`,
body: {},
auth,
json: true
});
this.ux.log(contextActivationResult);
// wait for a sign that the context is active
let contextActive = false;
do {
const context = JSON.parse(
await request.get(`${conn.instanceUrl}/services/data/v${version}/iot/contexts/${thisOrch.orchestrationContext.id}`, {
auth
})
);
// this.ux.logJson(context);
if (context.activationStatus === 'Active') {
contextActive = true;
}
} while (!contextActive);
if (this.flags.deactivate) {
const deactivationResult = await request.patch({
url: `${conn.instanceUrl}/services/data/v42.0/iot/orchestrations/${thisOrch.id}/activations/last`,
body: {
status: 'Inactive',
options
},
auth,
json: true
});
this.ux.log(chalk.green('Orchestration deactivating.'));
return deactivationResult;
}
const activationResult = await request.post({
url: `${conn.instanceUrl}/services/data/v42.0/iot/orchestrations/${thisOrch.id}/activations`,
body: {
options
},
auth,
json: true
});
this.ux.log(chalk.green('Orchestration activating.'));
// wait for a sign that the context is active
let orchActive = false;
do {
const newOrch = JSON.parse(
await request.get(`${conn.instanceUrl}/services/data/v${version}/iot/orchestrations/${thisOrch.id}`, {
auth
})
);
// this.ux.logJson(newOrch);
if (newOrch.status === 'Active') {
orchActive = true;
this.ux.log(chalk.green('Orchestration is active.'));
return newOrch;
}
} while (!orchActive);
return activationResult;
}
}