Skip to content

Commit 6c42970

Browse files
authored
Merge pull request #3251 from josh-ferrell/add_e2e_secgrp_functions
Add SecurityGroup functions, List functions, subnet tags
2 parents 3e7adcb + 051f48b commit 6c42970

File tree

1 file changed

+291
-2
lines changed

1 file changed

+291
-2
lines changed

test/e2e/shared/aws.go

Lines changed: 291 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -750,6 +750,27 @@ func DeleteVPC(e2eCtx *E2EContext, vpcID string) (bool, error) {
750750
return true, nil
751751
}
752752

753+
func ListVpcSubnets(e2eCtx *E2EContext, vpcID string) ([]*ec2.Subnet, error) {
754+
ec2Svc := ec2.New(e2eCtx.AWSSession)
755+
756+
filter := &ec2.Filter{
757+
Name: aws.String("vpc-id"),
758+
Values: aws.StringSlice([]string{vpcID}),
759+
}
760+
761+
input := &ec2.DescribeSubnetsInput{
762+
Filters: []*ec2.Filter{
763+
filter,
764+
},
765+
}
766+
767+
result, err := ec2Svc.DescribeSubnets(input)
768+
if err != nil {
769+
return nil, err
770+
}
771+
return result.Subnets, nil
772+
}
773+
753774
func GetSubnet(e2eCtx *E2EContext, subnetID string) (*ec2.Subnet, error) {
754775
ec2Svc := ec2.New(e2eCtx.AWSSession)
755776

@@ -771,7 +792,7 @@ func GetSubnet(e2eCtx *E2EContext, subnetID string) (*ec2.Subnet, error) {
771792
return result.Subnets[0], nil
772793
}
773794

774-
func CreateSubnet(e2eCtx *E2EContext, subnetName string, cidrBlock string, az string, vpcID string) (*ec2.Subnet, error) {
795+
func CreateSubnet(e2eCtx *E2EContext, clusterName string, cidrBlock string, az string, vpcID string, st string) (*ec2.Subnet, error) {
775796
ec2Svc := ec2.New(e2eCtx.AWSSession)
776797

777798
input := &ec2.CreateSubnetInput{
@@ -783,13 +804,31 @@ func CreateSubnet(e2eCtx *E2EContext, subnetName string, cidrBlock string, az st
783804
Tags: []*ec2.Tag{
784805
{
785806
Key: aws.String("Name"),
786-
Value: aws.String(subnetName),
807+
Value: aws.String(clusterName + "-subnet-" + st),
808+
},
809+
{
810+
Key: aws.String("kubernetes.io/cluster/" + clusterName),
811+
Value: aws.String("shared"),
787812
},
788813
},
789814
},
790815
},
791816
}
792817

818+
// Tag subnet based on type(st)
819+
switch st {
820+
case "private":
821+
input.TagSpecifications[0].Tags = append(input.TagSpecifications[0].Tags, &ec2.Tag{
822+
Key: aws.String("kubernetes.io/role/internal-elb"),
823+
Value: aws.String("1"),
824+
})
825+
case "public":
826+
input.TagSpecifications[0].Tags = append(input.TagSpecifications[0].Tags, &ec2.Tag{
827+
Key: aws.String("kubernetes.io/role/elb"),
828+
Value: aws.String("1"),
829+
})
830+
}
831+
793832
if az != "" {
794833
input.AvailabilityZone = aws.String(az)
795834
}
@@ -1069,6 +1108,48 @@ func CreateRouteTable(e2eCtx *E2EContext, rtName string, vpcID string) (*ec2.Rou
10691108
return result.RouteTable, nil
10701109
}
10711110

1111+
func ListVpcRouteTables(e2eCtx *E2EContext, vpcID string) ([]*ec2.RouteTable, error) {
1112+
ec2Svc := ec2.New(e2eCtx.AWSSession)
1113+
1114+
filter := &ec2.Filter{
1115+
Name: aws.String("vpc-id"),
1116+
Values: aws.StringSlice([]string{vpcID}),
1117+
}
1118+
1119+
input := &ec2.DescribeRouteTablesInput{
1120+
Filters: []*ec2.Filter{
1121+
filter,
1122+
},
1123+
}
1124+
1125+
result, err := ec2Svc.DescribeRouteTables(input)
1126+
if err != nil {
1127+
return nil, err
1128+
}
1129+
return result.RouteTables, nil
1130+
}
1131+
1132+
func ListSubnetRouteTables(e2eCtx *E2EContext, subnetID string) ([]*ec2.RouteTable, error) {
1133+
ec2Svc := ec2.New(e2eCtx.AWSSession)
1134+
1135+
filter := &ec2.Filter{
1136+
Name: aws.String("association.subnet-id"),
1137+
Values: aws.StringSlice([]string{subnetID}),
1138+
}
1139+
1140+
input := &ec2.DescribeRouteTablesInput{
1141+
Filters: []*ec2.Filter{
1142+
filter,
1143+
},
1144+
}
1145+
1146+
result, err := ec2Svc.DescribeRouteTables(input)
1147+
if err != nil {
1148+
return nil, err
1149+
}
1150+
return result.RouteTables, nil
1151+
}
1152+
10721153
func GetRouteTable(e2eCtx *E2EContext, rtID string) (*ec2.RouteTable, error) {
10731154
ec2Svc := ec2.New(e2eCtx.AWSSession)
10741155

@@ -1144,3 +1225,211 @@ func DeleteRoute(e2eCtx *E2EContext, rtID string, destinationCidr string) (bool,
11441225
}
11451226
return true, nil
11461227
}
1228+
1229+
func CreateSecurityGroup(e2eCtx *E2EContext, sgName string, sgDescription string, vpcID string) (*ec2.CreateSecurityGroupOutput, error) {
1230+
ec2Svc := ec2.New(e2eCtx.AWSSession)
1231+
1232+
input := &ec2.CreateSecurityGroupInput{
1233+
VpcId: aws.String(vpcID),
1234+
GroupName: aws.String(sgName),
1235+
Description: aws.String(sgDescription),
1236+
TagSpecifications: []*ec2.TagSpecification{
1237+
{
1238+
ResourceType: aws.String("security-group"),
1239+
Tags: []*ec2.Tag{
1240+
{
1241+
Key: aws.String("Name"),
1242+
Value: aws.String(sgName),
1243+
},
1244+
},
1245+
},
1246+
},
1247+
}
1248+
1249+
result, err := ec2Svc.CreateSecurityGroup(input)
1250+
if err != nil {
1251+
return nil, err
1252+
}
1253+
return result, nil
1254+
}
1255+
1256+
func GetSecurityGroup(e2eCtx *E2EContext, sgID string) (*ec2.SecurityGroup, error) {
1257+
ec2Svc := ec2.New(e2eCtx.AWSSession)
1258+
1259+
filter := &ec2.Filter{
1260+
Name: aws.String("group-id"),
1261+
Values: aws.StringSlice([]string{sgID}),
1262+
}
1263+
1264+
input := &ec2.DescribeSecurityGroupsInput{
1265+
Filters: []*ec2.Filter{
1266+
filter,
1267+
},
1268+
}
1269+
1270+
result, err := ec2Svc.DescribeSecurityGroups(input)
1271+
if err != nil {
1272+
return nil, err
1273+
}
1274+
return result.SecurityGroups[0], nil
1275+
}
1276+
1277+
func DeleteSecurityGroup(e2eCtx *E2EContext, sgID string) (bool, error) {
1278+
ec2Svc := ec2.New(e2eCtx.AWSSession)
1279+
1280+
input := &ec2.DeleteSecurityGroupInput{
1281+
GroupId: aws.String(sgID),
1282+
}
1283+
1284+
if _, err := ec2Svc.DeleteSecurityGroup(input); err != nil {
1285+
return false, err
1286+
}
1287+
return true, nil
1288+
}
1289+
1290+
func ListSecurityGroupRules(e2eCtx *E2EContext, sgID string) ([]*ec2.SecurityGroupRule, error) {
1291+
ec2Svc := ec2.New(e2eCtx.AWSSession)
1292+
1293+
filter := &ec2.Filter{
1294+
Name: aws.String("group-id"),
1295+
Values: aws.StringSlice([]string{sgID}),
1296+
}
1297+
1298+
input := &ec2.DescribeSecurityGroupRulesInput{
1299+
Filters: []*ec2.Filter{
1300+
filter,
1301+
},
1302+
}
1303+
1304+
result, err := ec2Svc.DescribeSecurityGroupRules(input)
1305+
if err != nil {
1306+
return nil, err
1307+
}
1308+
return result.SecurityGroupRules, nil
1309+
}
1310+
1311+
func GetSecurityGroupRule(e2eCtx *E2EContext, sgrID string) (*ec2.SecurityGroupRule, error) {
1312+
ec2Svc := ec2.New(e2eCtx.AWSSession)
1313+
1314+
filter := &ec2.Filter{
1315+
Name: aws.String("security-group-rule-id"),
1316+
Values: aws.StringSlice([]string{sgrID}),
1317+
}
1318+
1319+
input := &ec2.DescribeSecurityGroupRulesInput{
1320+
Filters: []*ec2.Filter{
1321+
filter,
1322+
},
1323+
}
1324+
1325+
result, err := ec2Svc.DescribeSecurityGroupRules(input)
1326+
if err != nil {
1327+
return nil, err
1328+
}
1329+
return result.SecurityGroupRules[0], nil
1330+
}
1331+
1332+
func CreateSecurityGroupIngressRule(e2eCtx *E2EContext, sgID string, sgrDescription string, cidr string, protocol string, fromPort int64, toPort int64) (bool, error) {
1333+
ec2Svc := ec2.New(e2eCtx.AWSSession)
1334+
1335+
ipPerm := &ec2.IpPermission{
1336+
FromPort: aws.Int64(fromPort),
1337+
ToPort: aws.Int64(toPort),
1338+
IpProtocol: aws.String(protocol),
1339+
IpRanges: []*ec2.IpRange{
1340+
{
1341+
CidrIp: aws.String(cidr),
1342+
Description: aws.String(sgrDescription),
1343+
},
1344+
},
1345+
}
1346+
1347+
input := &ec2.AuthorizeSecurityGroupIngressInput{
1348+
GroupId: aws.String(sgID),
1349+
IpPermissions: []*ec2.IpPermission{
1350+
ipPerm,
1351+
},
1352+
}
1353+
1354+
result, err := ec2Svc.AuthorizeSecurityGroupIngress(input)
1355+
if err != nil {
1356+
return false, err
1357+
}
1358+
return *result.Return, nil
1359+
}
1360+
1361+
func CreateSecurityGroupEgressRule(e2eCtx *E2EContext, sgID string, sgrDescription string, cidr string, protocol string, fromPort int64, toPort int64) (bool, error) {
1362+
ec2Svc := ec2.New(e2eCtx.AWSSession)
1363+
1364+
ipPerm := &ec2.IpPermission{
1365+
FromPort: aws.Int64(fromPort),
1366+
ToPort: aws.Int64(toPort),
1367+
IpProtocol: aws.String(protocol),
1368+
IpRanges: []*ec2.IpRange{
1369+
{
1370+
CidrIp: aws.String(cidr),
1371+
Description: aws.String(sgrDescription),
1372+
},
1373+
},
1374+
}
1375+
1376+
input := &ec2.AuthorizeSecurityGroupEgressInput{
1377+
GroupId: aws.String(sgID),
1378+
IpPermissions: []*ec2.IpPermission{
1379+
ipPerm,
1380+
},
1381+
}
1382+
result, err := ec2Svc.AuthorizeSecurityGroupEgress(input)
1383+
if err != nil {
1384+
return false, err
1385+
}
1386+
return *result.Return, nil
1387+
}
1388+
1389+
func CreateSecurityGroupRule(e2eCtx *E2EContext, sgID string, sgrDescription string, cidr string, protocol string, fromPort int64, toPort int64, rt string) (bool, error) {
1390+
switch rt {
1391+
case "ingress":
1392+
return CreateSecurityGroupIngressRule(e2eCtx, sgID, sgrDescription, cidr, protocol, fromPort, toPort)
1393+
case "egress":
1394+
return CreateSecurityGroupEgressRule(e2eCtx, sgID, sgrDescription, cidr, protocol, fromPort, toPort)
1395+
}
1396+
return false, nil
1397+
}
1398+
1399+
func DeleteSecurityGroupIngressRule(e2eCtx *E2EContext, sgrID string) (bool, error) {
1400+
ec2Svc := ec2.New(e2eCtx.AWSSession)
1401+
1402+
input := &ec2.RevokeSecurityGroupIngressInput{
1403+
SecurityGroupRuleIds: aws.StringSlice([]string{sgrID}),
1404+
}
1405+
1406+
result, err := ec2Svc.RevokeSecurityGroupIngress(input)
1407+
if err != nil {
1408+
return false, err
1409+
}
1410+
return *result.Return, nil
1411+
}
1412+
1413+
func DeleteSecurityGroupEgressRule(e2eCtx *E2EContext, sgrID string) (bool, error) {
1414+
ec2Svc := ec2.New(e2eCtx.AWSSession)
1415+
1416+
input := &ec2.RevokeSecurityGroupEgressInput{
1417+
SecurityGroupRuleIds: aws.StringSlice([]string{sgrID}),
1418+
}
1419+
1420+
result, err := ec2Svc.RevokeSecurityGroupEgress(input)
1421+
if err != nil {
1422+
return false, err
1423+
}
1424+
return *result.Return, nil
1425+
}
1426+
1427+
func DeleteSecurityGroupRule(e2eCtx *E2EContext, sgrID string, rt string) (bool, error) {
1428+
switch rt {
1429+
case "ingress":
1430+
return DeleteSecurityGroupIngressRule(e2eCtx, sgrID)
1431+
case "egress":
1432+
return DeleteSecurityGroupEgressRule(e2eCtx, sgrID)
1433+
}
1434+
return false, nil
1435+
}

0 commit comments

Comments
 (0)