config_ctrl.ts 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. import AzureLogAnalyticsDatasource from './azure_log_analytics/azure_log_analytics_datasource';
  2. import config from 'app/core/config';
  3. import { isVersionGtOrEq } from 'app/core/utils/version';
  4. import AzureMonitorDatasource from './azure_monitor/azure_monitor_datasource';
  5. interface AzureCloud {
  6. key: string;
  7. url: string;
  8. loginUrl: string;
  9. }
  10. export class AzureMonitorConfigCtrl {
  11. static templateUrl = 'public/app/plugins/datasource/grafana-azure-monitor-datasource/partials/config.html';
  12. current: any;
  13. azureLogAnalyticsDatasource: any;
  14. azureMonitorDatasource: any;
  15. workspaces: any[];
  16. subscriptions: Array<{ text: string; value: string }>;
  17. subscriptionsForLogAnalytics: Array<{ text: string; value: string }>;
  18. hasRequiredGrafanaVersion: boolean;
  19. azureClouds: AzureCloud[];
  20. token: string;
  21. /** @ngInject */
  22. constructor(private backendSrv, private $q, private templateSrv) {
  23. this.hasRequiredGrafanaVersion = this.hasMinVersion();
  24. this.current.jsonData.cloudName = this.current.jsonData.cloudName || 'azuremonitor';
  25. this.current.jsonData.azureLogAnalyticsSameAs = this.current.jsonData.azureLogAnalyticsSameAs || false;
  26. this.current.secureJsonData = this.current.secureJsonData || {};
  27. this.current.secureJsonFields = this.current.secureJsonFields || {};
  28. this.subscriptions = [];
  29. this.subscriptionsForLogAnalytics = [];
  30. this.azureClouds = [
  31. {
  32. key: 'azuremonitor',
  33. url: 'https://management.azure.com/',
  34. loginUrl: 'https://login.microsoftonline.com/',
  35. },
  36. {
  37. key: 'govazuremonitor',
  38. url: 'https://management.usgovcloudapi.net/',
  39. loginUrl: 'https://login.microsoftonline.us/',
  40. },
  41. {
  42. key: 'germanyazuremonitor',
  43. url: 'https://management.microsoftazure.de',
  44. loginUrl: 'https://management.microsoftazure.de/',
  45. },
  46. {
  47. key: 'chinaazuremonitor',
  48. url: 'https://management.chinacloudapi.cn',
  49. loginUrl: 'https://login.chinacloudapi.cn',
  50. },
  51. ];
  52. if (this.current.id) {
  53. this.current.url = '/api/datasources/proxy/' + this.current.id;
  54. this.init();
  55. }
  56. }
  57. async init() {
  58. this.azureMonitorDatasource = new AzureMonitorDatasource(this.current, this.backendSrv, this.templateSrv);
  59. await this.getSubscriptions();
  60. await this.getSubscriptionsForLogsAnalytics();
  61. this.azureLogAnalyticsDatasource = new AzureLogAnalyticsDatasource(
  62. this.current,
  63. this.backendSrv,
  64. this.templateSrv,
  65. this.$q
  66. );
  67. await this.getWorkspaces();
  68. }
  69. hasMinVersion(): boolean {
  70. return isVersionGtOrEq(config.buildInfo.version, '5.2');
  71. }
  72. showMinVersionWarning() {
  73. return !this.hasRequiredGrafanaVersion && this.current.secureJsonFields.logAnalyticsClientSecret;
  74. }
  75. async getWorkspaces() {
  76. const sameAs = this.current.jsonData.azureLogAnalyticsSameAs && this.subscriptions.length > 0;
  77. if (!sameAs && this.subscriptionsForLogAnalytics.length === 0) {
  78. return;
  79. }
  80. this.workspaces = await this.azureLogAnalyticsDatasource.getWorkspaces();
  81. if (this.workspaces.length > 0) {
  82. this.current.jsonData.logAnalyticsDefaultWorkspace =
  83. this.current.jsonData.logAnalyticsDefaultWorkspace || this.workspaces[0].value;
  84. }
  85. }
  86. async getSubscriptions() {
  87. if (!this.current.secureJsonFields.clientSecret && !this.current.secureJsonData.clientSecret) {
  88. return;
  89. }
  90. this.subscriptions = (await this.azureMonitorDatasource.getSubscriptions()) || [];
  91. if (this.subscriptions && this.subscriptions.length > 0) {
  92. this.current.jsonData.subscriptionId = this.current.jsonData.subscriptionId || this.subscriptions[0].value;
  93. }
  94. }
  95. async getSubscriptionsForLogsAnalytics() {
  96. if (
  97. !this.current.secureJsonFields.logAnalyticsClientSecret &&
  98. !this.current.secureJsonData.logAnalyticsClientSecret
  99. ) {
  100. return;
  101. }
  102. this.subscriptionsForLogAnalytics =
  103. (await this.azureMonitorDatasource.getSubscriptions('workspacesloganalytics')) || [];
  104. if (this.subscriptionsForLogAnalytics && this.subscriptionsForLogAnalytics.length > 0) {
  105. this.current.jsonData.logAnalyticsSubscriptionId =
  106. this.current.jsonData.logAnalyticsSubscriptionId || this.subscriptionsForLogAnalytics[0].value;
  107. }
  108. }
  109. }