summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/create_cluster/eks_cluster/services/aws_services_facade.js
blob: 58568b5dedbcd9fbd105458b7360ec9162912f44 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import AWS from 'aws-sdk/global';
import EC2 from 'aws-sdk/clients/ec2';
import IAM from 'aws-sdk/clients/iam';

const lookupVpcName = ({ Tags: tags, VpcId: id }) => {
  const nameTag = tags.find(({ Key: key }) => key === 'Name');

  return nameTag ? nameTag.Value : id;
};

export const setAWSConfig = ({ awsCredentials }) => {
  AWS.config = awsCredentials;
};

export const fetchRoles = () => {
  const iam = new IAM();

  return iam
    .listRoles()
    .promise()
    .then(({ Roles: roles }) => roles.map(({ RoleName: name, Arn: value }) => ({ name, value })));
};

export const fetchKeyPairs = ({ region }) => {
  const ec2 = new EC2({ region });

  return ec2
    .describeKeyPairs()
    .promise()
    .then(({ KeyPairs: keyPairs }) => keyPairs.map(({ KeyName: name }) => ({ name, value: name })));
};

export const fetchVpcs = ({ region }) => {
  const ec2 = new EC2({ region });

  return ec2
    .describeVpcs()
    .promise()
    .then(({ Vpcs: vpcs }) =>
      vpcs.map(vpc => ({
        value: vpc.VpcId,
        name: lookupVpcName(vpc),
      })),
    );
};

export const fetchSubnets = ({ vpc, region }) => {
  const ec2 = new EC2({ region });

  return ec2
    .describeSubnets({
      Filters: [
        {
          Name: 'vpc-id',
          Values: [vpc],
        },
      ],
    })
    .promise()
    .then(({ Subnets: subnets }) => subnets.map(({ SubnetId: id }) => ({ value: id, name: id })));
};

export const fetchSecurityGroups = ({ region, vpc }) => {
  const ec2 = new EC2({ region });

  return ec2
    .describeSecurityGroups({
      Filters: [
        {
          Name: 'vpc-id',
          Values: [vpc],
        },
      ],
    })
    .promise()
    .then(({ SecurityGroups: securityGroups }) =>
      securityGroups.map(({ GroupName: name, GroupId: value }) => ({ name, value })),
    );
};