Skip to content

Commit

Permalink
add support for reserved ipv6 api
Browse files Browse the repository at this point in the history
  • Loading branch information
vnegi-digitalocean committed Jan 16, 2025
1 parent 2a7e6e1 commit 32ee4dd
Show file tree
Hide file tree
Showing 16 changed files with 897 additions and 0 deletions.
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -585,6 +585,28 @@ Actions supported:
* `client.reserved_ip_actions.assign(ip: reserved_ip.ip, droplet_id: droplet.id)`
* `client.reserved_ip_actions.unassign(ip: reserved_ip.ip)`

## Reserved IPv6 resource

client = DropletKit::Client.new(access_token: 'TOKEN')
client.reserved_ipv6s #=> DropletKit::ReservedIpv6Resource

Actions supported:

* `client.reserved_ipv6s.all()`
* `client.reserved_ipv6s.find(ip: 'ip address')`
* `client.reserved_ipv6s.create(reserved_ipv6)`
* `client.reserved_ipv6s.delete(ip: 'ip address')`

## Reserved IPv6 Action resource

client = DropletKit::Client.new(access_token: 'TOKEN')
client.reserved_ipv6_actions #=> DropletKit::ReservedIpv6ActionResource

Actions supported:

* `client.reserved_ipv6_actions.assign(ip: reserved_ipv6.ip, droplet_id: droplet.id)`
* `client.reserved_ipv6_actions.unassign(ip: reserved_ipv6.ip)`

## Volume resource

client = DropletKit::Client.new(access_token: 'TOKEN')
Expand Down
4 changes: 4 additions & 0 deletions lib/droplet_kit.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ module DropletKit
autoload :DropletUpgrade, 'droplet_kit/models/droplet_upgrade'
autoload :FloatingIp, 'droplet_kit/models/floating_ip'
autoload :ReservedIp, 'droplet_kit/models/reserved_ip'
autoload :ReservedIpv6, 'droplet_kit/models/reserved_ipv6'
autoload :Project, 'droplet_kit/models/project'
autoload :ProjectAssignment, 'droplet_kit/models/project_assignment'
autoload :Links, 'droplet_kit/models/links'
Expand Down Expand Up @@ -155,7 +156,9 @@ module DropletKit
autoload :FloatingIpResource, 'droplet_kit/resources/floating_ip_resource'
autoload :FloatingIpActionResource, 'droplet_kit/resources/floating_ip_action_resource'
autoload :ReservedIpResource, 'droplet_kit/resources/reserved_ip_resource'
autoload :ReservedIpv6Resource, 'droplet_kit/resources/reserved_ipv6_resource'
autoload :ReservedIpActionResource, 'droplet_kit/resources/reserved_ip_action_resource'
autoload :ReservedIpv6ActionResource, 'droplet_kit/resources/reserved_ipv6_action_resource'
autoload :ProjectResource, 'droplet_kit/resources/project_resource'
autoload :TagResource, 'droplet_kit/resources/tag_resource'
autoload :VolumeResource, 'droplet_kit/resources/volume_resource'
Expand Down Expand Up @@ -197,6 +200,7 @@ module DropletKit
autoload :DropletUpgradeMapping, 'droplet_kit/mappings/droplet_upgrade_mapping'
autoload :FloatingIpMapping, 'droplet_kit/mappings/floating_ip_mapping'
autoload :ReservedIpMapping, 'droplet_kit/mappings/reserved_ip_mapping'
autoload :ReservedIpv6Mapping, 'droplet_kit/mappings/reserved_ipv6_mapping'
autoload :ProjectMapping, 'droplet_kit/mappings/project_mapping'
autoload :ProjectAssignmentMapping, 'droplet_kit/mappings/project_assignment_mapping'
autoload :LinksMapping, 'droplet_kit/mappings/links_mapping'
Expand Down
2 changes: 2 additions & 0 deletions lib/droplet_kit/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,9 @@ def self.resources
floating_ips: FloatingIpResource,
floating_ip_actions: FloatingIpActionResource,
reserved_ips: ReservedIpResource,
reserved_ipv6s: ReservedIpv6Resource,
reserved_ip_actions: ReservedIpActionResource,
reserved_ipv6_actions: ReservedIpv6ActionResource,
tags: TagResource,
projects: ProjectResource,
volumes: VolumeResource,
Expand Down
17 changes: 17 additions & 0 deletions lib/droplet_kit/mappings/reserved_ipv6_mapping.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# frozen_string_literal: true

module DropletKit
class ReservedIpv6Mapping
include Kartograph::DSL

kartograph do
mapping ReservedIpv6
root_key plural: 'reserved_ipv6s', singular: 'reserved_ipv6', scopes: [:read]

property :ip, scopes: [:read]
property :region_slug, scopes: [:read, :create]
property :droplet, scopes: [:read], include: DropletMapping
property :reserved_at, scopes: [:read]
end
end
end
19 changes: 19 additions & 0 deletions lib/droplet_kit/models/reserved_ipv6.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# frozen_string_literal: true

module DropletKit
class ReservedIpv6 < BaseModel
attribute :ip
attribute :droplet
attribute :reserved_at
# Used for creates
attribute :region_slug

def identifier
ip
end

def self.from_identifier(identifier)
new(ip: identifier)
end
end
end
20 changes: 20 additions & 0 deletions lib/droplet_kit/resources/reserved_ipv6_action_resource.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# frozen_string_literal: true

module DropletKit
class ReservedIpv6ActionResource < ResourceKit::Resource
resources do
default_handler(422) { |response| ErrorMapping.fail_with(FailedCreate, response.body) }
default_handler(400) { |response| ErrorMapping.fail_with(FailedCreate, response.body) }

action :assign, 'POST /v2/reserved_ipv6/:ip/actions' do
body { |hash| { type: 'assign', droplet_id: hash[:droplet_id] }.to_json }
handler(201, 200) { |response| ActionMapping.extract_single(response.body, :read) }
end

action :unassign, 'POST /v2/reserved_ipv6/:ip/actions' do
body { |hash| { type: 'unassign' }.to_json }
handler(201, 200) { |response| ActionMapping.extract_single(response.body, :read) }
end
end
end
end
32 changes: 32 additions & 0 deletions lib/droplet_kit/resources/reserved_ipv6_resource.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# frozen_string_literal: true
module DropletKit
class ReservedIpv6Resource < ResourceKit::Resource
resources do
action :all, 'GET /v2/reserved_ipv6' do
query_keys :per_page, :page
handler(200) { |response| ReservedIpv6Mapping.extract_collection(response.body, :read) }
end

action :find, 'GET /v2/reserved_ipv6/:ip' do
handler(200) { |response| ReservedIpv6Mapping.extract_single(response.body, :read) }
handler(404) { |response| ErrorMapping.fail_with(Error, response.body) }
end

action :create, 'POST /v2/reserved_ipv6' do
body { |object| ReservedIpv6Mapping.representation_for(:create, object) }
handler(201) { |response| ReservedIpv6Mapping.extract_single(response.body, :read) }
handler(422) { |response| ErrorMapping.fail_with(FailedCreate, response.body) }
end

action :delete, 'DELETE /v2/reserved_ipv6/:ip' do
handler(404) { |response| ErrorMapping.fail_with(FailedDelete, response.body) }
handler(202) { |response| ActionMapping.extract_single(response.body, :read) }
handler(204) { |response| true }
end
end

def all(*args)
PaginatedResource.new(action(:all), self, *args)
end
end
end
Loading

0 comments on commit 32ee4dd

Please sign in to comment.