AzureCredentialsForm.tsx 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. import React, { ChangeEvent, PureComponent } from 'react';
  2. import { SelectableValue } from '@grafana/data';
  3. import { Input, FormLabel, Select, Button } from '@grafana/ui';
  4. export interface Props {
  5. selectedAzureCloud?: string;
  6. selectedSubscription?: string;
  7. azureCloudOptions?: SelectableValue[];
  8. tenantId: string;
  9. clientId: string;
  10. clientSecret: string;
  11. clientSecretConfigured: boolean;
  12. subscriptionOptions?: SelectableValue[];
  13. onAzureCloudChange?: (value: SelectableValue<string>) => void;
  14. onSubscriptionSelectChange?: (value: SelectableValue<string>) => void;
  15. onTenantIdChange: (tenantId: string) => void;
  16. onClientIdChange: (clientId: string) => void;
  17. onClientSecretChange: (clientSecret: string) => void;
  18. onResetClientSecret: () => void;
  19. onLoadSubscriptions?: () => void;
  20. }
  21. export interface State {
  22. selectedAzureCloud?: string;
  23. selectedSubscription: string;
  24. tenantId: string;
  25. clientId: string;
  26. clientSecret: string;
  27. clientSecretConfigured: boolean;
  28. }
  29. export class AzureCredentialsForm extends PureComponent<Props, State> {
  30. constructor(props: Props) {
  31. super(props);
  32. const {
  33. selectedAzureCloud,
  34. selectedSubscription,
  35. tenantId,
  36. clientId,
  37. clientSecret,
  38. clientSecretConfigured,
  39. } = this.props;
  40. this.state = {
  41. selectedAzureCloud,
  42. selectedSubscription,
  43. tenantId,
  44. clientId,
  45. clientSecret,
  46. clientSecretConfigured,
  47. };
  48. }
  49. static getDerivedStateFromProps(nextProps: Props, prevState: Props) {
  50. const { selectedAzureCloud, tenantId, clientId, clientSecret, clientSecretConfigured } = nextProps;
  51. return {
  52. selectedAzureCloud,
  53. tenantId,
  54. clientId,
  55. clientSecret,
  56. clientSecretConfigured,
  57. };
  58. }
  59. render() {
  60. const {
  61. azureCloudOptions,
  62. subscriptionOptions,
  63. onAzureCloudChange,
  64. onSubscriptionSelectChange,
  65. onTenantIdChange,
  66. onClientIdChange,
  67. onClientSecretChange,
  68. onResetClientSecret,
  69. onLoadSubscriptions,
  70. } = this.props;
  71. const {
  72. selectedAzureCloud,
  73. selectedSubscription,
  74. tenantId,
  75. clientId,
  76. clientSecret,
  77. clientSecretConfigured,
  78. } = this.state;
  79. const hasRequiredFields = tenantId && clientId && (clientSecret || clientSecretConfigured);
  80. const hasSubscriptions = onLoadSubscriptions && subscriptionOptions;
  81. return (
  82. <>
  83. <div className="gf-form-group">
  84. {azureCloudOptions && (
  85. <div className="gf-form-inline">
  86. <div className="gf-form">
  87. <FormLabel className="width-12" tooltip="Choose an Azure Cloud.">
  88. Azure Cloud
  89. </FormLabel>
  90. <Select
  91. className="width-15"
  92. value={azureCloudOptions.find(azureCloud => azureCloud.value === selectedAzureCloud)}
  93. options={azureCloudOptions}
  94. defaultValue={selectedAzureCloud}
  95. onChange={onAzureCloudChange}
  96. />
  97. </div>
  98. </div>
  99. )}
  100. <div className="gf-form-inline">
  101. <div className="gf-form">
  102. <FormLabel className="width-12">Directory (tenant) ID</FormLabel>
  103. <div className="width-15">
  104. <Input
  105. className="width-30"
  106. placeholder="XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX"
  107. value={tenantId}
  108. onChange={(event: ChangeEvent<HTMLInputElement>) => onTenantIdChange(event.target.value)}
  109. />
  110. </div>
  111. </div>
  112. </div>
  113. <div className="gf-form-inline">
  114. <div className="gf-form">
  115. <FormLabel className="width-12">Application (client) ID</FormLabel>
  116. <div className="width-15">
  117. <Input
  118. className="width-30"
  119. placeholder="XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX"
  120. value={clientId}
  121. onChange={(event: ChangeEvent<HTMLInputElement>) => onClientIdChange(event.target.value)}
  122. />
  123. </div>
  124. </div>
  125. </div>
  126. {clientSecretConfigured ? (
  127. <div className="gf-form-inline">
  128. <div className="gf-form">
  129. <FormLabel className="width-12">Client Secret</FormLabel>
  130. <Input className="width-25" placeholder="configured" disabled={true} />
  131. </div>
  132. <div className="gf-form">
  133. <div className="max-width-30 gf-form-inline">
  134. <Button variant="secondary" type="button" onClick={onResetClientSecret}>
  135. reset
  136. </Button>
  137. </div>
  138. </div>
  139. </div>
  140. ) : (
  141. <div className="gf-form-inline">
  142. <div className="gf-form">
  143. <FormLabel className="width-12">Client Secret</FormLabel>
  144. <div className="width-15">
  145. <Input
  146. className="width-30"
  147. placeholder="XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX"
  148. value={clientSecret}
  149. onChange={(event: ChangeEvent<HTMLInputElement>) => onClientSecretChange(event.target.value)}
  150. />
  151. </div>
  152. </div>
  153. </div>
  154. )}
  155. {hasSubscriptions && (
  156. <>
  157. <div className="gf-form-inline">
  158. <div className="gf-form">
  159. <FormLabel className="width-12">Default Subscription</FormLabel>
  160. <div className="width-25">
  161. <Select
  162. value={subscriptionOptions.find(subscription => subscription.value === selectedSubscription)}
  163. options={subscriptionOptions}
  164. defaultValue={selectedSubscription}
  165. onChange={onSubscriptionSelectChange}
  166. />
  167. </div>
  168. </div>
  169. </div>
  170. <div className="gf-form-inline">
  171. <div className="gf-form">
  172. <div className="max-width-30 gf-form-inline">
  173. <Button
  174. variant="secondary"
  175. size="sm"
  176. type="button"
  177. onClick={onLoadSubscriptions}
  178. disabled={!hasRequiredFields}
  179. >
  180. Load Subscriptions
  181. </Button>
  182. </div>
  183. </div>
  184. </div>
  185. </>
  186. )}
  187. </div>
  188. </>
  189. );
  190. }
  191. }
  192. export default AzureCredentialsForm;