Skip to content

Commit

Permalink
Add Cloud-init CRUD
Browse files Browse the repository at this point in the history
  • Loading branch information
joakimk committed Dec 4, 2024
1 parent a2384bd commit e036c52
Show file tree
Hide file tree
Showing 15 changed files with 157 additions and 11 deletions.
4 changes: 4 additions & 0 deletions app/assets/stylesheets/application.scss
Original file line number Diff line number Diff line change
Expand Up @@ -149,3 +149,7 @@ form h4 {
}
}
}

.navbar-brand--logo {
margin-bottom: 100px !important;
}
49 changes: 49 additions & 0 deletions app/controllers/cloud_inits_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
class CloudInitsController < WebController
def index
locals cloud_inits: CloudInit.all
end

def new
locals cloud_init: CloudInit.new
end

def edit
locals cloud_init: CloudInit.find(params[:id])
end

def create
cloud_init = CloudInit.new(cloud_init_params)

if cloud_init.save
redirect_to cloud_inits_path, notice: "Cloud-init was successfully created."
else
render :new, locals: { cloud_init: }, status: :unprocessable_entity
end
end

def update
cloud_init = CloudInit.find(params[:id])

if cloud_init.update(cloud_init_params)
redirect_to cloud_inits_path, notice: "Cloud-init was successfully updated."
else
render :edit, locals: { cloud_init: }, status: :unprocessable_entity
end
end

def destroy
cloud_init = CloudInit.find(params[:id])
cloud_init.destroy!
redirect_to cloud_inits_path, notice: "Cloud-init was successfully destroyed.", status: :see_other
end

private

def setup_menu
active_menu_item_name :cloud_inits
end

def cloud_init_params
params.require(:cloud_init).permit(:name, :data)
end
end
6 changes: 0 additions & 6 deletions app/controllers/projects_controller.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
class ProjectsController < WebController
before_action :get_projects

# Keep this number low. We push updates that include revision data every time a change comes in.
# We've seen timeouts when this was set to 50.
MAX_REVISIONS = 15
Expand Down Expand Up @@ -68,8 +66,4 @@ def project_params
def setup_menu
active_menu_item_name :projects
end

def get_projects
@projects = Project.all_sorted
end
end
5 changes: 5 additions & 0 deletions app/controllers/web_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ class WebController < ApplicationController

before_action :require_password
before_action :setup_menu
before_action :get_projects

private

Expand All @@ -28,4 +29,8 @@ def require_password
session[:logged_in] = true
end
end

def get_projects
@projects = Project.all_sorted
end
end
3 changes: 3 additions & 0 deletions app/models/cloud_init.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class CloudInit < ActiveRecord::Base
validates :name, :data, presence: true
end
14 changes: 14 additions & 0 deletions app/views/cloud_inits/_form.html.slim
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
.row
.span12
.page
.row
.span9
= bootstrap_form_for(cloud_init) do |f|
= f.text_field :name
= f.text_area :data, style: "width: 875px; height: 400px;", label: "Contents"
p = link_to "Cloud-init docs", "https://cloudinit.readthedocs.io/en/latest/", target: "_blank"
.form-actions
= submit_tag "Save", class: "btn btn-primary"
- if cloud_init.persisted?
.span2
= button_to "Remove", cloud_init_path(cloud_init), method: :delete, data: { confirm: "Are you sure you want to remove #{cloud_init.name}?" }, class: "btn btn-danger"
8 changes: 8 additions & 0 deletions app/views/cloud_inits/edit.html.slim
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
h1 Editing cloud-init

= render "form", cloud_init: cloud_init

br

div
= link_to "Back to cloud-inits", cloud_inits_path
12 changes: 12 additions & 0 deletions app/views/cloud_inits/index.html.slim
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
h1 Cloud-inits

.row.index
.span12
- cloud_inits.each do |cloud_init|
.project
.actions
= link_to "Edit", edit_cloud_init_path(cloud_init)
.status
h1 = cloud_init.name
pre = cloud_init.data
p = link_to "New cloud-init", new_cloud_init_path
8 changes: 8 additions & 0 deletions app/views/cloud_inits/new.html.slim
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
h1 New cloud-init

= render "form", cloud_init: cloud_init

br

div
= link_to "Back to cloud-inits", cloud_inits_path
8 changes: 5 additions & 3 deletions app/views/layouts/application.html.slim
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@ html
.container
nav.navbar.navbar-default
.navbar-header
= link_to("Pipeline", root_path, class: "navbar-brand")
ul.nav.navbar-nav
= link_to("Pipeline", root_path, class: "navbar-brand navbar-brand--logo")
ul.nav.navbar-nav style="float: right"
li = link_to("Cloud-inits", cloud_inits_path)
ul.nav.navbar-nav style="width: 90%"
- @projects.each do |project|
li class=menu_item_class(:project)
li class=menu_item_class(project)
= link_to(project.name, project_path(id: project.id))


Expand Down
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
Rails.application.routes.draw do
resources :cloud_inits
get "revision" => ->(_) { [ 200, {}, [ File.exist?("built_from_revision") ? File.read("built_from_revision") : ENV.fetch("GIT_COMMIT") ] ] }
get "boom" => ->(_) { raise "Boom!" }

Expand Down
10 changes: 10 additions & 0 deletions db/migrate/20241203104945_create_cloud_inits.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class CreateCloudInits < ActiveRecord::Migration[7.1]
def change
create_table :cloud_inits do |t|
t.string :name, null: false
t.text :data, null: false

t.timestamps
end
end
end
9 changes: 8 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema[7.1].define(version: 2014_02_16_162823) do
ActiveRecord::Schema[7.1].define(version: 2024_12_03_104945) do
# These are extensions that must be enabled in order to support this database
enable_extension "pg_stat_statements"
enable_extension "plpgsql"
Expand All @@ -24,6 +24,13 @@
t.string "status_url", limit: 255
end

create_table "cloud_inits", force: :cascade do |t|
t.string "name", null: false
t.text "data", null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end

create_table "projects", id: :serial, force: :cascade do |t|
t.datetime "created_at", precision: nil, null: false
t.datetime "updated_at", precision: nil, null: false
Expand Down
29 changes: 29 additions & 0 deletions spec/features/cloud_inits_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
require "spec_helper"

RSpec.describe "Cloud-inits", type: :feature do
it "can be managed" do
visit root_path
click_link "Cloud-inits"
click_link "New cloud-init"

fill_in "Name", with: "foo"
fill_in "Content", with: "content"
click_button "Save"
expect(current_path).to eq(cloud_inits_path)
expect(page).to have_content("Cloud-init was successfully created.")
cloud_init = CloudInit.first!
expect(cloud_init.data).to eq("content")

click_link "Edit"
fill_in "Name", with: "bar"
click_button "Save"
cloud_init.reload
expect(cloud_init.name).to eq("bar")

click_link "Edit"
click_button "Remove"
expect(current_path).to eq(cloud_inits_path)
expect(page).to have_content("Cloud-init was successfully destroyed.")
expect(CloudInit.all).to be_empty
end
end
2 changes: 1 addition & 1 deletion spec/requests/api/cloud_inits_spec.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
require "spec_helper"

RSpec.describe "GET /api/cloud_init", type: :request do
it "gets a cloud init config if you have the right api token" do
it "gets a cloud-init config if you have the right api token" do
allow(App).to receive(:api_token).and_return("secret")

get "/api/cloud_init?token=secret"
Expand Down

0 comments on commit e036c52

Please sign in to comment.