Skip to content

Commit

Permalink
Merge pull request #2 from QualiSystemsLab/feature/private_image
Browse files Browse the repository at this point in the history
added support for private images
  • Loading branch information
kalsky authored Apr 28, 2022
2 parents a3dfdc9 + 856c6bb commit 3e3bdd4
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 6 deletions.
7 changes: 7 additions & 0 deletions deployments/deploy-instance-deployment-path.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@ node_types:
description: 'The id of the image to be used for deploying the app.'
default: ''
tags: [user_input] # editable_only_in_app_template
Image Source:
type: string
description: "The source of the image. Supported values: 'public' or 'private'. Use 'image' for private source not 'machine image'"
default: 'public'
tags: [ user_input ]
constraints:
- valid_values: [ public, private ]
Machine Type:
type: string
description: 'The size of the instance. Can be one of the pre-defined ones or a custom one.'
Expand Down
2 changes: 1 addition & 1 deletion shell-definition.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ tosca_definitions_version: tosca_simple_yaml_1_0
metadata:
template_name: Google Cloud Provider
template_author: yaniv.k
template_version: 0.9.8
template_version: 0.9.9
template_icon: shell-icon.png

description: >
Expand Down
25 changes: 20 additions & 5 deletions src/ccp/gcp/gcp_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,8 +214,10 @@ def deploy_instance(self, context, cloudshell_session, cloud_provider_resource,
deployment_model_attributes[deployment_path + '.Machine Type'],
deployment_model_attributes[deployment_path + '.Disk Type'],
deployment_model_attributes[deployment_path + '.Disk Size'],
network_data)
network_data,
image_source_type=deployment_model_attributes[deployment_path + '.Image Source'])
except Exception as e:
self.logger.exception("==>")
return DeployAppResult(actionId=deploy_app_action.actionId, success=False, errorMessage=e.message)

connect_subnet_results = []
Expand Down Expand Up @@ -289,9 +291,9 @@ def deploy_instance_from_template(self, context, cloudshell_session, cloud_provi
self.logger.error(ex.message)
raise ex


def _create_instance(self, actionId, cloud_provider_resource, vm_unique_name, image_project, image_id, machine_type,
disk_type, disk_size, network_data, input_user='', decrypted_input_password=''):
disk_type, disk_size, network_data, input_user='', decrypted_input_password='',
image_source_type='public'):

client = self._get_client()

Expand All @@ -302,6 +304,8 @@ def _create_instance(self, actionId, cloud_provider_resource, vm_unique_name, im

diskType = disk_type.lower()

source_image_uri = self._prepare_source_image(image_id, image_project, image_source_type)

instance_body = {
"kind": "compute#instance",
"name": vm_unique_name,
Expand All @@ -326,7 +330,7 @@ def _create_instance(self, actionId, cloud_provider_resource, vm_unique_name, im
"autoDelete": True,
"deviceName": "instance-1",
"initializeParams": {
"sourceImage": "projects/{}/global/images/{}".format(image_project, image_id),
"sourceImage": source_image_uri,
"diskType": "projects/{}/zones/{}/diskTypes/pd-{}".format(self.project, zone, diskType),
"diskSizeGb": disk_size
}
Expand Down Expand Up @@ -359,7 +363,7 @@ def _create_instance(self, actionId, cloud_provider_resource, vm_unique_name, im
"deletionProtection": False
}

self.logger.debug("instance_body: " + str(instance_body))
self.logger.info("instance_body: " + str(instance_body))

request = client.instances().insert(project=self.project, zone=zone, body=instance_body)
response = request.execute()
Expand All @@ -374,6 +378,17 @@ def _create_instance(self, actionId, cloud_provider_resource, vm_unique_name, im
deployedAppAddress=vm_details_data.vmNetworkData[0].privateIpAddress,
vmDetailsData=vm_details_data)

def _prepare_source_image(self, image_id, image_project, image_source_type):
source_image_uri = ""
if image_source_type == "public":
source_image_uri = "projects/{}/global/images/{}".format(image_project, image_id)
elif image_source_type == "private":
source_image_uri = "global/images/{}".format(image_id)
else:
raise ValueError("Unsupported image source {}".format(image_source_type))

return source_image_uri

def _create_instance_from_template(self, actionId, cloud_provider_resource, vm_unique_name, template_name,
network_data, input_user='', decrypted_input_password=''):

Expand Down

0 comments on commit 3e3bdd4

Please sign in to comment.