Skip to content

Commit

Permalink
Included Instance run and change proxy
Browse files Browse the repository at this point in the history
* Missing API call to create instance from Profile
* Missing button to delete proxy and service in one go
  • Loading branch information
RicYaben committed Sep 5, 2022
1 parent 2fcd013 commit 30b4722
Show file tree
Hide file tree
Showing 6 changed files with 148 additions and 49 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ require (

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/gin-contrib/cors v1.4.0 // indirect
github.com/gin-contrib/sse v0.1.0 // indirect
github.com/go-playground/locales v0.14.0 // indirect
github.com/go-playground/universal-translator v0.18.0 // indirect
Expand Down
7 changes: 7 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ github.com/fatih/color v1.9.0 h1:8xPHl4/q1VyqGIPif1F+1V3Y3lSmrq01EabUW3CoW5s=
github.com/fatih/color v1.9.0/go.mod h1:eQcE1qtQxscV5RaZvpXrrb8Drkc3/DdQ+uUYCNjL+zU=
github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4IgpuI1SZQ=
github.com/fxamacker/cbor/v2 v2.2.0/go.mod h1:TA1xS00nchWmaBnEIxPSE5oHLuJBAVvqrtAnWBwBCVo=
github.com/gin-contrib/cors v1.4.0 h1:oJ6gwtUl3lqV0WEIwM/LxPF1QZ5qe2lGWdY2+bz7y0g=
github.com/gin-contrib/cors v1.4.0/go.mod h1:bs9pNM0x/UsmHPBWT2xZz9ROh8xYjYkiURUfmBoMlcs=
github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE=
github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI=
github.com/gin-gonic/gin v1.8.1 h1:4+fr/el88TOO3ewCmQr8cx/CtZ/umlIRIs5M4NTNjf8=
Expand All @@ -39,9 +41,11 @@ github.com/go-playground/locales v0.14.0 h1:u50s323jtVGugKlcYeyzC0etD1HifMjqmJqb
github.com/go-playground/locales v0.14.0/go.mod h1:sawfccIbzZTqEDETgFXqTho0QybSa7l++s0DH+LDiLs=
github.com/go-playground/universal-translator v0.18.0 h1:82dyy6p4OuJq4/CByFNOn/jYrnRPArHwAcmLoJZxyho=
github.com/go-playground/universal-translator v0.18.0/go.mod h1:UvRDBj+xPUEGrFYl+lu/H90nyDXpg0fqeB/AQUGNTVA=
github.com/go-playground/validator/v10 v10.10.0/go.mod h1:74x4gJWsvQexRdW8Pn3dXSGrTK4nAUsbPlLADvpJkos=
github.com/go-playground/validator/v10 v10.11.0 h1:0W+xRM511GY47Yy3bZUbJVitCNg2BOGlCyvTqsp/xIw=
github.com/go-playground/validator/v10 v10.11.0/go.mod h1:i+3WkQ1FvaUjjxh1kSvIA4dMGDBiPU55YFDl0WbKdWU=
github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY=
github.com/goccy/go-json v0.9.7/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I=
github.com/goccy/go-json v0.9.11 h1:/pAaQDLHEoCq/5FFmSKBswWmK6H0e8g4159Kc/X/nqk=
github.com/goccy/go-json v0.9.11/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I=
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
Expand Down Expand Up @@ -125,6 +129,7 @@ github.com/montanaflynn/stats v0.6.6/go.mod h1:etXPPgVO6n31NxCd9KQUMvCM+ve0ruNzt
github.com/opentracing/opentracing-go v1.1.0/go.mod h1:UkNAQd3GIcIGf0SeVgPpRdFStlNbqXla1AfSYxPUl2o=
github.com/panjf2000/ants/v2 v2.4.3/go.mod h1:f6F0NZVFsGCp5A7QW/Zj/m92atWwOkY0OIhFxRNFr4A=
github.com/patrickmn/go-cache v2.1.0+incompatible/go.mod h1:3Qf8kWWT7OJRJbdiICTKqZju1ZixQ/KpMGzzAfe6+WQ=
github.com/pelletier/go-toml/v2 v2.0.1/go.mod h1:r9LEWfGN8R5k0VXJ+0BkIe7MYkRdwZOjgMj2KwnJFUo=
github.com/pelletier/go-toml/v2 v2.0.5 h1:ipoSadvV8oGUjnUbMub59IDPPwfxF694nG/jwbMiyQg=
github.com/pelletier/go-toml/v2 v2.0.5/go.mod h1:OMHamSCAODeSsVrwwvcJOaoN0LIUIaFVNZzmWyNfXas=
github.com/pion/dtls/v2 v2.0.1-0.20200503085337-8e86b3a7d585/go.mod h1:/GahSOC8ZY/+17zkaGJIG4OUkSGAcZu/N/g3roBOCkM=
Expand Down Expand Up @@ -222,6 +227,7 @@ golang.org/x/crypto v0.0.0-20200323165209-0ec3e9974c59/go.mod h1:LzIPMQfyMNhhGPh
golang.org/x/crypto v0.0.0-20200510223506-06a226fb4e37/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b/go.mod h1:T9bdIzuCu7OtxOm1hfPfRQxPLYneinmdGuTeoZ9dtd4=
golang.org/x/crypto v0.0.0-20210711020723-a769d52b0f97/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/crypto v0.0.0-20211215153901-e495a2d5b3d3/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
golang.org/x/crypto v0.0.0-20220427172511-eb4f295cb31f/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
Expand Down Expand Up @@ -335,6 +341,7 @@ google.golang.org/protobuf v1.20.1-0.20200309200217-e05f789c0967/go.mod h1:A+miE
google.golang.org/protobuf v1.21.0/go.mod h1:47Nbq4nVaFHyn7ilMalzfO3qCViNmqZ2kzikPIcrTAo=
google.golang.org/protobuf v1.22.0/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU=
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
google.golang.org/protobuf v1.28.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
google.golang.org/protobuf v1.28.1 h1:d0NfwRgPtno5B1Wa6L2DAG+KivqkdutMf1UhdNx175w=
google.golang.org/protobuf v1.28.1/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
Expand Down
1 change: 1 addition & 0 deletions ui/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"name": "ui",
"version": "0.1.0",
"private": true,
"proxy": "http://localhost:3000",
"dependencies": {
"@testing-library/jest-dom": "^5.16.4",
"@testing-library/react": "^13.3.0",
Expand Down
2 changes: 1 addition & 1 deletion ui/src/recoil/atoms/instances.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,6 @@ export const instanceProxySelector = selectorFamily({
const instance = get(instances(id));

// Return the proxy included in the instance from the API
return fetchProxy(instance);
return fetchProxy(instance.host);
},
});
154 changes: 119 additions & 35 deletions ui/src/routes/instances/InstanceAPI.tsx
Original file line number Diff line number Diff line change
@@ -1,49 +1,133 @@
import { InteractionOptions, NetworkOptions } from "../../constants/globals";
import { Instance, InstanceProxyService } from "../../recoil/atoms/instances";
import { Service } from "../../recoil/atoms/services";

export const fetchProxy = async (instance: Instance) => {
await new Promise((resolve) => setTimeout(resolve, 800));
let services;

let mockProxy = {
id: "",
port: 2,
status: "stopped",
service: {
id: "",
name: "CoAP",
network: NetworkOptions[0],
interaction: InteractionOptions[0],
host: "localhost",
port: 2022,
},
};

services = [mockProxy, mockProxy, mockProxy];

return services;
export const fetchProxy = async (host: string) => {
let response = [];
try {
response = await fetch("http://" + host + "/api/proxies/")
.then((response) => response.json())
// Map the content of the response
.then((data) =>
data.map((element: any) => {
if ("service" in element) {
let service = element["service"];

// Parse the network and the interaction
if ("network" in service)
service["network"] = NetworkOptions.find(
(x) => x.value === service["network"]
);
if ("interaction" in service)
service["interaction"] = InteractionOptions.find(
(x) => x.value === service["interaction"]
);

// Re-assign the service
element["service"] = service;
}

return element;
})
);
} catch (error) {
console.log(error);
}

return response;
};

export const patchService = async (id: number, service: Service) => {
await new Promise((resolve) => setTimeout(resolve, 800));
let servs;
export const patchService = async (host: string, service: Service) => {
try {
const response = await fetch(
"http://" + host + "/api/services/" + service.id + "/",
{
method: "POST",
body: JSON.stringify({
name: service.name,
port: service.port,
host: service.host,
}),
headers: {
"Content-type": "application/json; charset=UTF-8",
},
}
)
.then((response) => response.json())
.then((data) => {
// Parse the network and the interaction
if ("network" in data)
data["network"] = NetworkOptions.find(
(x) => x.value === data["network"]
);

if ("interaction" in data)
data["interaction"] = InteractionOptions.find(
(x) => x.value === data["interaction"]
);

return {
...service,
...data,
};
});

return response;
} catch (err) {
console.log(err);
}

return service;
};

export const patchProxy = async (id: number, proxy: InstanceProxyService) => {
await new Promise((resolve) => setTimeout(resolve, 800));
let servs;

return proxy;
export const changeProxyPort = async (
host: string,
proxyId: string,
port: number
) => {
try {
await fetch("http://" + host + "/api/proxies/" + proxyId + "/port", {
method: "POST",
body: JSON.stringify({
port: port,
}),
headers: {
"Content-type": "application/json; charset=UTF-8",
},
})
.then((response) => response.json())
.catch((err) => {
console.log(err.message);
});
} catch (err) {
console.log(err);
}
};

export const changeProxyStatus = async (
instanceId: number,
proxyId: string,
status: string
host: string,
id: string,
status: string,
changeStatus: (status: string) => any
) => {
await new Promise((resolve) => setTimeout(resolve, 800));
return status;
try {
await fetch("http://" + host + "/api/proxies/" + id + "/status", {
method: "POST",
body: JSON.stringify({
status: status,
}),
headers: {
"Content-type": "application/json; charset=UTF-8",
},
})
.then((response) => response.json())
.then((data) => {
if (["running", "stopped"].includes(data.status))
changeStatus(data.status);
})
.catch((err) => {
console.log(err.message);
});
} catch (err) {
console.log(err);
}
};
32 changes: 19 additions & 13 deletions ui/src/routes/instances/InstanceTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@ import {
instanceProxySelector,
} from "../../recoil/atoms/instances";
import { Service } from "../../recoil/atoms/services";
import { changeProxyStatus, patchProxy } from "./InstanceAPI";
import {
changeProxyStatus,
changeProxyPort,
patchService,
} from "./InstanceAPI";

const ServiceInfoHelp = ({
network,
Expand Down Expand Up @@ -56,16 +60,16 @@ const InstanceServiceInfo = ({ service }: { service: Service }) => {
};

const InstanceServiceProxy = ({
id,
host,
proxy,
}: {
id: number;
host: string;
proxy: InstanceProxyService;
}) => {
const [getProxy, setProxy] = useState(proxy.port);

const onClick = () => {
patchProxy(id, proxy);
const handler = () => {
changeProxyPort(host, proxy.id, getProxy);
};

return (
Expand All @@ -84,7 +88,7 @@ const InstanceServiceProxy = ({
<Button
variant="outline-secondary"
id="button-addon2"
onClick={onClick}
onClick={() => handler()}
>
<BsArrowRepeat />
</Button>
Expand All @@ -94,17 +98,19 @@ const InstanceServiceProxy = ({
};

const InstanceServiceToggle = ({
id,
host,
proxy,
}: {
id: number;
host: string;
proxy: InstanceProxyService;
}) => {
const [status, setStatus] = useState(proxy.status);

const handler = (isRunning: string) => {
setStatus(isRunning);
changeProxyStatus(id, proxy.id, proxy.status);
// Change the status of the thing
if (status !== isRunning) {
changeProxyStatus(host, proxy.id, isRunning, setStatus);
}
};

return (
Expand Down Expand Up @@ -138,16 +144,16 @@ const InstanceServiceRow = ({
}) => {
const cells = [
<InstanceServiceInfo service={proxy.service} />,
<InstanceServiceProxy id={instance.id} proxy={proxy} />,
<InstanceServiceToggle id={instance.id} proxy={proxy} />,
<InstanceServiceProxy host={instance.host} proxy={proxy} />,
<InstanceServiceToggle host={instance.host} proxy={proxy} />,
];

return <Row cells={cells} />;
};

const InstanceServicesTable = ({ instance }: { instance: Instance }) => {
const proxyList = useRecoilValue(instanceProxySelector(instance.id));
const rows = proxyList.map((proxy, index) => (
const rows = proxyList.map((proxy: any, index: number) => (
<InstanceServiceRow key={index} instance={instance} proxy={proxy} />
));
const data = {
Expand Down

0 comments on commit 30b4722

Please sign in to comment.