config_ctrl.ts 4.4 KB

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