-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: add credential settings for agent in enrolling agent (#116)
* chore: add collection mode to cluster editing * chore: add credential settings for agent when enroll agent * chore: update release notes --------- Co-authored-by: yaojiping <[email protected]>
- Loading branch information
Showing
21 changed files
with
669 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
117 changes: 117 additions & 0 deletions
117
web/src/pages/Agent/Instance/components/AgentCredential.jsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
import { Alert, Button, Form, message } from "antd"; | ||
import { useState } from "react"; | ||
import { formatMessage } from "umi/locale"; | ||
|
||
import request from "@/utils/request"; | ||
import AgentCredentialForm, { MANUAL_VALUE } from "./AgentCredentialForm"; | ||
import { ESPrefix } from "@/services/common"; | ||
|
||
const formItemLayout = { | ||
labelCol: { | ||
xs: { span: 24 }, | ||
sm: { span: 5 }, | ||
}, | ||
wrapperCol: { | ||
xs: { span: 24 }, | ||
sm: { span: 18 }, | ||
}, | ||
}; | ||
|
||
export default Form.create()((props) => { | ||
const { form, record, loading, tryConnect, onAgentCredentialSave } = props; | ||
|
||
const [isManual, setIsManual] = useState(false); | ||
const [saveLoading, setSaveLoading] = useState(false); | ||
|
||
const needAuth = !!(record.credential_id || record.basic_auth?.username); | ||
|
||
const onConfirm = async () => { | ||
form.validateFields(async (errors, values) => { | ||
if (errors) return; | ||
setSaveLoading(true); | ||
const { credential_id, basic_auth, metric_collection_mode } = record; | ||
const res = await request(`${ESPrefix}/${record.id}`, { | ||
method: "PUT", | ||
body: { | ||
credential_id, | ||
basic_auth, | ||
metric_collection_mode, | ||
agent_credential_id: | ||
values.agent_credential_id !== MANUAL_VALUE | ||
? values.agent_credential_id | ||
: undefined, | ||
agent_basic_auth: { | ||
username: values.agent_username, | ||
password: values.agent_password, | ||
}, | ||
}, | ||
}); | ||
if (res?.result === "updated") { | ||
message.success( | ||
formatMessage({ | ||
id: "app.message.update.success", | ||
}) | ||
); | ||
const res = await request(`/elasticsearch/${record.id}`); | ||
if (res?.found) { | ||
onAgentCredentialSave(res._source); | ||
if (res._source?.agent_credential_id) { | ||
setIsManual(false); | ||
} | ||
form.setFieldsValue({ | ||
agent_credential_id: res._source?.agent_credential_id | ||
? res._source?.agent_credential_id | ||
: res._source?.agent_basic_auth?.username | ||
? MANUAL_VALUE | ||
: undefined, | ||
agent_username: res._source.agent_basic_auth?.username, | ||
agent_password: res._source.agent_basic_auth?.password, | ||
}); | ||
} | ||
} else { | ||
message.error( | ||
formatMessage({ | ||
id: "app.message.update.failed", | ||
}) | ||
); | ||
} | ||
setSaveLoading(false); | ||
}); | ||
}; | ||
|
||
if (!needAuth) { | ||
return ( | ||
<Alert | ||
message={formatMessage({ id: "agent.credential.tip" })} | ||
type="success" | ||
/> | ||
); | ||
} | ||
|
||
return ( | ||
<Form {...formItemLayout} colon={false}> | ||
<AgentCredentialForm | ||
btnLoading={loading} | ||
needAuth={needAuth} | ||
form={form} | ||
initialValue={{ | ||
...record, | ||
username: record.agent_basic_auth?.username, | ||
password: record.agent_basic_auth?.password, | ||
}} | ||
isManual={isManual} | ||
setIsManual={setIsManual} | ||
isEdit={true} | ||
tryConnect={tryConnect} | ||
credentialRequired={false} | ||
/> | ||
<Form.Item label=" " colon={false}> | ||
<div style={{ textAlign: "right" }}> | ||
<Button loading={loading} type="primary" onClick={() => onConfirm()}> | ||
{formatMessage({ id: "cluster.regist.step.confirm.title" })} | ||
</Button> | ||
</div> | ||
</Form.Item> | ||
</Form> | ||
); | ||
}); |
143 changes: 143 additions & 0 deletions
143
web/src/pages/Agent/Instance/components/AgentCredentialForm.jsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
import React, { useEffect, useMemo, useState } from "react"; | ||
import { Button, Divider, Form, Input, Select, Row, Col } from "antd"; | ||
import { formatMessage } from "umi/locale"; | ||
|
||
import useFetch from "@/lib/hooks/use_fetch"; | ||
import { formatESSearchResult } from "@/lib/elasticsearch/util"; | ||
|
||
export const MANUAL_VALUE = "manual"; | ||
|
||
export default (props) => { | ||
const { | ||
btnLoading = false, | ||
needAuth, | ||
form: { getFieldDecorator }, | ||
initialValue, | ||
isEdit, | ||
tryConnect, | ||
credentialRequired = false, | ||
isManual, | ||
setIsManual, | ||
} = props; | ||
|
||
const { loading, error, value, run } = useFetch( | ||
"/credential/_search", | ||
{ | ||
queryParams: { | ||
from: 0, | ||
size: 1000, | ||
}, | ||
}, | ||
[] | ||
); | ||
|
||
const onCredentialChange = (value) => { | ||
if (value === "manual") { | ||
setIsManual(true); | ||
} else { | ||
setIsManual(false); | ||
} | ||
}; | ||
|
||
const { data, total } = useMemo(() => { | ||
return formatESSearchResult(value); | ||
}, [value]); | ||
|
||
if (!needAuth) { | ||
return null; | ||
} | ||
|
||
return ( | ||
<> | ||
<Form.Item | ||
label={formatMessage({ | ||
id: "cluster.regist.step.connect.label.agent_credential", | ||
})} | ||
> | ||
{getFieldDecorator("agent_credential_id", { | ||
initialValue: initialValue?.agent_credential_id | ||
? initialValue?.agent_credential_id | ||
: initialValue?.username | ||
? MANUAL_VALUE | ||
: undefined, | ||
rules: [ | ||
{ | ||
required: credentialRequired, | ||
message: formatMessage({ | ||
id: "cluster.regist.form.verify.required.agent_credential", | ||
}), | ||
}, | ||
], | ||
})( | ||
<Select loading={loading} onChange={onCredentialChange} allowClear> | ||
<Select.Option value={MANUAL_VALUE}> | ||
{formatMessage({ | ||
id: "cluster.regist.step.connect.credential.manual", | ||
})} | ||
</Select.Option> | ||
{data.map((item) => ( | ||
<Select.Option value={item.id}>{item.name}</Select.Option> | ||
))} | ||
</Select> | ||
)} | ||
</Form.Item> | ||
{isManual && ( | ||
<> | ||
<Form.Item | ||
label={formatMessage({ | ||
id: "cluster.regist.step.connect.label.username", | ||
})} | ||
> | ||
{getFieldDecorator("agent_username", { | ||
initialValue: initialValue?.username || "", | ||
rules: [ | ||
{ | ||
required: credentialRequired, | ||
message: formatMessage({ | ||
id: "cluster.regist.form.verify.required.auth_username", | ||
}), | ||
}, | ||
], | ||
})(<Input autoComplete="off" placeholder={formatMessage({id: "agent.form.placeholder.auth.username"})} />)} | ||
</Form.Item> | ||
<Form.Item | ||
label={formatMessage({ | ||
id: "cluster.regist.step.connect.label.password", | ||
})} | ||
hasFeedback | ||
> | ||
{getFieldDecorator("agent_password", { | ||
initialValue: initialValue?.password || "", | ||
rules: [ | ||
{ | ||
required: credentialRequired, | ||
message: formatMessage({ | ||
id: "cluster.regist.form.verify.required.auth_password", | ||
}), | ||
}, | ||
], | ||
})( | ||
<Input.Password | ||
autoComplete="off" | ||
placeholder={formatMessage({ | ||
id: "cluster.regist.form.verify.required.auth_password", | ||
})} | ||
/> | ||
)} | ||
</Form.Item> | ||
{isEdit && ( | ||
<> | ||
<Form.Item label={" "}> | ||
<div style={{ lineHeight: "20px" }}> | ||
{formatMessage({ | ||
id: "cluster.regist.form.credential.manual.desc", | ||
})} | ||
</div> | ||
</Form.Item> | ||
</> | ||
)} | ||
</> | ||
)} | ||
</> | ||
); | ||
}; |
Oops, something went wrong.