-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #186 from alces-flight/feature/network-devices
Add representation of network devices
- Loading branch information
Showing
25 changed files
with
458 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
class Device::NetworkDetails < Device::Details | ||
|
||
validate :device_uses_network_template | ||
|
||
private | ||
|
||
def device_uses_network_template | ||
reload_device | ||
return unless device.present? | ||
unless device.template.tag == 'network' | ||
self.errors.add(:device, 'must use the `network` template if it has a Device::NetworkDetails') | ||
end | ||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
class Device::NetworkDetailsPresenter < Device::DetailsPresenter | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
object @details | ||
|
||
attributes :public_ips, :private_ips, :ssh_key, :login_user, :volume_details |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
object @details | ||
|
||
attributes :admin_state_up, :dns_domain, :l2_adjacency, :mtu, | ||
:port_security_enabled, :qos_policy, :shared |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
db/migrate/20240214151411_create_device_network_details.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
class CreateDeviceNetworkDetails < ActiveRecord::Migration[7.1] | ||
def change | ||
create_table :device_network_details, id: :uuid, default: -> { "gen_random_uuid()" }, force: :cascade do |t| | ||
t.boolean :admin_state_up | ||
t.string :dns_domain | ||
t.boolean :l2_adjacency | ||
t.integer :mtu | ||
t.boolean :port_security_enabled | ||
t.boolean :shared | ||
t.string :qos_policy | ||
|
||
t.timestamps | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
class AddTagToTemplates < ActiveRecord::Migration[7.1] | ||
def change | ||
add_column :templates, :tag, :string, null: true | ||
add_index :templates, :tag, unique: true | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
class SeedNetworkDeviceTemplate < ActiveRecord::Migration[7.1] | ||
def change | ||
reversible do |dir| | ||
|
||
dir.up do | ||
t = Template.new( | ||
name: 'network', | ||
template_type: 'Device', | ||
tag: 'network', | ||
version: 1, | ||
height: 1, | ||
depth: 2, | ||
rows: 1, | ||
columns: 1, | ||
rackable: 'rackable', | ||
simple: true, | ||
description: 'Network', | ||
images: { | ||
'front' => 'switch_front_1u.png', | ||
'rear' => 'switch_rear_1u.png', | ||
} | ||
) | ||
t.save! | ||
end | ||
|
||
dir.down do | ||
Template.find_by_tag('network')&.destroy! | ||
end | ||
|
||
end | ||
end | ||
end |
26 changes: 26 additions & 0 deletions
26
db/migrate/20240219161225_migrate_default_rack_template_to_tag.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
class MigrateDefaultRackTemplateToTag < ActiveRecord::Migration[7.1] | ||
|
||
class Template < ApplicationRecord; end | ||
|
||
def change | ||
reversible do |dir| | ||
dir.up do | ||
# Intentionally not using Template.default_rack_template here! | ||
rack = Template.find_by(default_rack_template: true) | ||
rack.tag = 'rack' | ||
rack.save! | ||
end | ||
|
||
dir.down do | ||
rack = Template.find_by_tag('rack') | ||
rack.default_rack_template = true | ||
rack.tag = nil | ||
rack.save! | ||
end | ||
end | ||
|
||
# Remove index separately so that db:rollback can recreate it | ||
remove_index :templates, :default_rack_template, unique: true, where: "default_rack_template = true" | ||
remove_column :templates, :default_rack_template, :boolean, default: false, null: false | ||
end | ||
end |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
#!/bin/bash | ||
|
||
set -e | ||
set -o pipefail | ||
# set -x | ||
|
||
SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd ) | ||
|
||
# The base URL against which relative URLs are constructed. | ||
CONCERTIM_HOST=${CONCERTIM_HOST:-command.concertim.alces-flight.com} | ||
BASE_URL="https://${CONCERTIM_HOST}/api/v1" | ||
|
||
# Use the specified AUTH_TOKEN or generate one. If AUTH_TOKEN is being | ||
# generated LOGIN and PASSWORD environment variables must be set. | ||
AUTH_TOKEN=${AUTH_TOKEN:-$("${SCRIPT_DIR}"/get-auth-token.sh)} | ||
|
||
NAME=${1} | ||
RACK_ID=${2} | ||
FACING=${3} | ||
START_U=${4} | ||
|
||
NETWORK_TEMPLATE_ID=$( "${SCRIPT_DIR}/list-templates.sh" | jq -r "sort_by(.height) | (.[] | select(.tag | . and contains(\"network\"))) | .id" ) | ||
|
||
if [ -z "${NETWORK_TEMPLATE_ID}" ]; then | ||
echo "Couldn't find a template with tag='network'" | ||
exit 1 | ||
fi | ||
|
||
# The metadata below is hardcoded but it could be any valid JSON document. | ||
|
||
BODY=$(jq --null-input \ | ||
--arg name "${NAME}" \ | ||
--arg description "This is ${NAME} network" \ | ||
--arg facing "${FACING}" \ | ||
--arg start_u "${START_U}" \ | ||
--arg rack_id "${RACK_ID}" \ | ||
--arg template_id "${NETWORK_TEMPLATE_ID}" \ | ||
' | ||
{ | ||
"template_id": $template_id, | ||
"device": { | ||
"name": $name, | ||
"description": $description, | ||
"location": { | ||
"facing": $facing, | ||
"rack_id": $rack_id, | ||
"start_u": $start_u|tonumber | ||
}, | ||
"status": "IN_PROGRESS", | ||
"metadata": { | ||
"openstack_instance_id": "8f4e9068-5a39-4717-8a83-6b95e01031eb", | ||
"status": ["build", "scheduling", ""] | ||
}, | ||
"details": { | ||
"type": "Device::NetworkDetails", | ||
"admin_state_up": true, | ||
"dns_domain": "moose.local", | ||
"mtu": 1500 | ||
} | ||
} | ||
} | ||
' | ||
) | ||
|
||
# Run curl with funky redirection to capture response body and status code. | ||
BODY_FILE=$(mktemp) | ||
HTTP_STATUS=$( | ||
curl -s -k \ | ||
-w "%{http_code}" \ | ||
-o >(cat > "${BODY_FILE}") \ | ||
-H 'Content-Type: application/json' \ | ||
-H 'Accept: application/json' \ | ||
-H "Authorization: Bearer ${AUTH_TOKEN}" \ | ||
-X POST "${BASE_URL}/nodes" \ | ||
-d "${BODY}" | ||
) | ||
|
||
if [ "${HTTP_STATUS}" == "200" ] || [ "${HTTP_STATUS}" == "201" ] ; then | ||
cat "$BODY_FILE" | ||
else | ||
echo "Device creation failed" >&2 | ||
cat "$BODY_FILE" >&2 | ||
exit 1 | ||
fi |
Oops, something went wrong.