Skip to content

Commit

Permalink
Merge pull request #114 from viola/add-lb-and-cert-resource
Browse files Browse the repository at this point in the history
Add Certificate and LoadBalancer resources.
  • Loading branch information
viola authored Feb 27, 2017
2 parents cca60ca + 781ba02 commit 4d61b76
Show file tree
Hide file tree
Showing 23 changed files with 820 additions and 3 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@ tmp
*.o
*.a
mkmf.log
vendor
34 changes: 34 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,21 @@ droplet = client.droplets.find(id: 123)

# All Resources and actions.

## Certificate resource

```ruby
client = DropletKit::Client.new(access_token: 'TOKEN')
client.certificates #=> DropletKit::CertificateResource
```

Actions supported:

* `client.certificates.find(id: 'id')`
* `client.certificates.all()`
* `client.certificates.create(certificate)`
* `client.certificates.delete(id: 'id')`


## Droplet resource

```ruby
Expand Down Expand Up @@ -189,6 +204,25 @@ Image Actions Supported:
* `client.image_actions.transfer(image_id: 123, region: 'nyc3')`


## Load balancer resource
```ruby
client = DropletKit::Client.new(access_token: 'TOKEN')
client.load_balancers #=> DropletKit::LoadBalancerResource
```

Actions supported:

* `client.load_balancers.find(id: 'id')`
* `client.load_balancers.all()`
* `client.load_balancers.create(load_balancer)`
* `client.load_balancers.update(load_balancer, id: 'id')`
* `client.load_balancers.delete(id: 'id')`
* `client.load_balancers.add_droplets([droplet.id], id: 'id')`
* `client.load_balancers.remove_droplets([droplet.id], id: 'id')`
* `client.load_balancers.add_forwarding_rules([forwarding_rule], id: 'id')`
* `client.load_balancers.remove_forwarding_rules([forwarding_rule], id: 'id')`


## Region resource

```ruby
Expand Down
12 changes: 12 additions & 0 deletions lib/droplet_kit.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ module DropletKit
autoload :TaggedResources, 'droplet_kit/models/tagged_resources'
autoload :TaggedDropletsResources, 'droplet_kit/models/tagged_droplets_resources'
autoload :Volume, 'droplet_kit/models/volume'
autoload :LoadBalancer, 'droplet_kit/models/load_balancer'
autoload :StickySession, 'droplet_kit/models/sticky_session'
autoload :HealthCheck, 'droplet_kit/models/health_check'
autoload :ForwardingRule, 'droplet_kit/models/forwarding_rule'
autoload :Certificate, 'droplet_kit/models/certificate'

# Resources
autoload :DropletResource, 'droplet_kit/resources/droplet_resource'
Expand All @@ -48,6 +53,8 @@ module DropletKit
autoload :VolumeResource, 'droplet_kit/resources/volume_resource'
autoload :VolumeActionResource, 'droplet_kit/resources/volume_action_resource'
autoload :SnapshotResource, 'droplet_kit/resources/snapshot_resource'
autoload :LoadBalancerResource, 'droplet_kit/resources/load_balancer_resource'
autoload :CertificateResource, 'droplet_kit/resources/certificate_resource'

# JSON Maps
autoload :DropletMapping, 'droplet_kit/mappings/droplet_mapping'
Expand All @@ -71,6 +78,11 @@ module DropletKit
autoload :TaggedResourcesMapping, 'droplet_kit/mappings/tagged_resources_mapping'
autoload :TaggedDropletsResourcesMapping, 'droplet_kit/mappings/tagged_droplets_resources_mapping'
autoload :VolumeMapping, 'droplet_kit/mappings/volume_mapping'
autoload :LoadBalancerMapping, 'droplet_kit/mappings/load_balancer_mapping'
autoload :StickySessionMapping, 'droplet_kit/mappings/sticky_session_mapping'
autoload :HealthCheckMapping, 'droplet_kit/mappings/health_check_mapping'
autoload :ForwardingRuleMapping, 'droplet_kit/mappings/forwarding_rule_mapping'
autoload :CertificateMapping, 'droplet_kit/mappings/certificate_mapping'

