Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pull request update/241108 #457

Merged
merged 3 commits into from
Nov 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,13 @@ def _get(self):
return result

@staticmethod
def _get_aws_region_insecure_sg(cloud_adapter, region, sg_ids, insecure_ports):
def _format_protocol(protocol):
if not protocol or protocol.lower() in ['any', 'all']:
return '*'
return protocol

def _get_aws_region_insecure_sg(
self, cloud_adapter, region, sg_ids, insecure_ports):
res = []
security_groups = cloud_adapter.describe_security_groups(
region, sg_ids)
Expand Down Expand Up @@ -131,10 +137,14 @@ def _get_aws_region_insecure_sg(cloud_adapter, region, sg_ids, insecure_ports):
if any(filter(lambda x:
x[cidr_key] in INSECURE_CIDRS,
ranges)):
found_insecure_ports.add((insecure_port_value, insecure_port_protocol))
found_insecure_ports.add((insecure_port_value,
insecure_port_protocol))
if found_insecure_ports:
res.append(security_group)
insecure_ports_js = [{'port': port, 'protocol': protocol} for port, protocol in found_insecure_ports]
insecure_ports_js = [
{'port': port, 'protocol': self._format_protocol(protocol)}
for port, protocol in found_insecure_ports
]
security_group['insecure_ports'] = insecure_ports_js
return {region: res}

