Skip to content

Commit

Permalink
Introduce VPC Peering resource (#318)
Browse files Browse the repository at this point in the history
* Introduce VPC Peering resource

VPC Peerings is a closed beta feature and not available for public use.

* Fix linter

* Fix vpc peering return codes

* Return vpc peering when deleting

* Remove PUT /vpc_peerings

* Adapt rubocop

* Update .rubocop_todo.yml
  • Loading branch information
brunograsselli authored May 17, 2024
1 parent 50fab3b commit f71bddd
Show file tree
Hide file tree
Showing 12 changed files with 274 additions and 28 deletions.
55 changes: 30 additions & 25 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# This configuration was generated by
# `rubocop --auto-gen-config`
# on 2022-06-23 17:10:56 UTC using RuboCop version 1.30.1.
# on 2024-05-17 08:19:08 UTC using RuboCop version 1.63.5.
# The point is for the user to remove these configuration records
# one by one as the offenses are removed from the code base.
# Note that changes in the inspected code, or installation of new
Expand All @@ -27,37 +27,33 @@ Lint/StructNewOverride:
- 'lib/droplet_kit/models/pagination_information.rb'
- 'spec/lib/droplet_kit/paginated_resource_spec.rb'

# Offense count: 1
Lint/ToEnumArguments:
Exclude:
- 'lib/droplet_kit/utils.rb'

# Offense count: 1
# Configuration parameters: AllowKeywordBlockArguments.
Lint/UnderscorePrefixedVariableName:
Exclude:
- 'spec/lib/droplet_kit/client_spec.rb'

# Offense count: 36
# Offense count: 35
# This cop supports safe autocorrection (--autocorrect).
# Configuration parameters: IgnoreEmptyBlocks, AllowUnusedKeywordArguments.
# Configuration parameters: AutoCorrect, IgnoreEmptyBlocks, AllowUnusedKeywordArguments.
Lint/UnusedBlockArgument:
Enabled: false

# Offense count: 2
# Configuration parameters: CheckForMethodsWithNoSideEffects.
# This cop supports safe autocorrection (--autocorrect).
# Configuration parameters: AutoCorrect, CheckForMethodsWithNoSideEffects.
Lint/Void:
Exclude:
- 'spec/lib/droplet_kit/paginated_resource_spec.rb'

# Offense count: 4
# Configuration parameters: IgnoredMethods, CountRepeatedAttributes.
# Configuration parameters: AllowedMethods, AllowedPatterns, CountRepeatedAttributes.
Metrics/AbcSize:
Max: 184

# Offense count: 16
# Configuration parameters: CountComments, CountAsOne, ExcludedMethods, IgnoredMethods, inherit_mode.
# IgnoredMethods: refine
# Configuration parameters: CountComments, CountAsOne, AllowedMethods, AllowedPatterns, inherit_mode.
# AllowedMethods: refine
Metrics/BlockLength:
Max: 127

Expand All @@ -66,17 +62,18 @@ Metrics/BlockLength:
Metrics/ClassLength:
Max: 133

# Offense count: 4
# Configuration parameters: CountComments, CountAsOne, ExcludedMethods, IgnoredMethods.
# Offense count: 6
# Configuration parameters: CountComments, CountAsOne, AllowedMethods, AllowedPatterns.
Metrics/MethodLength:
Max: 55

# Offense count: 1
# Configuration parameters: CountComments, CountAsOne.
Metrics/ModuleLength:
Max: 164
Max: 167

# Offense count: 1
# This cop supports unsafe autocorrection (--autocorrect-all).
# Configuration parameters: EnforcedStyleForLeadingUnderscores.
# SupportedStylesForLeadingUnderscores: disallowed, required, optional
Naming/MemoizedInstanceVariableName:
Expand All @@ -86,15 +83,15 @@ Naming/MemoizedInstanceVariableName:
# Offense count: 14
# Configuration parameters: EnforcedStyle, CheckMethodNames, CheckSymbols, AllowedIdentifiers, AllowedPatterns.
# SupportedStyles: snake_case, normalcase, non_integer
# AllowedIdentifiers: capture3, iso8601, rfc1123_date, rfc822, rfc2822, rfc3339
# AllowedIdentifiers: capture3, iso8601, rfc1123_date, rfc822, rfc2822, rfc3339, x86_64
Naming/VariableNumber:
Exclude:
- 'spec/lib/droplet_kit/resources/droplet_action_resource_spec.rb'
- 'spec/lib/droplet_kit/resources/firewall_resource_spec.rb'
- 'spec/lib/droplet_kit/resources/load_balancer_resource_spec.rb'

# Offense count: 33
# Configuration parameters: Prefixes.
# Configuration parameters: Prefixes, AllowedPatterns.
# Prefixes: when, with, without
RSpec/ContextWording:
Exclude:
Expand All @@ -113,12 +110,12 @@ RSpec/ContextWording:
- 'spec/lib/droplet_kit/resources/vpc_resource_spec.rb'
- 'spec/support/resource_context.rb'

# Offense count: 114
# Offense count: 120
# Configuration parameters: CountAsOne.
RSpec/ExampleLength:
Max: 43

# Offense count: 36
# Offense count: 37
# Configuration parameters: Include, CustomTransform, IgnoreMethods, SpecSuffixOnly.
# Include: **/*_spec*rb*, **/spec/**/*
RSpec/FilePath:
Expand All @@ -134,7 +131,7 @@ RSpec/LeakyConstantDeclaration:
Exclude:
- 'spec/lib/droplet_kit/client_spec.rb'

# Offense count: 118
# Offense count: 122
RSpec/MultipleExpectations:
Max: 30

Expand All @@ -144,15 +141,23 @@ RSpec/MultipleMemoizedHelpers:
Max: 11

# Offense count: 3
# Configuration parameters: AllowedGroups.
RSpec/NestedGroups:
Max: 4

# Offense count: 58
# Offense count: 37
# Configuration parameters: Include, CustomTransform, IgnoreMethods, IgnoreMetadata.
# Include: **/*_spec.rb
RSpec/SpecFilePathFormat:
Enabled: false

# Offense count: 168
# Configuration parameters: AllowedConstants.
Style/Documentation:
Enabled: false

# Offense count: 1
# This cop supports safe autocorrection (--autocorrect).
# Configuration parameters: MinBodyLength, AllowConsecutiveConditionals.
Style/GuardClause:
Exclude:
Expand All @@ -165,18 +170,18 @@ Style/MissingRespondToMissing:

# Offense count: 5
# This cop supports unsafe autocorrection (--autocorrect-all).
# Configuration parameters: AllowMethodsWithArguments, IgnoredMethods, AllowComments.
# IgnoredMethods: respond_to, define_method
# Configuration parameters: AllowMethodsWithArguments, AllowedMethods, AllowedPatterns, AllowComments.
# AllowedMethods: define_method
Style/SymbolProc:
Exclude:
- 'lib/droplet_kit/resources/container_registry_resource.rb'
- 'lib/droplet_kit/resources/droplet_action_resource.rb'
- 'lib/droplet_kit/resources/kubernetes_cluster_resource.rb'
- 'lib/tasks/resource_doc.rake'

# Offense count: 86
# Offense count: 90
# This cop supports safe autocorrection (--autocorrect).
# Configuration parameters: AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, AllowedPatterns, IgnoredPatterns.
# Configuration parameters: AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, AllowedPatterns.
# URISchemes: http, https
Layout/LineLength:
Max: 221
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -622,6 +622,20 @@ Actions supported:
* `client.vpcs.delete(id: 'id')`
* `client.vpcs.all_members(id: 'id')`

## VPC Peering resource

client = DropletKit::Client.new(access_token: 'TOKEN')
client.vpc_peerings #=> DropletKit::VPCPeeringResource

Actions supported:

* `client.vpc_peerings.find(id: 'id')`
* `client.vpc_peerings.all()`
* `client.vpc_peerings.create(vpc_peering)`
* `client.vpc_peerings.update(vpc_peering, id: 'id')`
* `client.vpc_peerings.patch(vpc_peering, id: 'id')`
* `client.vpc_peerings.delete(id: 'id')`

## Container Registry resource

client = DropletKit::Client.new(access_token: 'TOKEN')
Expand Down
3 changes: 3 additions & 0 deletions lib/droplet_kit.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ module DropletKit
autoload :KubernetesOptions, 'droplet_kit/models/kubernetes_options'
autoload :VPC, 'droplet_kit/models/vpc'
autoload :VPCMember, 'droplet_kit/models/vpc_member'
autoload :VPCPeering, 'droplet_kit/models/vpc_peering'

# Resources
autoload :DropletResource, 'droplet_kit/resources/droplet_resource'
Expand Down Expand Up @@ -104,6 +105,7 @@ module DropletKit
autoload :KubernetesClusterResource, 'droplet_kit/resources/kubernetes_cluster_resource'
autoload :KubernetesOptionsResource, 'droplet_kit/resources/kubernetes_options_resource'
autoload :VPCResource, 'droplet_kit/resources/vpc_resource'
autoload :VPCPeeringResource, 'droplet_kit/resources/vpc_peering_resource'

# JSON Maps
autoload :DropletMapping, 'droplet_kit/mappings/droplet_mapping'
Expand Down Expand Up @@ -168,6 +170,7 @@ module DropletKit
autoload :KubernetesOptionsMapping, 'droplet_kit/mappings/kubernetes_options_mapping'
autoload :VPCMapping, 'droplet_kit/mappings/vpc_mapping'
autoload :VPCMemberMapping, 'droplet_kit/mappings/vpc_member_mapping'
autoload :VPCPeeringMapping, 'droplet_kit/mappings/vpc_peering_mapping'

# Utils
autoload :PaginatedResource, 'droplet_kit/paginated_resource'
Expand Down
3 changes: 2 additions & 1 deletion lib/droplet_kit/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,8 @@ def self.resources
projects: ProjectResource,
volumes: VolumeResource,
volume_actions: VolumeActionResource,
vpcs: VPCResource
vpcs: VPCResource,
vpc_peerings: VPCPeeringResource
}
end

