1 # -*- coding: utf-8 -*-
3 # Licensed under the Apache License, Version 2.0 (the "License"); you may
4 # not use this file except in compliance with the License. You may obtain
5 # a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 # License for the specific language governing permissions and limitations
19 from random
import choice
as random_choice
22 from cryptography
.hazmat
.backends
import default_backend
as crypto_default_backend
23 from cryptography
.hazmat
.primitives
import serialization
as crypto_serialization
24 from cryptography
.hazmat
.primitives
.asymmetric
import rsa
25 from google
.oauth2
import service_account
26 import googleapiclient
.discovery
27 from osm_ro_plugin
import vimconn
29 __author__
= "Sergio Gallardo Ruiz"
30 __date__
= "$11-aug-2021 08:30:00$"
33 if getenv("OSMRO_PDB_DEBUG"):
42 class vimconnector(vimconn
.VimConnector
):
43 # Translate Google Cloud provisioning state to OSM provision state
44 # The first three ones are the transitional status once a user initiated action has been requested
45 # Once the operation is complete, it will transition into the states Succeeded or Failed
46 # https://cloud.google.com/compute/docs/instances/instance-life-cycle
47 provision_state2osm
= {
48 "PROVISIONING": "BUILD",
52 # Translate azure power state to OSM provision state
56 "STOPPING": "INACTIVE",
57 "SUSPENDING": "INACTIVE",
58 "SUSPENDED": "INACTIVE",
59 "TERMINATED": "INACTIVE",
62 # If a net or subnet is tried to be deleted and it has an associated resource, the net is marked "to be deleted"
63 # (incluid it's name in the following list). When the instance is deleted, its associated net will be deleted if
64 # they are present in that list
65 nets_to_be_deleted
= []
82 Constructor of VIM. Raise an exception is some needed parameter is missing, but it must not do any connectivity
83 checking against the VIM
84 Using common constructor parameters.
85 In this case: config must include the following parameters:
86 subscription_id: assigned subscription identifier
87 region_name: current region for network
88 config may also include the following parameter:
89 flavors_pattern: pattern that will be used to select a range of vm sizes, for example
90 "^((?!Standard_B).)*$" will filter out Standard_B range that is cheap but is very overused
91 "^Standard_B" will select a serie B maybe for test environment
93 vimconn
.VimConnector
.__init
__(
108 # Variable that indicates if client must be reloaded or initialized
109 self
.reload_client
= False
113 log_format_simple
= (
114 "%(asctime)s %(levelname)s %(name)s %(filename)s:%(lineno)s %(message)s"
116 log_format_complete
= "%(asctime)s %(levelname)s %(name)s %(filename)s:%(lineno)s %(funcName)s(): %(message)s"
117 log_formatter_simple
= logging
.Formatter(
118 log_format_simple
, datefmt
="%Y-%m-%dT%H:%M:%S"
120 self
.handler
= logging
.StreamHandler()
121 self
.handler
.setFormatter(log_formatter_simple
)
123 self
.logger
= logging
.getLogger("ro.vim.gcp")
124 self
.logger
.addHandler(self
.handler
)
126 self
.logger
.setLevel(getattr(logging
, log_level
))
128 if self
.logger
.getEffectiveLevel() == logging
.DEBUG
:
129 log_formatter
= logging
.Formatter(
130 log_format_complete
, datefmt
="%Y-%m-%dT%H:%M:%S"
132 self
.handler
.setFormatter(log_formatter
)
134 self
.logger
.debug("Google Cloud connection init")
136 self
.project
= tenant_id
or tenant_name
138 # REGION - Google Cloud considers regions and zones. A specific region can have more than one zone
139 # (for instance: region us-west1 with the zones us-west1-a, us-west1-b and us-west1-c)
140 # So the region name specified in the config will be considered as a specific zone for GC and
141 # the region will be calculated from that without the preffix.
142 if "region_name" in config
:
143 self
.zone
= config
.get("region_name")
144 self
.region
= self
.zone
.rsplit("-", 1)[0]
146 raise vimconn
.VimConnException(
147 "Google Cloud region_name is not specified at config"
151 self
.logger
.debug("Config: %s", config
)
152 scopes
= ["https://www.googleapis.com/auth/cloud-platform"]
153 self
.credentials
= None
154 if "credentials" in config
:
155 self
.logger
.debug("Setting credentials")
156 # Settings Google Cloud credentials dict
157 credentials_body
= config
["credentials"]
158 # self.logger.debug("Credentials filtered: %s", credentials_body)
159 credentials
= service_account
.Credentials
.from_service_account_info(
162 if "sa_file" in config
:
163 credentials
= service_account
.Credentials
.from_service_account_file(
164 config
.get("sa_file"), scopes
=scopes
166 self
.logger
.debug("Credentials: %s", credentials
)
167 # Construct a Resource for interacting with an API.
168 self
.credentials
= credentials
170 self
.conn_compute
= googleapiclient
.discovery
.build(
171 "compute", "v1", credentials
=credentials
173 except Exception as e
:
174 self
._format
_vimconn
_exception
(e
)
176 raise vimconn
.VimConnException(
177 "It is not possible to init GCP with no credentials"
180 def _reload_connection(self
):
182 Called before any operation, checks python Google Cloud clientsself.reload_client
184 if self
.reload_client
:
185 self
.logger
.debug("reloading google cloud client")
188 # Set to client created
189 self
.conn_compute
= googleapiclient
.discovery
.build("compute", "v1")
190 except Exception as e
:
191 self
._format
_vimconn
_exception
(e
)
193 def _format_vimconn_exception(self
, e
):
195 Transforms a generic exception to a vimConnException
197 self
.logger
.error("Google Cloud plugin error: {}".format(e
))
198 if isinstance(e
, vimconn
.VimConnException
):
201 # In case of generic error recreate client
202 self
.reload_client
= True
203 raise vimconn
.VimConnException(type(e
).__name
__ + ": " + str(e
))
205 def _wait_for_global_operation(self
, operation
):
207 Waits for the end of the specific operation
208 :operation: operation name
211 self
.logger
.debug("Waiting for operation %s", operation
)
215 self
.conn_compute
.globalOperations()
216 .get(project
=self
.project
, operation
=operation
)
220 if result
["status"] == "DONE":
221 if "error" in result
:
222 raise vimconn
.VimConnException(result
["error"])
227 def _wait_for_zone_operation(self
, operation
):
229 Waits for the end of the specific operation
230 :operation: operation name
233 self
.logger
.debug("Waiting for operation %s", operation
)
237 self
.conn_compute
.zoneOperations()
238 .get(project
=self
.project
, operation
=operation
, zone
=self
.zone
)
242 if result
["status"] == "DONE":
243 if "error" in result
:
244 raise vimconn
.VimConnException(result
["error"])
249 def _wait_for_region_operation(self
, operation
):
251 Waits for the end of the specific operation
252 :operation: operation name
255 self
.logger
.debug("Waiting for operation %s", operation
)
259 self
.conn_compute
.regionOperations()
260 .get(project
=self
.project
, operation
=operation
, region
=self
.region
)
264 if result
["status"] == "DONE":
265 if "error" in result
:
266 raise vimconn
.VimConnException(result
["error"])
277 provider_network_profile
=None,
280 Adds a network to VIM
281 :param net_name: name of the network
282 :param net_type: not used for Google Cloud networks
283 :param ip_profile: not used for Google Cloud networks
284 :param shared: Not allowed for Google Cloud Connector
285 :param provider_network_profile: (optional)
287 contains {segmentation-id: vlan, provider-network: vim_netowrk}
288 :return: a tuple with the network identifier and created_items, or raises an exception on error
289 created_items can be None or a dictionary where this method can include key-values that will be passed to
290 the method delete_network. Can be used to store created segments, created l2gw connections, etc.
291 Format is vimconnector dependent, but do not use nested dictionaries and a value of None should be the same
296 "new_network begin: net_name %s net_type %s ip_profile %s shared %s provider_network_profile %s",
301 provider_network_profile
,
303 net_name
= self
._check
_vm
_name
(net_name
)
304 net_name
= self
._randomize
_name
(net_name
)
305 self
.logger
.debug("create network name %s, ip_profile %s", net_name
, ip_profile
)
308 self
.logger
.debug("creating network_name: {}".format(net_name
))
310 network
= "projects/{}/global/networks/default".format(self
.project
)
312 if ip_profile
is not None:
313 if "subnet_address" in ip_profile
:
314 subnet_address
= ip_profile
["subnet_address"]
316 "name": str(net_name
),
317 "description": net_name
,
319 "ipCidrRange": subnet_address
,
320 # The network is created in AUTO mode (one subnet per region is created)
321 # "autoCreateSubnetworks": True,
322 "autoCreateSubnetworks": False,
326 self
.conn_compute
.networks()
327 .insert(project
=self
.project
, body
=network_body
)
330 self
._wait
_for
_global
_operation
(operation
["name"])
331 self
.logger
.debug("created network_name: {}".format(net_name
))
333 # Adding firewall rules to allow the traffic in the network:
334 self
._create
_firewall
_rules
(net_name
)
336 # create subnetwork, even if there is no profile
341 if not ip_profile
.get("subnet_address"):
342 # Fake subnet is required
343 subnet_rand
= random
.randint(0, 255)
344 ip_profile
["subnet_address"] = "192.168.{}.0/24".format(subnet_rand
)
346 subnet_name
= net_name
+ "-subnet"
347 subnet_id
= self
._new
_subnet
(
348 subnet_name
, ip_profile
, operation
["targetLink"]
351 self
.logger
.debug("new_network Return: subnet_id: %s", subnet_id
)
353 except Exception as e
:
354 self
._format
_vimconn
_exception
(e
)
356 def _new_subnet(self
, subnet_name
, ip_profile
, network
):
358 Adds a tenant network to VIM. It creates a new subnet at existing base vnet
359 :param net_name: subnet name
361 subnet-address: if it is not provided a subnet/24 in the default vnet is created,
362 otherwise it creates a subnet in the indicated address
363 :return: a tuple with the network identifier and created_items, or raises an exception on error
366 "_new_subnet begin: subnet_name %s ip_profile %s network %s",
372 "create subnet name %s, ip_profile %s", subnet_name
, ip_profile
376 self
.logger
.debug("creating subnet_name: {}".format(subnet_name
))
379 "name": str(subnet_name
),
380 "description": subnet_name
,
382 "ipCidrRange": ip_profile
["subnet_address"],
386 self
.conn_compute
.subnetworks()
388 project
=self
.project
,
390 body
=subnetwork_body
,
394 self
._wait
_for
_region
_operation
(operation
["name"])
396 self
.logger
.debug("created subnet_name: {}".format(subnet_name
))
399 "_new_subnet Return: (%s,%s)",
400 "regions/%s/subnetworks/%s" % (self
.region
, subnet_name
),
403 return "regions/%s/subnetworks/%s" % (self
.region
, subnet_name
), None
404 except Exception as e
:
405 self
._format
_vimconn
_exception
(e
)
407 def get_network_list(self
, filter_dict
={}):
408 """Obtain tenant networks of VIM
412 shared: boolean, not implemented in GC
413 tenant_id: tenant, not used in GC, all networks same tenants
414 admin_state_up: boolean, not implemented in GC
415 status: 'ACTIVE', not implemented in GC #
416 Returns the network list of dictionaries
418 self
.logger
.debug("get_network_list begin: filter_dict %s", filter_dict
)
420 "Getting network (subnetwork) from VIM filter: {}".format(str(filter_dict
))
424 if self
.reload_client
:
425 self
._reload
_connection
()
429 request
= self
.conn_compute
.subnetworks().list(
430 project
=self
.project
, region
=self
.region
433 while request
is not None:
434 response
= request
.execute()
435 self
.logger
.debug("Network list: %s", response
)
436 for net
in response
["items"]:
437 self
.logger
.debug("Network in list: {}".format(str(net
["name"])))
438 if filter_dict
is not None:
439 if "name" in filter_dict
.keys():
441 filter_dict
["name"] == net
["name"]
442 or filter_dict
["name"] == net
["selfLink"]
444 self
.logger
.debug("Network found: %s", net
["name"])
447 "id": str(net
["selfLink"]),
448 "name": str(net
["name"]),
449 "network": str(net
["network"]),
455 "id": str(net
["selfLink"]),
456 "name": str(net
["name"]),
457 "network": str(net
["network"]),
460 request
= self
.conn_compute
.subnetworks().list_next(
461 previous_request
=request
, previous_response
=response
464 self
.logger
.debug("get_network_list Return: net_list %s", net_list
)
467 except Exception as e
:
468 self
.logger
.error("Error in get_network_list()", exc_info
=True)
469 raise vimconn
.VimConnException(e
)
471 def get_network(self
, net_id
):
472 self
.logger
.debug("get_network begin: net_id %s", net_id
)
473 # res_name = self._get_resource_name_from_resource_id(net_id)
474 self
._reload
_connection
()
476 self
.logger
.debug("Get network: %s", net_id
)
477 filter_dict
= {"name": net_id
}
478 network_list
= self
.get_network_list(filter_dict
)
479 self
.logger
.debug("Network list: %s", network_list
)
484 self
.logger
.debug("get_network Return: network_list[0] %s", network_list
[0])
485 return network_list
[0]
487 def delete_network(self
, net_id
, created_items
=None):
489 Removes a tenant network from VIM and its associated elements
490 :param net_id: VIM identifier of the network, provided by method new_network
491 :param created_items: dictionary with extra items to be deleted. provided by method new_network
492 Returns the network identifier or raises an exception upon error or when network is not found
496 "delete_network begin: net_id %s created_items %s",
500 self
.logger
.debug("Deleting network: {}".format(str(net_id
)))
503 net_name
= self
._get
_resource
_name
_from
_resource
_id
(net_id
)
505 # Check associated VMs
506 self
.conn_compute
.instances().list(
507 project
=self
.project
, zone
=self
.zone
510 net_id
= self
.delete_subnet(net_name
, created_items
)
512 self
.logger
.debug("delete_network Return: net_id %s", net_id
)
515 except Exception as e
:
516 self
.logger
.error("Error in delete_network()", exc_info
=True)
517 raise vimconn
.VimConnException(e
)
519 def delete_subnet(self
, net_id
, created_items
=None):
521 Removes a tenant network from VIM and its associated elements
522 :param net_id: VIM identifier of the network, provided by method new_network
523 :param created_items: dictionary with extra items to be deleted. provided by method new_network
524 Returns the network identifier or raises an exception upon error or when network is not found
528 "delete_subnet begin: net_id %s created_items %s",
532 self
.logger
.debug("Deleting subnetwork: {}".format(str(net_id
)))
535 # If the network has no more subnets, it will be deleted too
536 net_info
= self
.get_network(net_id
)
537 # If the subnet is in use by another resource, the deletion will
538 # be retried N times before abort the operation
539 created_items
= created_items
or {}
540 created_items
[net_id
] = False
544 self
.conn_compute
.subnetworks()
546 project
=self
.project
,
552 self
._wait
_for
_region
_operation
(operation
["name"])
553 if net_id
in self
.nets_to_be_deleted
:
554 self
.nets_to_be_deleted
.remove(net_id
)
555 except Exception as e
:
557 e
.args
[0]["status"] == "400"
558 ): # Resource in use, so the net is marked to be deleted
559 self
.logger
.debug("Subnet still in use")
560 self
.nets_to_be_deleted
.append(net_id
)
562 raise vimconn
.VimConnException(e
)
564 self
.logger
.debug("nets_to_be_deleted: %s", self
.nets_to_be_deleted
)
566 # If the network has no more subnets, it will be deleted too
567 # if "network" in net_info and net_id not in self.nets_to_be_deleted:
568 if "network" in net_info
:
569 network_name
= self
._get
_resource
_name
_from
_resource
_id
(
574 # Deletion of the associated firewall rules:
575 self
._delete
_firewall
_rules
(network_name
)
578 self
.conn_compute
.networks()
580 project
=self
.project
,
581 network
=network_name
,
585 self
._wait
_for
_global
_operation
(operation
["name"])
586 except Exception as e
:
587 self
.logger
.debug("error deleting associated network %s", e
)
589 self
.logger
.debug("delete_subnet Return: net_id %s", net_id
)
592 except Exception as e
:
593 self
.logger
.error("Error in delete_network()", exc_info
=True)
594 raise vimconn
.VimConnException(e
)
596 def new_flavor(self
, flavor_data
):
598 It is not allowed to create new flavors (machine types) in Google Cloud, must always use an existing one
600 raise vimconn
.VimConnNotImplemented(
601 "It is not possible to create new flavors in Google Cloud"
604 def new_tenant(self
, tenant_name
, tenant_description
):
606 It is not allowed to create new tenants in Google Cloud
608 raise vimconn
.VimConnNotImplemented(
609 "It is not possible to create a TENANT in Google Cloud"
612 def get_flavor(self
, flavor_id
):
614 Obtains the flavor_data from the flavor_id/machine type id
616 self
.logger
.debug("get_flavor begin: flavor_id %s", flavor_id
)
620 self
.conn_compute
.machineTypes()
621 .get(project
=self
.project
, zone
=self
.zone
, machineType
=flavor_id
)
624 flavor_data
= response
625 self
.logger
.debug("Machine type data: %s", flavor_data
)
629 "id": flavor_data
["id"],
631 "id_complete": flavor_data
["selfLink"],
632 "ram": flavor_data
["memoryMb"],
633 "vcpus": flavor_data
["guestCpus"],
634 "disk": flavor_data
["maximumPersistentDisksSizeGb"],
637 self
.logger
.debug("get_flavor Return: flavor %s", flavor
)
640 raise vimconn
.VimConnNotFoundException(
641 "flavor '{}' not found".format(flavor_id
)
643 except Exception as e
:
644 self
._format
_vimconn
_exception
(e
)
646 # Google Cloud VM names can not have some special characters
647 def _check_vm_name(self
, vm_name
):
649 Checks vm name, in case the vm has not allowed characters they are removed, not error raised
650 Only lowercase and hyphens are allowed
652 chars_not_allowed_list
= "~!@#$%^&*()=+_[]{}|;:<>/?."
654 # First: the VM name max length is 64 characters
655 vm_name_aux
= vm_name
[:62]
657 # Second: replace not allowed characters
658 for elem
in chars_not_allowed_list
:
659 # Check if string is in the main string
660 if elem
in vm_name_aux
:
661 # self.logger.debug("Dentro del IF")
663 vm_name_aux
= vm_name_aux
.replace(elem
, "-")
665 return vm_name_aux
.lower()
667 def get_flavor_id_from_data(self
, flavor_dict
):
668 self
.logger
.debug("get_flavor_id_from_data begin: flavor_dict %s", flavor_dict
)
669 filter_dict
= flavor_dict
or {}
673 self
.conn_compute
.machineTypes()
674 .list(project
=self
.project
, zone
=self
.zone
)
677 machine_types_list
= response
["items"]
678 # self.logger.debug("List of machine types: %s", machine_types_list)
680 cpus
= filter_dict
.get("vcpus") or 0
681 memMB
= filter_dict
.get("ram") or 0
682 # Workaround (it should be 0)
683 numberInterfaces
= len(filter_dict
.get("interfaces", [])) or 4
686 filtered_machines
= []
687 for machine_type
in machine_types_list
:
689 machine_type
["guestCpus"] >= cpus
690 and machine_type
["memoryMb"] >= memMB
691 # In Google Cloud the number of virtual network interfaces scales with
692 # the number of virtual CPUs with a minimum of 2 and a maximum of 8:
693 # https://cloud.google.com/vpc/docs/create-use-multiple-interfaces#max-interfaces
694 and machine_type
["guestCpus"] >= numberInterfaces
696 filtered_machines
.append(machine_type
)
698 # self.logger.debug("Filtered machines: %s", filtered_machines)
701 listedFilteredMachines
= sorted(
705 float(k
["memoryMb"]),
706 int(k
["maximumPersistentDisksSizeGb"]),
710 # self.logger.debug("Sorted filtered machines: %s", listedFilteredMachines)
712 if listedFilteredMachines
:
714 "get_flavor_id_from_data Return: listedFilteredMachines[0][name] %s",
715 listedFilteredMachines
[0]["name"],
717 return listedFilteredMachines
[0]["name"]
719 raise vimconn
.VimConnNotFoundException(
720 "Cannot find any flavor matching '{}'".format(str(flavor_dict
))
723 except Exception as e
:
724 self
._format
_vimconn
_exception
(e
)
726 def delete_flavor(self
, flavor_id
):
727 raise vimconn
.VimConnNotImplemented(
728 "It is not possible to delete a flavor in Google Cloud"
731 def delete_tenant(self
, tenant_id
):
732 raise vimconn
.VimConnNotImplemented(
733 "It is not possible to delete a TENANT in Google Cloud"
736 def new_image(self
, image_dict
):
738 This function comes from the early days when we though the image could be embedded in the package.
739 Unless OSM manages VM images E2E from NBI to RO, this function does not make sense to be implemented.
741 raise vimconn
.VimConnNotImplemented("Not implemented")
743 def get_image_id_from_path(self
, path
):
745 This function comes from the early days when we though the image could be embedded in the package.
746 Unless OSM manages VM images E2E from NBI to RO, this function does not make sense to be implemented.
748 raise vimconn
.VimConnNotImplemented("Not implemented")
750 def get_image_list(self
, filter_dict
={}):
751 """Obtain tenant images from VIM
753 name: image name with the format: image project:image family:image version
754 If some part of the name is provide ex: publisher:offer it will search all availables skus and version
755 for the provided publisher and offer
756 id: image uuid, currently not supported for azure
757 Returns the image list of dictionaries:
758 [{<the fields at Filter_dict plus some VIM specific>}, ...]
761 self
.logger
.debug("get_image_list begin: filter_dict %s", filter_dict
)
765 # Get image id from parameter image_id:
766 # <image Project>:image-family:<family> => Latest version of the family
767 # <image Project>:image:<image> => Specific image
768 # <image Project>:<image> => Specific image
770 image_info
= filter_dict
["name"].split(":")
771 image_project
= image_info
[0]
772 if len(image_info
) == 2:
774 image_item
= image_info
[1]
775 if len(image_info
) == 3:
776 image_type
= image_info
[1]
777 image_item
= image_info
[2]
779 raise vimconn
.VimConnNotFoundException("Wrong format for image")
782 if image_type
== "image-family":
784 self
.conn_compute
.images()
785 .getFromFamily(project
=image_project
, family
=image_item
)
788 elif image_type
== "image":
790 self
.conn_compute
.images()
791 .get(project
=image_project
, image
=image_item
)
795 raise vimconn
.VimConnNotFoundException("Wrong format for image")
798 "id": "projects/%s/global/images/%s"
799 % (image_project
, image_response
["name"]),
801 [image_project
, image_item
, image_response
["name"]]
806 self
.logger
.debug("get_image_list Return: image_list %s", image_list
)
809 except Exception as e
:
810 self
._format
_vimconn
_exception
(e
)
812 def delete_inuse_nic(self
, nic_name
):
813 raise vimconn
.VimConnNotImplemented("Not necessary")
815 def delete_image(self
, image_id
):
816 raise vimconn
.VimConnNotImplemented("Not implemented")
818 def action_vminstance(self
, vm_id
, action_dict
, created_items
={}):
819 """Send and action over a VM instance from VIM
820 Returns the vm_id if the action was successfully sent to the VIM
822 raise vimconn
.VimConnNotImplemented("Not necessary")
824 def _randomize_name(self
, name
):
825 """Adds a random string to allow requests with the same VM name
826 Returns the name with an additional random string (if the total size is bigger
827 than 62 the original name will be truncated)
836 + "".join(random_choice("0123456789abcdef") for _
in range(12))
838 self
.conn_compute
.instances().get(
839 project
=self
.project
, zone
=self
.zone
, instance
=random_name
841 # If no exception is arisen, the random name exists for an instance,
842 # so a new random name must be generated
844 except Exception as e
:
845 if e
.args
[0]["status"] == "404":
846 self
.logger
.debug("New random name: %s", random_name
)
850 "Exception generating random name (%s) for the instance", name
852 self
._format
_vimconn
_exception
(e
)
861 image_id
=None, # <image project>:(image|image-family):<image/family id>
863 affinity_group_list
=None,
867 availability_zone_index
=None,
868 availability_zone_list
=None,
871 "new_vminstance begin: name: %s, image_id: %s, flavor_id: %s, net_list: %s, cloud_config: %s, "
872 "disk_list: %s, availability_zone_index: %s, availability_zone_list: %s",
879 availability_zone_index
,
880 availability_zone_list
,
883 if self
.reload_client
:
884 self
._reload
_connection
()
886 # Validate input data is valid
887 # # First of all, the name must be adapted because Google Cloud only allows names consist of
888 # lowercase letters (a-z), numbers and hyphens (?:[a-z](?:[-a-z0-9]{0,61}[a-z0-9])?)
889 vm_name
= self
._check
_vm
_name
(name
)
890 vm_name
= self
._randomize
_name
(vm_name
)
893 # At least one network must be provided
895 raise vimconn
.VimConnException(
896 "At least one net must be provided to create a new VM"
901 metadata
= self
._build
_metadata
(vm_name
, cloud_config
)
903 # Building network interfaces list
904 network_interfaces
= []
907 if not net
.get("net_id"):
908 if not net
.get("name"):
913 ] = "regions/%s/subnetworks/" % self
.region
+ net
.get("name")
915 net_iface
["subnetwork"] = net
.get("net_id")
916 # In order to get an external IP address, the key "accessConfigs" must be used
917 # in the interace. It has to be of type "ONE_TO_ONE_NAT" and name "External NAT"
918 if net
.get("floating_ip", False) or (
919 net
["use"] == "mgmt" and self
.config
.get("use_floating_ip")
921 net_iface
["accessConfigs"] = [
922 {"type": "ONE_TO_ONE_NAT", "name": "External NAT"}
925 network_interfaces
.append(net_iface
)
927 self
.logger
.debug("Network interfaces: %s", network_interfaces
)
929 self
.logger
.debug("Source image: %s", image_id
)
933 "machineType": self
.get_flavor(flavor_id
)["id_complete"],
934 # Specify the boot disk and the image to use as a source.
939 "initializeParams": {
940 "sourceImage": image_id
,
944 # Specify the network interfaces
945 "networkInterfaces": network_interfaces
,
946 "metadata": metadata
,
950 self
.conn_compute
.instances()
951 .insert(project
=self
.project
, zone
=self
.zone
, body
=vm_parameters
)
954 self
._wait
_for
_zone
_operation
(response
["name"])
956 # The created instance info is obtained to get the name of the generated network interfaces (nic0, nic1...)
958 self
.conn_compute
.instances()
959 .get(project
=self
.project
, zone
=self
.zone
, instance
=vm_name
)
962 self
.logger
.debug("instance get: %s", response
)
963 vm_id
= response
["name"]
965 # The generated network interfaces in the instance are include in net_list:
966 for _
, net
in enumerate(net_list
):
967 for net_ifaces
in response
["networkInterfaces"]:
970 network_id
= self
._get
_resource
_name
_from
_resource
_id
(
974 network_id
= self
._get
_resource
_name
_from
_resource
_id
(
977 if network_id
== self
._get
_resource
_name
_from
_resource
_id
(
978 net_ifaces
["subnetwork"]
980 net
["vim_id"] = net_ifaces
["name"]
983 "new_vminstance Return: (name %s, created_items %s)",
987 return vm_name
, created_items
989 except Exception as e
:
990 # Rollback vm creacion
991 if vm_id
is not None:
993 self
.logger
.debug("exception creating vm try to rollback")
994 self
.delete_vminstance(vm_id
, created_items
)
995 except Exception as e2
:
996 self
.logger
.error("new_vminstance rollback fail {}".format(e2
))
1000 "Exception creating new vminstance: %s", e
, exc_info
=True
1002 self
._format
_vimconn
_exception
(e
)
1004 def _build_metadata(self
, vm_name
, cloud_config
):
1007 metadata
["items"] = []
1009 # if there is a cloud-init load it
1011 self
.logger
.debug("cloud config: %s", cloud_config
)
1012 _
, userdata
= self
._create
_user
_data
(cloud_config
)
1013 metadata
["items"].append({"key": "user-data", "value": userdata
})
1015 # either password of ssh-keys are required
1016 # we will always use ssh-keys, in case it is not available we will generate it
1018 if cloud_config and cloud_config.get("key-pairs"):
1021 if cloud_config.get("key-pairs"):
1022 if isinstance(cloud_config["key-pairs"], list):
1023 # Transform the format "<key> <user@host>" into "<user>:<key>"
1025 for key in cloud_config.get("key-pairs"):
1026 key_data = key_data + key + "\n"
1032 # If there is no ssh key in cloud config, a new key is generated:
1033 _, key_data = self._generate_keys()
1036 "value": "" + key_data
1038 self.logger.debug("generated keys: %s", key_data)
1040 metadata["items"].append(key_pairs)
1042 self
.logger
.debug("metadata: %s", metadata
)
1046 def _generate_keys(self
):
1047 """Method used to generate a pair of private/public keys.
1048 This method is used because to create a vm in Azure we always need a key or a password
1049 In some cases we may have a password in a cloud-init file but it may not be available
1051 key
= rsa
.generate_private_key(
1052 backend
=crypto_default_backend(), public_exponent
=65537, key_size
=2048
1054 private_key
= key
.private_bytes(
1055 crypto_serialization
.Encoding
.PEM
,
1056 crypto_serialization
.PrivateFormat
.PKCS8
,
1057 crypto_serialization
.NoEncryption(),
1059 public_key
= key
.public_key().public_bytes(
1060 crypto_serialization
.Encoding
.OpenSSH
,
1061 crypto_serialization
.PublicFormat
.OpenSSH
,
1063 private_key
= private_key
.decode("utf8")
1064 # Change first line because Paramiko needs a explicit start with 'BEGIN RSA PRIVATE KEY'
1065 i
= private_key
.find("\n")
1066 private_key
= "-----BEGIN RSA PRIVATE KEY-----" + private_key
[i
:]
1067 public_key
= public_key
.decode("utf8")
1069 return private_key
, public_key
1071 def _get_unused_vm_name(self
, vm_name
):
1073 Checks the vm name and in case it is used adds a suffix to the name to allow creation
1077 self
.conn_compute
.instances()
1078 .list(project
=self
.project
, zone
=self
.zone
)
1081 # Filter to vms starting with the indicated name
1082 vms
= list(filter(lambda vm
: (vm
.name
.startswith(vm_name
)), all_vms
))
1083 vm_names
= [str(vm
.name
) for vm
in vms
]
1085 # get the name with the first not used suffix
1087 # name = subnet_name + "-" + str(name_suffix)
1088 name
= vm_name
# first subnet created will have no prefix
1090 while name
in vm_names
:
1092 name
= vm_name
+ "-" + str(name_suffix
)
1096 def get_vminstance(self
, vm_id
):
1098 Obtaing the vm instance data from v_id
1100 self
.logger
.debug("get_vminstance begin: vm_id %s", vm_id
)
1101 self
._reload
_connection
()
1105 self
.conn_compute
.instances()
1106 .get(project
=self
.project
, zone
=self
.zone
, instance
=vm_id
)
1109 # vm = response["source"]
1110 except Exception as e
:
1111 self
._format
_vimconn
_exception
(e
)
1113 self
.logger
.debug("get_vminstance Return: response %s", response
)
1116 def delete_vminstance(self
, vm_id
, created_items
=None, volumes_to_hold
=None):
1117 """Deletes a vm instance from the vim."""
1119 "delete_vminstance begin: vm_id %s created_items %s",
1123 if self
.reload_client
:
1124 self
._reload
_connection
()
1126 created_items
= created_items
or {}
1128 vm
= self
.get_vminstance(vm_id
)
1131 self
.conn_compute
.instances()
1132 .delete(project
=self
.project
, zone
=self
.zone
, instance
=vm_id
)
1135 self
._wait
_for
_zone
_operation
(operation
["name"])
1137 # The associated subnets must be checked if they are marked to be deleted
1138 for netIface
in vm
["networkInterfaces"]:
1140 self
._get
_resource
_name
_from
_resource
_id
(netIface
["subnetwork"])
1141 in self
.nets_to_be_deleted
1143 self
._get
_resource
_name
_from
_resource
_id
(
1144 self
.delete_network(netIface
["subnetwork"])
1147 self
.logger
.debug("delete_vminstance end")
1149 except Exception as e
:
1150 # The VM can be deleted previously during network deletion
1151 if e
.args
[0]["status"] == "404":
1152 self
.logger
.debug("The VM doesn't exist or has been deleted")
1154 self
._format
_vimconn
_exception
(e
)
1156 def _get_net_name_from_resource_id(self
, resource_id
):
1158 net_name
= str(resource_id
.split("/")[-1])
1162 raise vimconn
.VimConnException(
1163 "Unable to get google cloud net_name from invalid resource_id format '{}'".format(
1168 def _get_resource_name_from_resource_id(self
, resource_id
):
1170 Obtains resource_name from the google cloud complete identifier: resource_name will always be last item
1173 "_get_resource_name_from_resource_id begin: resource_id %s",
1177 resource
= str(resource_id
.split("/")[-1])
1180 "_get_resource_name_from_resource_id Return: resource %s",
1184 except Exception as e
:
1185 raise vimconn
.VimConnException(
1186 "Unable to get resource name from resource_id '{}' Error: '{}'".format(
1191 def refresh_nets_status(self
, net_list
):
1192 """Get the status of the networks
1193 Params: the list of network identifiers
1194 Returns a dictionary with:
1195 net_id: #VIM id of this network
1196 status: #Mandatory. Text with one of:
1197 # DELETED (not found at vim)
1198 # VIM_ERROR (Cannot connect to VIM, VIM response error, ...)
1199 # OTHER (Vim reported other status not understood)
1200 # ERROR (VIM indicates an ERROR status)
1201 # ACTIVE, INACTIVE, DOWN (admin down),
1202 # BUILD (on building process)
1204 error_msg: #Text with VIM error message, if any. Or the VIM connection ERROR
1205 vim_info: #Text with plain information obtained from vim (yaml.safe_dump)
1207 self
.logger
.debug("refresh_nets_status begin: net_list %s", net_list
)
1209 self
._reload
_connection
()
1211 for net_id
in net_list
:
1213 resName
= self
._get
_resource
_name
_from
_resource
_id
(net_id
)
1216 self
.conn_compute
.subnetworks()
1217 .get(project
=self
.project
, region
=self
.region
, subnetwork
=resName
)
1220 self
.logger
.debug("get subnetwork: %s", net
)
1222 out_nets
[net_id
] = {
1223 "status": "ACTIVE", # Google Cloud does not provide the status in subnetworks getting
1224 "vim_info": str(net
),
1226 except vimconn
.VimConnNotFoundException
as e
:
1228 "VimConnNotFoundException %s when searching subnet", e
1230 out_nets
[net_id
] = {
1231 "status": "DELETED",
1232 "error_msg": str(e
),
1234 except Exception as e
:
1236 "Exception %s when searching subnet", e
, exc_info
=True
1238 out_nets
[net_id
] = {
1239 "status": "VIM_ERROR",
1240 "error_msg": str(e
),
1243 self
.logger
.debug("refresh_nets_status Return: out_nets %s", out_nets
)
1246 def refresh_vms_status(self
, vm_list
):
1247 """Get the status of the virtual machines and their interfaces/ports
1248 Params: the list of VM identifiers
1249 Returns a dictionary with:
1250 vm_id: # VIM id of this Virtual Machine
1251 status: # Mandatory. Text with one of:
1252 # DELETED (not found at vim)
1253 # VIM_ERROR (Cannot connect to VIM, VIM response error, ...)
1254 # OTHER (Vim reported other status not understood)
1255 # ERROR (VIM indicates an ERROR status)
1256 # ACTIVE, PAUSED, SUSPENDED, INACTIVE (not running),
1257 # BUILD (on building process), ERROR
1258 # ACTIVE:NoMgmtIP (Active but none of its interfaces has an IP address
1259 # (ACTIVE:NoMgmtIP is not returned for Azure)
1261 error_msg: #Text with VIM error message, if any. Or the VIM connection ERROR
1262 vim_info: #Text with plain information obtained from vim (yaml.safe_dump)
1263 interfaces: list with interface info. Each item a dictionary with:
1264 vim_interface_id - The ID of the interface
1265 mac_address - The MAC address of the interface.
1266 ip_address - The IP address of the interface within the subnet.
1268 self
.logger
.debug("refresh_vms_status begin: vm_list %s", vm_list
)
1270 self
._reload
_connection
()
1272 search_vm_list
= vm_list
or {}
1274 for vm_id
in search_vm_list
:
1277 res_name
= self
._get
_resource
_name
_from
_resource
_id
(vm_id
)
1280 self
.conn_compute
.instances()
1281 .get(project
=self
.project
, zone
=self
.zone
, instance
=res_name
)
1285 out_vm
["vim_info"] = str(vm
["name"])
1286 out_vm
["status"] = self
.provision_state2osm
.get(vm
["status"], "OTHER")
1288 # In Google Cloud the there is no difference between provision or power status,
1289 # so if provision status method doesn't return a specific state (OTHER), the
1290 # power method is called
1291 if out_vm
["status"] == "OTHER":
1292 out_vm
["status"] = self
.power_state2osm
.get(vm
["status"], "OTHER")
1294 network_interfaces
= vm
["networkInterfaces"]
1295 out_vm
["interfaces"] = self
._get
_vm
_interfaces
_status
(
1296 vm_id
, network_interfaces
1298 except Exception as e
:
1299 self
.logger
.error("Exception %s refreshing vm_status", e
, exc_info
=True)
1300 out_vm
["status"] = "VIM_ERROR"
1301 out_vm
["error_msg"] = str(e
)
1302 out_vm
["vim_info"] = None
1304 out_vms
[vm_id
] = out_vm
1306 self
.logger
.debug("refresh_vms_status Return: out_vms %s", out_vms
)
1309 def _get_vm_interfaces_status(self
, vm_id
, interfaces
):
1311 Gets the interfaces detail for a vm
1312 :param interfaces: List of interfaces.
1313 :return: Dictionary with list of interfaces including, vim_interface_id, mac_address and ip_address
1316 "_get_vm_interfaces_status begin: vm_id %s interfaces %s",
1322 for network_interface
in interfaces
:
1324 interface_dict
["vim_interface_id"] = network_interface
["name"]
1327 ips
.append(network_interface
["networkIP"])
1328 interface_dict
["ip_address"] = ";".join(ips
)
1329 interface_list
.append(interface_dict
)
1332 "_get_vm_interfaces_status Return: interface_list %s",
1335 return interface_list
1336 except Exception as e
:
1338 "Exception %s obtaining interface data for vm: %s",
1343 self
._format
_vimconn
_exception
(e
)
1345 def _get_default_admin_user(self
, image_id
):
1346 if "ubuntu" in image_id
.lower():
1349 return self
._default
_admin
_user
1351 def _create_firewall_rules(self
, network
):
1353 Creates the necessary firewall rules to allow the traffic in the network
1354 (https://cloud.google.com/vpc/docs/firewalls)
1356 :return: a list with the names of the firewall rules
1358 self
.logger
.debug("_create_firewall_rules begin: network %s", network
)
1362 # Adding firewall rule to allow http:
1363 self
.logger
.debug("creating firewall rule to allow http")
1364 firewall_rule_body
= {
1365 "name": "fw-rule-http-" + network
,
1366 "network": "global/networks/" + network
,
1367 "allowed": [{"IPProtocol": "tcp", "ports": ["80"]}],
1369 self
.conn_compute
.firewalls().insert(
1370 project
=self
.project
, body
=firewall_rule_body
1373 # Adding firewall rule to allow ssh:
1374 self
.logger
.debug("creating firewall rule to allow ssh")
1375 firewall_rule_body
= {
1376 "name": "fw-rule-ssh-" + network
,
1377 "network": "global/networks/" + network
,
1378 "allowed": [{"IPProtocol": "tcp", "ports": ["22"]}],
1380 self
.conn_compute
.firewalls().insert(
1381 project
=self
.project
, body
=firewall_rule_body
1384 # Adding firewall rule to allow ping:
1385 self
.logger
.debug("creating firewall rule to allow ping")
1386 firewall_rule_body
= {
1387 "name": "fw-rule-icmp-" + network
,
1388 "network": "global/networks/" + network
,
1389 "allowed": [{"IPProtocol": "icmp"}],
1391 self
.conn_compute
.firewalls().insert(
1392 project
=self
.project
, body
=firewall_rule_body
1395 # Adding firewall rule to allow internal:
1396 self
.logger
.debug("creating firewall rule to allow internal")
1397 firewall_rule_body
= {
1398 "name": "fw-rule-internal-" + network
,
1399 "network": "global/networks/" + network
,
1401 {"IPProtocol": "tcp", "ports": ["0-65535"]},
1402 {"IPProtocol": "udp", "ports": ["0-65535"]},
1403 {"IPProtocol": "icmp"},
1406 self
.conn_compute
.firewalls().insert(
1407 project
=self
.project
, body
=firewall_rule_body
1410 # Adding firewall rule to allow microk8s:
1411 self
.logger
.debug("creating firewall rule to allow microk8s")
1412 firewall_rule_body
= {
1413 "name": "fw-rule-microk8s-" + network
,
1414 "network": "global/networks/" + network
,
1415 "allowed": [{"IPProtocol": "tcp", "ports": ["16443"]}],
1417 self
.conn_compute
.firewalls().insert(
1418 project
=self
.project
, body
=firewall_rule_body
1421 # Adding firewall rule to allow rdp:
1422 self
.logger
.debug("creating firewall rule to allow rdp")
1423 firewall_rule_body
= {
1424 "name": "fw-rule-rdp-" + network
,
1425 "network": "global/networks/" + network
,
1426 "allowed": [{"IPProtocol": "tcp", "ports": ["3389"]}],
1428 self
.conn_compute
.firewalls().insert(
1429 project
=self
.project
, body
=firewall_rule_body
1432 # Adding firewall rule to allow osm:
1433 self
.logger
.debug("creating firewall rule to allow osm")
1434 firewall_rule_body
= {
1435 "name": "fw-rule-osm-" + network
,
1436 "network": "global/networks/" + network
,
1437 "allowed": [{"IPProtocol": "tcp", "ports": ["9001", "9999"]}],
1439 self
.conn_compute
.firewalls().insert(
1440 project
=self
.project
, body
=firewall_rule_body
1444 "_create_firewall_rules Return: list_rules %s", rules_list
1447 except Exception as e
:
1449 "Unable to create google cloud firewall rules for network '{}'".format(
1453 self
._format
_vimconn
_exception
(e
)
1455 def _delete_firewall_rules(self
, network
):
1457 Deletes the associated firewall rules to the network
1459 :return: a list with the names of the firewall rules
1461 self
.logger
.debug("_delete_firewall_rules begin: network %s", network
)
1466 self
.conn_compute
.firewalls().list(project
=self
.project
).execute()
1468 for item
in rules_list
["items"]:
1469 if network
== self
._get
_resource
_name
_from
_resource
_id
(item
["network"]):
1470 self
.conn_compute
.firewalls().delete(
1471 project
=self
.project
, firewall
=item
["name"]
1474 self
.logger
.debug("_delete_firewall_rules Return: list_rules %s", 0)
1476 except Exception as e
:
1478 "Unable to delete google cloud firewall rules for network '{}'".format(
1482 self
._format
_vimconn
_exception
(e
)
1484 def migrate_instance(self
, vm_id
, compute_host
=None):
1488 vm_id: ID of an instance
1489 compute_host: Host to migrate the vdu to
1491 # TODO: Add support for migration
1492 raise vimconn
.VimConnNotImplemented("Not implemented")
1494 def resize_instance(self
, vm_id
, flavor_id
=None):
1498 vm_id: ID of an instance
1499 flavor_id: flavor_id to resize the vdu to
1501 # TODO: Add support for resize
1502 raise vimconn
.VimConnNotImplemented("Not implemented")