-
Notifications
You must be signed in to change notification settings - Fork 1
/
tmdl_tool.cs
435 lines (400 loc) · 20.4 KB
/
tmdl_tool.cs
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
using Microsoft.AnalysisServices.Tabular;
using Newtonsoft.Json;
namespace tmdl_tool
{
internal class tmdl_tool
{
private const int ERROR_SUCCESS = 0x0;
private const int ERROR_INVALID_ACTION = 0x667;
private const int ERROR_PATH_NOT_FOUND = 0x3;
private const int ERROR_NETWORK_ACCESS = 0x44;
private const int ERROR_UNEXPECTED_ERROR = 0x999;
private static string VersionString = "";
private static Settings runtimeSettings = new();
static void Main(string[] args)
{
var assemblyInfo = System.Reflection.Assembly.GetExecutingAssembly().GetName();
var tmdlLib = System.Reflection.Assembly.GetAssembly(typeof(Server));
var libName = tmdlLib?.GetName()?.Name ?? "Unknown";
var libVersion = tmdlLib?.GetName()?.Version?.ToString() ?? "Unknown";
VersionString = $"{assemblyInfo.Name} v.{assemblyInfo.Version}, {libName}: {libVersion}";
Environment.ExitCode = ERROR_UNEXPECTED_ERROR;
PrintHelpIfRequested(args);
GetArguments(args);
GetSettings(runtimeSettings.SettingsFilePath);
LogToConsole($"Starting with options:\n{JsonConvert.SerializeObject(runtimeSettings.GetSafe(), Formatting.Indented)}");
PBI(runtimeSettings);
}
/// <summary>
/// Prints the help message if the "--help" or "-h" option is present in the command-line arguments.
/// </summary>
/// <param name="args">The command-line arguments.</param>
private static void PrintHelpIfRequested(string[] args)
{
if (args.Contains("--help") || args.Contains("-h"))
{
Console.WriteLine("Usage: tmdl_tool [arguments]");
Console.WriteLine();
Console.WriteLine("Arguments:");
Console.WriteLine(" -w\tThe URL of the Power BI workspace\n\t--workspacexmla \"<url>\"");
Console.WriteLine(" -d\tThe name of the Power BI dataset \n\t--datasetname \"<name>\"");
Console.WriteLine(" -t\tThe path to the TMDL folder \n\t--tmdlfolderpath \"<path>\"");
Console.WriteLine(" -a\tThe action to perform (pull or deploy) \n\t--action \"<pull|deploy>\"");
Console.WriteLine(" -s\tThe path to the settings file (default: settings.json) \n\t--settingsfilepath \"<path>\"");
Console.WriteLine(" -ai\tThe application ID of the Azure AD app \n\t--appid \"<id>\"");
Console.WriteLine(" -as\tThe application secret of the Azure AD app \n\t--appsecret \"<secret>\"");
Console.WriteLine(" -ti\tThe tenant ID of the Azure AD app \n\t--tenantid \"<id>\"");
Console.WriteLine(" -at\tExternally aquired Access Token for Azure AD app \n\t--accessToken \"<AccessToken>\"");
Console.WriteLine(" -v\tShow progress on STDOUT \n\t--verbose \"<true|false>\" (default: true)");
Console.WriteLine();
Console.WriteLine(" -h\tShow this help message \n\t--help\n");
Console.WriteLine();
Console.WriteLine("Environment:");
Console.WriteLine(" Use an Environment Variable `tmdl_accessToken` to pass a Bearer Access Token to the PowerBI Server");
Console.WriteLine();
Console.WriteLine("Examples:");
Console.WriteLine(" tmdl_tool --workspacexmla \"powerbi://api.powerbi.com/v1.0/myorg/MyWorkspace\" --datasetname \"MyDataset\" --tmdlfolderpath \"C:\\TMDL\" --action \"pull\"");
Console.WriteLine(" tmdl_tool -w \"powerbi://api.powerbi.com/v1.0/myorg/MyWorkspace\" -d \"MyDataset\" -t \"C:\\TMDL\" -a \"pull\"");
Console.WriteLine(" tmdl_tool -s \"C:\\settings.json\" -a \"deploy\"");
Console.WriteLine(" tmdl_tool -ai \"xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx\" -as \"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\" -ti \"xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx\" -a \"deploy\"");
Console.WriteLine(" tmdl_tool deploy");
Console.WriteLine(" tmdl_tool pull");
Environment.Exit(ERROR_SUCCESS);
}
}
/// <summary>
/// Log to console if verbose is set to true
/// </summary>
/// <param name="message"></param>
private static void LogToConsole(string message)
{
if (!runtimeSettings.Verbose) return;
var timeStampString = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff");
Console.WriteLine($"{timeStampString} [{VersionString}]: {message}");
}
/// <summary>
/// Parses the command-line arguments and sets the corresponding variables.
/// </summary>
/// <param name="args">The command-line arguments.</param>
private static void GetArguments(string[] args)
{
for (int i = 0; i < args.Length; i++)
{
string arg = args[i];
if (arg.StartsWith("-"))
{
string value = "";
if (i + 1 < args.Length && !args[i + 1].StartsWith("-"))
{
value = args[i + 1];
i++;
}
switch (arg.ToLower())
{
case "--workspacexmla":
case "-w":
runtimeSettings.WorkspaceXMLA = value ?? "";
break;
case "--datasetname":
case "-d":
runtimeSettings.DatasetName = value ?? "";
break;
case "--tmdlfolderpath":
case "-t":
runtimeSettings.TmdlFolderPath = value ?? "";
break;
case "--action":
case "-a":
runtimeSettings.Action = value.ToLower() ?? "";
break;
case "--settingsfilepath":
case "-s":
runtimeSettings.SettingsFilePath = value ?? "";
break;
case "--appid":
case "-ai":
runtimeSettings.AppId = value ?? "";
break;
case "--appsecret":
case "-as":
runtimeSettings.AppSecret = value ?? "";
break;
case "--tenantid":
case "-ti":
runtimeSettings.TenantId = value ?? "";
break;
case "--accesstoken":
case "-at":
runtimeSettings.AccessToken = value ?? "";
break;
case "--verbose":
case "-v":
runtimeSettings.Verbose = bool.Parse(value);
break;
default:
Console.WriteLine($"Unknown argument: {arg}");
Environment.ExitCode = ERROR_INVALID_ACTION;
return;
}
}
else if (i == 0 && (arg == "pull" || arg == "deploy"))
{
runtimeSettings.Action = arg;
}
else
{
Console.WriteLine($"Unknown argument: {arg}");
Environment.ExitCode = ERROR_INVALID_ACTION;
return;
}
}
}
/// <summary>
/// Reads the settings file and updates the workspaceXMLA, datasetName, tmdlfolderPath, and action variables with the values from the settings file.
/// </summary>
/// <param name="settingsFilePath">The path to the settings file (default: settings.json).</param>
private static void GetSettings(string settingsFilePath)
{
settingsFilePath = string.IsNullOrEmpty(settingsFilePath) ? "settings.json" : settingsFilePath;
if (File.Exists(settingsFilePath))
{
runtimeSettings.SettingsFilePath = settingsFilePath;
var settings = JsonConvert.DeserializeObject<Settings>(File.ReadAllText(settingsFilePath));
if (settings == null)
{
Console.WriteLine("Error: Unable to read settings file.");
Environment.ExitCode = ERROR_PATH_NOT_FOUND;
return;
}
runtimeSettings.WorkspaceXMLA = string.IsNullOrEmpty(settings.WorkspaceXMLA) ? runtimeSettings.WorkspaceXMLA : settings.WorkspaceXMLA;
runtimeSettings.DatasetName = string.IsNullOrEmpty(settings.DatasetName) ? runtimeSettings.DatasetName : settings.DatasetName;
runtimeSettings.TmdlFolderPath = string.IsNullOrEmpty(settings.TmdlFolderPath) ? runtimeSettings.TmdlFolderPath : settings.TmdlFolderPath;
runtimeSettings.Action = string.IsNullOrEmpty(settings.Action) ? runtimeSettings.Action : settings.Action.ToLower();
runtimeSettings.AppId = string.IsNullOrEmpty(settings.AppId) ? runtimeSettings.AppId : settings.AppId;
runtimeSettings.AppSecret = string.IsNullOrEmpty(settings.AppSecret) ? runtimeSettings.AppSecret : settings.AppSecret;
runtimeSettings.TenantId = string.IsNullOrEmpty(settings.TenantId) ? runtimeSettings.TenantId : settings.TenantId;
runtimeSettings.AccessToken = string.IsNullOrEmpty(settings.AccessToken) ? runtimeSettings.AccessToken : settings.AccessToken;
runtimeSettings.Verbose = ! settings.Verbose ? false : runtimeSettings.Verbose;
}
}
/// <summary>
/// Performs the specified action (pull or deploy) on the specified Power BI dataset using the specified TMDL folder.
/// </summary>
/// <param name="workspaceXMLA">The URL of the Power BI workspace.</param>
/// <param name="datasetName">The name of the Power BI dataset.</param>
/// <param name="tmdlfolderPath">The path to the TMDL folder.</param>
/// <param name="action">The action to perform (pull or deploy).</param>
/// <param name="appId">Application Id</param>
/// <param name="appSecret">Application Secret</param>
/// <param name="tenantId">Tenant Id</param>
/// <param name="accessToken">Access Token</param>
private static void PBI(Settings settings)
{
string[] actions = { "pull", "deploy" };
if (Array.IndexOf(actions, settings.Action) < 0)
{
Console.WriteLine("Please specify action as pull or deploy (-h | --help for Help)");
Environment.ExitCode = ERROR_INVALID_ACTION;
return;
}
var server = Connect(settings);
if (server == null)
{
Environment.ExitCode = ERROR_NETWORK_ACCESS;
return;
}
if (settings.Action == "pull")
{
Pull_TMDL(server, settings.DatasetName, settings.TmdlFolderPath);
}
else if (settings.Action == "deploy")
{
Deploy_TMDL(server, settings.DatasetName, settings.TmdlFolderPath);
}
}
/// <summary>
/// Pulls the TMDL from the specified Power BI dataset and saves it to the specified TMDL folder.
/// </summary>
/// <param name="server">PBI Server</param>
/// <param name="datasetName">The name of the Power BI dataset.</param>
/// <param name="tmdlfolderPath">The path to the TMDL folder.</param>
private static void Pull_TMDL(Server server, string datasetName, string tmdlfolderPath)
{
try
{
using (server)
{
LogToConsole($"Getting Databases for {datasetName}");
var database = server.Databases.GetByName(datasetName);
LogToConsole($"Serializing model from {database.Name} to {tmdlfolderPath}");
TmdlSerializer.SerializeModelToFolder(database.Model, tmdlfolderPath);
LogToConsole($"Model pulled successfully to {tmdlfolderPath}");
Environment.ExitCode = ERROR_SUCCESS;
}
}
catch (Exception ex)
{
Console.WriteLine(ex.GetType().ToString());
Console.WriteLine(ex.GetBaseException().Message);
Console.WriteLine(ex.ToString());
Environment.ExitCode = ERROR_PATH_NOT_FOUND;
}
}
/// <summary>
/// Deploys the TMDL from the specified TMDL folder to the specified Power BI dataset.
/// </summary>
/// <param name="workspaceXMLA">The URL of the Power BI workspace.</param>
/// <param name="datasetName">The name of the Power BI dataset.</param>
/// <param name="tmdlfolderPath">The path to the TMDL folder.</param>
private static void Deploy_TMDL(Server server, string datasetName, string tmdlfolderPath)
{
try
{
var model = TmdlSerializer.DeserializeModelFromFolder(tmdlfolderPath);
using (server)
{
using (var remoteDatabase = server.Databases.GetByName(datasetName))
{
LogToConsole($"Deploying model to {datasetName}");
model.CopyTo(remoteDatabase.Model);
LogToConsole($"Saving changes to {datasetName}");
remoteDatabase.Model.SaveChanges();
LogToConsole($"Model deployed successfully to {datasetName}");
Environment.ExitCode = ERROR_SUCCESS;
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.GetType().ToString());
Console.WriteLine(ex.ToString());
if (ex.ToString().Contains("path does not exists"))
{
Console.WriteLine("Error: The path does not exist.");
Console.WriteLine("Please check the path and try again.");
Environment.ExitCode = ERROR_PATH_NOT_FOUND;
}
}
}
/// <summary>
/// Connects to the specified Power BI workspace using the provided URL.
/// </summary>
/// <param name="workspaceXMLA">The URL of the Power BI workspace.</param>
/// <param name="appId">The application ID for authentication.</param>
/// <param name="appSecret">The application secret for authentication.</param>
/// <param name="tenantId">The tenant ID for authentication.</param>
/// <param name="accessToken">The access token for authentication.</param>
/// <returns>A Server object representing the connected workspace, or null if the connection failed.</returns>
private static Server? Connect(Settings settings)
{
try
{
var server = new Server();
// Check if we have an access token in Environment variable
var tokenString = Environment.GetEnvironmentVariable("tmdl_accesstoken");
if (! string.IsNullOrEmpty(settings.AccessToken))
{
LogToConsole("Using Access Token from Settings / Command Line");
var accessTokenObj = new Microsoft.AnalysisServices.AccessToken(settings.AccessToken, DateTime.UtcNow + TimeSpan.FromHours(1));
server.AccessToken = accessTokenObj;
}
else if (! string.IsNullOrEmpty(tokenString))
{
LogToConsole("Using Access Token from Environment Variable");
var accessTokenObj = new Microsoft.AnalysisServices.AccessToken(tokenString, DateTime.UtcNow + TimeSpan.FromHours(1));
server.AccessToken = accessTokenObj;
};
string connectionString = $"Data source={settings.WorkspaceXMLA};User ID=app:{settings.AppId}@{settings.TenantId};Password={settings.AppSecret}";
LogToConsole($"Connecting to {settings.WorkspaceXMLA}");
server.Connect(connectionString);
return server;
}
catch (Exception ex)
{
string exType = ex.GetType().ToString();
if (exType == "Microsoft.AnalysisServices.Authentication.AuthenticationException")
{
Console.WriteLine("Error: " + ex.GetBaseException().Message);
Console.WriteLine(ex.ToString());
Environment.ExitCode = ERROR_NETWORK_ACCESS;
}
else if (exType == "Microsoft.AnalysisServices.ConnectionException")
{
Console.WriteLine("Error: " + ex.GetBaseException().Message);
Console.WriteLine(ex.ToString());
Environment.ExitCode = ERROR_NETWORK_ACCESS;
}
else
{
Console.WriteLine(exType);
Console.WriteLine(ex.GetBaseException().Message);
Console.WriteLine(ex.ToString());
Environment.ExitCode = ERROR_NETWORK_ACCESS;
}
return null;
}
}
/// <summary>
/// Represents the settings used by the tmdl_tool program.
/// </summary>
private class Settings
{
/// <summary>
/// The URL of the Power BI workspace.
/// </summary>
public string WorkspaceXMLA { get; set; } = "";
/// <summary>
/// The name of the Power BI dataset.
/// </summary>
public string DatasetName { get; set; } = "";
/// <summary>
/// The path to the TMDL folder.
/// </summary>
public string TmdlFolderPath { get; set; } = "";
/// <summary>
/// The action to perform (pull or deploy).
/// </summary>
public string Action { get; set; } = "";
/// <summary>
/// Application ID of the Azure AD app.
/// </summary>
public string AppId { get; set; } = "";
/// <summary>
/// Password of the Azure AD app.
/// </summary>
public string AppSecret { get; set; } = "";
/// <summary>
/// Tenant ID of the Azure AD app.
/// </summary>
public string TenantId { get; set; } = "";
/// <summary>
/// Externally aquired Access Token for Azure AD app.
/// </summary>
public string AccessToken { get; set; } = "";
/// <summary>
/// Verbose output
/// </summary>
public bool Verbose { get; set; } = true;
/// <summary>
/// Settings file name
/// </summary>
public string SettingsFilePath { get; set; } = "";
public Settings GetSafe()
{
return new Settings
{
WorkspaceXMLA = WorkspaceXMLA,
DatasetName = DatasetName,
TmdlFolderPath = TmdlFolderPath,
Action = Action,
AppId = AppId,
AppSecret = string.IsNullOrEmpty(AppSecret) ? "" : "********",
TenantId = TenantId,
AccessToken = string.IsNullOrEmpty(AccessToken) ? "" : "********",
Verbose = Verbose,
SettingsFilePath = SettingsFilePath
};
}
}
}
}