Expand Down
29 changes: 29 additions & 0 deletions lib/droplet_kit/mappings/vpc_peering_mapping.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# frozen_string_literal: true

module DropletKit
class VPCPeeringMapping
include Kartograph::DSL

kartograph do
mapping VPCPeering
root_key plural: 'vpc_peerings', singular: 'vpc_peering', scopes: [:read]

scoped :read do
property :id
property :name
property :vpc_ids
property :created_at
property :status
end

scoped :update, :patch do
property :name
end

scoped :create do
property :name
property :vpc_ids
end
end
end
end
11 changes: 11 additions & 0 deletions lib/droplet_kit/models/vpc_peering.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# frozen_string_literal: true

module DropletKit
class VPCPeering < BaseModel
attribute :id
attribute :name
attribute :vpc_ids
attribute :created_at
attribute :status
end
end
2 changes: 1 addition & 1 deletion lib/droplet_kit/resources/droplet_action_resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class DropletActionResource < ResourceKit::Resource
end

TAG_ACTIONS.each do |action_name|
action "#{action_name}_for_tag".to_sym, 'POST /v2/droplets/actions' do
action :"#{action_name}_for_tag", 'POST /v2/droplets/actions' do
query_keys :tag_name
body { |_| { type: action_name }.to_json }
handler(201, 200) { |response| ActionMapping.extract_collection(response.body, :read) }
Expand Down
43 changes: 43 additions & 0 deletions lib/droplet_kit/resources/vpc_peering_resource.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# frozen_string_literal: true

module DropletKit
class VPCPeeringResource < ResourceKit::Resource
include ErrorHandlingResourcable

resources do
action :find, 'GET /v2/vpc_peerings/:id' do
handler(200) do |response|
VPCPeeringMapping.extract_single(response.body, :read)
end
end

action :all, 'GET /v2/vpc_peerings' do
query_keys :per_page, :page

handler(200) do |response|
VPCPeeringMapping.extract_collection(response.body, :read)
end
end

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

action :patch, 'PATCH /v2/vpc_peerings/:id' do
body { |vpc_peering| VPCPeeringMapping.representation_for(:patch, vpc_peering) }
handler(200) { |response| VPCPeeringMapping.extract_single(response.body, :read) }
handler(422) { |response| ErrorMapping.fail_with(FailedUpdate, response.body) }
end

action :delete, 'DELETE /v2/vpc_peerings/:id' do
handler(202) { |response| VPCPeeringMapping.extract_single(response.body, :read) }
end
end

def all(*args)
PaginatedResource.new(action(:all), self, *args)
end
end
end
29 changes: 29 additions & 0 deletions spec/fixtures/vpc_peerings/all.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"vpc_peerings": [
{
"id":"6e9bff8e-2d65-4301-9a48-f80a25ad89fa",
"name":"my-new-vpc-peering-1",
"vpc_ids":["880b7f98-f062-404d-b33c-458d545696f6","be76c5b4-c6c4-4fbb-a710-edbfe76c1982"],
"created_at":"2024-04-03T21:48:41.995304079Z",
"status":"ACTIVE"
},
{
"id":"cea63fe9-d95a-4bf9-b1ad-a599c67dec73",
"name":"my-new-vpc-peering-1",
"vpc_ids":["4598983b-e0f4-4039-ac6e-07e487e2712b","d4b88c23-528b-462d-8433-8740398a46cf"],
"created_at":"2024-04-03T21:48:45.995304079Z",
"status":"ACTIVE"
}
],
"links": {
"pages": {
"first": "https://api.digitalocean.com/v2/vpc_peerings?page=1&per_page=2",
"last": "https://api.digitalocean.com/v2/vpc_peerings?page=4&per_page=2",
"next": "https://api.digitalocean.com/v2/vpc_peerings?page=3&per_page=2",
"prev": "https://api.digitalocean.com/v2/vpc_peerings?page=1&per_page=2"
}
},
"meta": {
"total": 10
}
}
10 changes: 10 additions & 0 deletions spec/fixtures/vpc_peerings/find.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"vpc_peering":
{
"id":"6e9bff8e-2d65-4301-9a48-f80a25ad89fa",
"name":"my-new-vpc-peering-1",
"vpc_ids":["880b7f98-f062-404d-b33c-458d545696f6","be76c5b4-c6c4-4fbb-a710-edbfe76c1982"],
"created_at":"2024-04-03T21:48:41.995304079Z",
"status":"ACTIVE"
}
}
2 changes: 1 addition & 1 deletion spec/lib/droplet_kit/resources/droplet_resource_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def check_droplet(droplet, tags = [], overrides = {})
status: 'active'
}.merge(overrides)

attrs.each do |attr, val|
attrs.each_key do |attr|
expect(droplet.send(attr)).to eq attrs[attr]
end

Expand Down
Loading

0 comments on commit f71bddd

Please sign in to comment.