-
Notifications
You must be signed in to change notification settings - Fork 0
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 #384 from alphagov/models
Refactor Discovery Engine resource handling
- Loading branch information
Showing
24 changed files
with
185 additions
and
33 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# Represents a branch on a Discovery Engine data store. | ||
# | ||
# Currently, every data store on Discovery Engine has exactly *one* branch (the default branch), and | ||
# we are not able to make any changes to that resource. However, we still need to model it here | ||
# because documents are children of the branch, not the datastore itself. | ||
# | ||
# see https://cloud.google.com/ruby/docs/reference/google-cloud-discovery_engine-v1/latest/Google-Cloud-DiscoveryEngine-V1-DocumentService-Client | ||
# (there is no documentation specific to branches) | ||
Branch = Data.define(:remote_resource_id) do | ||
include DiscoveryEngineNameable | ||
|
||
# The default branch automatically available on a data store | ||
def self.default | ||
new("default_branch") | ||
end | ||
|
||
def parent | ||
# We only use a single data store in our architecture, so we can hardcode it here. | ||
DataStore.default | ||
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,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 `#remote_resource_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, remote_resource_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 |
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 @@ | ||
# Represents a data store on Discovery Engine. | ||
# | ||
# A data store contains the indexed documents that can be searched through an engine. | ||
# | ||
# Our architecture currently only has a single data store, so we do not need the ability to manage | ||
# data stores through Search Admin. | ||
# | ||
# see https://cloud.google.com/ruby/docs/reference/google-cloud-discovery_engine-v1/latest/Google-Cloud-DiscoveryEngine-V1-DataStore | ||
DataStore = Data.define(:remote_resource_id) do | ||
include DiscoveryEngineNameable | ||
|
||
def self.default | ||
new("govuk_content") | ||
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,18 @@ | ||
# Represents an engine on Discovery Engine. | ||
# | ||
# An engine (called "app" in the Google Cloud UI) is an abstraction over the data stores that | ||
# contain our searchable documents, and is used for querying. It is the parent resource of several | ||
# other resources such as controls and serving configs. | ||
# | ||
# Our architecture currently only has a single engine, so we do not need the ability to manage | ||
# engines through Search Admin. | ||
# | ||
# see https://cloud.google.com/ruby/docs/reference/google-cloud-discovery_engine-v1/latest/Google-Cloud-DiscoveryEngine-V1-Engine | ||
Engine = Data.define(:remote_resource_id) do | ||
include DiscoveryEngineNameable | ||
|
||
# The default engine created through Terraform in `govuk-infrastructure` | ||
def self.default | ||
new("govuk") | ||
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,21 @@ | ||
# Represents a serving config on Discovery Engine. | ||
# | ||
# A serving config is an endpoint on an engine that can be used for | ||
# querying. Each serving config can have different configuration (in particular, different sets of | ||
# active controls), which allows us to test out new configuration changes outside of the default | ||
# serving config. | ||
# | ||
# see https://cloud.google.com/ruby/docs/reference/google-cloud-discovery_engine-v1beta/latest/Google-Cloud-DiscoveryEngine-V1beta-ServingConfig | ||
ServingConfig = Data.define(:remote_resource_id) do | ||
include DiscoveryEngineNameable | ||
|
||
# The default serving config automatically available on an engine | ||
def self.default | ||
new("default_search") | ||
end | ||
|
||
def parent | ||
# We only use a single engine in our architecture, so we can hardcode it here. | ||
Engine.default | ||
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
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,15 @@ | ||
RSpec.describe Branch do | ||
subject(:branch) { described_class.new("my-branch") } | ||
|
||
describe ".default" do | ||
it "returns the default branch" do | ||
expect(described_class.default).to eq(described_class.new("default_branch")) | ||
end | ||
end | ||
|
||
describe "#name" do | ||
it "returns the fully qualified name of the branch" do | ||
expect(branch.name).to eq("[collection]/dataStores/govuk_content/branches/my-branch") | ||
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 @@ | ||
RSpec.describe DataStore do | ||
subject(:data_store) { described_class.new("my-data-store") } | ||
|
||
describe ".default" do | ||
it "returns the default data store" do | ||
expect(described_class.default).to eq(described_class.new("govuk_content")) | ||
end | ||
end | ||
|
||
describe "#name" do | ||
it "returns the fully qualified name of the data store" do | ||
expect(data_store.name).to eq("[collection]/dataStores/my-data-store") | ||
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 @@ | ||
RSpec.describe Engine do | ||
subject(:engine) { described_class.new("my-engine") } | ||
|
||
describe ".default" do | ||
it "returns the default engine" do | ||
expect(described_class.default).to eq(described_class.new("govuk")) | ||
end | ||
end | ||
|
||
describe "#name" do | ||
it "returns the fully qualified name of the engine" do | ||
expect(engine.name).to eq("[collection]/engines/my-engine") | ||
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 @@ | ||
RSpec.describe ServingConfig do | ||
subject(:serving_config) { described_class.new("my-serving-config") } | ||
|
||
describe ".default" do | ||
it "returns the default serving config" do | ||
expect(described_class.default).to eq(described_class.new("default_search")) | ||
end | ||
end | ||
|
||
describe "#name" do | ||
it "returns the fully qualified name of the serving config" do | ||
expect(serving_config.name).to eq("[collection]/engines/govuk/servingConfigs/my-serving-config") | ||
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
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