-
Notifications
You must be signed in to change notification settings - Fork 1
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
Save ram and storage of a request in gb internally #342
Changes from 9 commits
08f5de2
50ed72a
edaf6d1
2c8bee1
f634494
d32c762
0e3ee74
d500ffa
529f7ec
c83aca6
0840a04
55b93da
e3046cb
0361e6e
4062db5
f3abcf9
36cf54a
3f222d6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -473,4 +473,4 @@ RUBY VERSION | |
ruby 2.5.0p0 | ||
|
||
BUNDLED WITH | ||
1.16.2 | ||
1.17.1 |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,18 +13,18 @@ class Request < ApplicationRecord | |
|
||
MAX_NAME_LENGTH = 20 | ||
MAX_CPU_CORES = 64 | ||
MAX_RAM_MB = 256_000 | ||
MAX_STORAGE_MB = 1_000_000 | ||
MAX_RAM_GB = 256 | ||
MAX_STORAGE_GB = 1_000 | ||
|
||
enum status: %i[pending accepted rejected] | ||
validates :name, | ||
length: { maximum: MAX_NAME_LENGTH, message: 'only allows a maximum of %{count} characters' }, | ||
format: { with: /\A[a-zA-Z1-9\-\s]+\z/, message: 'only letters and numbers allowed' }, | ||
uniqueness: true | ||
validates :cpu_cores, :ram_mb, :storage_mb, :operating_system, :description, presence: true | ||
validates :cpu_cores, :ram_gb, :storage_gb, :operating_system, :description, presence: true | ||
validates :cpu_cores, numericality: { only_integer: true, greater_than: 0, less_than_or_equal_to: MAX_CPU_CORES } | ||
validates :ram_mb, numericality: { only_integer: true, greater_than: 0, less_than_or_equal_to: MAX_RAM_MB } | ||
validates :storage_mb, numericality: { only_integer: true, greater_than: 0, less_than_or_equal_to: MAX_STORAGE_MB } | ||
validates :ram_gb, numericality: { only_integer: true, greater_than: 0, less_than_or_equal_to: MAX_RAM_GB } | ||
validates :storage_gb, numericality: { only_integer: true, greater_than: 0, less_than_or_equal_to: MAX_STORAGE_GB } | ||
|
||
def description_text(host_name) | ||
description = "- VM Name: #{name}\n" | ||
|
@@ -69,7 +69,7 @@ def non_sudo_user_assignments | |
def create_vm | ||
folder = VSphere::Connection.instance.root_folder | ||
clusters = VSphere::Cluster.all | ||
folder.create_vm(cpu_cores, ram_mb, storage_mb, name, clusters.first) if clusters.first | ||
folder.create_vm(cpu_cores, gibi_to_mibi(ram_gb), gibi_to_kibi(storage_gb), name, clusters.first) if clusters.first | ||
end | ||
|
||
def push_to_git | ||
|
@@ -114,4 +114,12 @@ def generate_puppet_name_script | |
def url(host_name) | ||
Rails.application.routes.url_helpers.request_url self, host: host_name | ||
end | ||
|
||
def gibi_to_mibi(gibi) | ||
gibi * 1024 | ||
end | ||
|
||
def gibi_to_kibi(gibi) | ||
gibi * 1024**2 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. please use 2** 10** 2 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. or (2**10) **2 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. or 2**20 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. or 2**(10*2) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. or 2**(2**3+2) **2 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. or 2**(2**(2+2/2)+2) **2 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. or make a method "binary_base", returning 2 and change formula to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
that way we have no magic numbers There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @BjoernDaase we should remove all magic numbers that way There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @Kenneth-Schroeder Are you sober? |
||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,8 @@ | ||
# frozen_string_literal: true | ||
|
||
class RequestTemplate < ApplicationRecord | ||
MAX_CPU_CORES = 64 | ||
MAX_RAM_GB = 256 | ||
MAX_STORAGE_GB = 1_000 | ||
|
||
validates :name, :cpu_cores, :ram_gb, :storage_gb, :operating_system, presence: true | ||
validates :cpu_cores, numericality: { only_integer: true, greater_than: 0, less_than_or_equal_to: MAX_CPU_CORES } | ||
validates :ram_gb, numericality: { only_integer: true, greater_than: 0, less_than_or_equal_to: MAX_RAM_GB } | ||
validates :storage_gb, numericality: { only_integer: true, greater_than: 0, less_than_or_equal_to: MAX_STORAGE_GB } | ||
validates :cpu_cores, numericality: { only_integer: true, greater_than: 0, less_than_or_equal_to: Request::MAX_CPU_CORES } | ||
validates :ram_gb, numericality: { only_integer: true, greater_than: 0, less_than_or_equal_to: Request::MAX_RAM_GB } | ||
validates :storage_gb, numericality: { only_integer: true, greater_than: 0, less_than_or_equal_to: Request::MAX_STORAGE_GB } | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
# frozen_string_literal: true | ||
|
||
json.extract! request, :id, :name, :cpu_cores, :ram_mb, :storage_mb, :operating_system, :comment, :rejection_information, :status, :created_at, :updated_at | ||
json.extract! request, :id, :name, :cpu_cores, :ram_gb, :storage_gb, :operating_system, :comment, :rejection_information, :status, :created_at, :updated_at | ||
json.url request_url(request, format: :json) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# frozen_string_literal: true | ||
|
||
class RenameRequestMbToGb < ActiveRecord::Migration[5.2] | ||
def up | ||
update_requests | ||
rename_column :requests, :ram_mb, :ram_gb | ||
rename_column :requests, :storage_mb, :storage_gb | ||
end | ||
|
||
def update_requests | ||
Request.all.each do |request| | ||
unless request.nil? | ||
request.ram_mb = request.ram_mb / 1000 | ||
request.storage_mb = request.storage_mb / 1000 | ||
end | ||
request.save | ||
end | ||
end | ||
|
||
def down | ||
raise ActiveRecord::IrreversibleMigration | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'rails_helper' | ||
|
||
RSpec.describe RequestTemplate, type: :model do | ||
describe 'validation tests' do | ||
let(:request_template) { FactoryBot.build :request_template } | ||
|
||
context 'when request template is invalid' do | ||
it 'is invalid with no name' do | ||
request_template.name = '' | ||
expect(request_template).to be_invalid | ||
end | ||
|
||
it 'is invalid with no cpu_cores specifiation' do | ||
request_template.cpu_cores = nil | ||
expect(request_template).to be_invalid | ||
end | ||
|
||
it 'is invalid with no ram specifiation' do | ||
request_template.ram_gb = nil | ||
expect(request_template).to be_invalid | ||
end | ||
|
||
it 'is invalid with no storage specifiation' do | ||
request_template.storage_gb = nil | ||
expect(request_template).to be_invalid | ||
end | ||
|
||
it 'is invalid with no operating_system specification' do | ||
request_template.operating_system = '' | ||
expect(request_template).to be_invalid | ||
end | ||
|
||
it 'is invalid with negative cpu_cores specifiation' do | ||
request_template.cpu_cores = -1 | ||
expect(request_template).to be_invalid | ||
end | ||
|
||
it 'is invalid with too many cpu_cores ' do | ||
request_template.cpu_cores = Request::MAX_CPU_CORES + 1 | ||
expect(request_template).to be_invalid | ||
end | ||
|
||
it 'is invalid with negative ram specifiation' do | ||
request_template.ram_gb = -1 | ||
expect(request_template).to be_invalid | ||
end | ||
|
||
it 'is invalid with to much ram' do | ||
request_template.ram_gb = Request::MAX_RAM_GB + 1 | ||
expect(request_template).to be_invalid | ||
end | ||
|
||
it 'is invalid with negative storage specifiation' do | ||
request_template.storage_gb = -1 | ||
expect(request_template).to be_invalid | ||
end | ||
|
||
it 'is invalid with to much storage' do | ||
request_template.storage_gb = Request::MAX_STORAGE_GB + 1 | ||
expect(request_template).to be_invalid | ||
end | ||
end | ||
end | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
magic number, please use 2**10