Skip to content

Commit

Permalink
Merge pull request #11 from chander12shekhar/PackageVariantSetEditor
Browse files Browse the repository at this point in the history
Created Package variant set editor
  • Loading branch information
nephio-prow[bot] committed Jun 28, 2023
2 parents 1523e65 + 34b7905 commit 0908de0
Show file tree
Hide file tree
Showing 17 changed files with 1,631 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,11 @@ export const PackageRevisionResourcesTable = ({
kind: 'PackageVariant',
namespaceScoped: true,
},
{
apiVersion: 'config.porch.kpt.dev/v1alpha1',
kind: 'PackageVariantSet',
namespaceScoped: true,
},
{
apiVersion: 'fn.kpt.dev/v1alpha1',
kind: 'ApplyReplacements',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import { RoleEditor } from './FirstClassEditors/RoleEditor';
import { ServiceAccountEditor } from './FirstClassEditors/ServiceAccountEditor';
import { ServiceEditor } from './FirstClassEditors/ServiceEditor';
import { SetLabelsEditor } from './FirstClassEditors/SetLabelsEditor';
import { PackageVariantSetEditor } from './FirstClassEditors/PackageVariantSetEditor';

type OnUpdatedYamlFn = (yaml: string) => void;
type OnNoNamedEditorFn = () => void;
Expand Down Expand Up @@ -87,6 +88,16 @@ export const FirstClassEditorSelector = ({
/>
);

case 'config.porch.kpt.dev/v1alpha2/PackageVariantSet':
case 'config.porch.kpt.dev/v1alpha1/PackageVariantSet':
return (
<PackageVariantSetEditor
yaml={yaml}
onUpdatedYaml={onUpdatedYaml}
packageResources={packageResources}
/>
);

case 'fn.kpt.dev/v1alpha1/ApplyReplacements':
return (
<ApplyReplacementsEditor
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,8 @@ export const PackageVariantEditor = ({
onUpdate={metadata => setState(s => ({ ...s, metadata }))}
/>
<PackageVariantSpecEditor
id="spec"
title="Spec Data"
state={[expanded, setExpanded]}
value={specState.spec}
packageResources={packageResources}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ import { InjectorEditorAccordion } from './container/InjectorEditorAccordion';
type OnUpdate = (value: PackageVariantSpec) => void;

type PackageVariantSpecEditorProps = {
id: string;
title: string;
state: AccordionState;
value: PackageVariantSpec;
onUpdate: OnUpdate;
Expand All @@ -51,6 +53,8 @@ const adoptFunctionList = [
];

export const PackageVariantSpecEditor = ({
id,
title,
state,
value,
onUpdate,
Expand All @@ -74,7 +78,7 @@ export const PackageVariantSpecEditor = ({
};

return (
<EditorAccordion id="spec" title="Spec Data" state={state}>
<EditorAccordion id={id} title={title} state={state}>
<KeyValueEditorAccordion
id="labels"
state={[specExpanded, setSpecExpanded]}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ export const UpstreamPackageEditorAccordion = ({
const refViewModel = useRef<InternalKeyValue>(keyValueObject);
const viewModel = refViewModel.current;
const repositoryName = viewModel.repo;
const packageName = viewModel.package;

const [repository, setRepository] = useState<Repository>();
const [packageRevision, setPackageRevision] = useState<PackageRevision>();
Expand Down Expand Up @@ -137,10 +138,14 @@ export const UpstreamPackageEditorAccordion = ({
repositoryPackages.map(mapPackageRevisionToSelectItem),
);

setPackageRevision(undefined);
const thisPackage = packageName
? getPackageData(allPackageRevisions.current, packageName)
: undefined;

setPackageRevision(thisPackage);
setPackageRevisionSelectItems(allowPackageRevisions);
}
}, [repository]);
}, [repository, packageName]);

const description = `${viewModel.repo ? `${viewModel.repo}/` : ''}${
viewModel.package ? `${viewModel.package}` : ''
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
/**
* Copyright 2023 The Nephio Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import React, { useEffect, useState } from 'react';
import { cloneDeep } from 'lodash';
import { PackageResource } from '../../../../../utils/packageRevisionResources';
import { dumpYaml, loadYaml } from '../../../../../utils/yaml';
import { EditorAccordion, ResourceMetadataAccordion } from '../Controls';
import { useEditorStyles } from '../styles';
import {
PackageVariantSet,
PackageVariantSetTargets,
} from '../../../../../types/PackageVariantSet';
import { PackageVariantUpstream } from '../../../../../types/PackageVariant';
import { UpstreamPackageEditorAccordion } from '../PackageVariantEditor/components/container/UpstreamPackageEditorAccordion';
import { TargetEditorAccordion } from './components/TargetEditorAccordion';

type OnUpdatedYamlFn = (yaml: string) => void;

type ResourceEditorProps = {
yaml: string;
onUpdatedYaml: OnUpdatedYamlFn;
packageResources: PackageResource[];
};

type State = {
upstream: PackageVariantUpstream;
targets: PackageVariantSetTargets[];
};

const getResourceState = (packageVariantSet: PackageVariantSet): State => {
packageVariantSet.spec = packageVariantSet.spec || { targets: [] };
const pvSetSpec = packageVariantSet.spec;

pvSetSpec.upstream = pvSetSpec.upstream || {};
pvSetSpec.targets = pvSetSpec.targets || [];

return {
upstream: pvSetSpec.upstream,
targets: pvSetSpec.targets,
};
};
export const PackageVariantSetEditor = ({
yaml,
onUpdatedYaml,
packageResources,
}: ResourceEditorProps) => {
const resourceYaml = loadYaml(yaml) as PackageVariantSet;

const classes = useEditorStyles();

const [state, setState] = useState<PackageVariantSet>(resourceYaml);
const [specState, setSpecState] = useState<State>(
getResourceState(resourceYaml),
);
const [expanded, setExpanded] = useState<string>();
const [specExpanded, setSpecExpanded] = useState<string>();

useEffect(() => {
resourceYaml.metadata = state.metadata;
const spec = resourceYaml.spec;
spec.upstream = cloneDeep(specState.upstream);
spec.targets = cloneDeep(specState.targets);

onUpdatedYaml(dumpYaml(resourceYaml));
}, [state, specState, resourceYaml, onUpdatedYaml]);

return (
<div className={classes.root}>
<ResourceMetadataAccordion
id="metadata"
state={[expanded, setExpanded]}
value={state.metadata}
onUpdate={metadata => setState(s => ({ ...s, metadata }))}
/>
<EditorAccordion id="spec" title="Spec" state={[expanded, setExpanded]}>
<UpstreamPackageEditorAccordion
id="upstream"
state={[specExpanded, setSpecExpanded]}
title="Upstream"
keyValueObject={specState.upstream || {}}
onUpdatedKeyValueObject={upstream =>
setSpecState(s => ({ ...s, upstream }))
}
/>
<TargetEditorAccordion
id="Target"
title="Target"
packageResources={packageResources}
targetState={[specExpanded, setSpecExpanded]}
keyValueObject={specState.targets || []}
onUpdate={targetData => setSpecState(s => ({ ...s, targetData }))}
/>
</EditorAccordion>
</div>
);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
/**
* Copyright 2023 The Nephio Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import React, { useRef, useState } from 'react';
import { clone } from 'lodash';
import DeleteIcon from '@material-ui/icons/Delete';
import { TextField, Button } from '@material-ui/core';
import {
AccordionState,
EditorAccordion,
} from '../../Controls/EditorAccordion';
import {
PackageVariantSetObjectSelector,
PackageVariantSetTempleate,
} from '../../../../../../types/PackageVariantSet';
import { KeyValueEditorAccordion } from '../../Controls';
import { PackageResource } from '../../../../../../utils/packageRevisionResources';
import { TemplateEditorAccordion } from './templates/TemplateEditorAccordion';
import { useEditorStyles } from '../../styles';

type RepositoriesState = {
objectSelector: PackageVariantSetObjectSelector;
template: PackageVariantSetTempleate;
};

type OnUpdate = (newValue?: RepositoriesState) => void;

type objectSelectorEditorProps = {
id: number;
title: string;
targetState: AccordionState;
target: RepositoriesState;
packageResources: PackageResource[];
onUpdate: OnUpdate;
};

export const ObjectSelectorEditorAccordion = ({
id,
title,
targetState: accordionState,
target,
packageResources,
onUpdate,
}: objectSelectorEditorProps) => {
const classes = useEditorStyles();
const refViewModel = useRef<RepositoriesState>(clone(target));
const viewModel = refViewModel.current;
const [expanded, setExpanded] = useState<string>();

const [state, setState] = useState<RepositoriesState>(viewModel);
const valueUpdated = (): void => {
onUpdate(state);
};

return (
<EditorAccordion
id={`objectSelector-${id}`}
title={title}
description={`ObjectSelector-${id}`}
state={accordionState}
>
<TextField
label="API Version"
variant="outlined"
value={state.objectSelector.apiVersion}
onChange={e => {
state.objectSelector.apiVersion = e.target.value;
valueUpdated();
}}
fullWidth
/>
<TextField
label="Kind"
variant="outlined"
value={state.objectSelector.kind}
onChange={e => {
state.objectSelector.kind = e.target.value;
valueUpdated();
}}
fullWidth
/>
<KeyValueEditorAccordion
id="matchLabels"
state={[expanded, setExpanded]}
title="Match Labels"
keyValueObject={state.objectSelector.matchLabels || {}}
onUpdatedKeyValueObject={matchLabels => {
state.objectSelector.matchLabels = matchLabels;
valueUpdated();
}}
/>
<TemplateEditorAccordion
id="template"
title="Template Data"
state={[expanded, setExpanded]}
value={state.template}
packageResources={packageResources}
onUpdate={updatedRepositories => {
setState(s => ({
...s,
template: updatedRepositories,
}));
valueUpdated();
}}
/>
<div className={classes.multiControlRow}>
<Button
variant="outlined"
startIcon={<DeleteIcon />}
onClick={() => onUpdate(undefined)}
>
Delete Object Selector
</Button>
</div>
</EditorAccordion>
);
};
Loading

0 comments on commit 0908de0

Please sign in to comment.