# Utils
autoload :PaginatedResource, 'droplet_kit/paginated_resource'
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 @@ -19,12 +19,14 @@ def connection
def self.resources
{
actions: ActionResource,
certificates: CertificateResource,
droplets: DropletResource,
domains: DomainResource,
domain_records: DomainRecordResource,
droplet_actions: DropletActionResource,
images: ImageResource,
image_actions: ImageActionResource,
load_balancers: LoadBalancerResource,
regions: RegionResource,
sizes: SizeResource,
ssh_keys: SSHKeyResource,
Expand Down
25 changes: 25 additions & 0 deletions lib/droplet_kit/mappings/certificate_mapping.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
module DropletKit
class CertificateMapping
include Kartograph::DSL

kartograph do
mapping Certificate
root_key singular: 'certificate', plural: 'certificates', scopes: [:read]

scoped :read do
property :id
property :name
property :not_after
property :sha1_fingerprint
property :created_at
end

scoped :create do
property :name
property :private_key
property :leaf_certificate
property :certificate_chain
end
end
end
end
19 changes: 19 additions & 0 deletions lib/droplet_kit/mappings/forwarding_rule_mapping.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
module DropletKit
class ForwardingRuleMapping
include Kartograph::DSL

kartograph do
root_key plural: 'forwarding_rules', scopes: [:create, :update]
mapping ForwardingRule

scoped :read, :create, :update do
property :entry_protocol
property :entry_port
property :target_protocol
property :target_port
property :certificate_id
property :tls_passthrough
end
end
end
end
19 changes: 19 additions & 0 deletions lib/droplet_kit/mappings/health_check_mapping.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
module DropletKit
class HealthCheckMapping
include Kartograph::DSL

kartograph do
mapping HealthCheck

scoped :read, :create, :update do
property :protocol
property :port
property :path
property :check_interval_seconds
property :response_timeout_seconds
property :healthy_threshold
property :unhealthy_threshold
end
end
end
end
33 changes: 33 additions & 0 deletions lib/droplet_kit/mappings/load_balancer_mapping.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
module DropletKit
class LoadBalancerMapping
include Kartograph::DSL

kartograph do
mapping LoadBalancer
root_key singular: 'load_balancer', plural: 'load_balancers', scopes: [:read]

scoped :read do
property :id
property :ip
property :status
property :created_at
property :region, include: RegionMapping
end

scoped :read, :update, :create do
property :name
property :algorithm
property :tag
property :redirect_http_to_https
property :droplet_ids
property :sticky_sessions, include: StickySessionMapping
property :health_check, include: HealthCheckMapping
property :forwarding_rules, plural: true, include: ForwardingRuleMapping
end

scoped :update, :create do
property :region
end
end
end
end
15 changes: 15 additions & 0 deletions lib/droplet_kit/mappings/sticky_session_mapping.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module DropletKit
class StickySessionMapping
include Kartograph::DSL

kartograph do
mapping StickySession

scoped :read, :create, :update do
property :type
property :cookie_name
property :cookie_ttl_seconds
end
end
end
end
12 changes: 12 additions & 0 deletions lib/droplet_kit/models/certificate.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module DropletKit
class Certificate < BaseModel
attribute :id
attribute :name
attribute :not_after
attribute :sha1_fingerprint
attribute :created_at
attribute :private_key
attribute :leaf_certificate
attribute :certificate_chain
end
end
10 changes: 10 additions & 0 deletions lib/droplet_kit/models/forwarding_rule.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module DropletKit
class ForwardingRule < BaseModel
attribute :entry_protocol
attribute :entry_port
attribute :target_protocol
attribute :target_port
attribute :certificate_id
attribute :tls_passthrough
end
end
11 changes: 11 additions & 0 deletions lib/droplet_kit/models/health_check.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module DropletKit
class HealthCheck < BaseModel
attribute :protocol
attribute :port
attribute :path
attribute :check_interval_seconds
attribute :response_timeout_seconds
attribute :healthy_threshold
attribute :unhealthy_threshold
end
end
17 changes: 17 additions & 0 deletions lib/droplet_kit/models/load_balancer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
module DropletKit
class LoadBalancer < BaseModel
attribute :id
attribute :ip
attribute :name
attribute :algorithm
attribute :status
attribute :created_at
attribute :tag
attribute :region
attribute :redirect_http_to_https
attribute :droplet_ids
attribute :sticky_sessions
attribute :health_check
attribute :forwarding_rules
end
end
7 changes: 7 additions & 0 deletions lib/droplet_kit/models/sticky_session.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module DropletKit
class StickySession < BaseModel
attribute :type
attribute :cookie_name
attribute :cookie_ttl_seconds
end
end
30 changes: 30 additions & 0 deletions lib/droplet_kit/resources/certificate_resource.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
module DropletKit
class CertificateResource < ResourceKit::Resource
include ErrorHandlingResourcable

resources do
action :find, 'GET /v2/certificates/:id' do
handler(200) { |response| CertificateMapping.extract_single(response.body, :read) }
end

action :all, 'GET /v2/certificates' do
query_keys :per_page, :page
handler(200) { |response| CertificateMapping.extract_collection(response.body, :read) }
end

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

action :delete, 'DELETE /v2/certificates/:id' do
handler(204) { |_| true }
end
end

def all(*args)
PaginatedResource.new(action(:all), self, *args)
end
end
end
56 changes: 56 additions & 0 deletions lib/droplet_kit/resources/load_balancer_resource.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
module DropletKit
class LoadBalancerResource < ResourceKit::Resource
include ErrorHandlingResourcable

resources do
action :find, 'GET /v2/load_balancers/:id' do
handler(200) { |response| LoadBalancerMapping.extract_single(response.body, :read) }
end

action :all, 'GET /v2/load_balancers' do
query_keys :per_page, :page
handler(200) { |response| LoadBalancerMapping.extract_collection(response.body, :read) }
end

action :create, 'POST /v2/load_balancers' do
body { |load_balancer| LoadBalancerMapping.representation_for(:create, load_balancer) }
handler(202) { |response| LoadBalancerMapping.extract_single(response.body, :read) }
handler(422) { |response| ErrorMapping.fail_with(FailedCreate, response.body) }
end

action :update, 'PUT /v2/load_balancers/:id' do
body {|load_balancer| LoadBalancerMapping.representation_for(:update, load_balancer) }
handler(200) { |response| LoadBalancerMapping.extract_single(response.body, :read) }
handler(422) { |response| ErrorMapping.fail_with(FailedUpdate, response.body) }
end

action :delete, 'DELETE /v2/load_balancers/:id' do
handler(204) { |_| true }
end

action :add_droplets, 'POST /v2/load_balancers/:id/droplets' do
body { |droplet_ids| { droplet_ids: droplet_ids }.to_json }
handler(204) { |_| true }
end

action :remove_droplets, 'DELETE /v2/load_balancers/:id/droplets' do
body { |droplet_ids| { droplet_ids: droplet_ids }.to_json }
handler(204) { |_| true }
end

action :add_forwarding_rules, 'POST /v2/load_balancers/:id/forwarding_rules' do
body { |forwarding_rules| ForwardingRuleMapping.represent_collection_for(:create, forwarding_rules) }
handler(204) { |_| true }
end

action :remove_forwarding_rules, 'DELETE /v2/load_balancers/:id/forwarding_rules' do
body { |forwarding_rules| ForwardingRuleMapping.represent_collection_for(:update, forwarding_rules) }
handler(204) { |_| true }
end
end

def all(*args)
PaginatedResource.new(action(:all), self, *args)
end
end
end
16 changes: 16 additions & 0 deletions spec/fixtures/certificates/all.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"certificates": [
{
"id": "892071a0-bb95-49bc-8021-3afd67a210bf",
"name": "web-cert-01",
"not_after": "2017-02-22T00:23:00Z",
"sha1_fingerprint": "dfcc9f57d86bf58e321c2c6c31c7a971be244ac7",
"created_at": "2017-02-08T16:02:37Z"
}
],
"links": {
},
"meta": {
"total": 1
}
}
9 changes: 9 additions & 0 deletions spec/fixtures/certificates/find.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"certificate": {
"id": "892071a0-bb95-49bc-8021-3afd67a210bf",
"name": "web-cert-01",
"not_after": "2017-02-22T00:23:00Z",
"sha1_fingerprint": "dfcc9f57d86bf58e321c2c6c31c7a971be244ac7",
"created_at": "2017-02-08T16:02:37Z"
}
}
Loading

0 comments on commit 4d61b76

Please sign in to comment.