Skip to content

Commit

Permalink
update for power on/off remote_refresh_ip and others
Browse files Browse the repository at this point in the history
  • Loading branch information
Costya-Y committed Sep 14, 2024
1 parent b527cd1 commit a514396
Show file tree
Hide file tree
Showing 12 changed files with 53 additions and 35 deletions.
2 changes: 1 addition & 1 deletion cloudshell/cp/gcp/flows/deploy_instance/base_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ def _deploy(self, request_actions: DeployVMRequestActions) -> DeployAppResult:
deploy_app: BaseGCPDeployApp = request_actions.deploy_app
subnet_list = [x.subnet_id for x in request_actions.connect_subnets]
if not subnet_list:
subnet_list = network_handler.get_subnets()
subnet_list = list(network_handler.get_subnets())

if not deploy_app.machine_type or deploy_app.machine_type == "Inherited":
deploy_app.machine_type = self.resource_config.machine_type
Expand Down
6 changes: 3 additions & 3 deletions cloudshell/cp/gcp/flows/power_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from cloudshell.cp.gcp.handlers.instance import InstanceHandler

if TYPE_CHECKING:

from google.cloud.compute_v1.types import compute
from cloudshell.cp.gcp.models.deployed_app import BaseGCPDeployApp
from cloudshell.cp.gcp.resource_conf import GCPResourceConfig

Expand All @@ -18,10 +18,10 @@ class GCPPowerFlow:
deployed_app: BaseGCPDeployApp
resource_config: GCPResourceConfig

def _get_instance(self, instance_uuid) -> InstanceHandler:
def _get_instance(self, instance_uuid) -> compute.Instance:
instance_data = json.loads(instance_uuid)
instance_data["credentials"] = self.resource_config.credentials
return InstanceHandler.get(**instance_data)
return InstanceHandler.get(**instance_data).instance

def power_on(self):
instance = self._get_instance(self.deployed_app.vmdetails.uid)
Expand Down
1 change: 1 addition & 0 deletions cloudshell/cp/gcp/flows/prepare_infra_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ def prepare_subnets(self, request_actions: RequestActions) -> dict[str, str]:
subnet_name=self.name_generator.subnet(
subnet_request.get_alias() or
f"Subnet {subnet_request.get_cidr().replace('/', '-')}",
self.config.reservation_info.reservation_id
),
region=self.config.region,
).self_link
Expand Down
10 changes: 7 additions & 3 deletions cloudshell/cp/gcp/flows/refresh_ip_flow.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from __future__ import annotations

import json

from attr import define
from typing_extensions import TYPE_CHECKING

Expand All @@ -24,8 +26,10 @@ class GCPRefreshIPFlow:
_resource_config: GCPResourceConfig
_cancellation_manager: CancellationContextManager

def _get_instance(self, instance_name) -> compute.Instance:
return InstanceHandler.get_vm_by_name(instance_name)
def _get_instance(self, instance_uuid) -> compute.Instance:
instance_data = json.loads(instance_uuid)
instance_data["credentials"] = self._resource_config.credentials
return InstanceHandler.get(**instance_data).instance

