-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsecurity_group_list.R
81 lines (66 loc) · 1.78 KB
/
security_group_list.R
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
80
#' security_group_list
#' @importFrom purrr map_df
#' @importFrom tibble tibble
#' @export security_group_list
security_group_list <- function() {
resource <- resource_ec2()
client <- client_ec2()
response <- client$describe_security_groups()
response <- response$SecurityGroups
security_group_list <-
map_df(response, function(x) {
tibble(group_name = x$GroupName,
group_id = x$GroupId)
})
security_group_list
}
#' security_group_list
#' @importFrom purrr map_df
#' @importFrom tibble tibble
#' @export security_group_data
security_group_data <- function() {
resource <- resource_ec2()
client <- client_ec2()
response <- client$describe_security_groups()
response <- response$SecurityGroups
response <-
response %>%
map(
function(sg) {
dfs <-
sg$IpPermissions %>%
map(
function(sg_id) {
if(is.null(unlist(sg_id$IpRanges))) {
return('')
} else {
sg_tibble <- tibble(ip_ranges = unlist(sg_id$IpRanges))
if(is.null(sg_id$FromPort) & is.null(sg_id$ToPort)) {
sg_tibble$from_port = 'Anywhere'
sg_tibble$to_port = 'Anywhere'
} else {
sg_tibble$from_port = sg_id$FromPort
sg_tibble$to_port = sg_id$ToPort
}
sg_tibble %>%
mutate_all(as.character)
}
}
) %>%
keep(is.data.frame)
if(length(dfs) > 0) {
dfs <- bind_rows(dfs)
dfs$group_name = sg$GroupName
dfs$group_id = sg$GroupId
}
dfs
}
)
response <-
response %>%
keep(
~ length(.) > 0
) %>%
bind_rows
response
}