-
Notifications
You must be signed in to change notification settings - Fork 74
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 #3857 from 3scale/THREESCALE-1789-annotations
THREESCALE-1786: Managed by operator
- Loading branch information
Showing
28 changed files
with
621 additions
and
13 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
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,11 @@ | ||
# frozen_string_literal: true | ||
|
||
class Annotation < ApplicationRecord | ||
SUPPORTED_ANNOTATIONS = %w[managed_by].freeze | ||
|
||
belongs_to :annotated, polymorphic: true, optional: false, inverse_of: :annotations | ||
|
||
validates :name, presence: true, inclusion: { in: SUPPORTED_ANNOTATIONS } | ||
validates :value, presence: true | ||
validates :name, :value, :annotated_type, length: { maximum: 255 } | ||
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
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 @@ | ||
# frozen_string_literal: true | ||
|
||
module Annotating | ||
extend ActiveSupport::Concern | ||
|
||
class_methods do | ||
def annotated | ||
class_eval do | ||
include Model | ||
include ManagedBy | ||
end | ||
end | ||
end | ||
|
||
class << self | ||
def models | ||
return @models if @models | ||
|
||
# This is to see all models when creating the DB trigger, otherwise the resulting trigger could be incorrect | ||
# https://github.com/3scale/porta/pull/3857#discussion_r1707235658 | ||
Rails.autoloaders.main.eager_load_dir("#{Rails.root}/app/models") | ||
|
||
@models = ActiveRecord::Base.descendants.select { |model| model.include?(Model) } | ||
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,15 @@ | ||
# frozen_string_literal: true | ||
|
||
module Annotating | ||
module ManagedBy | ||
extend ActiveSupport::Concern | ||
|
||
def managed_by | ||
value_of_annotation("managed_by") | ||
end | ||
|
||
def managed_by=(value) | ||
annotate("managed_by", value) | ||
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,56 @@ | ||
# frozen_string_literal: true | ||
|
||
module Annotating | ||
module Model | ||
extend ActiveSupport::Concern | ||
|
||
included do | ||
has_many :annotations, as: :annotated, dependent: :destroy, autosave: true, inverse_of: :annotated | ||
end | ||
|
||
def annotations=(hash) | ||
hash.each do |k ,v| | ||
annotate(k, v) | ||
end | ||
end | ||
|
||
def annotations_hash | ||
annotations.pluck(:name, :value).to_h | ||
end | ||
|
||
def annotations_xml(options = {}) | ||
xml = options[:builder] || ThreeScale::XML::Builder.new | ||
|
||
xml.annotations do | ||
annotations.each do |annotation| | ||
xml.tag!(annotation.name, annotation.value) | ||
end | ||
end | ||
|
||
xml.to_xml | ||
end | ||
|
||
def annotation(name) | ||
annotations.find { _1.name == name } | ||
end | ||
|
||
def value_of_annotation(name) | ||
annotation(name)&.value | ||
end | ||
|
||
def annotate(name, value) | ||
return remove_annotation(name) if value.blank? | ||
|
||
existing = annotation(name) | ||
if existing | ||
existing.value = value | ||
else | ||
annotations.build(name: name, value: value) | ||
end | ||
end | ||
|
||
def remove_annotation(name) | ||
annotation(name)&.mark_for_destruction | ||
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
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,33 @@ | ||
class CreateAnnotationReferences < ActiveRecord::Migration[6.1] | ||
disable_ddl_transaction! if System::Database.postgres? | ||
|
||
def change | ||
options = "CHARSET=utf8mb4 COLLATE=utf8mb4_bin" if System::Database.mysql? | ||
|
||
create_table :annotations, options: options do |t| | ||
t.string :name, null: false | ||
t.string :value | ||
t.references :annotated, polymorphic: true, index: false, null: false | ||
t.integer :tenant_id | ||
t.timestamps | ||
|
||
t.index %i[annotated_type annotated_id name], unique: true | ||
end | ||
|
||
reversible do |direction| | ||
direction.up do | ||
self.class.execute_trigger_action(:recreate) | ||
end | ||
direction.down do | ||
self.class.execute_trigger_action(:drop) | ||
end | ||
end | ||
end | ||
|
||
def self.execute_trigger_action(action) | ||
trigger = System::Database.triggers.detect { |trigger| trigger.name == "annotations_tenant_id" } | ||
|
||
expressions = [trigger.public_send(action)].flatten | ||
expressions.each(&ActiveRecord::Base.connection.method(:execute)) | ||
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
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
Oops, something went wrong.