/
primary_ip.rb
150 lines (137 loc) · 3.79 KB
/
primary_ip.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# frozen_string_literal: true
module HCloud
##
# Represents a primary IP
#
# == List all primary IPs
#
# HCloud::PrimaryIP.all
# # => [#<HCloud::PrimaryIP id: 1, ...>, ...]
#
# == Sort primary IPs
#
# HCloud::PrimaryIP.sort(id: :asc)
# # => [#<HCloud::PrimaryIP id: 1, ...>, ...]
#
# HCloud::PrimaryIP.sort(:id, created: :asc)
# # => [#<HCloud::PrimaryIP id: 1, ...>, ...]
#
# == Search primary IPs
#
# HCloud::PrimaryIP.where(name: "my_primary_ip")
# # => #<HCloud::PrimaryIP id: 1, ...>
#
# HCloud::PrimaryIP.where(ip: "1.2.3.4")
# # => #<HCloud::PrimaryIP id: 1, ...>
#
# HCloud::PrimaryIP.where(label_selector: { environment: "production" })
# # => #<HCloud::PrimaryIP id: 1, ...>
#
# == Find primary IP by ID
#
# HCloud::PrimaryIP.find(1)
# # => #<HCloud::PrimaryIP id: 1, ...>
#
# == Create primary IP
#
# primary IP = HCloud::PrimaryIP.new(name: "my_primary_ip", type: "ipv4", datacenter: "fsn1-dc8")
# primary IP.create
# primary IP.created?
# # => true
#
# firewall = HCloud::PrimaryIP.create(name: "my_primary_ip")
# # => #<HCloud::PrimaryIP id: 1, ...>
#
# == Update primary IP
#
# primary IP = HCloud::PrimaryIP.find(1)
# primary IP.name = "another_primary_ip"
# primary IP.update
#
# == Delete primary IP
#
# primary IP = HCloud::PrimaryIP.find(1)
# primary IP.delete
# primary IP.deleted?
# # => true
#
# = Actions
# == List actions
#
# actions = HCloud::PrimaryIP.find(1).actions
# # => [#<HCloud::Action id: 1, ...>, ...]
#
# == Sort actions
#
# HCloud::PrimaryIP.find(1).actions.sort(finished: :desc)
# # => [#<HCloud::Action id: 1, ...>, ...]
#
# HCloud::PrimaryIP.find(1).actions.sort(:command, finished: :asc)
# # => [#<HCloud::Actions id: 1, ...>, ...]
#
# == Search actions
#
# HCloud::PrimaryIP.find(1).actions.where(command: "assign_primary_ip")
# # => #<HCloud::Action id: 1, ...>
#
# HCloud::PrimaryIP.find(1).actions.where(status: "success")
# # => #<HCloud::Action id: 1, ...>
#
# == Find action by ID
#
# HCloud::PrimaryIP.find(1).actions.find(1)
# # => #<HCloud::Action id: 1, ...>
#
# = Resource-specific actions
# == Assign a primary IP to a server
#
# HCloud::PrimaryIP.find(1).assign(assignee_id: 1, assignee_type: "server")
# # => #<HCloud::Action id: 1, ...>
#
# == Unassign a primary IP from a server
#
# HCloud::PrimaryIP.find(1).unassign
# # => #<HCloud::Action id: 1, ...>
#
# == Change reverse DNS entry
#
# HCloud::PrimaryIP.find(1).change_dns_ptr(dns_ptr: "server.example.com", ip: "1.2.3.4")
# # => #<HCloud::Action id: 1, ...>
#
# == Change protection
#
# HCloud::PrimaryIP.find(1).change_protection(delete: true)
# # => #<HCloud::Action id: 1, ...>
#
class PrimaryIP < Resource
actionable
queryable
creatable
updatable
deletable
labelable
attribute :id, :integer
attribute :name
attribute :type
attribute :ip
attribute :dns_ptr, :dns_pointer, array: true, default: -> { [] }
attribute :assignee_id
attribute :assignee_type, default: "server"
attribute :auto_delete, :boolean
attribute :blocked, :boolean
attribute :datacenter, :datacenter
attribute :protection, :protection
alias auto_delete? auto_delete
alias blocked? blocked
action :assign
action :unassign
action :change_dns_ptr
action :change_protection
def creatable_attributes
[:name, :type, :assignee_id, :assignee_type, :auto_delete, :labels, datacenter: [:id, :name]]
end
def updatable_attributes
[:name, :auto_delete, :labels]
end
end
end