def refresh_ip(self) -> str:
internal_ip = ""
Expand All @@ -41,7 +45,7 @@ def refresh_ip(self) -> str:
external_ip = network_interface.get_public_ip()
if not internal_ip:
raise Exception("Internal IP address not found")
self._deployed_app.update_private_ip(self._deployed_app.name, ip)
self._deployed_app.update_private_ip(self._deployed_app.name, internal_ip)
if external_ip:
self._deployed_app.update_public_ip(
external_ip
Expand Down
20 changes: 12 additions & 8 deletions cloudshell/cp/gcp/handlers/firewall_rule.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,21 @@ class FirewallRuleHandler(BaseGCPHandler):
def firewall_client(self):
return compute_v1.FirewallsClient(credentials=self.credentials)

def get_higher_priority(self, network_name: str) -> int:
def get_higher_priority(self, network_name: str, start_priority=4000) -> int:
"""Get the next available priority for the security group."""
rules = self.list_firewall_rules_by_network(network_name)
priorities = [rule.priority for rule in (rules.allowed | rules.denied)]
return max(priorities) + 1 if priorities else 4000
priorities = [rule.priority for rule in rules]
if start_priority in priorities:
return self.get_higher_priority(network_name, start_priority + 1)
return start_priority

def get_lower_priority(self, network_name: str) -> int:
def get_lower_priority(self, network_name: str, max_priority=2000) -> int:
"""Get the next available priority for the security group."""
rules = self.list_firewall_rules_by_network(network_name)
priorities = [rule.priority for rule in (rules.allowed | rules.denied)]
return min(priorities) - 1 if priorities else 1000
priorities = [rule.priority for rule in rules]
if max_priority in priorities:
return self.get_lower_priority(network_name, max_priority - 1)
return max_priority

def list_firewall_rules_by_network(self, network_name) -> list[Firewall]:
"""List all Security Groups."""
Expand Down Expand Up @@ -118,7 +122,7 @@ def create_ingress_firewall_rule(
direction="INGRESS",
allowed=[ports_rule],
source_ranges=[src_cidr], # Specify source IP ranges
destination_ranges=[dst_cidr], # Specify destination IP ranges
# destination_ranges=[dst_cidr], # Specify destination IP ranges
priority=priority,
target_tags=[network_tag] if network_tag else None
)
Expand All @@ -131,7 +135,7 @@ def create_ingress_firewall_rule(
direction="INGRESS",
denied=[ports_rule],
source_ranges=[src_cidr], # Specify source IP ranges
destination_ranges=[dst_cidr], # Specify destination IP ranges
# destination_ranges=[dst_cidr], # Specify destination IP ranges
priority=priority,
)

Expand Down
19 changes: 10 additions & 9 deletions cloudshell/cp/gcp/handlers/instance.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ def from_template(self):
else:
disk_initialize_params.disk_type = disk.initialize_params.disk_type

if self.deploy_app.disk_image:
if self.deploy_app.project_cloud and self.deploy_app.disk_image:
disk_initialize_params.source_image = f"projects/{self.deploy_app.project_cloud}/global/images/{self.deploy_app.disk_image}"
else:
disk_initialize_params.source_image = disk.initialize_params.source_image
Expand All @@ -135,6 +135,7 @@ def from_template(self):
attached_disk.auto_delete = disk.auto_delete
attached_disk.boot = disk.boot
attached_disk.type_ = disk.type_

attached_disk.initialize_params = disk.initialize_params

instance.disks.append(attached_disk)
Expand Down Expand Up @@ -163,8 +164,8 @@ def from_machine_image(self): # machine_image.source_instance_properties

# Get the machine image
machine_image = machine_image_client.get(
project=self.resource_config.project_id,
machine_image=self.deploy_app.machine_image_name
project=self.resource_config.credentials.project_id,
machine_image=self.deploy_app.machine_image
)

# Prepare the instance configuration
Expand Down Expand Up @@ -192,7 +193,7 @@ def from_machine_image(self): # machine_image.source_instance_properties
attached_disk.auto_delete = disk.auto_delete
attached_disk.boot = disk.boot
attached_disk.type_ = disk.type_
attached_disk.initialize_params = disk.initialize_params
# attached_disk.initialize_params = disk.initialize_params
instance.disks.append(attached_disk)

# Create Network Interfaces
Expand All @@ -219,11 +220,11 @@ def _add_interfaces(self, instance):
iface_num=subnet_num
)
network_interface.subnetwork = subnet

access_config = compute_v1.AccessConfig()
access_config.name = network_interface.name
access_config.type_ = "ONE_TO_ONE_NAT"
network_interface.access_configs = [access_config]
if self.deploy_app.add_public_ip:
access_config = compute_v1.AccessConfig()
access_config.name = network_interface.name
access_config.type_ = "ONE_TO_ONE_NAT"
network_interface.access_configs = [access_config]

instance.network_interfaces.append(network_interface)

