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

docs(samples): add samples to create/delete featurestore #980

Merged
merged 13 commits into from
Feb 24, 2022
Merged
34 changes: 34 additions & 0 deletions samples/model-builder/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -364,3 +364,37 @@ def mock_endpoint_explain(mock_endpoint):
with patch.object(mock_endpoint, "explain") as mock_endpoint_explain:
mock_get_endpoint.return_value = mock_endpoint
yield mock_endpoint_explain


"""
----------------------------------------------------------------------------
FeatureStore Fixtures
----------------------------------------------------------------------------
"""


@pytest.fixture
def mock_featurestore():
mock = MagicMock(aiplatform.Featurestore)
nayaknishant marked this conversation as resolved.
Show resolved Hide resolved
yield mock


@pytest.fixture
def mock_init_featurestore(mock_featurestore):
nayaknishant marked this conversation as resolved.
Show resolved Hide resolved
with patch.object(aiplatform, "Featurestore") as mock:
mock.return_value = mock_featurestore
yield mock


@pytest.fixture
def mock_create_featurestore(mock_featurestore):
with patch.object(aiplatform.Featurestore, "create") as mock_create_featurestore:
mock_create_featurestore.return_value = mock_featurestore
yield mock_create_featurestore


@pytest.fixture
def mock_delete_featurestore(mock_featurestore):
with patch.object(mock_featurestore, "delete") as mock_delete_featurestore:
mock_delete_featurestore.return_value = mock_featurestore
nayaknishant marked this conversation as resolved.
Show resolved Hide resolved
yield mock_delete_featurestore
39 changes: 39 additions & 0 deletions samples/model-builder/create_featurestore_sample.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Copyright 2022 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
#
# https://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 aiplatform_sdk_create_featurestore_sample]
from google.cloud import aiplatform


def create_featurestore_sample(
project: str,
location: str,
featurestore_id: str,
online_store_fixed_node_count: int = 1,
):

aiplatform.init(project=project, location=location)

fs = aiplatform.Featurestore.create(
featurestore_id=featurestore_id,
online_store_fixed_node_count=online_store_fixed_node_count,
)
nayaknishant marked this conversation as resolved.
Show resolved Hide resolved

fs.wait()

return fs


# [END aiplatform_sdk_create_featurestore_sample]
34 changes: 34 additions & 0 deletions samples/model-builder/create_featurestore_sample_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Copyright 2021 Google LLC
nayaknishant marked this conversation as resolved.
Show resolved Hide resolved
#
# 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
#
# https://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.

import create_featurestore_sample
import test_constants as constants


def test_create_featurestore_sample(mock_sdk_init, mock_create_featurestore):

create_featurestore_sample.create_featurestore_sample(
project=constants.PROJECT,
location=constants.LOCATION,
featurestore_id=constants.FEAUTURESTORE_NAME,
nayaknishant marked this conversation as resolved.
Show resolved Hide resolved
online_store_fixed_node_count=constants.ONLINE_STORE_FIXED_NODE_COUNT,
)

mock_sdk_init.assert_called_once_with(
project=constants.PROJECT, location=constants.LOCATION
)
mock_create_featurestore.assert_called_once_with(
featurestore_id=constants.FEAUTURESTORE_NAME,
nayaknishant marked this conversation as resolved.
Show resolved Hide resolved
online_store_fixed_node_count=constants.ONLINE_STORE_FIXED_NODE_COUNT,
nayaknishant marked this conversation as resolved.
Show resolved Hide resolved
)
32 changes: 32 additions & 0 deletions samples/model-builder/delete_featurestore_sample.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Copyright 2022 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
#
# https://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 aiplatform_sdk_delete_featurestore_sample]
from google.cloud import aiplatform


def delete_featurestore_sample(
project: str, location: str, featurestore_name: str,
):

aiplatform.init(project=project, location=location)

fs = aiplatform.Featurestore(featurestore_name=featurestore_name)
fs.delete()

return fs
nayaknishant marked this conversation as resolved.
Show resolved Hide resolved


# [END aiplatform_sdk_delete_featurestore_sample]
37 changes: 37 additions & 0 deletions samples/model-builder/delete_featurestore_sample_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Copyright 2021 Google LLC
nayaknishant marked this conversation as resolved.
Show resolved Hide resolved
#
# 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
#
# https://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.

import delete_featurestore_sample
import test_constants as constants


def test_delete_featurestore_sample(
mock_sdk_init, mock_init_featurestore, mock_delete_featurestore
):

delete_featurestore_sample.delete_featurestore_sample(
project=constants.PROJECT,
location=constants.LOCATION,
featurestore_name=constants.FEAUTURESTORE_NAME,
)

mock_sdk_init.assert_called_once_with(
project=constants.PROJECT, location=constants.LOCATION
)

mock_init_featurestore.assert_called_once_with(
featurestore_name=constants.FEAUTURESTORE_NAME
)

mock_delete_featurestore.assert_called_once_with()
nayaknishant marked this conversation as resolved.
Show resolved Hide resolved
2 changes: 2 additions & 0 deletions samples/model-builder/test_constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
EXPERIMENT_NAME = "fraud-detection-trial-72"
CREDENTIALS = credentials.AnonymousCredentials()

FEAUTURESTORE_NAME = "featurestore_sample"
RESOURCE_ID = str(randint(10000000, 99999999)) # Create random resource ID
RESOURCE_ID_2 = str(randint(10000000, 99999999))

Expand Down Expand Up @@ -103,6 +104,7 @@
MACHINE_TYPE = "n1-standard-4"
ACCELERATOR_TYPE = "ACCELERATOR_TYPE_UNSPECIFIED"
ACCELERATOR_COUNT = 0
ONLINE_STORE_FIXED_NODE_COUNT = 1
nayaknishant marked this conversation as resolved.
Show resolved Hide resolved

# Model constants
MODEL_RESOURCE_NAME = f"{PARENT}/models/1234"
Expand Down