-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathfirewall.rb
136 lines (129 loc) · 3.45 KB
/
firewall.rb
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# frozen_string_literal: true
module HCloud
##
# Represents a firewall
#
# == List all firewalls
#
# HCloud::Firewall.all
# # => [#<HCloud::Firewall id: 1, ...>, ...]
#
# == Sort firewalls
#
# HCloud::Firewall.sort(name: :desc)
# # => [#<HCloud::Firewall id: 1, ...>, ...]
#
# HCloud::Firewall.sort(:id, name: :asc)
# # => [#<HCloud::Firewall id: 1, ...>, ...]
#
# == Search firewalls
#
# HCloud::Firewall.where(name: "my_firewall")
# # => #<HCloud::Firewall id: 1, ...>
#
# HCloud::Firewall.where(label_selector: { environment: "production" })
# # => #<HCloud::Firewall id: 1, ...>
#
# == Find firewall by ID
#
# HCloud::Firewall.find(1)
# # => #<HCloud::Firewall id: 1, ...>
#
# == Create firewall
#
# firewall = HCloud::Firewall.new(name: "my_firewall")
# firewall.create
# firewall.created?
# # => true
#
# firewall = HCloud::Firewall.create(name: "my_firewall")
# # => #<HCloud::Firewall id: 1, ...>
#
# == Update firewall
#
# firewall = HCloud::Firewall.find(1)
# firewall.name = "another_firewall"
# firewall.update
#
# == Delete firewall
#
# firewall = HCloud::Firewall.find(1)
# firewall.delete
# firewall.deleted?
# # => true
#
# = Actions
# == List actions
#
# actions = HCloud::Firewall.find(1).actions
# # => [#<HCloud::Action id: 1, ...>, ...]
#
# == Sort actions
#
# HCloud::Firewall.find(1).actions.sort(finished: :desc)
# # => [#<HCloud::Action id: 1, ...>, ...]
#
# HCloud::Firewall.find(1).actions.sort(:command, finished: :asc)
# # => [#<HCloud::Actions id: 1, ...>, ...]
#
# == Search actions
#
# HCloud::Firewall.find(1).actions.where(command: "set_rules")
# # => #<HCloud::Action id: 1, ...>
#
# HCloud::Firewall.find(1).actions.where(status: "success")
# # => #<HCloud::Action id: 1, ...>
#
# == Find action by ID
#
# HCloud::Firewall.find(1).actions.find(1)
# # => #<HCloud::Action id: 1, ...>
#
# = Resource-specific actions
# == Apply a firewall to resources
#
# firewall = HCloud::Firewall.find(1)
# server = HCloud::Server.find(1)
#
# firewall.apply_to_resources(apply_to: [{ type: "server", server: { id: server.id } }])
# # => #<HCloud::Action ...>
#
# == Remove a firewall from resources
#
# firewall = HCloud::Firewall.find(1)
# server = HCloud::Server.find(1)
#
# firewall.remove_from_resources(remove_from: [{ type: "server", server: { id: server.id } }])
# # => #<HCloud::Action ...>
#
# == Set firewall rules
#
# firewall = HCloud::Firewall.find(1)
#
# firewall.set_rules(rules: [{ direction: "in", protocol: "tcp", ... }])
# # => #<HCloud::Action ...>
#
class Firewall < Resource
actionable
queryable
creatable
updatable
deletable
labelable
attribute :id, :integer
attribute :name
attribute :applied_to, :applied_to, array: true, default: -> { [] }
# TODO: use only for creation
attribute :apply_to, :apply_to, array: true, default: -> { [] }
attribute :rules, :rule, array: true, default: -> { [] }
action :apply_to_resources
action :remove_from_resources
action :set_rules
def creatable_attributes
[:name, :labels, :apply_to, :rules]
end
def updatable_attributes
[:name, :labels]
end
end
end