Skip to content
This repository was archived by the owner on Sep 3, 2022. It is now read-only.

Added Shared VPC Support #2134

Merged
merged 4 commits into from
Jun 4, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 29 additions & 5 deletions tools/cli/commands/create.py
Original file line number Diff line number Diff line change
Expand Up @@ -766,25 +766,27 @@ def create_firewall_rule(args, gcloud_compute, network_name, rule_name):
Raises:
subprocess.CalledProcessError: If the `gcloud` command fails
"""
firewall_args = get_firewall_args(args, network_name)
if utils.print_info_messages(args):
print('Creating the firewall rule {0}'.format(rule_name))
create_cmd = [
'firewall-rules', 'create', rule_name,
'--allow', 'tcp:22',
'--network', network_name,
'--description', _DATALAB_FIREWALL_RULE_DESCRIPTION]
utils.call_gcloud_quietly(args, gcloud_compute, create_cmd)
utils.call_gcloud_quietly(firewall_args, gcloud_compute, create_cmd)
return


def has_unexpected_firewall_rules(args, gcloud_compute, network_name):
rule_name = _DATALAB_FIREWALL_RULE_TEMPLATE.format(network_name)
rule_name = generate_firewall_rule_name(network_name)
firewall_args = get_firewall_args(args, network_name)
list_cmd = [
'firewall-rules', 'list',
'--filter', 'network~.^*{0}$'.format(network_name),
'--format', 'value(name)']
with tempfile.TemporaryFile() as tf:
gcloud_compute(args, list_cmd, stdout=tf)
gcloud_compute(firewall_args, list_cmd, stdout=tf)
tf.seek(0)
matching_rules = tf.read().decode('utf-8').strip()
if matching_rules and (matching_rules != rule_name):
Expand Down Expand Up @@ -813,17 +815,39 @@ def ensure_firewall_rule_exists(args, gcloud_compute, network_name):
Raises:
subprocess.CalledProcessError: If the `gcloud` command fails
"""
rule_name = _DATALAB_FIREWALL_RULE_TEMPLATE.format(network_name)
firewall_args = get_firewall_args(args, network_name)
rule_name = generate_firewall_rule_name(network_name)
get_cmd = [
'firewall-rules', 'describe', rule_name, '--format', 'value(name)']
try:
utils.call_gcloud_quietly(
args, gcloud_compute, get_cmd, report_errors=False)
firewall_args, gcloud_compute, get_cmd, report_errors=False)
except subprocess.CalledProcessError:
create_firewall_rule(args, gcloud_compute, network_name, rule_name)
return


def generate_firewall_rule_name(network_name):
"""Converts network name to a valid rule name to support shared vpc"""
if "/" in network_name:
return _DATALAB_FIREWALL_RULE_TEMPLATE.format(
network_name.split("/")[-1])
else:
return _DATALAB_FIREWALL_RULE_TEMPLATE.format(network_name)


def get_firewall_args(args, network_name):
"""
Shared VPCs firewall rules need to be created in the host project.
This modifies the args to the host project for commands that need it.
"""
if "/" in network_name:
project_name = network_name.split("/")[1]
args.project = project_name

return args


def create_disk(args, gcloud_compute, disk_name):
"""Create the user's persistent disk.

Expand Down