Expand Down Expand Up @@ -175,9 +185,11 @@ def _get_aws_insecure(self, config, resources, excluded_pools,
for region, insecure_sgs in region_insecure_sgs_map.items():
for insecure_sg in insecure_sgs:
security_groups_map = region_sg_map.get(region, {})
for instance in security_groups_map.get(insecure_sg['GroupId'], []):
for instance in security_groups_map.get(
insecure_sg['GroupId'], []):
result.append({
'cloud_resource_id': instance.get('cloud_resource_id'),
'cloud_resource_id': instance.get(
'cloud_resource_id'),
'resource_name': instance.get('name'),
'cloud_account_id': instance.get(
'cloud_account_id'),
Expand Down Expand Up @@ -259,15 +271,16 @@ def _get_azure_insecure(self, config, resources, excluded_pools,
to_port = from_port
for insecure_port in insecure_ports:
insecure_port_protocol = insecure_port.get('protocol')
if rule.protocol.lower() not in [insecure_port_protocol, '*']:
if rule.protocol.lower() not in [
insecure_port_protocol, '*']:
continue
insecure_port_value = insecure_port.get('port')
if from_port <= insecure_port_value <= to_port:
found_insecure_ports.add((insecure_port_value,
insecure_port_protocol))
found_insecure_ports.add(
(insecure_port_value, insecure_port_protocol))
if found_insecure_ports:
insecure_ports_js = [
{'port': port, 'protocol': protocol}
{'port': port, 'protocol': self._format_protocol(protocol)}
for port, protocol in found_insecure_ports]
result.append({
'cloud_resource_id': cloud_resource.get(
Expand Down Expand Up @@ -349,13 +362,13 @@ def _get_nebius_insecure(self, config, resources, excluded_pools,
insecure_port == from_port or
insecure_port == to_port) and
protocol in [insecure_protocol, 'ANY']):
found_insecure_ports.add(
(insecure_port, protocol.lower()))
found_insecure_ports.add((insecure_port, protocol))

if found_insecure_ports:
insecure_ports_js = [
{'port': port, 'protocol': protocol}
for port, protocol in found_insecure_ports]
insecure_ports_js = [{
'port': port,
'protocol': self._format_protocol(protocol)
} for port, protocol in found_insecure_ports]
region = instance.get('region') or instance.get(
'meta', {}).get('zone_id')
result.append({
Expand Down Expand Up @@ -413,7 +426,7 @@ def _get_gcp_insecure(self, config, resources, excluded_pools,
if protocol in insecure_protocols or protocol == 'all':
insecure_firewall_ports.append({
'port': '*',
'protocol': protocol
'protocol': self._format_protocol(protocol)
})
continue
for port_range in rule_ports:
Expand All @@ -432,6 +445,8 @@ def _get_gcp_insecure(self, config, resources, excluded_pools,
ins_protocol = insecure_port['protocol']
if ins_port in ports and (
ins_protocol == protocol or protocol == 'all'):
insecure_port['protocol'] = self._format_protocol(
ins_protocol)
insecure_firewall_ports.append(insecure_port)
if insecure_firewall_ports:
firewall_network_id = network_self_link_id[firewall.network]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ const AssignmentRuleForm = ({
onCancel,
pools,
cloudAccounts,
resourceTypes,
regions,
isEdit = false,
onPoolChange,
poolOwners,
Expand All @@ -35,8 +37,16 @@ const AssignmentRuleForm = ({

const onFormSubmit = (formData: FormValues) => {
const getConditions = () => {
const { FIELD_NAME, META_INFO, TYPE, TAG_KEY_FIELD_NAME, TAG_VALUE_FIELD_NAME, CLOUD_IS_FIELD_NAME } =
FIELD_NAMES.CONDITIONS_FIELD_ARRAY;
const {
FIELD_NAME,
META_INFO,
TYPE,
TAG_KEY_FIELD_NAME,
TAG_VALUE_FIELD_NAME,
CLOUD_IS_FIELD_NAME,
RESOURCE_TYPE_IS_FIELD_NAME,
REGION_IS_FIELD_NAME
} = FIELD_NAMES.CONDITIONS_FIELD_ARRAY;

return formData[FIELD_NAME].map((item) => {
if (TAG_KEY_FIELD_NAME in item) {
Expand All @@ -54,6 +64,18 @@ const AssignmentRuleForm = ({
[TYPE]: item[TYPE]
};
}
if (RESOURCE_TYPE_IS_FIELD_NAME in item) {
return {
[META_INFO]: item[RESOURCE_TYPE_IS_FIELD_NAME].trim(),
[TYPE]: item[TYPE]
};
}
if (REGION_IS_FIELD_NAME in item) {
return {
[META_INFO]: item[REGION_IS_FIELD_NAME].trim(),
[TYPE]: item[TYPE]
};
}

return { ...item, meta_info: item[META_INFO].trim() };
});
Expand All @@ -69,7 +91,13 @@ const AssignmentRuleForm = ({

return (
<FormProvider {...methods}>
<form onSubmit={handleSubmit(onFormSubmit)} noValidate data-test-id="add_rule_form">
<form
onSubmit={handleSubmit((formData) => {
onFormSubmit(formData);
})}
noValidate
data-test-id="add_rule_form"
>
<ActiveCheckboxField isLoading={isLoadingProps.isActiveCheckboxLoading} />
<NameField isLoading={isLoadingProps.isNameInputLoading} />
<Box display="flex" alignItems="center">
Expand All @@ -78,7 +106,12 @@ const AssignmentRuleForm = ({
</FormLabel>
<QuestionMark dataTestId="conditions_help" messageId="assignmentRuleConditionsDescription" fontSize="small" />
</Box>
<ConditionsFieldArray isLoading={isLoadingProps.isConditionsFieldLoading} cloudAccounts={cloudAccounts} />
<ConditionsFieldArray
isLoading={isLoadingProps.isConditionsFieldLoading}
cloudAccounts={cloudAccounts}
resourceTypes={resourceTypes}
regions={regions}
/>
<FormLabel data-test-id="lbl_assign" component="p">
<FormattedMessage id="assignTo" />
</FormLabel>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
import { Fragment } from "react";
import AddOutlinedIcon from "@mui/icons-material/AddOutlined";
import DeleteOutlinedIcon from "@mui/icons-material/DeleteOutlined";
import { Autocomplete } from "@mui/material";
import Box from "@mui/material/Box";
import FormControl from "@mui/material/FormControl";
import { useFieldArray, useFormContext } from "react-hook-form";
import { FormattedMessage } from "react-intl";
import { Controller, useFieldArray, useFormContext } from "react-hook-form";
import { FormattedMessage, useIntl } from "react-intl";
import { makeStyles } from "tss-react/mui";
import Button from "components/Button";
import CloudTypeIcon from "components/CloudTypeIcon";
import { Selector, TextInput } from "components/forms/common/fields";
import IconButton from "components/IconButton";
import IconLabel from "components/IconLabel";
import Input from "components/Input";
import InputLoader from "components/InputLoader";
import ResourceTypeLabel from "components/ResourceTypeLabel";
import { ItemContent, ItemContentWithDataSourceIcon } from "components/Selector";
import {
CONDITION_TYPES,
Expand All @@ -18,9 +23,13 @@ import {
TAG_VALUE_STARTS_WITH,
TAG_EXISTS,
DEFAULT_CONDITION,
ARRAY_FORM_FIELD_FLEX_BASIS_WIDTH
ARRAY_FORM_FIELD_FLEX_BASIS_WIDTH,
RESOURCE_TYPE_IS,
REGION_IS,
OPTSCALE_RESOURCE_TYPES
} from "utils/constants";
import { SPACING_1 } from "utils/layouts";
import { idx } from "utils/objects";
import { FIELD_NAMES } from "../utils";

const { FIELD_NAME, TYPE, META_INFO } = FIELD_NAMES.CONDITIONS_FIELD_ARRAY;
Expand All @@ -41,10 +50,16 @@ const useStyles = makeStyles()((theme) => ({
}
}));

const ConditionsFieldArray = ({ name = FIELD_NAME, isLoading = false, cloudAccounts }) => {
const ConditionsFieldArray = ({ name = FIELD_NAME, isLoading = false, cloudAccounts, resourceTypes, regions }) => {
const intl = useIntl();

const { classes, cx } = useStyles();

const { control, watch } = useFormContext();
const {
control,
watch,
formState: { errors }
} = useFormContext();

const { fields, append, remove } = useFieldArray({
control,
Expand Down Expand Up @@ -114,6 +129,149 @@ const ConditionsFieldArray = ({ name = FIELD_NAME, isLoading = false, cloudAccou
);
};

const renderResourceTypeIsAutocompleteField = (field, count) => {
const NAME = FIELD_NAMES.CONDITIONS_FIELD_ARRAY.RESOURCE_TYPE_IS_FIELD_NAME;

const fieldName = `${name}.${count}.${NAME}`;
const fieldError = idx(fieldName.split("."), errors);

return (
<Controller
name={`${name}.${count}.${NAME}`}
control={control}
defaultValue={field[NAME] ?? ""}
rules={{
required: {
value: true,
message: intl.formatMessage({ id: "thisFieldIsRequired" })
}
}}
render={({ field: { value: formFieldValue, onChange, ...rest } }) => (
<Autocomplete
freeSolo
options={resourceTypes.toSorted((resourceTypeA, resourceTypeB) => {
if (resourceTypeA.type === resourceTypeB.type) {
return resourceTypeA.name.localeCompare(resourceTypeB.name);
}

return resourceTypeA.type.localeCompare(resourceTypeB.type);
})}
value={formFieldValue}
onChange={(event, newValue) => {
onChange(newValue?.name ?? "");
}}
onInputChange={(event, newInputValue) => {
onChange(newInputValue);
}}
isOptionEqualToValue={(option, value) => option.name === value}
getOptionLabel={(option) => {
if (typeof option === "string") {
return option;
}
return option.name;
}}
renderOption={(props, option) => {
const getOptionLabel = () => {
if ([OPTSCALE_RESOURCE_TYPES.CLUSTER, OPTSCALE_RESOURCE_TYPES.ENVIRONMENT].includes(option.type)) {
return (
<ResourceTypeLabel
resourceInfo={{
resourceType: option.name,
clusterTypeId: option.type === OPTSCALE_RESOURCE_TYPES.CLUSTER,
isEnvironment: option.type === OPTSCALE_RESOURCE_TYPES.ENVIRONMENT
}}
/>
);
}

return option.name;
};

return <li {...props}>{getOptionLabel()}</li>;
}}
renderInput={(autoCompleteParams) => (
<Input
required
label={<FormattedMessage id="resourceType" />}
dataTestId={`input_${fieldName}`}
error={!!fieldError}
helperText={fieldError?.message}
{...autoCompleteParams}
{...rest}
/>
)}
/>
)}
/>
);
};

const renderRegionIsAutocompleteField = (field, count) => {
const NAME = FIELD_NAMES.CONDITIONS_FIELD_ARRAY.REGION_IS_FIELD_NAME;

const fieldName = `${name}.${count}.${NAME}`;
const fieldError = idx(fieldName.split("."), errors);

return (
<Controller
name={fieldName}
control={control}
defaultValue={field[NAME] ?? ""}
rules={{
required: {
value: true,
message: intl.formatMessage({ id: "thisFieldIsRequired" })
}
}}
render={({ field: { value: formFieldValue, onChange, ...rest } }) => (
<Autocomplete
freeSolo
options={regions
// Exclude empty (null) regions
.filter(Boolean)
.toSorted((regionA, regionB) => {
if (regionA.cloud_type === regionB.cloud_type) {
return regionA.name.localeCompare(regionB.name);
}

return regionA.cloud_type.localeCompare(regionB.cloud_type);
})}
value={formFieldValue}
onChange={(event, newValue) => {
onChange(newValue?.name ?? "");
}}
onInputChange={(event, newInputValue) => {
onChange(newInputValue);
}}
isOptionEqualToValue={(option, value) => option.name === value}
getOptionLabel={(option) => {
if (typeof option === "string") {
return option;
}
return option.name;
}}
renderOption={(props, option) => (
<li {...props}>
<IconLabel icon={<CloudTypeIcon type={option.cloud_type} hasRightMargin />} label={option.name} />
</li>
)}
renderInput={(autoCompleteParams) => (
<Input
required
label={<FormattedMessage id="region" />}
dataTestId={`input_${fieldName}`}
error={!!fieldError}
helperText={fieldError?.message}
{...autoCompleteParams}
{...rest}
/>
)}
/>
)}
/>
);
};

const conditionRow = (field, count) => {
const condition = watchConditions?.[count]?.[TYPE];

Expand All @@ -126,6 +284,10 @@ const ConditionsFieldArray = ({ name = FIELD_NAME, isLoading = false, cloudAccou
return renderCloudAccountSelector(field, count);
case TAG_EXISTS:
return renderInputField(field, count, "key");
case RESOURCE_TYPE_IS:
return renderResourceTypeIsAutocompleteField(field, count);
case REGION_IS:
return renderRegionIsAutocompleteField(field, count);
default:
return renderInputField(field, count);
}
Expand Down
Loading
Loading