Skip to content

Commit

Permalink
docs(samples): Importing code samples for VMWare Engine (GoogleCloudP…
Browse files Browse the repository at this point in the history
…latform#10292)

* docs(samples): Adding VMWare Engine code samples

* docs(samples): Adding samples for VMWare Engine

* WIP: creating private cloud

* Adding credentials samples

* Working on docstrings

* Progress...

* WIP

* Adding region tags and tests

* Black + lint

* Adding pytest to test stuff

* some fixes

* Use single project

* Testing things

* Testing things

* Making tests work on 3.7

* Update test_cluster.py

* 🦉 Updates from OwlBot post-processor

See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md

* Apply suggestions from code review

Co-authored-by: Remigiusz Samborski <[email protected]>

* 🦉 Updates from OwlBot post-processor

See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md

* Update according to comments

* 🦉 Updates from OwlBot post-processor

See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md

* lint

* 🦉 Updates from OwlBot post-processor

See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md

* Update vmwareengine/cloud-client/update_policy.py

Co-authored-by: Remigiusz Samborski <[email protected]>

* Applying suggestions

---------

Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com>
Co-authored-by: Remigiusz Samborski <[email protected]>
  • Loading branch information
3 people authored Jul 7, 2023
1 parent bc0c7ec commit 859efd7
Show file tree
Hide file tree
Showing 33 changed files with 1,363 additions and 0 deletions.
1 change: 1 addition & 0 deletions .github/CODEOWNERS
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
/recaptcha_enterprise/demosite/**/* @GoogleCloudPlatform/dee-infra @GoogleCloudPlatform/recaptcha-customer-obsession-reviewers @GoogleCloudPlatform/python-samples-reviewers
/secretmanager/**/* @GoogleCloudPlatform/dee-infra @GoogleCloudPlatform/python-samples-reviewers
/securitycenter/**/* @GoogleCloudPlatform/dee-infra @GoogleCloudPlatform/python-samples-reviewers
/vmwareengine/**/* @GoogleCloudPlatform/dee-infra @GoogleCloudPlatform/python-samples-reviewers
/webrisk/**/* @GoogleCloudPlatform/dee-infra @GoogleCloudPlatform/python-samples-reviewers

# DEE Platform Ops (DEEPO)
Expand Down
1 change: 1 addition & 0 deletions .github/auto-label.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ path:
trace: "cloudtrace"
translate: "translate"
vision: "vision"
vmwareengine: "vmwareengine"
webrisk: "webrisk"
workflows: "workflows"
- labelprefix: "asset: "
Expand Down
1 change: 1 addition & 0 deletions .github/blunderbuss.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ assign_issues_by:
- "api: recaptchaenterprise"
- "api: secretmanager"
- "api: securitycenter"
- "api: vmwareengine"
to:
- GoogleCloudPlatform/dee-infra
- labels:
Expand Down
Empty file added vmwareengine/__init__.py
Empty file.
Empty file.
57 changes: 57 additions & 0 deletions vmwareengine/cloud-client/cancel_private_cloud_deletion.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# Copyright 2023 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 vmwareengine_cancel_cloud_deletion]
from google.api_core import operation
from google.cloud import vmwareengine_v1


def cancel_private_cloud_deletion_by_full_name(cloud_name: str) -> operation.Operation:
"""
Cancels in progress deletion of VMWare Private Cloud.
Args:
cloud_name: identifier of the Private Cloud you want to cancel deletion for.
Expected format:
projects/{project_name}/locations/{zone}/privateClouds/{cloud}
Returns:
An Operation object related to canceling private cloud deletion operation.
"""
client = vmwareengine_v1.VmwareEngineClient()
request = vmwareengine_v1.UndeletePrivateCloudRequest()
request.name = cloud_name
return client.undelete_private_cloud(request)


def cancel_private_cloud_deletion(
project_id: str, zone: str, cloud_name: str
) -> operation.Operation:
"""
Cancels in progress deletion of deletion of VMWare Private Cloud.
Args:
project_id: name of the project hosting the private cloud.
zone: zone in which the private cloud is located in.
cloud_name: name of the private cloud to cancel deletion for.
Returns:
An Operation object related to canceling private cloud deletion operation.
"""
return cancel_private_cloud_deletion_by_full_name(
f"projects/{project_id}/locations/{zone}/privateClouds/{cloud_name}"
)


# [END vmwareengine_cancel_cloud_deletion]
66 changes: 66 additions & 0 deletions vmwareengine/cloud-client/create_cluster.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# Copyright 2023 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 vmwareengine_create_cluster]
from google.api_core import operation
from google.cloud import vmwareengine_v1


def create_cluster(
project_id: str,
zone: str,
private_cloud_name: str,
cluster_name: str,
node_count: int = 4,
) -> operation.Operation:
"""
Create a new cluster in a private cloud.
Creation of a new cluster is a long-running operation and it might take well over 15 minutes.
Args:
project_id: name of the project you want to use.
zone: region in which your private cloud is located.
private_cloud_name: name of the private cloud hosting the new cluster.
cluster_name: name of the new cluster.
node_count: number of nodes in the new cluster. (Must be >= 3)
Returns:
An Operation object related to started cluster creation operation.
Raises:
ValueError in case an incorrect number of nodes is provided.
"""
if node_count < 3:
raise ValueError("Cluster needs to have at least 3 nodes")

request = vmwareengine_v1.CreateClusterRequest()
request.parent = (
f"projects/{project_id}/locations/{zone}/privateClouds/{private_cloud_name}"
)

request.cluster = vmwareengine_v1.Cluster()
request.cluster.name = cluster_name

# Currently standard-72 is the only supported node type.
request.cluster.node_type_configs = {
"standard-72": vmwareengine_v1.NodeTypeConfig()
}
request.cluster.node_type_configs["standard-72"].node_count = node_count

client = vmwareengine_v1.VmwareEngineClient()
return client.create_cluster(request)


# [END vmwareengine_create_cluster]
70 changes: 70 additions & 0 deletions vmwareengine/cloud-client/create_custom_cluster.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
# Copyright 2023 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 vmwareengine_create_custom_cluster]
from google.api_core import operation
from google.cloud import vmwareengine_v1


def create_custom_cluster(
project_id: str,
zone: str,
private_cloud_name: str,
cluster_name: str,
node_count: int = 4,
core_count: int = 28,
) -> operation.Operation:
"""
Create a new cluster with custom number of cores in its nodes
in a private cloud.
Creation of a new cluster is a long-running operation and it might take well over 15 minutes.
Args:
project_id: name of the project you want to use.
zone: region in which your private cloud is located.
private_cloud_name: name of the private cloud hosting the new cluster.
cluster_name: name of the new cluster.
node_count: number of nodes in the new cluster.
core_count: number of CPU cores in the new cluster nodes.
Returns:
An Operation object related to started cluster creation operation.
Raises:
ValueError in case an incorrect number of nodes is provided.
"""
if node_count < 3:
raise ValueError("Cluster needs to have at least 3 nodes")

request = vmwareengine_v1.CreateClusterRequest()
request.parent = (
f"projects/{project_id}/locations/{zone}/privateClouds/{private_cloud_name}"
)

request.cluster = vmwareengine_v1.Cluster()
request.cluster.name = cluster_name

# Currently standard-72 is the only supported node type.
request.cluster.node_type_configs = {
"standard-72": vmwareengine_v1.NodeTypeConfig()
}
request.cluster.node_type_configs["standard-72"].node_count = node_count
request.cluster.node_type_configs["standard-72"].custom_core_count = core_count

client = vmwareengine_v1.VmwareEngineClient()
return client.create_cluster(request)


# [END vmwareengine_create_custom_cluster]
51 changes: 51 additions & 0 deletions vmwareengine/cloud-client/create_legacy_network.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# Copyright 2023 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 vmwareengine_create_legacy_network]
from google.cloud import vmwareengine_v1

TIMEOUT = 1200 # 20 minutes


def create_legacy_network(
project_id: str, region: str
) -> vmwareengine_v1.VmwareEngineNetwork:
"""
Creates a new legacy network.
Args:
project_id: name of the project you want to use.
region: name of the region you want to use. I.e. "us-central1"
Returns:
The newly created VmwareEngineNetwork object.
"""
network = vmwareengine_v1.VmwareEngineNetwork()
network.description = (
"Legacy network created using vmwareengine_v1.VmwareEngineNetwork"
)
network.type_ = vmwareengine_v1.VmwareEngineNetwork.Type.LEGACY

request = vmwareengine_v1.CreateVmwareEngineNetworkRequest()
request.parent = f"projects/{project_id}/locations/{region}"
request.vmware_engine_network_id = f"{region}-default"
request.vmware_engine_network = network

client = vmwareengine_v1.VmwareEngineClient()
result = client.create_vmware_engine_network(request, timeout=TIMEOUT).result()

return result


# [END vmwareengine_create_legacy_network]
65 changes: 65 additions & 0 deletions vmwareengine/cloud-client/create_policy.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
# Copyright 2023 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 vmwareengine_create_policy]
from google.api_core import operation
from google.cloud import vmwareengine_v1


def create_network_policy(
project_id: str,
region: str,
ip_range: str,
internet_access: bool,
external_ip: bool,
) -> operation.Operation:
"""
Creates a new network policy in a given network.
Args:
project_id: name of the project you want to use.
region: name of the region you want to use. I.e. "us-central1"
ip_range: the CIDR range to use for internet access and external IP access gateways,
in CIDR notation. An RFC 1918 CIDR block with a "/26" suffix is required.
internet_access: should internet access be allowed.
external_ip: should external IP addresses be assigned.
Returns:
An operation object representing the started operation. You can call its .result() method to wait for
it to finish.
Raises:
ValueError if the provided ip_range doesn't end with /26.
"""
if not ip_range.endswith("/26"):
raise ValueError(
"The ip_range needs to be an RFC 1918 CIDR block with a '/26' suffix"
)

network_policy = vmwareengine_v1.NetworkPolicy()
network_policy.vmware_engine_network = f"projects/{project_id}/locations/{region}/vmwareEngineNetworks/{region}-default"
network_policy.edge_services_cidr = ip_range
network_policy.internet_access.enabled = internet_access
network_policy.external_ip.enabled = external_ip

request = vmwareengine_v1.CreateNetworkPolicyRequest()
request.network_policy = network_policy
request.parent = f"projects/{project_id}/locations/{region}"
request.network_policy_id = f"{region}-default"

client = vmwareengine_v1.VmwareEngineClient()
return client.create_network_policy(request)


# [END vmwareengine_create_policy]
Loading

0 comments on commit 859efd7

Please sign in to comment.