MonitorConfig.tsx 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. import React, { PureComponent } from 'react';
  2. import { SelectableValue } from '@grafana/data';
  3. import { AzureCredentialsForm } from './AzureCredentialsForm';
  4. export interface Props {
  5. datasourceConfig: any;
  6. subscriptions: SelectableValue[];
  7. onDatasourceUpdate: (config: any) => void;
  8. onLoadSubscriptions: () => void;
  9. }
  10. export interface State {
  11. config: any;
  12. azureClouds: SelectableValue[];
  13. subscriptions: SelectableValue[];
  14. }
  15. export class MonitorConfig extends PureComponent<Props, State> {
  16. constructor(props: Props) {
  17. super(props);
  18. const { datasourceConfig } = this.props;
  19. this.state = {
  20. config: datasourceConfig,
  21. azureClouds: [
  22. { value: 'azuremonitor', label: 'Azure' },
  23. { value: 'govazuremonitor', label: 'Azure US Government' },
  24. { value: 'germanyazuremonitor', label: 'Azure Germany' },
  25. { value: 'chinaazuremonitor', label: 'Azure China' },
  26. ],
  27. subscriptions: [],
  28. };
  29. }
  30. static getDerivedStateFromProps(props: Props, state: State) {
  31. return {
  32. ...state,
  33. config: props.datasourceConfig,
  34. subscriptions: props.subscriptions,
  35. };
  36. }
  37. onAzureCloudSelect = (cloudName: SelectableValue<string>) => {
  38. this.props.onDatasourceUpdate({
  39. ...this.state.config,
  40. jsonData: {
  41. ...this.state.config.jsonData,
  42. cloudName,
  43. },
  44. });
  45. };
  46. onTenantIdChange = (tenantId: string) => {
  47. this.props.onDatasourceUpdate({
  48. ...this.state.config,
  49. editorJsonData: {
  50. ...this.state.config.editorJsonData,
  51. tenantId,
  52. },
  53. });
  54. };
  55. onClientIdChange = (clientId: string) => {
  56. this.props.onDatasourceUpdate({
  57. ...this.state.config,
  58. editorJsonData: {
  59. ...this.state.config.editorJsonData,
  60. clientId,
  61. },
  62. });
  63. };
  64. onClientSecretChange = (clientSecret: string) => {
  65. this.props.onDatasourceUpdate({
  66. ...this.state.config,
  67. editorSecureJsonData: {
  68. ...this.state.config.editorSecureJsonData,
  69. clientSecret,
  70. },
  71. });
  72. };
  73. onResetClientSecret = () => {
  74. this.props.onDatasourceUpdate({
  75. ...this.state.config,
  76. version: this.state.config.version + 1,
  77. secureJsonFields: {
  78. ...this.state.config.secureJsonFields,
  79. clientSecret: false,
  80. },
  81. });
  82. };
  83. onSubscriptionSelect = (subscription: SelectableValue<string>) => {
  84. this.props.onDatasourceUpdate({
  85. ...this.state.config,
  86. editorJsonData: {
  87. ...this.state.config.editorJsonData,
  88. subscriptionId: subscription.value,
  89. },
  90. });
  91. };
  92. render() {
  93. const { azureClouds, config, subscriptions } = this.state;
  94. return (
  95. <>
  96. <h3 className="page-heading">Azure Monitor Details</h3>
  97. <AzureCredentialsForm
  98. selectedAzureCloud={config.jsonData.cloudName}
  99. azureCloudOptions={azureClouds}
  100. subscriptionOptions={subscriptions}
  101. selectedSubscription={config.editorJsonData.subscriptionId}
  102. tenantId={config.editorJsonData.tenantId}
  103. clientId={config.editorJsonData.clientId}
  104. clientSecret={config.editorSecureJsonData.clientSecret}
  105. clientSecretConfigured={config.secureJsonFields.clientSecret}
  106. onAzureCloudChange={this.onAzureCloudSelect}
  107. onSubscriptionSelectChange={this.onSubscriptionSelect}
  108. onTenantIdChange={this.onTenantIdChange}
  109. onClientIdChange={this.onClientIdChange}
  110. onClientSecretChange={this.onClientSecretChange}
  111. onResetClientSecret={this.onResetClientSecret}
  112. onLoadSubscriptions={this.props.onLoadSubscriptions}
  113. />
  114. </>
  115. );
  116. }
  117. }
  118. export default MonitorConfig;