import React from 'react';
import superagent from 'superagent';
import { Segment, Header, Icon, Button, Table, Label, Modal, Message, Form } from 'semantic-ui-react'
import ApiButtonModal from '../../CloudApi/ApiButtonModal.jsx';
export default class AccountIPManager extends React.Component {
constructor(props) {
super(props);
this.state = {
loading: false,
securityGroup: null,
newIpAddress: ""
};
}
async componentDidMount() {
return this.reload();
}
async reload() {
try {
this.setState({securityGroup: null, loading: true});
const result = await superagent.get(`/api/admin/account/${this.props.accountId}/security_groups`).accept('json');
this.setState({
loading: false,
securityGroup: result.body
});
} catch (e) {
console.log(e);
this.setState({
loading: false,
securityGroup: null
});
}
}
async onChange(e, { name, value }) {
this.setState({ [name]: value })
}
stripCidrSuffix(cidrIp) {
// Strip /32 for IPv4 or /128 for IPv6
if (cidrIp.endsWith('/32') || cidrIp.endsWith('/128')) {
return cidrIp.substring(0, cidrIp.lastIndexOf('/'));
}
return cidrIp;
}
async onAddIP() {
try {
const ipAddress = this.stripCidrSuffix(this.state.newIpAddress);
const result = await superagent.put(`/api/admin/account/${this.props.accountId}/security_groups/${ipAddress}`).accept('json').send();
console.log(result.body);
this.setState({ newIpAddress: "" });
await this.reload();
} catch (e) {
console.log(e);
}
}
async onRemoveIP(ipRange) {
try {
const ipAddress = this.stripCidrSuffix(ipRange.CidrIp);
const result = await superagent.delete(`/api/admin/account/${this.props.accountId}/security_groups/${ipAddress}`).accept('json').send();
console.log(result.body);
await this.reload();
} catch (e) {
console.log(e);
}
}
getAccessType(ipPermission) {
if (ipPermission.IpProtocol === 'tcp' && ipPermission.FromPort === 443 && ipPermission.ToPort === 443) {
return 'Web Access';
}
if (ipPermission.FromPort && ipPermission.ToPort) {
return `${ipPermission.IpProtocol} ports ${ipPermission.FromPort}-${ipPermission.ToPort}`;
}
return ipPermission.IpProtocol;
}
render() {
if (this.state.loading) {
return (