diff --git a/.devcontainer/Dockerfile b/.devcontainer/Dockerfile new file mode 100644 index 0000000..d35e1a3 --- /dev/null +++ b/.devcontainer/Dockerfile @@ -0,0 +1,15 @@ +ARG RUBY_VERSION=3.2 +FROM ruby:${RUBY_VERSION} + +RUN apt-get update && export DEBIAN_FRONTEND=noninteractive \ + # Remove imagemagick due to https://security-tracker.debian.org/tracker/CVE-2019-10131 + && apt-get purge -y imagemagick imagemagick-6-common + +# Install the Dependencies +# we don't install git here as it's installed in the devcontainer.json +RUN apt-get update && export DEBIAN_FRONTEND=noninteractive \ + && apt-get install -y --no-install-recommends \ + # build-essential is required for native extensions + build-essential \ + ruby-dev \ + && rm -rf /var/lib/apt/lists/* diff --git a/.devcontainer/devcontainer.dist.env b/.devcontainer/devcontainer.dist.env new file mode 100644 index 0000000..99c69da --- /dev/null +++ b/.devcontainer/devcontainer.dist.env @@ -0,0 +1,2 @@ +# If you'd like to pass environment variables to the devcontainer, +# Copy this file to ./devcontainer.env, then add them below. diff --git a/.devcontainer/devcontainer.json b/.devcontainer/devcontainer.json new file mode 100644 index 0000000..6fb91d3 --- /dev/null +++ b/.devcontainer/devcontainer.json @@ -0,0 +1,43 @@ +{ + "name": "Netbox Client Ruby", + "build": { + "dockerfile": "./Dockerfile", + "context": "..", + "args": { + "RUBY_VERSION": "${localEnv:RUBY_VERSION:3.2.5}" + } + }, + "runArgs": [ + "--env-file", "${localWorkspaceFolder}/.devcontainer/devcontainer.env" + ], + "features": { + "ghcr.io/devcontainers/features/common-utils:2": { + "installZsh": "true", + "username": "vscode", + "userUid": "1000", + "userGid": "1000", + "upgradePackages": "true" + }, + "ghcr.io/devcontainers/features/ruby:1": "none", + "ghcr.io/devcontainers/features/node:1": "none", + "ghcr.io/devcontainers/features/git:1": { + "version": "latest", + "ppa": "false" + } + }, + "customizations": { + "vscode": { + "extensions": [ + "github.vscode-github-actions", + "github.vscode-pull-request-github", + // a language server + "shopify.ruby-lsp" + ] + } + }, + "remoteUser": "vscode", + // we need to sudo bundle install otherwise the prism gem won't compile and install. + "postCreateCommand": "sudo bin/setup", + // make sure we're updated each time we start. + "postStartCommand": "bin/setup" +} diff --git a/.gitignore b/.gitignore index 3dc4645..ddbe5d6 100644 --- a/.gitignore +++ b/.gitignore @@ -19,3 +19,4 @@ Gemfile.lock # Ignore MacOS files .DS_Store +.devcontainer/devcontainer.env diff --git a/.vscode/extensions.json b/.vscode/extensions.json new file mode 100644 index 0000000..aefa206 --- /dev/null +++ b/.vscode/extensions.json @@ -0,0 +1,15 @@ +{ + // See https://go.microsoft.com/fwlink/?LinkId=827846 to learn about workspace recommendations. + // Extension identifier format: ${publisher}.${name}. Example: vscode.csharp + + // List of extensions which should be recommended for users of this workspace. + "recommendations": [ + "github.vscode-github-actions", + "github.vscode-pull-request-github", + "shopify.ruby-lsp", + ], + // List of extensions recommended by VS Code that should not be recommended for users of this workspace. + "unwantedRecommendations": [ + + ] +} diff --git a/README.md b/README.md index 8d03341..8e186a8 100644 --- a/README.md +++ b/README.md @@ -3,6 +3,7 @@ [![Build Status](https://travis-ci.org/ninech/netbox-client-ruby.svg?branch=master)](https://travis-ci.org/ninech/netbox-client-ruby) [![Gem Version](https://badge.fury.io/rb/netbox-client-ruby.svg)](https://badge.fury.io/rb/netbox-client-ruby) [![Code Climate](https://codeclimate.com/github/ninech/netbox-client-ruby/badges/gpa.svg)](https://codeclimate.com/github/ninech/netbox-client-ruby) +[![Open in Dev Containers](https://img.shields.io/static/v1?label=Dev%20Containers&message=Open&color=blue)](https://vscode.dev/redirect?url=vscode://ms-vscode-remote.remote-containers/cloneInVolume?url=https://github.com/ninech/netbox-client-ruby) This is a gem to pragmatically access your [Netbox instance](https://github.com/digitalocean/netbox) via it's API from Ruby. This gem is currently only compatible with Netbox v2.4 or newer. @@ -236,6 +237,24 @@ docker-compose exec postgres pg_dump -U netbox --exclude-table-data=extras_objec (Remove `--exclude-table-data=extras_objectchange` from the command if you want to retain the history!) +### Dev Containers + +If you'd like to use a [Dev Container](https://code.visualstudio.com/docs/devcontainers/create-dev-container) +development environment in VS Code, the configuration files for the development environment has been provided +in the `./.devcontainer` folder. + +By default, the Dockerfile/devcontainer uses the `ruby:3.2` image. If you want to use a different version: + +- Update the value for `build.args.RUBY_VERSION` from `${localEnv:RUBY_VERSION:3.2.5}` to the actual version number +(which is just an image tag) +- Or set the local/host environment variable `RUBY_VERSION` before starting/restarting VS Code. + See: [Variables in devcontainer.json](https://containers.dev/implementors/json_reference/#variables-in-devcontainerjson) for more details. + +If you would like to pass environment variables into the Dev Container, +copy the `.devcontainer/devcontainer.dist.env` to `.devcontainer/devcontainer.env` and put them in there. + +For more details and prerequisite, see: + ## Contributing Bug reports and pull requests are very welcome [on GitHub](https://github.com/ninech/netbox-client-ruby). diff --git a/bin/test b/bin/test new file mode 100644 index 0000000..69367b6 --- /dev/null +++ b/bin/test @@ -0,0 +1,4 @@ +#!/usr/bin/env bash +set -euo pipefail + +bundle exec rake spec diff --git a/gemfiles/.bundle/config b/gemfiles/.bundle/config new file mode 100644 index 0000000..c127f80 --- /dev/null +++ b/gemfiles/.bundle/config @@ -0,0 +1,2 @@ +--- +BUNDLE_RETRY: "1" diff --git a/lib/netbox_client_ruby/api/tenancy.rb b/lib/netbox_client_ruby/api/tenancy.rb index 03e8a0d..708de6d 100644 --- a/lib/netbox_client_ruby/api/tenancy.rb +++ b/lib/netbox_client_ruby/api/tenancy.rb @@ -4,7 +4,7 @@ module NetboxClientRuby module Tenancy { tenants: Tenants, - tenant_groups: TenantGroups, + tenant_groups: TenantGroups contacts: Contacts, contact_groups: ContactGroups }.each_pair do |method_name, class_name| @@ -14,7 +14,7 @@ module Tenancy { tenant: Tenant, - tenant_group: TenantGroup, + tenant_group: TenantGroup contact: Contact, contact_group: ContactGroup }.each_pair do |method_name, class_name| diff --git a/netbox-client-ruby-0.10.3.gem b/netbox-client-ruby-0.10.3.gem new file mode 100644 index 0000000..c506907 Binary files /dev/null and b/netbox-client-ruby-0.10.3.gem differ diff --git a/spec/fixtures/tenancy/contact-groups.json b/spec/fixtures/tenancy/contact-groups.json index 86c2934..01944ba 100644 --- a/spec/fixtures/tenancy/contact-groups.json +++ b/spec/fixtures/tenancy/contact-groups.json @@ -5,24 +5,24 @@ "results": [ { "id": 1, - "display": "Parent Customers", - "name": "Parent Customers", - "slug": "parent-customers", + "display": "Customers", + "name": "Customers", + "slug": "customers", "description": "Parent Contact contact group", "parent": null }, { "id": 2, - "display": "Child Customers", - "name": "Child Customers", - "slug": "child-customers", + "display": "East Coast Customers", + "name": "East Coast Customers", + "slug": "east-coast-customers", "description": "Child contact group", "parent": { "id": 1, "url": "http://localhost/api/tenancy/contact_groups/1/", - "display": "Parent Customers", - "name": "Parent Customers", - "slug": "parent-customers", + "display": "Customers", + "name": "Customers", + "slug": "customers", "description": "Parent Contact contact group" } } diff --git a/spec/fixtures/tenancy/contact_3.json b/spec/fixtures/tenancy/contact_3.json index 4ee6242..5cbb39d 100644 --- a/spec/fixtures/tenancy/contact_3.json +++ b/spec/fixtures/tenancy/contact_3.json @@ -3,12 +3,10 @@ "name": "tenant3", "email": "contact3@customer.test", "group": { - "id": 2, - "url": "http://localhost/api/tenancy/contact_groups/2/", - "display": "Child Customers", - "name": "Child Customers", - "slug": "child-customers", - "description": "Child contact group" + "id": 1, + "url": "http://localhost/api/tenancy/contact-groups/1/", + "name": "Customer", + "slug": "customer" }, "description": "", "comments": "", diff --git a/spec/fixtures/tenancy/contacts.json b/spec/fixtures/tenancy/contacts.json index 7961618..a74e005 100644 --- a/spec/fixtures/tenancy/contacts.json +++ b/spec/fixtures/tenancy/contacts.json @@ -8,12 +8,10 @@ "name": "contact3", "email": "contact3@customer.test", "group": { - "id": 2, - "url": "http://localhost/api/tenancy/contact_groups/2/", - "display": "Child Customers", - "name": "Child Customers", - "slug": "child-customers", - "description": "Child contact group" + "id": 1, + "url": "http://localhost/api/tenancy/contact-groups/1/", + "name": "Customer", + "slug": "customer" }, "description": "", "comments": "", diff --git a/spec/netbox_client_ruby/api/dcim/site_spec.rb b/spec/netbox_client_ruby/api/dcim/site_spec.rb index 18805ec..4232a60 100644 --- a/spec/netbox_client_ruby/api/dcim/site_spec.rb +++ b/spec/netbox_client_ruby/api/dcim/site_spec.rb @@ -3,11 +3,12 @@ require 'spec_helper' RSpec.describe NetboxClientRuby::DCIM::Site, faraday_stub: true do + let(:class_under_test) { NetboxClientRuby::DCIM::Site } let(:entity_id) { 1 } let(:response) { File.read("spec/fixtures/dcim/site_#{entity_id}.json") } let(:request_url) { "/api/dcim/sites/#{entity_id}/" } - subject { described_class.new entity_id } + subject { class_under_test.new entity_id } describe '#id' do it 'shall be the expected id' do @@ -55,6 +56,24 @@ end end + { + region: NetboxClientRuby::DCIM::Region, + tenant: NetboxClientRuby::Tenancy::Tenant, + status: Symbol + }.each_pair do |method_name, expected_type| + describe ".#{method_name}" do + it 'should fetch the data' do + expect(faraday).to receive(:get).and_call_original + + expect(subject.public_send(method_name)).to_not be_nil + end + + it 'shall return the expected type' do + expect(subject.public_send(method_name)).to be_a(expected_type) + end + end + end + describe '.delete' do let(:request_method) { :delete } let(:response_status) { 204 } @@ -94,7 +113,7 @@ let(:request_method) { :patch } subject do - entity = described_class.new entity_id + entity = class_under_test.new entity_id entity.name = name entity.slug = slug entity @@ -128,7 +147,7 @@ let(:request_url) { '/api/dcim/sites/' } subject do - entity = described_class.new + entity = class_under_test.new entity.name = name entity.slug = slug entity diff --git a/spec/netbox_client_ruby/api/tenancy/contact_spec.rb b/spec/netbox_client_ruby/api/tenancy/contact_spec.rb index 340ccd8..026c805 100644 --- a/spec/netbox_client_ruby/api/tenancy/contact_spec.rb +++ b/spec/netbox_client_ruby/api/tenancy/contact_spec.rb @@ -39,7 +39,7 @@ it 'should be a ContactGroup object' do contact_group = subject.group expect(contact_group).to be_a NetboxClientRuby::Tenancy::ContactGroup - expect(contact_group.id).to eq(2) + expect(contact_group.id).to eq(1) end end end diff --git a/spec/netbox_client_ruby/api/tenancy_spec.rb b/spec/netbox_client_ruby/api/tenancy_spec.rb index 171b457..8c6e97c 100644 --- a/spec/netbox_client_ruby/api/tenancy_spec.rb +++ b/spec/netbox_client_ruby/api/tenancy_spec.rb @@ -6,7 +6,7 @@ { tenant_groups: NetboxClientRuby::Tenancy::TenantGroups, tenants: NetboxClientRuby::Tenancy::Tenants, - contact_groups: NetboxClientRuby::Tenancy::ContactGroups, + contact_groups: NetboxClientRuby::Tenancy::ContacttGroups, contacts: NetboxClientRuby::Tenancy::Contacts }.each do |method, klass| describe ".#{method}" do