Commit c032399e by Shavonn Brown Committed by GitHub

remove editor keys and null coalescing (#20115)

* remove editor keys and null coalescing

* remove commented, update snap
parent 64752f38
......@@ -24,7 +24,7 @@ export class ConfigEditor extends PureComponent<Props, State> {
const { options } = this.props;
this.state = {
config: ConfigEditor.keyFill(options),
config: ConfigEditor.defaults(options),
subscriptions: [],
logAnalyticsSubscriptions: [],
logAnalyticsWorkspaces: [],
......@@ -44,36 +44,19 @@ export class ConfigEditor extends PureComponent<Props, State> {
static getDerivedStateFromProps(props: Props, state: State) {
return {
...state,
config: ConfigEditor.keyFill(props.options),
config: ConfigEditor.defaults(props.options),
};
}
static keyFill = (options: any) => {
static defaults = (options: any) => {
options.jsonData.cloudName = options.jsonData.cloudName || 'azuremonitor';
if (!options.jsonData.hasOwnProperty('azureLogAnalyticsSameAs')) {
options.jsonData.azureLogAnalyticsSameAs = true;
}
if (!options.hasOwnProperty('editorSecureJsonData')) {
options.editorSecureJsonData = {
clientSecret: '',
logAnalyticsClientSecret: '',
appInsightsApiKey: '',
};
}
if (!options.hasOwnProperty('editorJsonData')) {
options.editorJsonData = {
clientId: options.jsonData.clientId || '',
tenantId: options.jsonData.tenantId || '',
subscriptionId: options.jsonData.subscriptionId || '',
logAnalyticsClientId: options.jsonData.logAnalyticsClientId || '',
logAnalyticsDefaultWorkspace: options.jsonData.logAnalyticsDefaultWorkspace || '',
logAnalyticsTenantId: options.jsonData.logAnalyticsTenantId || '',
logAnalyticsSubscriptionId: options.jsonData.logAnalyticsSubscriptionId || '',
appInsightsAppId: options.jsonData.appInsightsAppId || '',
};
if (!options.hasOwnProperty('secureJsonData')) {
options.secureJsonData = {};
}
if (!options.hasOwnProperty('secureJsonFields')) {
......@@ -111,39 +94,13 @@ export class ConfigEditor extends PureComponent<Props, State> {
}
}
for (const m in config.editorJsonData) {
if (!config.hasOwnProperty('jsonData')) {
config.jsonData = {};
}
if (config.editorJsonData[m].length === 0) {
if (config.hasOwnProperty('jsonData') && config.jsonData.hasOwnProperty(m)) {
delete config.jsonData[m];
}
} else {
config.jsonData[m] = config.editorJsonData[m];
}
}
for (const l in config.editorSecureJsonData) {
if (!config.hasOwnProperty('secureJsonData')) {
config.secureJsonData = {};
}
if (config.editorSecureJsonData[l].length === 0) {
if (config.hasOwnProperty('secureJsonData') && config.secureJsonData.hasOwnProperty(l)) {
delete config.secureJsonData[l];
}
} else {
config.secureJsonData[l] = config.editorSecureJsonData[l];
}
}
this.props.onOptionsChange({
...config,
});
};
hasNecessaryCredentials = () => {
if (!this.state.config.secureJsonFields.clientSecret && !this.state.config.editorSecureJsonData.clientSecret) {
if (!this.state.config.secureJsonFields.clientSecret && !this.state.config.secureJsonData.clientSecret) {
return false;
}
......@@ -157,7 +114,7 @@ export class ConfigEditor extends PureComponent<Props, State> {
logAnalyticsHasNecessaryCredentials = () => {
if (
!this.state.config.secureJsonFields.logAnalyticsClientSecret &&
!this.state.config.editorSecureJsonData.logAnalyticsClientSecret
!this.state.config.secureJsonData.logAnalyticsClientSecret
) {
return false;
}
......@@ -206,11 +163,10 @@ export class ConfigEditor extends PureComponent<Props, State> {
if (subscriptions && subscriptions.length > 0) {
this.setState({ subscriptions });
this.state.config.editorJsonData.subscriptionId =
this.state.config.editorJsonData.subscriptionId || subscriptions[0].value;
this.state.config.jsonData.subscriptionId = this.state.config.jsonData.subscriptionId || subscriptions[0].value;
}
if (this.state.config.editorJsonData.subscriptionId && this.state.config.jsonData.azureLogAnalyticsSameAs) {
if (this.state.config.jsonData.subscriptionId && this.state.config.jsonData.azureLogAnalyticsSameAs) {
await this.getWorkspaces();
}
};
......@@ -233,19 +189,18 @@ export class ConfigEditor extends PureComponent<Props, State> {
if (logAnalyticsSubscriptions && logAnalyticsSubscriptions.length > 0) {
this.setState({ logAnalyticsSubscriptions });
this.state.config.editorJsonData.logAnalyticsSubscriptionId =
this.state.config.editorJsonData.logAnalyticsSubscriptionId || logAnalyticsSubscriptions[0].value;
this.state.config.jsonData.logAnalyticsSubscriptionId =
this.state.config.jsonData.logAnalyticsSubscriptionId || logAnalyticsSubscriptions[0].value;
}
if (this.state.config.editorJsonData.logAnalyticsSubscriptionId) {
if (this.state.config.jsonData.logAnalyticsSubscriptionId) {
await this.getWorkspaces();
}
};
getWorkspaces = async () => {
const sameAs =
this.state.config.jsonData.azureLogAnalyticsSameAs && this.state.config.editorJsonData.subscriptionId;
if (!sameAs && !this.state.config.editorJsonData.logAnalyticsSubscriptionId) {
const sameAs = this.state.config.jsonData.azureLogAnalyticsSameAs && this.state.config.jsonData.subscriptionId;
if (!sameAs && !this.state.config.jsonData.logAnalyticsSubscriptionId) {
return;
}
......@@ -256,9 +211,7 @@ export class ConfigEditor extends PureComponent<Props, State> {
);
let logAnalyticsWorkspaces = await azureLogAnalyticsDatasource.getWorkspaces(
sameAs
? this.state.config.editorJsonData.subscriptionId
: this.state.config.editorJsonData.logAnalyticsSubscriptionId
sameAs ? this.state.config.jsonData.subscriptionId : this.state.config.jsonData.logAnalyticsSubscriptionId
);
logAnalyticsWorkspaces = logAnalyticsWorkspaces.map((workspace: any) => {
return {
......@@ -270,8 +223,8 @@ export class ConfigEditor extends PureComponent<Props, State> {
if (logAnalyticsWorkspaces.length > 0) {
this.setState({ logAnalyticsWorkspaces });
this.state.config.editorJsonData.logAnalyticsDefaultWorkspace =
this.state.config.editorJsonData.logAnalyticsDefaultWorkspace || logAnalyticsWorkspaces[0].value;
this.state.config.jsonData.logAnalyticsDefaultWorkspace =
this.state.config.jsonData.logAnalyticsDefaultWorkspace || logAnalyticsWorkspaces[0].value;
}
};
......
......@@ -10,21 +10,6 @@ exports[`Render should render component 1`] = `
"basicAuthPassword": "",
"basicAuthUser": "",
"database": "",
"editorJsonData": Object {
"appInsightsAppId": "",
"clientId": "",
"logAnalyticsClientId": "",
"logAnalyticsDefaultWorkspace": "",
"logAnalyticsSubscriptionId": "",
"logAnalyticsTenantId": "",
"subscriptionId": "",
"tenantId": "",
},
"editorSecureJsonData": Object {
"appInsightsApiKey": "",
"clientSecret": "",
"logAnalyticsClientSecret": "",
},
"id": 21,
"isDefault": false,
"jsonData": Object {
......@@ -57,21 +42,6 @@ exports[`Render should render component 1`] = `
"basicAuthPassword": "",
"basicAuthUser": "",
"database": "",
"editorJsonData": Object {
"appInsightsAppId": "",
"clientId": "",
"logAnalyticsClientId": "",
"logAnalyticsDefaultWorkspace": "",
"logAnalyticsSubscriptionId": "",
"logAnalyticsTenantId": "",
"subscriptionId": "",
"tenantId": "",
},
"editorSecureJsonData": Object {
"appInsightsApiKey": "",
"clientSecret": "",
"logAnalyticsClientSecret": "",
},
"id": 21,
"isDefault": false,
"jsonData": Object {
......@@ -106,21 +76,6 @@ exports[`Render should render component 1`] = `
"basicAuthPassword": "",
"basicAuthUser": "",
"database": "",
"editorJsonData": Object {
"appInsightsAppId": "",
"clientId": "",
"logAnalyticsClientId": "",
"logAnalyticsDefaultWorkspace": "",
"logAnalyticsSubscriptionId": "",
"logAnalyticsTenantId": "",
"subscriptionId": "",
"tenantId": "",
},
"editorSecureJsonData": Object {
"appInsightsApiKey": "",
"clientSecret": "",
"logAnalyticsClientSecret": "",
},
"id": 21,
"isDefault": false,
"jsonData": Object {
......
......@@ -20,18 +20,16 @@ const setup = (propOverrides?: object) => {
basicAuthPassword: '',
withCredentials: false,
isDefault: false,
jsonData: {
azureLogAnalyticsSameAs: false,
},
secureJsonFields: {
logAnalyticsClientSecret: false,
},
editorJsonData: {
jsonData: {
azureLogAnalyticsSameAs: false,
logAnalyticsDefaultWorkspace: '',
logAnalyticsClientSecret: '',
logAnalyticsTenantId: '',
},
editorSecureJsonData: {
secureJsonData: {
logAnalyticsClientSecret: '',
},
version: 1,
......@@ -67,7 +65,7 @@ describe('Render', () => {
it('should enable azure log analytics load workspaces button', () => {
const wrapper = setup({
editorJsonData: {
jsonData: {
logAnalyticsDefaultWorkspace: '',
logAnalyticsTenantId: 'e7f3f661-a933-4b3f-8176-51c4f982ec48',
logAnalyticsClientId: '44693801-6ee6-49de-9b2d-9106972f9572',
......
......@@ -43,8 +43,8 @@ export class AnalyticsConfig extends PureComponent<Props, State> {
onLogAnalyticsTenantIdChange = (logAnalyticsTenantId: string) => {
this.props.onDatasourceUpdate({
...this.state.config,
editorJsonData: {
...this.state.config.editorJsonData,
jsonData: {
...this.state.config.jsonData,
logAnalyticsTenantId,
},
});
......@@ -53,8 +53,8 @@ export class AnalyticsConfig extends PureComponent<Props, State> {
onLogAnalyticsClientIdChange = (logAnalyticsClientId: string) => {
this.props.onDatasourceUpdate({
...this.state.config,
editorJsonData: {
...this.state.config.editorJsonData,
jsonData: {
...this.state.config.jsonData,
logAnalyticsClientId,
},
});
......@@ -63,8 +63,8 @@ export class AnalyticsConfig extends PureComponent<Props, State> {
onLogAnalyticsClientSecretChange = (logAnalyticsClientSecret: string) => {
this.props.onDatasourceUpdate({
...this.state.config,
editorSecureJsonData: {
...this.state.config.editorSecureJsonData,
secureJsonData: {
...this.state.config.secureJsonData,
logAnalyticsClientSecret,
},
});
......@@ -81,8 +81,8 @@ export class AnalyticsConfig extends PureComponent<Props, State> {
onLogAnalyticsSubscriptionSelect = (logAnalyticsSubscription: SelectableValue<string>) => {
this.props.onDatasourceUpdate({
...this.state.config,
editorJsonData: {
...this.state.config.editorJsonData,
jsonData: {
...this.state.config.jsonData,
logAnalyticsSubscriptionId: logAnalyticsSubscription.value,
},
});
......@@ -91,8 +91,8 @@ export class AnalyticsConfig extends PureComponent<Props, State> {
onWorkspaceSelectChange = (logAnalyticsDefaultWorkspace: SelectableValue<string>) => {
this.props.onDatasourceUpdate({
...this.state.config,
editorJsonData: {
...this.state.config.editorJsonData,
jsonData: {
...this.state.config.jsonData,
logAnalyticsDefaultWorkspace: logAnalyticsDefaultWorkspace.value,
},
});
......@@ -110,29 +110,30 @@ export class AnalyticsConfig extends PureComponent<Props, State> {
hasWorkspaceRequiredFields = () => {
const {
config: { editorJsonData, editorSecureJsonData, jsonData, secureJsonFields },
config: { jsonData, secureJsonData, secureJsonFields },
} = this.state;
if (jsonData.azureLogAnalyticsSameAs) {
return (
editorJsonData.tenantId &&
editorJsonData.clientId &&
editorJsonData.subscriptionId &&
(editorSecureJsonData.clientSecret || secureJsonFields.clientSecret)
jsonData.tenantId &&
jsonData.clientId &&
jsonData.subscriptionId &&
(secureJsonData.clientSecret || secureJsonFields.clientSecret)
);
}
return (
editorJsonData.logAnalyticsTenantId.length &&
editorJsonData.logAnalyticsClientId.length &&
editorJsonData.logAnalyticsSubscriptionId &&
(secureJsonFields.logAnalyticsClientSecret || editorSecureJsonData.logAnalyticsClientSecret)
jsonData.logAnalyticsTenantId &&
jsonData.logAnalyticsTenantId.length &&
(jsonData.logAnalyticsClientId && jsonData.logAnalyticsClientId.length) &&
jsonData.logAnalyticsSubscriptionId &&
(secureJsonFields.logAnalyticsClientSecret || secureJsonData.logAnalyticsClientSecret)
);
};
render() {
const {
config: { editorJsonData, editorSecureJsonData, jsonData, secureJsonFields },
config: { jsonData, secureJsonData, secureJsonFields },
logAnalyticsSubscriptions,
logAnalyticsWorkspaces,
} = this.state;
......@@ -154,10 +155,10 @@ export class AnalyticsConfig extends PureComponent<Props, State> {
{!jsonData.azureLogAnalyticsSameAs && (
<AzureCredentialsForm
subscriptionOptions={logAnalyticsSubscriptions}
selectedSubscription={editorJsonData.logAnalyticsSubscriptionId}
tenantId={editorJsonData.logAnalyticsTenantId}
clientId={editorJsonData.logAnalyticsClientId}
clientSecret={editorSecureJsonData.logAnalyticsClientSecret}
selectedSubscription={jsonData.logAnalyticsSubscriptionId}
tenantId={jsonData.logAnalyticsTenantId}
clientId={jsonData.logAnalyticsClientId}
clientSecret={secureJsonData.logAnalyticsClientSecret}
clientSecretConfigured={secureJsonFields.logAnalyticsClientSecret}
onSubscriptionSelectChange={this.onLogAnalyticsSubscriptionSelect}
onTenantIdChange={this.onLogAnalyticsTenantIdChange}
......@@ -179,10 +180,10 @@ export class AnalyticsConfig extends PureComponent<Props, State> {
<div className="width-25">
<Select
value={logAnalyticsWorkspaces.find(
workspace => workspace.value === editorJsonData.logAnalyticsDefaultWorkspace
workspace => workspace.value === jsonData.logAnalyticsDefaultWorkspace
)}
options={logAnalyticsWorkspaces}
defaultValue={editorJsonData.logAnalyticsDefaultWorkspace}
defaultValue={jsonData.logAnalyticsDefaultWorkspace}
onChange={this.onWorkspaceSelectChange}
/>
</div>
......
......@@ -111,7 +111,7 @@ export class AzureCredentialsForm extends PureComponent<Props, State> {
<Input
className="width-30"
placeholder="XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX"
value={tenantId}
value={tenantId || ''}
onChange={(event: ChangeEvent<HTMLInputElement>) => onTenantIdChange(event.target.value)}
/>
</div>
......@@ -124,7 +124,7 @@ export class AzureCredentialsForm extends PureComponent<Props, State> {
<Input
className="width-30"
placeholder="XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX"
value={clientId}
value={clientId || ''}
onChange={(event: ChangeEvent<HTMLInputElement>) => onClientIdChange(event.target.value)}
/>
</div>
......@@ -152,7 +152,7 @@ export class AzureCredentialsForm extends PureComponent<Props, State> {
<Input
className="width-30"
placeholder="XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX"
value={clientSecret}
value={clientSecret || ''}
onChange={(event: ChangeEvent<HTMLInputElement>) => onClientSecretChange(event.target.value)}
/>
</div>
......
......@@ -20,14 +20,13 @@ const setup = (propOverrides?: object) => {
basicAuthPassword: '',
withCredentials: false,
isDefault: false,
jsonData: {},
secureJsonFields: {
appInsightsApiKey: false,
},
editorJsonData: {
jsonData: {
appInsightsAppId: 'cddcc020-2c94-460a-a3d0-df3147ffa792',
},
editorSecureJsonData: {
secureJsonData: {
appInsightsApiKey: 'e7f3f661-a933-4b3f-8176-51c4f982ec48',
},
version: 1,
......@@ -54,10 +53,10 @@ describe('Render', () => {
secureJsonFields: {
appInsightsApiKey: true,
},
editorJsonData: {
jsonData: {
appInsightsAppId: 'cddcc020-2c94-460a-a3d0-df3147ffa792',
},
editorSecureJsonData: {
secureJsonData: {
appInsightsApiKey: 'e7f3f661-a933-4b3f-8176-51c4f982ec48',
},
},
......@@ -71,10 +70,10 @@ describe('Render', () => {
secureJsonFields: {
appInsightsApiKey: false,
},
editorJsonData: {
jsonData: {
appInsightsAppId: 'cddcc020-2c94-460a-a3d0-df3147ffa792',
},
editorSecureJsonData: {
secureJsonData: {
appInsightsApiKey: 'e7f3f661-a933-4b3f-8176-51c4f982ec48',
},
},
......
......@@ -31,8 +31,8 @@ export class InsightsConfig extends PureComponent<Props, State> {
onAppInsightsAppIdChange = (appInsightsAppId: string) => {
this.props.onDatasourceUpdate({
...this.state.config,
editorJsonData: {
...this.state.config.editorJsonData,
jsonData: {
...this.state.config.jsonData,
appInsightsAppId,
},
});
......@@ -41,8 +41,8 @@ export class InsightsConfig extends PureComponent<Props, State> {
onAppInsightsApiKeyChange = (appInsightsApiKey: string) => {
this.props.onDatasourceUpdate({
...this.state.config,
editorSecureJsonData: {
...this.state.config.editorSecureJsonData,
secureJsonData: {
...this.state.config.secureJsonData,
appInsightsApiKey,
},
});
......@@ -87,7 +87,7 @@ export class InsightsConfig extends PureComponent<Props, State> {
<Input
className="width-30"
placeholder="XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX"
value={config.editorSecureJsonData.appInsightsApiKey}
value={config.secureJsonData.appInsightsApiKey || ''}
onChange={event => this.onAppInsightsApiKeyChange(event.target.value)}
/>
</div>
......@@ -100,7 +100,7 @@ export class InsightsConfig extends PureComponent<Props, State> {
<div className="width-15">
<Input
className="width-30"
value={config.editorJsonData.appInsightsAppId}
value={config.jsonData.appInsightsAppId || ''}
onChange={event => this.onAppInsightsAppIdChange(event.target.value)}
/>
</div>
......
......@@ -54,8 +54,8 @@ export class MonitorConfig extends PureComponent<Props, State> {
onTenantIdChange = (tenantId: string) => {
this.props.onDatasourceUpdate({
...this.state.config,
editorJsonData: {
...this.state.config.editorJsonData,
jsonData: {
...this.state.config.jsonData,
tenantId,
},
});
......@@ -64,8 +64,8 @@ export class MonitorConfig extends PureComponent<Props, State> {
onClientIdChange = (clientId: string) => {
this.props.onDatasourceUpdate({
...this.state.config,
editorJsonData: {
...this.state.config.editorJsonData,
jsonData: {
...this.state.config.jsonData,
clientId,
},
});
......@@ -74,8 +74,8 @@ export class MonitorConfig extends PureComponent<Props, State> {
onClientSecretChange = (clientSecret: string) => {
this.props.onDatasourceUpdate({
...this.state.config,
editorSecureJsonData: {
...this.state.config.editorSecureJsonData,
secureJsonData: {
...this.state.config.secureJsonData,
clientSecret,
},
});
......@@ -95,8 +95,8 @@ export class MonitorConfig extends PureComponent<Props, State> {
onSubscriptionSelect = (subscription: SelectableValue<string>) => {
this.props.onDatasourceUpdate({
...this.state.config,
editorJsonData: {
...this.state.config.editorJsonData,
jsonData: {
...this.state.config.jsonData,
subscriptionId: subscription.value,
},
});
......@@ -111,10 +111,10 @@ export class MonitorConfig extends PureComponent<Props, State> {
selectedAzureCloud={config.jsonData.cloudName}
azureCloudOptions={azureClouds}
subscriptionOptions={subscriptions}
selectedSubscription={config.editorJsonData.subscriptionId}
tenantId={config.editorJsonData.tenantId}
clientId={config.editorJsonData.clientId}
clientSecret={config.editorSecureJsonData.clientSecret}
selectedSubscription={config.jsonData.subscriptionId}
tenantId={config.jsonData.tenantId}
clientId={config.jsonData.clientId}
clientSecret={config.secureJsonData.clientSecret}
clientSecretConfigured={config.secureJsonFields.clientSecret}
onAzureCloudChange={this.onAzureCloudSelect}
onSubscriptionSelectChange={this.onSubscriptionSelect}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment