Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce VPC Peering resource #318

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)`
* `client.vpc_peerings.update(vpc, id: 'id')`
* `client.vpc_peerings.patch(vpc, 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
30 changes: 30 additions & 0 deletions lib/droplet_kit/mappings/vpc_peering_mapping.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# 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
49 changes: 49 additions & 0 deletions lib/droplet_kit/resources/vpc_peering_resource.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# 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| VPCPeeringMapping.representation_for(:create, vpc) }
handler(201) { |response| VPCPeeringMapping.extract_single(response.body, :read) }
brunograsselli marked this conversation as resolved.
Show resolved Hide resolved
handler(422) { |response| ErrorMapping.fail_with(FailedCreate, response.body) }
end

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

action :patch, 'PATCH /v2/vpc_peerings/:id' do
body { |vpc| VPCPeeringMapping.representation_for(:patch, vpc) }
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(204) { |_| true }
brunograsselli marked this conversation as resolved.
Show resolved Hide resolved
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"
}
}
118 changes: 118 additions & 0 deletions spec/lib/droplet_kit/resources/vpc_peering_resource_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe DropletKit::VPCPeeringResource do
subject(:resource) { described_class.new(connection: connection) }

include_context 'resources'

let(:vpc_peering_fixture_path) { 'vpc_peerings/find' }
let(:base_path) { '/v2/vpc_peerings' }
let(:vpc_peering_uuid) { '880b7f98-f062-404d-b33c-458d545696f6' }

RSpec::Matchers.define :match_vpc_peering_fixture do
match do |vpc_peering|
expect(vpc_peering.id).to eq('6e9bff8e-2d65-4301-9a48-f80a25ad89fa')
expect(vpc_peering.name).to eq('my-new-vpc-peering-1')
expect(vpc_peering.vpc_ids).to eq(["880b7f98-f062-404d-b33c-458d545696f6","be76c5b4-c6c4-4fbb-a710-edbfe76c1982"])
expect(vpc_peering.created_at).to eq('2024-04-03T21:48:41.995304079Z')
expect(vpc_peering.status).to eq('ACTIVE')
end
end

describe '#find' do
it 'returns vpc peering' do
stub_do_api(File.join(base_path, vpc_peering_uuid), :get).to_return(body: api_fixture(vpc_peering_fixture_path))
vpc_peering = resource.find(id: vpc_peering_uuid)

expect(vpc_peering).to match_vpc_peering_fixture
end
end

describe '#all' do
let(:vpcs_fixture_path) { 'vpc_peerings/all' }

it 'returns all of the vpc peerings' do
stub_do_api(base_path, :get).to_return(body: api_fixture(vpcs_fixture_path))
vpcs = resource.all

expect(vpcs).to all(be_a(DropletKit::VPCPeering))
expect(vpcs.first).to match_vpc_peering_fixture
end

it_behaves_like 'a paginated index' do
let(:fixture_path) { vpcs_fixture_path }
let(:api_path) { base_path }
end
end

context 'create, update and patch' do
let(:vpc_peering) do
DropletKit::VPCPeering.new(
name: 'example-vpc-peering'
)
end

describe '#create' do
let(:path) { base_path }

it 'returns created vpc peering' do
json_body = DropletKit::VPCPeeringMapping.representation_for(:create, vpc_peering)
stub_do_api(path, :post).with(body: json_body).to_return(body: api_fixture(vpc_peering_fixture_path), status: 201)

expect(resource.create(vpc_peering)).to match_vpc_peering_fixture
end

it_behaves_like 'an action that handles invalid parameters' do
let(:action) { 'create' }
let(:arguments) { DropletKit::VPCPeering.new }
end
end

describe '#update' do
let(:path) { base_path }

it 'returns updated vpc peering' do
json_body = DropletKit::VPCPeeringMapping.representation_for(:update, vpc_peering)
stub_do_api(File.join(base_path, vpc_peering_uuid), :put).with(body: json_body).to_return(body: api_fixture(vpc_peering_fixture_path), status: 200)

expect(resource.update(vpc_peering, id: vpc_peering_uuid)).to match_vpc_peering_fixture
end

it_behaves_like 'an action that handles invalid parameters' do
let(:verb) { :put }
let(:exception) { DropletKit::FailedUpdate }
let(:action) { 'update' }
let(:arguments) { DropletKit::VPCPeering.new }
end
end

describe '#patch' do
let(:path) { base_path }

it 'returns patched vpc peering' do
json_body = DropletKit::VPCPeeringMapping.representation_for(:patch, vpc_peering)
stub_do_api(File.join(base_path, vpc_peering_uuid), :patch).with(body: json_body).to_return(body: api_fixture(vpc_peering_fixture_path), status: 200)

expect(resource.patch(vpc_peering, id: vpc_peering_uuid)).to match_vpc_peering_fixture
end

it_behaves_like 'an action that handles invalid parameters' do
let(:verb) { :patch }
let(:exception) { DropletKit::FailedUpdate }
let(:action) { 'patch' }
let(:arguments) { DropletKit::VPCPeering.new }
end
end
end

describe '#delete' do
it 'sends a delete request for the vpc' do
request = stub_do_api(File.join(base_path, vpc_peering_uuid), :delete)
resource.delete(id: vpc_peering_uuid)

expect(request).to have_been_made
end
end
end
Loading