Skip to content

Commit

Permalink
chore(datastore-admin-v1): Add samples
Browse files Browse the repository at this point in the history
* Add acceptance:cleanup to Rakefile
* Add samples tasks to Rakefile

closes: #7597
pr: #7613
  • Loading branch information
quartzmo committed Sep 11, 2020
1 parent fb449a1 commit 787aba5
Show file tree
Hide file tree
Showing 11 changed files with 429 additions and 23 deletions.
26 changes: 26 additions & 0 deletions google-cloud-datastore-admin-v1/Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,32 @@ namespace :ci do
end
end

task :samples do
Rake::Task["samples:latest"].invoke
end

namespace :samples do
task :latest do
Dir.chdir "samples" do
Bundler.with_clean_env do
ENV["GOOGLE_CLOUD_SAMPLES_TEST"] = "not_master"
sh "bundle update"
sh "bundle exec rake test"
end
end
end

task :master do
Dir.chdir "samples" do
Bundler.with_clean_env do
ENV["GOOGLE_CLOUD_SAMPLES_TEST"] = "master"
sh "bundle update"
sh "bundle exec rake test"
end
end
end
end

task default: :test

def header str, token = "#"
Expand Down
14 changes: 14 additions & 0 deletions google-cloud-datastore-admin-v1/samples/.rubocop.yml
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"
36 changes: 36 additions & 0 deletions google-cloud-datastore-admin-v1/samples/Gemfile
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
61 changes: 61 additions & 0 deletions google-cloud-datastore-admin-v1/samples/README.md
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
```
20 changes: 20 additions & 0 deletions google-cloud-datastore-admin-v1/samples/Rakefile
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 google-cloud-datastore-admin-v1/samples/acceptance/helper.rb
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
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
23 changes: 23 additions & 0 deletions google-cloud-datastore-admin-v1/samples/index.yaml
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
94 changes: 94 additions & 0 deletions google-cloud-datastore-admin-v1/samples/snippets.rb
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

0 comments on commit 787aba5

Please sign in to comment.