Add parser and API
DataModels
DataModels
are the core data structures in the Cloud Connector. Each model inherits from DataModel::Base
and can define associations with other models. The data is loaded from YAML files into memory at runtime, making it efficient for read operations and relationship traversal.
module Gitlab
module CloudConnector
module DataModels
class UnitPrimitive < DataModel::Base
has_and_belongs_to_many :backend_services
has_and_belongs_to_many :add_ons
has_and_belongs_to_many :license_types
attr_reader :cut_off_date, :deprecated_by_url, :deprecation_message, :description, :documentation_url,
:feature_category, :group, :introduced_by_url, :milestone, :min_gitlab_version,
:min_gitlab_version_for_free_access, :name, :unit_primitive_issue_url
end
class BackendService < DataModel::Base
attr_reader :description, :group, :jwt_aud, :name, :project_url
end
class LicenseType < DataModel::Base; end
class AddOn < DataModel::Base; end
end
end
end
Associations
The library supports many-to-many relationships through the has_and_belongs_to_many
association type.
class UnitPrimitive < DataModel::Base
has_and_belongs_to_many :backend_services
end
Note: I simplified the association support to the has_and_belongs_to_many
association type.
It's no longer a bidirectional association (until we really need it)
unit_primitive = UnitPrimitive.find_by_name(:duo_chat)
unit_primitive.backend_services.map(&:jwt_wud) # Returns an array of BackendService audiences, that we use for issuing the jwt token.
Also in Add adapter to generate old service configuration (!55 - merged), we benefit from this as we can read service properties from associated unit primitives.
Working with DataModels
# Find unit primitive by name
# Note: value should be a symbol (e.g., `:duo_chat` instead of `'duo_chat'`).
duo_chat = Gitlab::CloudConnector::DataModels::UnitPrimitive.find_by_name(:duo_chat)
duo_chat.description
duo_chat.cut_off_date
# access associations
duo_chat.add_ons
duo_chat.license_types
duo_chat.backend_services
# read duo_chat audiences
duo_chat.backend_services.map(&:jwt_aud)
How to test locally
- go to
cd src/ruby
make build
gem install gitlab-cloud-connector-0.2.1.gem
irb
require 'cloud_connector'
puts Gitlab::DataModels::UnitPrimitives.map(&:name)
Edited by Nikola Milojevic