-
Notifications
You must be signed in to change notification settings - Fork 536
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(datastore-admin-v1): Add samples
- Loading branch information
Showing
11 changed files
with
429 additions
and
23 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,14 @@ | ||
inherit_gem: | ||
google-style: google-style.yml | ||
|
||
|
||
Lint/UselessAssignment: | ||
Exclude: | ||
- "snippets.rb" | ||
Metrics/BlockLength: | ||
Exclude: | ||
- "acceptance/*.rb" | ||
- "Rakefile" | ||
Style/GlobalVars: | ||
Exclude: | ||
- "Rakefile" |
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,36 @@ | ||
# Copyright 2020 Google LLC | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
# [START datastore_admin_dependencies] | ||
source "https://rubygems.org" | ||
|
||
# [END datastore_admin_dependencies] | ||
|
||
if ENV["GOOGLE_CLOUD_SAMPLES_TEST"] == "master" | ||
gem "google-cloud-datastore-admin-v1", path: "../../google-cloud-datastore-admin-v1" | ||
else | ||
# rubocop:disable Bundler/DuplicatedGem | ||
# [START datastore_admin_dependencies] | ||
gem "google-cloud-datastore-admin-v1" | ||
# [END datastore_admin_dependencies] | ||
# rubocop:enable Bundler/DuplicatedGem | ||
end | ||
|
||
group :test do | ||
gem "google-cloud-storage" | ||
gem "google-style", "~> 1.24.0" | ||
gem "minitest", "~> 5.14" | ||
gem "minitest-focus", "~> 1.1" | ||
gem "rake" | ||
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,61 @@ | ||
# Ruby Client for the Firestore in Datastore mode Admin V1 API Samples | ||
|
||
Firestore in Datastore mode is a NoSQL document database built for automatic scaling, high performance, and ease of application development. | ||
|
||
## Description | ||
|
||
These samples show how to use the [Firestore in Datastore mode Admin V1 API] | ||
(https://cloud.google.com/datastore). | ||
|
||
## Setup | ||
|
||
Before you can run or test the sample, you will need to enable the Cloud Datastore API in the [Google Cloud Console](https://console.cloud.google.com/apis/library/datastore.googleapis.com). | ||
|
||
## Testing | ||
|
||
The tests for the sample are integration tests that run against the Datastore | ||
service and require authentication. | ||
|
||
### Authenticating | ||
|
||
Set the following environment variable to your Google Cloud Platform | ||
project ID: `GOOGLE_CLOUD_PROJECT` | ||
|
||
Set one of the following environment variables to the path to your Google Cloud | ||
Platform keyfile: | ||
|
||
* `DATASTORE_KEYFILE` | ||
* `GOOGLE_CLOUD_KEYFILE` | ||
* `DATASTORE_KEYFILE_JSON` | ||
* `GOOGLE_CLOUD_KEYFILE_JSON` | ||
|
||
For more information, see | ||
[Authentication](https://googleapis.dev/ruby/google-cloud-datastore/latest/file.AUTHENTICATION.html). | ||
|
||
### Creating the Datastore indexes | ||
|
||
Install the [gcloud command-line | ||
tool](https://cloud.google.com/sdk/gcloud) and use it to create the | ||
indexes used in the tests. From the Datastore Admin V1 samples directory: | ||
|
||
``` sh | ||
# Set the default project in your env | ||
$ gcloud config set project PROJECT_ID | ||
|
||
# Authenticate the gcloud tool with your account | ||
$ gcloud auth login | ||
|
||
# Create the indexes | ||
$ gcloud datastore indexes create index.yaml | ||
``` | ||
|
||
### Setting the Google Cloud Storage (GCS) export bucket | ||
|
||
Set the following environment variable to an existing Google Cloud Storage | ||
bucket to be used in the export and import tests: `GOOGLE_CLOUD_STORAGE_BUCKET` | ||
|
||
### Running the tests | ||
|
||
```bash | ||
$ bundle exec rake test | ||
``` |
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,20 @@ | ||
# Copyright 2020 Google LLC | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
require "rake/testtask" | ||
|
||
Rake::TestTask.new "test" do |t| | ||
t.test_files = FileList["**/*_test.rb"] | ||
t.warning = false | ||
end |
32 changes: 32 additions & 0 deletions
32
google-cloud-datastore-admin-v1/samples/acceptance/helper.rb
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,32 @@ | ||
# Copyright 2020 Google LLC | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
require "minitest/autorun" | ||
require "minitest/focus" | ||
require "securerandom" | ||
|
||
require "google/cloud/datastore/admin/v1" | ||
|
||
def storage_bucket_name | ||
ENV["GOOGLE_CLOUD_STORAGE_BUCKET"] || "ruby-samples-test" | ||
end | ||
|
||
def random_storage_file_prefix | ||
"datastore-admin-v1-#{SecureRandom.hex 4}" | ||
end | ||
|
||
# Returns URL to sample image in the fixtures storage bucket | ||
def storage_url prefix: "datastore-admin-v1" | ||
"gs://#{storage_bucket_name}/#{prefix}" | ||
end |
73 changes: 73 additions & 0 deletions
73
google-cloud-datastore-admin-v1/samples/acceptance/snippets_test.rb
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,73 @@ | ||
# Copyright 2020 Google LLC | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
require_relative "helper" | ||
require_relative "../snippets" | ||
|
||
describe "Firestore in Datastore mode Admin V1 samples" do | ||
let(:project_id) { ENV["GOOGLE_CLOUD_PROJECT"] || raise("missing GOOGLE_CLOUD_PROJECT") } | ||
let(:storage_file_prefix) { random_storage_file_prefix } | ||
let(:output_url_prefix) { storage_url prefix: storage_file_prefix } | ||
|
||
it "client_create" do | ||
client = client_create | ||
assert_kind_of Google::Cloud::Datastore::Admin::V1::DatastoreAdmin::Client, client | ||
end | ||
|
||
it "index_list, index_get" do | ||
indexes = nil | ||
out, _err = capture_io do | ||
indexes = index_list project_id: project_id | ||
end | ||
assert indexes | ||
refute_empty indexes | ||
index = indexes.first | ||
assert_kind_of Google::Cloud::Datastore::Admin::V1::Index, index | ||
assert_includes out, "Got index: #{index.index_id}" | ||
assert_includes out, "Got list of indexes" | ||
|
||
out, _err = capture_io do | ||
index = index_get project_id: project_id, index_id: index.index_id | ||
end | ||
assert_kind_of Google::Cloud::Datastore::Admin::V1::Index, index | ||
assert_includes out, "Got index: #{index.index_id}" | ||
end | ||
|
||
it "entities_export, entities_import" do | ||
begin | ||
op = nil | ||
out, _err = capture_io do | ||
op = entities_export project_id: project_id, output_url_prefix: output_url_prefix | ||
end | ||
assert_includes out, "Entities were exported" | ||
assert op | ||
assert op.response | ||
assert_equal "#{output_url_prefix}/#{storage_file_prefix}.overall_export_metadata", op.response.output_url | ||
|
||
out, _err = capture_io do | ||
entities_import project_id: project_id, input_url: op.response.output_url | ||
end | ||
assert_includes out, "Entities were imported" | ||
ensure | ||
# cleanup: delete exported objects | ||
require "google/cloud/storage" | ||
storage = Google::Cloud::Storage.new | ||
files = storage.bucket(storage_bucket_name).files prefix: storage_file_prefix | ||
files.each do |f| | ||
f.delete | ||
puts "Deleted: #{f.name}" | ||
end | ||
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,23 @@ | ||
indexes: | ||
|
||
- kind: Task | ||
ancestor: no | ||
properties: | ||
- name: done | ||
- name: priority | ||
direction: desc | ||
|
||
- kind: Task | ||
properties: | ||
- name: collaborators | ||
direction: asc | ||
- name: created | ||
direction: desc | ||
|
||
- kind: Task | ||
ancestor: no | ||
properties: | ||
- name: priority | ||
direction: asc | ||
- name: created | ||
direction: asc |
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,94 @@ | ||
# Copyright 2020 Google LLC | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License");x | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
require "google/cloud/datastore/admin/v1" | ||
|
||
def client_create | ||
# [START datastore_admin_client_create] | ||
# [START require_library] | ||
# Import the client library | ||
require "google/cloud/datastore/admin/v1" | ||
# [END require_library] | ||
|
||
# Instantiate a client | ||
client = Google::Cloud::Datastore::Admin::V1::DatastoreAdmin::Client.new | ||
# [END datastore_admin_client_create] | ||
end | ||
|
||
def entities_export project_id:, output_url_prefix: | ||
client = Google::Cloud::Datastore::Admin::V1::DatastoreAdmin::Client.new | ||
# [START datastore_admin_entities_export] | ||
# project_id = "project-id" | ||
# output_url_prefix = "gs://bucket-name" | ||
op = client.export_entities project_id: project_id, output_url_prefix: output_url_prefix | ||
|
||
op.wait_until_done! | ||
raise op.error.message if op.error? | ||
|
||
response = op.response | ||
# Process the response. | ||
|
||
metadata = op.metadata | ||
# Process the metadata. | ||
|
||
puts "Entities were exported" | ||
# [END datastore_admin_entities_export] | ||
op | ||
end | ||
|
||
def entities_import project_id:, input_url: | ||
client = Google::Cloud::Datastore::Admin::V1::DatastoreAdmin::Client.new | ||
# [START datastore_admin_entities_import] | ||
# project_id = "project-id" | ||
# input_url = "gs://bucket-name/overall-export-metadata-file" | ||
op = client.import_entities project_id: project_id, input_url: input_url | ||
|
||
op.wait_until_done! | ||
raise op.error.message if op.error? | ||
|
||
response = op.response | ||
# Process the response. | ||
|
||
metadata = op.metadata | ||
# Process the metadata. | ||
|
||
puts "Entities were imported" | ||
# [END datastore_admin_entities_import] | ||
op | ||
end | ||
|
||
def index_get project_id:, index_id: | ||
client = Google::Cloud::Datastore::Admin::V1::DatastoreAdmin::Client.new | ||
# [START datastore_admin_index_get] | ||
# project_id = "project-id" | ||
# index_id = "my-index" | ||
index = client.get_index project_id: project_id, index_id: index_id | ||
puts "Got index: #{index.index_id}" | ||
# [END datastore_admin_index_get] | ||
index | ||
end | ||
|
||
def index_list project_id: | ||
client = Google::Cloud::Datastore::Admin::V1::DatastoreAdmin::Client.new | ||
# [START datastore_admin_index_list] | ||
# project_id = "project-id" | ||
indexes = client.list_indexes(project_id: project_id).map do |index| | ||
puts "Got index: #{index.index_id}" | ||
index | ||
end | ||
|
||
puts "Got list of indexes" | ||
# [END datastore_admin_index_list] | ||
indexes | ||
end |
Oops, something went wrong.