Skip to content
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

Refactor handling of non-managed resources #1360

Merged
merged 6 commits into from
Feb 6, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions app/models/concerns/discovery_engine_nameable.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Enhances models with a `#name` method returning their fully qualified Google Cloud Platform
# resource name (like a path).
#
# For example, for a `Control`, this would be:
# projects/{project}/locations/{location}/collections/{collection_id}/engines/
# {engine_id}/controls/{control_id}
#
# Requires the including class to implement `#discovery_engine_id`, and optionally `#parent` if the
# parent resource is not the default collection.
module DiscoveryEngineNameable
# The name (fully qualified path) of this Discovery Engine resource on GCP
def name
[parent_name, resource_path_fragment, discovery_engine_id].join("/")
end

private

def resource_path_fragment
# For example: `DataStore` -> `dataStores`
self.class.name.downcase_first.pluralize
end

def parent_name
if respond_to?(:parent)
parent.name
else
# The default collection is the parent of all root-level resources
Rails.configuration.discovery_engine_default_collection_name
end
end
end
6 changes: 1 addition & 5 deletions app/models/control.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
# see
# https://cloud.google.com/ruby/docs/reference/google-cloud-discovery_engine-v1/latest/Google-Cloud-DiscoveryEngine-V1-Control
class Control < ApplicationRecord
include DiscoveryEngineNameable
include RemoteSynchronizable
remote_synchronize with: DiscoveryEngine::ControlClient

Expand All @@ -30,11 +31,6 @@ def to_discovery_engine_control
}
end

# The fully qualified name of the control on Discovery Engine (like a path)
def name
[parent.name, "controls", discovery_engine_id].join("/")
end

# The parent of the control on Discovery Engine (always the default engine)
def parent
Engine.default
Expand Down
11 changes: 2 additions & 9 deletions app/models/data_store.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
#
# see https://cloud.google.com/ruby/docs/reference/google-cloud-discovery_engine-v1/latest/Google-Cloud-DiscoveryEngine-V1-DataStore
class DataStore
include DiscoveryEngineNameable

# The ID of the default datastore created through Terraform in `govuk_infrastructure`
DEFAULT_DATA_STORE_ID = "govuk_content".freeze

Expand All @@ -20,15 +22,6 @@ def initialize(discovery_engine_id)
@discovery_engine_id = discovery_engine_id
end

# The fully qualified name of the data store on Discovery Engine (like a path)
def name
[
Rails.configuration.discovery_engine_default_collection_name,
"dataStores",
discovery_engine_id
].join("/")
end

def ==(other)
discovery_engine_id == other.discovery_engine_id
end
Expand Down
11 changes: 2 additions & 9 deletions app/models/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
#
# see https://cloud.google.com/ruby/docs/reference/google-cloud-discovery_engine-v1/latest/Google-Cloud-DiscoveryEngine-V1-Engine
class Engine
include DiscoveryEngineNameable

# The ID of the default engine created through Terraform in `govuk-infrastructure`
DEFAULT_ENGINE_ID = "govuk".freeze

Expand All @@ -22,15 +24,6 @@ def initialize(discovery_engine_id)
@discovery_engine_id = discovery_engine_id
end

# The fully qualified name of the engine on Discovery Engine (like a path)
def name
[
Rails.configuration.discovery_engine_default_collection_name,
"engines",
discovery_engine_id
].join("/")
end

def ==(other)
discovery_engine_id == other.discovery_engine_id
end
Expand Down