Expand Down
5 changes: 2 additions & 3 deletions cloudshell/cp/gcp/handlers/vpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,7 @@ def get_vpc_by_sandbox_id(
"""Get VPC instance by Sandbox ID."""
logger.info("Getting VPC")
network_name = GCPNameGenerator().network(sandbox_id)
network = cls.get_vpc_by_name(network_name, credentials)
return network
return cls.get_vpc_by_name(network_name, credentials)

@classmethod
def get_or_create_vpc(cls, sandbox_id: str, credentials: compute.Credentials) -> (
Expand Down Expand Up @@ -87,7 +86,7 @@ def create(cls, sandbox_id: str, credentials: compute.Credentials) -> Self:
operation_client.wait(operation=operation.name, project=credentials.project_id)

logger.info(f"VPC network '{network.name}' created successfully.")
return cls.get_vpc_by_name(credentials=credentials, network_name=network)
return cls.get_vpc_by_name(credentials=credentials, network_name=network.name)

def delete(self) -> None:
operation = self.network_client.delete(
Expand Down
2 changes: 1 addition & 1 deletion cloudshell/cp/gcp/helpers/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

SHELL_NAME = "Google GCP Cloud Provider 2G"

VM_FROM_SCRATCH_DEPLOYMENT_PATH = f"{SHELL_NAME}.GCP Instance From Scratch"
VM_FROM_SCRATCH_DEPLOYMENT_PATH = f"{SHELL_NAME}.GCP Instance From Image"
VM_FROM_TEMPLATE_DEPLOYMENT_PATH = f"{SHELL_NAME}.GCP Instance From Template"
VM_FROM_MACHINE_IMAGE_DEPLOYMENT_PATH = f"{SHELL_NAME}.GCP Instance From Machine Image" # noqa E501

Expand Down
4 changes: 2 additions & 2 deletions cloudshell/cp/gcp/helpers/name_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ def ssh_keys(self) -> str:
pass

@validator
def subnet(self, cs_subnet: str) -> str:
def subnet(self, cs_subnet: str, sandbox_id: str) -> str:
"""quali-CS_Subnet"""
return f"{GCP_NAME_PREFIX}-{re.sub(f'[{re.escape(CS_ALLOWED_SYMBOLS)}]+', '-', cs_subnet.lower().replace('-', '--'))}"
return f"{GCP_NAME_PREFIX}-{re.sub(f'[{re.escape(CS_ALLOWED_SYMBOLS)}]+', '-', cs_subnet.lower().replace('-', '--'))}-{sandbox_id[:8]}"

@validator
def network(self, reservation_id: str) -> str:
Expand Down
13 changes: 11 additions & 2 deletions cloudshell/cp/gcp/models/deploy_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from cloudshell.cp.core.request_actions.models import DeployApp

from cloudshell.cp.gcp.helpers import constants
from cloudshell.cp.gcp.helpers.constants import DISK_TYPE_MAP
from cloudshell.cp.gcp.helpers.constants import DISK_TYPE_MAP, PUBLIC_IMAGE_PROJECTS
from cloudshell.cp.gcp.helpers.network_tag_helper import parse_port_range
from cloudshell.cp.gcp.helpers.password_generator import generate_password
from cloudshell.cp.gcp.models.attributes import (
Expand Down Expand Up @@ -63,6 +63,15 @@ def __get__(self, instance, owner):
return DISK_TYPE_MAP.get(attr, "pd-standard")


class ImageProjectAttrRO(ResourceAttrRODeploymentPath):
def __get__(self, instance, owner):
if instance is None:
return self

attr = instance.attributes.get(self.get_key(instance), self.default)
return PUBLIC_IMAGE_PROJECTS.get(attr, attr)


class BaseGCPDeployApp(DeployApp):
_DO_NOT_EDIT_APP_NAME = True
ATTR_NAMES = BaseGCPDeploymentAppAttributeNames
Expand Down Expand Up @@ -97,7 +106,7 @@ class InstanceFromScratchDeployApp(BaseGCPDeployApp):
disk_size = ResourceIntAttrRODeploymentPath(ATTR_NAMES.disk_size)
# disk_rule = ResourceAttrRODeploymentPath(ATTR_NAMES.disk_rule)
disk_rule = True
project_cloud = ResourceAttrRODeploymentPath(ATTR_NAMES.project_cloud)
project_cloud = ImageProjectAttrRO(ATTR_NAMES.project_cloud)
disk_image = ResourceAttrRODeploymentPath(ATTR_NAMES.disk_image)

@property
Expand Down
4 changes: 2 additions & 2 deletions cloudshell/cp/gcp/models/deployed_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
GCPFromTemplateDeploymentAppAttributeNames,
GCPFromVMImageDeploymentAppAttributeNames,
)
from cloudshell.cp.gcp.models.deploy_app import CustomTagsAttrRO
from cloudshell.cp.gcp.models.deploy_app import CustomTagsAttrRO, OnOffBoolAttrRO


class BaseGCPDeployedApp(DeployedApp):
Expand All @@ -26,7 +26,7 @@ class BaseGCPDeployedApp(DeployedApp):
zone = ResourceAttrRODeploymentPath(ATTR_NAMES.zone)
machine_type = ResourceAttrRODeploymentPath(ATTR_NAMES.machine_type)
maintenance = ResourceAttrRODeploymentPath(ATTR_NAMES.maintenance)
auto_restart = ResourceBoolAttrRODeploymentPath(ATTR_NAMES.auto_restart)
auto_restart = OnOffBoolAttrRO(ATTR_NAMES.auto_restart)
ip_forwarding = ResourceBoolAttrRODeploymentPath(ATTR_NAMES.ip_forwarding)
ip_regex = ResourceAttrRODeploymentPath(ATTR_NAMES.ip_regex)
refresh_ip_timeout = ResourceIntAttrRODeploymentPath(ATTR_NAMES.refresh_ip_timeout)
Expand Down
2 changes: 1 addition & 1 deletion version.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.1.40
0.1.54

0 comments on commit a514396

Please sign in to comment.