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

[Image Copy] Fix snapshot create issue #1172

Merged
merged 6 commits into from
Jan 9, 2020
Merged
Show file tree
Hide file tree
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
17 changes: 15 additions & 2 deletions src/image-copy/azext_imagecopy/create_target.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
from knack.util import CLIError
from knack.log import get_logger

from msrestazure.tools import resource_id
from azure.cli.core.commands.client_factory import get_subscription_id

from azext_imagecopy.cli_utils import run_cli_command, prepare_cli_command

logger = get_logger(__name__)
Expand All @@ -18,7 +21,7 @@

# pylint: disable=too-many-statements
# pylint: disable=too-many-locals
def create_target_image(location, transient_resource_group_name, source_type, source_object_name,
def create_target_image(cmd, location, transient_resource_group_name, source_type, source_object_name,
source_os_disk_snapshot_name, source_os_disk_snapshot_url, source_os_type,
target_resource_group_name, azure_pool_frequency, tags, target_name, target_subscription,
export_as_snapshot, timeout):
Expand Down Expand Up @@ -113,11 +116,21 @@ def create_target_image(location, transient_resource_group_name, source_type, so
else:
snapshot_resource_group_name = transient_resource_group_name

storage_account_name = target_blob_path.split('.')[0].split('/')[-1]
if target_subscription:
subscription_id = target_subscription
else:
subscription_id = get_subscription_id(cmd.cli_ctx)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this original service behavior? otherwise, pls help to document the behavior.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The goal of this PR is to infer storage account ID. If target_subscription is None, use the default subscription.
Code that creates storage account.

cli_cmd = prepare_cli_command(['storage', 'account', 'create',
                                   '--name', target_storage_account_name,
                                   '--resource-group', transient_resource_group_name,
                                   '--location', location,
                                   '--sku', 'Standard_LRS'],
                                  subscription=target_subscription)

source_storage_account_id = resource_id(
subscription=subscription_id, resource_group=transient_resource_group_name,
namespace='Microsoft.Storage', type='storageAccounts', name=storage_account_name)

cli_cmd = prepare_cli_command(['snapshot', 'create',
'--resource-group', snapshot_resource_group_name,
'--name', target_snapshot_name,
'--location', location,
'--source', target_blob_path],
'--source', target_blob_path,
'--source-storage-account-id', source_storage_account_id],
subscription=target_subscription)

json_output = run_cli_command(cli_cmd, return_as_json=True)
Expand Down
4 changes: 2 additions & 2 deletions src/image-copy/azext_imagecopy/custom.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
# pylint: disable=too-many-statements
# pylint: disable=too-many-locals
# pylint: disable=too-many-branches
def imagecopy(source_resource_group_name, source_object_name, target_location,
def imagecopy(cmd, source_resource_group_name, source_object_name, target_location,
target_resource_group_name, temporary_resource_group_name='image-copy-rg',
source_type='image', cleanup='false', parallel_degree=-1, tags=None, target_name=None,
target_subscription=None, export_as_snapshot='false', timeout=3600):
Expand Down Expand Up @@ -130,7 +130,7 @@ def imagecopy(source_resource_group_name, source_object_name, target_location,
logger.debug("Starting sync process for all locations")
for location in target_location:
location = location.strip()
create_target_image(location, transient_resource_group_name, source_type,
create_target_image(cmd, location, transient_resource_group_name, source_type,
source_object_name, source_os_disk_snapshot_name, source_os_disk_snapshot_url,
source_os_type, target_resource_group_name, azure_pool_frequency,
tags, target_name, target_subscription, export_as_snapshot, timeout)
Expand Down
Loading