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
17 from osm_ro_plugin
import vimconn
21 from random
import choice
as random_choice
24 from google
.api_core
.exceptions
import NotFound
25 import googleapiclient
.discovery
26 from google
.oauth2
import service_account
28 from cryptography
.hazmat
.primitives
import serialization
as crypto_serialization
29 from cryptography
.hazmat
.primitives
.asymmetric
import rsa
30 from cryptography
.hazmat
.backends
import default_backend
as crypto_default_backend
34 __author__
= "Sergio Gallardo Ruiz"
35 __date__
= "$11-aug-2021 08:30:00$"
38 if getenv("OSMRO_PDB_DEBUG"):
47 class vimconnector(vimconn
.VimConnector
):
49 # Translate Google Cloud provisioning state to OSM provision state
50 # The first three ones are the transitional status once a user initiated action has been requested
51 # Once the operation is complete, it will transition into the states Succeeded or Failed
52 # https://cloud.google.com/compute/docs/instances/instance-life-cycle
53 provision_state2osm
= {
54 "PROVISIONING": "BUILD",
58 # Translate azure power state to OSM provision state
62 "STOPPING": "INACTIVE",
63 "SUSPENDING": "INACTIVE",
64 "SUSPENDED": "INACTIVE",
65 "TERMINATED": "INACTIVE",
68 # If a net or subnet is tried to be deleted and it has an associated resource, the net is marked "to be deleted"
69 # (incluid it's name in the following list). When the instance is deleted, its associated net will be deleted if
70 # they are present in that list
71 nets_to_be_deleted
= []
88 Constructor of VIM. Raise an exception is some needed parameter is missing, but it must not do any connectivity
89 checking against the VIM
90 Using common constructor parameters.
91 In this case: config must include the following parameters:
92 subscription_id: assigned subscription identifier
93 region_name: current region for network
94 config may also include the following parameter:
95 flavors_pattern: pattern that will be used to select a range of vm sizes, for example
96 "^((?!Standard_B).)*$" will filter out Standard_B range that is cheap but is very overused
97 "^Standard_B" will select a serie B maybe for test environment
99 vimconn
.VimConnector
.__init
__(
114 # Variable that indicates if client must be reloaded or initialized
115 self
.reload_client
= False
119 log_format_simple
= (
120 "%(asctime)s %(levelname)s %(name)s %(filename)s:%(lineno)s %(message)s"
122 log_format_complete
= "%(asctime)s %(levelname)s %(name)s %(filename)s:%(lineno)s %(funcName)s(): %(message)s"
123 log_formatter_simple
= logging
.Formatter(
124 log_format_simple
, datefmt
="%Y-%m-%dT%H:%M:%S"
126 self
.handler
= logging
.StreamHandler()
127 self
.handler
.setFormatter(log_formatter_simple
)
129 self
.logger
= logging
.getLogger("ro.vim.gcp")
130 self
.logger
.addHandler(self
.handler
)
132 self
.logger
.setLevel(getattr(logging
, log_level
))
134 if self
.logger
.getEffectiveLevel() == logging
.DEBUG
:
135 log_formatter
= logging
.Formatter(
136 log_format_complete
, datefmt
="%Y-%m-%dT%H:%M:%S"
138 self
.handler
.setFormatter(log_formatter
)
140 self
.logger
.debug("Google Cloud connection init")
142 self
.project
= tenant_id
or tenant_name
144 # REGION - Google Cloud considers regions and zones. A specific region can have more than one zone
145 # (for instance: region us-west1 with the zones us-west1-a, us-west1-b and us-west1-c)
146 # So the region name specified in the config will be considered as a specific zone for GC and
147 # the region will be calculated from that without the preffix.
148 if "region_name" in config
:
149 self
.zone
= config
.get("region_name")
150 self
.region
= self
.zone
.rsplit("-", 1)[0]
152 raise vimconn
.VimConnException(
153 "Google Cloud region_name is not specified at config"
157 self
.logger
.debug("Config: %s", config
)
158 scopes
= ["https://www.googleapis.com/auth/cloud-platform"]
159 self
.credentials
= None
160 if "credentials" in config
:
161 self
.logger
.debug("Setting credentials")
162 # Settings Google Cloud credentials dict
163 credentials_body
= config
["credentials"]
164 # self.logger.debug("Credentials filtered: %s", credentials_body)
165 credentials
= service_account
.Credentials
.from_service_account_info(
168 if "sa_file" in config
:
169 credentials
= service_account
.Credentials
.from_service_account_file(
170 config
.get("sa_file"), scopes
=scopes
172 self
.logger
.debug("Credentials: %s", credentials
)
173 # Construct a Resource for interacting with an API.
174 self
.credentials
= credentials
176 self
.conn_compute
= googleapiclient
.discovery
.build(
177 "compute", "v1", credentials
=credentials
179 except Exception as e
:
180 self
._format
_vimconn
_exception
(e
)
182 raise vimconn
.VimConnException(
183 "It is not possible to init GCP with no credentials"
186 def _reload_connection(self
):
188 Called before any operation, checks python Google Cloud clientsself.reload_client
190 if self
.reload_client
:
191 self
.logger
.debug("reloading google cloud client")
194 # Set to client created
195 self
.conn_compute
= googleapiclient
.discovery
.build("compute", "v1")
196 except Exception as e
:
197 self
._format
_vimconn
_exception
(e
)
199 def _format_vimconn_exception(self
, e
):
201 Transforms a generic exception to a vimConnException
203 self
.logger
.error("Google Cloud plugin error: {}".format(e
))
204 if isinstance(e
, vimconn
.VimConnException
):
207 # In case of generic error recreate client
208 self
.reload_client
= True
209 raise vimconn
.VimConnException(type(e
).__name
__ + ": " + str(e
))
211 def _wait_for_global_operation(self
, operation
):
213 Waits for the end of the specific operation
214 :operation: operation name
217 self
.logger
.debug("Waiting for operation %s", operation
)
221 self
.conn_compute
.globalOperations()
222 .get(project
=self
.project
, operation
=operation
)
226 if result
["status"] == "DONE":
227 if "error" in result
:
228 raise vimconn
.VimConnException(result
["error"])
233 def _wait_for_zone_operation(self
, operation
):
235 Waits for the end of the specific operation
236 :operation: operation name
239 self
.logger
.debug("Waiting for operation %s", operation
)
243 self
.conn_compute
.zoneOperations()
244 .get(project
=self
.project
, operation
=operation
, zone
=self
.zone
)
248 if result
["status"] == "DONE":
249 if "error" in result
:
250 raise vimconn
.VimConnException(result
["error"])
255 def _wait_for_region_operation(self
, operation
):
257 Waits for the end of the specific operation
258 :operation: operation name
261 self
.logger
.debug("Waiting for operation %s", operation
)
265 self
.conn_compute
.regionOperations()
266 .get(project
=self
.project
, operation
=operation
, region
=self
.region
)
270 if result
["status"] == "DONE":
271 if "error" in result
:
272 raise vimconn
.VimConnException(result
["error"])
283 provider_network_profile
=None,
286 Adds a network to VIM
287 :param net_name: name of the network
288 :param net_type: not used for Google Cloud networks
289 :param ip_profile: not used for Google Cloud networks
290 :param shared: Not allowed for Google Cloud Connector
291 :param provider_network_profile: (optional)
293 contains {segmentation-id: vlan, provider-network: vim_netowrk}
294 :return: a tuple with the network identifier and created_items, or raises an exception on error
295 created_items can be None or a dictionary where this method can include key-values that will be passed to
296 the method delete_network. Can be used to store created segments, created l2gw connections, etc.
297 Format is vimconnector dependent, but do not use nested dictionaries and a value of None should be the same
302 "new_network begin: net_name %s net_type %s ip_profile %s shared %s provider_network_profile %s",
307 provider_network_profile
,
309 net_name
= self
._check
_vm
_name
(net_name
)
310 net_name
= self
._randomize
_name
(net_name
)
311 self
.logger
.debug("create network name %s, ip_profile %s", net_name
, ip_profile
)
315 self
.logger
.debug("creating network_name: {}".format(net_name
))
317 network
= "projects/{}/global/networks/default".format(self
.project
)
319 if ip_profile
is not None:
320 if "subnet_address" in ip_profile
:
321 subnet_address
= ip_profile
["subnet_address"]
323 "name": str(net_name
),
324 "description": net_name
,
326 "ipCidrRange": subnet_address
,
327 # "autoCreateSubnetworks": True, # The network is created in AUTO mode (one subnet per region is created)
328 "autoCreateSubnetworks": False,
332 self
.conn_compute
.networks()
333 .insert(project
=self
.project
, body
=network_body
)
336 self
._wait
_for
_global
_operation
(operation
["name"])
337 self
.logger
.debug("created network_name: {}".format(net_name
))
339 # Adding firewall rules to allow the traffic in the network:
340 rules_list
= self
._create
_firewall
_rules
(net_name
)
342 # create subnetwork, even if there is no profile
347 if not ip_profile
.get("subnet_address"):
348 # Fake subnet is required
349 subnet_rand
= random
.randint(0, 255)
350 ip_profile
["subnet_address"] = "192.168.{}.0/24".format(subnet_rand
)
352 subnet_name
= net_name
+ "-subnet"
353 subnet_id
= self
._new
_subnet
(
354 subnet_name
, ip_profile
, operation
["targetLink"]
357 self
.logger
.debug("new_network Return: subnet_id: %s", subnet_id
)
359 except Exception as e
:
360 self
._format
_vimconn
_exception
(e
)
362 def _new_subnet(self
, subnet_name
, ip_profile
, network
):
364 Adds a tenant network to VIM. It creates a new subnet at existing base vnet
365 :param net_name: subnet name
367 subnet-address: if it is not provided a subnet/24 in the default vnet is created,
368 otherwise it creates a subnet in the indicated address
369 :return: a tuple with the network identifier and created_items, or raises an exception on error
372 "_new_subnet begin: subnet_name %s ip_profile %s network %s",
378 "create subnet name %s, ip_profile %s", subnet_name
, ip_profile
383 self
.logger
.debug("creating subnet_name: {}".format(subnet_name
))
386 "name": str(subnet_name
),
387 "description": subnet_name
,
389 "ipCidrRange": ip_profile
["subnet_address"],
393 self
.conn_compute
.subnetworks()
395 project
=self
.project
,
397 body
=subnetwork_body
,
401 self
._wait
_for
_region
_operation
(operation
["name"])
403 self
.logger
.debug("created subnet_name: {}".format(subnet_name
))
406 "_new_subnet Return: (%s,%s)",
407 "regions/%s/subnetworks/%s" % (self
.region
, subnet_name
),
410 return "regions/%s/subnetworks/%s" % (self
.region
, subnet_name
), None
411 except Exception as e
:
412 self
._format
_vimconn
_exception
(e
)
414 def get_network_list(self
, filter_dict
={}):
415 """Obtain tenant networks of VIM
419 shared: boolean, not implemented in GC
420 tenant_id: tenant, not used in GC, all networks same tenants
421 admin_state_up: boolean, not implemented in GC
422 status: 'ACTIVE', not implemented in GC #
423 Returns the network list of dictionaries
425 self
.logger
.debug("get_network_list begin: filter_dict %s", filter_dict
)
427 "Getting network (subnetwork) from VIM filter: {}".format(str(filter_dict
))
432 if self
.reload_client
:
433 self
._reload
_connection
()
437 request
= self
.conn_compute
.subnetworks().list(
438 project
=self
.project
, region
=self
.region
441 while request
is not None:
442 response
= request
.execute()
443 self
.logger
.debug("Network list: %s", response
)
444 for net
in response
["items"]:
445 self
.logger
.debug("Network in list: {}".format(str(net
["name"])))
446 if filter_dict
is not None:
447 if "name" in filter_dict
.keys():
449 filter_dict
["name"] == net
["name"]
450 or filter_dict
["name"] == net
["selfLink"]
452 self
.logger
.debug("Network found: %s", net
["name"])
455 "id": str(net
["selfLink"]),
456 "name": str(net
["name"]),
457 "network": str(net
["network"]),
463 "id": str(net
["selfLink"]),
464 "name": str(net
["name"]),
465 "network": str(net
["network"]),
468 request
= self
.conn_compute
.subnetworks().list_next(
469 previous_request
=request
, previous_response
=response
472 self
.logger
.debug("get_network_list Return: net_list %s", net_list
)
475 except Exception as e
:
476 self
.logger
.error("Error in get_network_list()", exc_info
=True)
477 raise vimconn
.VimConnException(e
)
479 def get_network(self
, net_id
):
480 self
.logger
.debug("get_network begin: net_id %s", net_id
)
481 # res_name = self._get_resource_name_from_resource_id(net_id)
482 self
._reload
_connection
()
484 self
.logger
.debug("Get network: %s", net_id
)
485 filter_dict
= {"name": net_id
}
486 network_list
= self
.get_network_list(filter_dict
)
487 self
.logger
.debug("Network list: %s", network_list
)
492 self
.logger
.debug("get_network Return: network_list[0] %s", network_list
[0])
493 return network_list
[0]
495 def delete_network(self
, net_id
, created_items
=None):
497 Removes a tenant network from VIM and its associated elements
498 :param net_id: VIM identifier of the network, provided by method new_network
499 :param created_items: dictionary with extra items to be deleted. provided by method new_network
500 Returns the network identifier or raises an exception upon error or when network is not found
504 "delete_network begin: net_id %s created_items %s",
508 self
.logger
.debug("Deleting network: {}".format(str(net_id
)))
512 net_name
= self
._get
_resource
_name
_from
_resource
_id
(net_id
)
514 # Check associated VMs
516 self
.conn_compute
.instances()
517 .list(project
=self
.project
, zone
=self
.zone
)
521 net_id
= self
.delete_subnet(net_name
, created_items
)
523 self
.logger
.debug("delete_network Return: net_id %s", net_id
)
526 except Exception as e
:
527 self
.logger
.error("Error in delete_network()", exc_info
=True)
528 raise vimconn
.VimConnException(e
)
530 def delete_subnet(self
, net_id
, created_items
=None):
532 Removes a tenant network from VIM and its associated elements
533 :param net_id: VIM identifier of the network, provided by method new_network
534 :param created_items: dictionary with extra items to be deleted. provided by method new_network
535 Returns the network identifier or raises an exception upon error or when network is not found
539 "delete_subnet begin: net_id %s created_items %s",
543 self
.logger
.debug("Deleting subnetwork: {}".format(str(net_id
)))
546 # If the network has no more subnets, it will be deleted too
547 net_info
= self
.get_network(net_id
)
548 # If the subnet is in use by another resource, the deletion will be retried N times before abort the operation
549 created_items
= created_items
or {}
550 created_items
[net_id
] = False
554 self
.conn_compute
.subnetworks()
556 project
=self
.project
,
562 self
._wait
_for
_region
_operation
(operation
["name"])
563 if net_id
in self
.nets_to_be_deleted
:
564 self
.nets_to_be_deleted
.remove(net_id
)
565 except Exception as e
:
567 e
.args
[0]["status"] == "400"
568 ): # Resource in use, so the net is marked to be deleted
569 self
.logger
.debug("Subnet still in use")
570 self
.nets_to_be_deleted
.append(net_id
)
572 raise vimconn
.VimConnException(e
)
574 self
.logger
.debug("nets_to_be_deleted: %s", self
.nets_to_be_deleted
)
576 # If the network has no more subnets, it will be deleted too
577 # if "network" in net_info and net_id not in self.nets_to_be_deleted:
578 if "network" in net_info
:
579 network_name
= self
._get
_resource
_name
_from
_resource
_id
(
584 # Deletion of the associated firewall rules:
585 rules_list
= self
._delete
_firewall
_rules
(network_name
)
588 self
.conn_compute
.networks()
590 project
=self
.project
,
591 network
=network_name
,
595 self
._wait
_for
_global
_operation
(operation
["name"])
596 except Exception as e
:
597 self
.logger
.debug("error deleting associated network %s", e
)
599 self
.logger
.debug("delete_subnet Return: net_id %s", net_id
)
602 except Exception as e
:
603 self
.logger
.error("Error in delete_network()", exc_info
=True)
604 raise vimconn
.VimConnException(e
)
606 def new_flavor(self
, flavor_data
):
608 It is not allowed to create new flavors (machine types) in Google Cloud, must always use an existing one
610 raise vimconn
.VimConnNotImplemented(
611 "It is not possible to create new flavors in Google Cloud"
614 def new_tenant(self
, tenant_name
, tenant_description
):
616 It is not allowed to create new tenants in Google Cloud
618 raise vimconn
.VimConnNotImplemented(
619 "It is not possible to create a TENANT in Google Cloud"
622 def get_flavor(self
, flavor_id
):
624 Obtains the flavor_data from the flavor_id/machine type id
626 self
.logger
.debug("get_flavor begin: flavor_id %s", flavor_id
)
630 self
.conn_compute
.machineTypes()
631 .get(project
=self
.project
, zone
=self
.zone
, machineType
=flavor_id
)
634 flavor_data
= response
635 self
.logger
.debug("Machine type data: %s", flavor_data
)
639 "id": flavor_data
["id"],
641 "id_complete": flavor_data
["selfLink"],
642 "ram": flavor_data
["memoryMb"],
643 "vcpus": flavor_data
["guestCpus"],
644 "disk": flavor_data
["maximumPersistentDisksSizeGb"],
647 self
.logger
.debug("get_flavor Return: flavor %s", flavor
)
650 raise vimconn
.VimConnNotFoundException(
651 "flavor '{}' not found".format(flavor_id
)
653 except Exception as e
:
654 self
._format
_vimconn
_exception
(e
)
656 # Google Cloud VM names can not have some special characters
657 def _check_vm_name(self
, vm_name
):
659 Checks vm name, in case the vm has not allowed characters they are removed, not error raised
660 Only lowercase and hyphens are allowed
662 chars_not_allowed_list
= "~!@#$%^&*()=+_[]{}|;:<>/?."
664 # First: the VM name max length is 64 characters
665 vm_name_aux
= vm_name
[:62]
667 # Second: replace not allowed characters
668 for elem
in chars_not_allowed_list
:
669 # Check if string is in the main string
670 if elem
in vm_name_aux
:
671 # self.logger.debug("Dentro del IF")
673 vm_name_aux
= vm_name_aux
.replace(elem
, "-")
675 return vm_name_aux
.lower()
677 def get_flavor_id_from_data(self
, flavor_dict
):
678 self
.logger
.debug("get_flavor_id_from_data begin: flavor_dict %s", flavor_dict
)
679 filter_dict
= flavor_dict
or {}
683 self
.conn_compute
.machineTypes()
684 .list(project
=self
.project
, zone
=self
.zone
)
687 machine_types_list
= response
["items"]
688 # self.logger.debug("List of machine types: %s", machine_types_list)
690 cpus
= filter_dict
.get("vcpus") or 0
691 memMB
= filter_dict
.get("ram") or 0
692 # Workaround (it should be 0)
693 numberInterfaces
= len(filter_dict
.get("interfaces", [])) or 4
696 filtered_machines
= []
697 for machine_type
in machine_types_list
:
699 machine_type
["guestCpus"] >= cpus
700 and machine_type
["memoryMb"] >= memMB
701 # In Google Cloud the number of virtual network interfaces scales with
702 # the number of virtual CPUs with a minimum of 2 and a maximum of 8:
703 # https://cloud.google.com/vpc/docs/create-use-multiple-interfaces#max-interfaces
704 and machine_type
["guestCpus"] >= numberInterfaces
706 filtered_machines
.append(machine_type
)
708 # self.logger.debug("Filtered machines: %s", filtered_machines)
711 listedFilteredMachines
= sorted(
715 float(k
["memoryMb"]),
716 int(k
["maximumPersistentDisksSizeGb"]),
720 # self.logger.debug("Sorted filtered machines: %s", listedFilteredMachines)
722 if listedFilteredMachines
:
724 "get_flavor_id_from_data Return: listedFilteredMachines[0][name] %s",
725 listedFilteredMachines
[0]["name"],
727 return listedFilteredMachines
[0]["name"]
729 raise vimconn
.VimConnNotFoundException(
730 "Cannot find any flavor matching '{}'".format(str(flavor_dict
))
733 except Exception as e
:
734 self
._format
_vimconn
_exception
(e
)
736 def delete_flavor(self
, flavor_id
):
737 raise vimconn
.VimConnNotImplemented(
738 "It is not possible to delete a flavor in Google Cloud"
741 def delete_tenant(self
, tenant_id
):
742 raise vimconn
.VimConnNotImplemented(
743 "It is not possible to delete a TENANT in Google Cloud"
746 def new_image(self
, image_dict
):
748 This function comes from the early days when we though the image could be embedded in the package.
749 Unless OSM manages VM images E2E from NBI to RO, this function does not make sense to be implemented.
751 raise vimconn
.VimConnNotImplemented("Not implemented")
753 def get_image_id_from_path(self
, path
):
755 This function comes from the early days when we though the image could be embedded in the package.
756 Unless OSM manages VM images E2E from NBI to RO, this function does not make sense to be implemented.
758 raise vimconn
.VimConnNotImplemented("Not implemented")
760 def get_image_list(self
, filter_dict
={}):
761 """Obtain tenant images from VIM
763 name: image name with the format: image project:image family:image version
764 If some part of the name is provide ex: publisher:offer it will search all availables skus and version
765 for the provided publisher and offer
766 id: image uuid, currently not supported for azure
767 Returns the image list of dictionaries:
768 [{<the fields at Filter_dict plus some VIM specific>}, ...]
771 self
.logger
.debug("get_image_list begin: filter_dict %s", filter_dict
)
775 # Get image id from parameter image_id:
776 # <image Project>:image-family:<family> => Latest version of the family
777 # <image Project>:image:<image> => Specific image
778 # <image Project>:<image> => Specific image
780 image_info
= filter_dict
["name"].split(":")
781 image_project
= image_info
[0]
782 if len(image_info
) == 2:
784 image_item
= image_info
[1]
785 if len(image_info
) == 3:
786 image_type
= image_info
[1]
787 image_item
= image_info
[2]
789 raise vimconn
.VimConnNotFoundException("Wrong format for image")
792 if image_type
== "image-family":
794 self
.conn_compute
.images()
795 .getFromFamily(project
=image_project
, family
=image_item
)
798 elif image_type
== "image":
800 self
.conn_compute
.images()
801 .get(project
=image_project
, image
=image_item
)
805 raise vimconn
.VimConnNotFoundException("Wrong format for image")
808 "id": "projects/%s/global/images/%s"
809 % (image_project
, image_response
["name"]),
811 [image_project
, image_item
, image_response
["name"]]
816 self
.logger
.debug("get_image_list Return: image_list %s", image_list
)
819 except Exception as e
:
820 self
._format
_vimconn
_exception
(e
)
822 def delete_inuse_nic(self
, nic_name
):
823 raise vimconn
.VimConnNotImplemented("Not necessary")
825 def delete_image(self
, image_id
):
826 raise vimconn
.VimConnNotImplemented("Not implemented")
828 def action_vminstance(self
, vm_id
, action_dict
, created_items
={}):
829 """Send and action over a VM instance from VIM
830 Returns the vm_id if the action was successfully sent to the VIM
832 raise vimconn
.VimConnNotImplemented("Not necessary")
834 def _randomize_name(self
, name
):
835 """Adds a random string to allow requests with the same VM name
836 Returns the name with an additional random string (if the total size is bigger
837 than 62 the original name will be truncated)
846 + "".join(random_choice("0123456789abcdef") for _
in range(12))
849 self
.conn_compute
.instances()
850 .get(project
=self
.project
, zone
=self
.zone
, instance
=random_name
)
853 # If no exception is arisen, the random name exists for an instance, so a new random name must be generated
855 except Exception as e
:
856 if e
.args
[0]["status"] == "404":
857 self
.logger
.debug("New random name: %s", random_name
)
861 "Exception generating random name (%s) for the instance", name
863 self
._format
_vimconn
_exception
(e
)
872 image_id
=None, # <image project>:(image|image-family):<image/family id>
877 availability_zone_index
=None,
878 availability_zone_list
=None,
881 "new_vminstance begin: name: %s, image_id: %s, flavor_id: %s, net_list: %s, cloud_config: %s, "
882 "disk_list: %s, availability_zone_index: %s, availability_zone_list: %s",
889 availability_zone_index
,
890 availability_zone_list
,
893 if self
.reload_client
:
894 self
._reload
_connection
()
896 # Validate input data is valid
897 # # First of all, the name must be adapted because Google Cloud only allows names consist of
898 # lowercase letters (a-z), numbers and hyphens (?:[a-z](?:[-a-z0-9]{0,61}[a-z0-9])?)
899 vm_name
= self
._check
_vm
_name
(name
)
900 vm_name
= self
._randomize
_name
(vm_name
)
903 # At least one network must be provided
905 raise vimconn
.VimConnException(
906 "At least one net must be provided to create a new VM"
911 metadata
= self
._build
_metadata
(vm_name
, cloud_config
)
913 # Building network interfaces list
914 network_interfaces
= []
917 if not net
.get("net_id"):
918 if not net
.get("name"):
923 ] = "regions/%s/subnetworks/" % self
.region
+ net
.get("name")
925 net_iface
["subnetwork"] = net
.get("net_id")
926 # In order to get an external IP address, the key "accessConfigs" must be used
927 # in the interace. It has to be of type "ONE_TO_ONE_NAT" and name "External NAT"
928 if net
.get("floating_ip", False) or (
929 net
["use"] == "mgmt" and self
.config
.get("use_floating_ip")
931 net_iface
["accessConfigs"] = [
932 {"type": "ONE_TO_ONE_NAT", "name": "External NAT"}
935 network_interfaces
.append(net_iface
)
937 self
.logger
.debug("Network interfaces: %s", network_interfaces
)
939 self
.logger
.debug("Source image: %s", image_id
)
943 "machineType": self
.get_flavor(flavor_id
)["id_complete"],
944 # Specify the boot disk and the image to use as a source.
949 "initializeParams": {
950 "sourceImage": image_id
,
954 # Specify the network interfaces
955 "networkInterfaces": network_interfaces
,
956 "metadata": metadata
,
960 self
.conn_compute
.instances()
961 .insert(project
=self
.project
, zone
=self
.zone
, body
=vm_parameters
)
964 self
._wait
_for
_zone
_operation
(response
["name"])
966 # The created instance info is obtained to get the name of the generated network interfaces (nic0, nic1...)
968 self
.conn_compute
.instances()
969 .get(project
=self
.project
, zone
=self
.zone
, instance
=vm_name
)
972 self
.logger
.debug("instance get: %s", response
)
973 vm_id
= response
["name"]
975 # The generated network interfaces in the instance are include in net_list:
976 for _
, net
in enumerate(net_list
):
977 for net_ifaces
in response
["networkInterfaces"]:
980 network_id
= self
._get
_resource
_name
_from
_resource
_id
(
984 network_id
= self
._get
_resource
_name
_from
_resource
_id
(
987 if network_id
== self
._get
_resource
_name
_from
_resource
_id
(
988 net_ifaces
["subnetwork"]
990 net
["vim_id"] = net_ifaces
["name"]
993 "new_vminstance Return: (name %s, created_items %s)",
997 return vm_name
, created_items
999 except Exception as e
:
1000 # Rollback vm creacion
1001 if vm_id
is not None:
1003 self
.logger
.debug("exception creating vm try to rollback")
1004 self
.delete_vminstance(vm_id
, created_items
)
1005 except Exception as e2
:
1006 self
.logger
.error("new_vminstance rollback fail {}".format(e2
))
1010 "Exception creating new vminstance: %s", e
, exc_info
=True
1012 self
._format
_vimconn
_exception
(e
)
1014 def _build_metadata(self
, vm_name
, cloud_config
):
1018 metadata
["items"] = []
1021 # if there is a cloud-init load it
1023 self
.logger
.debug("cloud config: %s", cloud_config
)
1024 _
, userdata
= self
._create
_user
_data
(cloud_config
)
1025 metadata
["items"].append({"key": "user-data", "value": userdata
})
1027 # either password of ssh-keys are required
1028 # we will always use ssh-keys, in case it is not available we will generate it
1030 if cloud_config and cloud_config.get("key-pairs"):
1033 if cloud_config.get("key-pairs"):
1034 if isinstance(cloud_config["key-pairs"], list):
1035 # Transform the format "<key> <user@host>" into "<user>:<key>"
1037 for key in cloud_config.get("key-pairs"):
1038 key_data = key_data + key + "\n"
1044 # If there is no ssh key in cloud config, a new key is generated:
1045 _, key_data = self._generate_keys()
1048 "value": "" + key_data
1050 self.logger.debug("generated keys: %s", key_data)
1052 metadata["items"].append(key_pairs)
1054 self
.logger
.debug("metadata: %s", metadata
)
1058 def _generate_keys(self
):
1059 """Method used to generate a pair of private/public keys.
1060 This method is used because to create a vm in Azure we always need a key or a password
1061 In some cases we may have a password in a cloud-init file but it may not be available
1063 key
= rsa
.generate_private_key(
1064 backend
=crypto_default_backend(), public_exponent
=65537, key_size
=2048
1066 private_key
= key
.private_bytes(
1067 crypto_serialization
.Encoding
.PEM
,
1068 crypto_serialization
.PrivateFormat
.PKCS8
,
1069 crypto_serialization
.NoEncryption(),
1071 public_key
= key
.public_key().public_bytes(
1072 crypto_serialization
.Encoding
.OpenSSH
,
1073 crypto_serialization
.PublicFormat
.OpenSSH
,
1075 private_key
= private_key
.decode("utf8")
1076 # Change first line because Paramiko needs a explicit start with 'BEGIN RSA PRIVATE KEY'
1077 i
= private_key
.find("\n")
1078 private_key
= "-----BEGIN RSA PRIVATE KEY-----" + private_key
[i
:]
1079 public_key
= public_key
.decode("utf8")
1081 return private_key
, public_key
1083 def _get_unused_vm_name(self
, vm_name
):
1085 Checks the vm name and in case it is used adds a suffix to the name to allow creation
1089 self
.conn_compute
.instances()
1090 .list(project
=self
.project
, zone
=self
.zone
)
1093 # Filter to vms starting with the indicated name
1094 vms
= list(filter(lambda vm
: (vm
.name
.startswith(vm_name
)), all_vms
))
1095 vm_names
= [str(vm
.name
) for vm
in vms
]
1097 # get the name with the first not used suffix
1099 # name = subnet_name + "-" + str(name_suffix)
1100 name
= vm_name
# first subnet created will have no prefix
1102 while name
in vm_names
:
1104 name
= vm_name
+ "-" + str(name_suffix
)
1108 def get_vminstance(self
, vm_id
):
1110 Obtaing the vm instance data from v_id
1112 self
.logger
.debug("get_vminstance begin: vm_id %s", vm_id
)
1113 self
._reload
_connection
()
1117 self
.conn_compute
.instances()
1118 .get(project
=self
.project
, zone
=self
.zone
, instance
=vm_id
)
1121 # vm = response["source"]
1122 except Exception as e
:
1123 self
._format
_vimconn
_exception
(e
)
1125 self
.logger
.debug("get_vminstance Return: response %s", response
)
1128 def delete_vminstance(self
, vm_id
, created_items
=None):
1129 """Deletes a vm instance from the vim."""
1131 "delete_vminstance begin: vm_id %s created_items %s",
1135 if self
.reload_client
:
1136 self
._reload
_connection
()
1138 created_items
= created_items
or {}
1140 vm
= self
.get_vminstance(vm_id
)
1143 self
.conn_compute
.instances()
1144 .delete(project
=self
.project
, zone
=self
.zone
, instance
=vm_id
)
1147 self
._wait
_for
_zone
_operation
(operation
["name"])
1149 # The associated subnets must be checked if they are marked to be deleted
1150 for netIface
in vm
["networkInterfaces"]:
1152 self
._get
_resource
_name
_from
_resource
_id
(netIface
["subnetwork"])
1153 in self
.nets_to_be_deleted
1155 net_id
= self
._get
_resource
_name
_from
_resource
_id
(
1156 self
.delete_network(netIface
["subnetwork"])
1159 self
.logger
.debug("delete_vminstance end")
1161 except Exception as e
:
1162 # The VM can be deleted previously during network deletion
1163 if e
.args
[0]["status"] == "404":
1164 self
.logger
.debug("The VM doesn't exist or has been deleted")
1166 self
._format
_vimconn
_exception
(e
)
1168 def _get_net_name_from_resource_id(self
, resource_id
):
1170 net_name
= str(resource_id
.split("/")[-1])
1174 raise vimconn
.VimConnException(
1175 "Unable to get google cloud net_name from invalid resource_id format '{}'".format(
1180 def _get_resource_name_from_resource_id(self
, resource_id
):
1182 Obtains resource_name from the google cloud complete identifier: resource_name will always be last item
1185 "_get_resource_name_from_resource_id begin: resource_id %s",
1189 resource
= str(resource_id
.split("/")[-1])
1192 "_get_resource_name_from_resource_id Return: resource %s",
1196 except Exception as e
:
1197 raise vimconn
.VimConnException(
1198 "Unable to get resource name from resource_id '{}' Error: '{}'".format(
1203 def refresh_nets_status(self
, net_list
):
1204 """Get the status of the networks
1205 Params: the list of network identifiers
1206 Returns a dictionary with:
1207 net_id: #VIM id of this network
1208 status: #Mandatory. Text with one of:
1209 # DELETED (not found at vim)
1210 # VIM_ERROR (Cannot connect to VIM, VIM response error, ...)
1211 # OTHER (Vim reported other status not understood)
1212 # ERROR (VIM indicates an ERROR status)
1213 # ACTIVE, INACTIVE, DOWN (admin down),
1214 # BUILD (on building process)
1216 error_msg: #Text with VIM error message, if any. Or the VIM connection ERROR
1217 vim_info: #Text with plain information obtained from vim (yaml.safe_dump)
1219 self
.logger
.debug("refresh_nets_status begin: net_list %s", net_list
)
1221 self
._reload
_connection
()
1223 for net_id
in net_list
:
1225 netName
= self
._get
_net
_name
_from
_resource
_id
(net_id
)
1226 resName
= self
._get
_resource
_name
_from
_resource
_id
(net_id
)
1229 self
.conn_compute
.subnetworks()
1230 .get(project
=self
.project
, region
=self
.region
, subnetwork
=resName
)
1233 self
.logger
.debug("get subnetwork: %s", net
)
1235 out_nets
[net_id
] = {
1236 "status": "ACTIVE", # Google Cloud does not provide the status in subnetworks getting
1237 "vim_info": str(net
),
1239 except vimconn
.VimConnNotFoundException
as e
:
1241 "VimConnNotFoundException %s when searching subnet", e
1243 out_nets
[net_id
] = {
1244 "status": "DELETED",
1245 "error_msg": str(e
),
1247 except Exception as e
:
1249 "Exception %s when searching subnet", e
, exc_info
=True
1251 out_nets
[net_id
] = {
1252 "status": "VIM_ERROR",
1253 "error_msg": str(e
),
1256 self
.logger
.debug("refresh_nets_status Return: out_nets %s", out_nets
)
1259 def refresh_vms_status(self
, vm_list
):
1260 """Get the status of the virtual machines and their interfaces/ports
1261 Params: the list of VM identifiers
1262 Returns a dictionary with:
1263 vm_id: # VIM id of this Virtual Machine
1264 status: # Mandatory. Text with one of:
1265 # DELETED (not found at vim)
1266 # VIM_ERROR (Cannot connect to VIM, VIM response error, ...)
1267 # OTHER (Vim reported other status not understood)
1268 # ERROR (VIM indicates an ERROR status)
1269 # ACTIVE, PAUSED, SUSPENDED, INACTIVE (not running),
1270 # BUILD (on building process), ERROR
1271 # ACTIVE:NoMgmtIP (Active but none of its interfaces has an IP address
1272 # (ACTIVE:NoMgmtIP is not returned for Azure)
1274 error_msg: #Text with VIM error message, if any. Or the VIM connection ERROR
1275 vim_info: #Text with plain information obtained from vim (yaml.safe_dump)
1276 interfaces: list with interface info. Each item a dictionary with:
1277 vim_interface_id - The ID of the interface
1278 mac_address - The MAC address of the interface.
1279 ip_address - The IP address of the interface within the subnet.
1281 self
.logger
.debug("refresh_vms_status begin: vm_list %s", vm_list
)
1283 self
._reload
_connection
()
1285 search_vm_list
= vm_list
or {}
1287 for vm_id
in search_vm_list
:
1290 res_name
= self
._get
_resource
_name
_from
_resource
_id
(vm_id
)
1293 self
.conn_compute
.instances()
1294 .get(project
=self
.project
, zone
=self
.zone
, instance
=res_name
)
1298 out_vm
["vim_info"] = str(vm
["name"])
1299 out_vm
["status"] = self
.provision_state2osm
.get(vm
["status"], "OTHER")
1301 # In Google Cloud the there is no difference between provision or power status,
1302 # so if provision status method doesn't return a specific state (OTHER), the
1303 # power method is called
1304 if out_vm
["status"] == "OTHER":
1305 out_vm
["status"] = self
.power_state2osm
.get(vm
["status"], "OTHER")
1307 network_interfaces
= vm
["networkInterfaces"]
1308 out_vm
["interfaces"] = self
._get
_vm
_interfaces
_status
(
1309 vm_id
, network_interfaces
1311 except Exception as e
:
1312 self
.logger
.error("Exception %s refreshing vm_status", e
, exc_info
=True)
1313 out_vm
["status"] = "VIM_ERROR"
1314 out_vm
["error_msg"] = str(e
)
1315 out_vm
["vim_info"] = None
1317 out_vms
[vm_id
] = out_vm
1319 self
.logger
.debug("refresh_vms_status Return: out_vms %s", out_vms
)
1322 def _get_vm_interfaces_status(self
, vm_id
, interfaces
):
1324 Gets the interfaces detail for a vm
1325 :param interfaces: List of interfaces.
1326 :return: Dictionary with list of interfaces including, vim_interface_id, mac_address and ip_address
1329 "_get_vm_interfaces_status begin: vm_id %s interfaces %s",
1335 for network_interface
in interfaces
:
1337 nic_name
= network_interface
["name"]
1338 interface_dict
["vim_interface_id"] = network_interface
["name"]
1341 ips
.append(network_interface
["networkIP"])
1342 interface_dict
["ip_address"] = ";".join(ips
)
1343 interface_list
.append(interface_dict
)
1346 "_get_vm_interfaces_status Return: interface_list %s",
1349 return interface_list
1350 except Exception as e
:
1352 "Exception %s obtaining interface data for vm: %s",
1357 self
._format
_vimconn
_exception
(e
)
1359 def _get_default_admin_user(self
, image_id
):
1360 if "ubuntu" in image_id
.lower():
1363 return self
._default
_admin
_user
1365 def _create_firewall_rules(self
, network
):
1367 Creates the necessary firewall rules to allow the traffic in the network
1368 (https://cloud.google.com/vpc/docs/firewalls)
1370 :return: a list with the names of the firewall rules
1372 self
.logger
.debug("_create_firewall_rules begin: network %s", network
)
1376 # Adding firewall rule to allow http:
1377 self
.logger
.debug("creating firewall rule to allow http")
1378 firewall_rule_body
= {
1379 "name": "fw-rule-http-" + network
,
1380 "network": "global/networks/" + network
,
1381 "allowed": [{"IPProtocol": "tcp", "ports": ["80"]}],
1383 operation_firewall
= (
1384 self
.conn_compute
.firewalls()
1385 .insert(project
=self
.project
, body
=firewall_rule_body
)
1389 # Adding firewall rule to allow ssh:
1390 self
.logger
.debug("creating firewall rule to allow ssh")
1391 firewall_rule_body
= {
1392 "name": "fw-rule-ssh-" + network
,
1393 "network": "global/networks/" + network
,
1394 "allowed": [{"IPProtocol": "tcp", "ports": ["22"]}],
1396 operation_firewall
= (
1397 self
.conn_compute
.firewalls()
1398 .insert(project
=self
.project
, body
=firewall_rule_body
)
1402 # Adding firewall rule to allow ping:
1403 self
.logger
.debug("creating firewall rule to allow ping")
1404 firewall_rule_body
= {
1405 "name": "fw-rule-icmp-" + network
,
1406 "network": "global/networks/" + network
,
1407 "allowed": [{"IPProtocol": "icmp"}],
1409 operation_firewall
= (
1410 self
.conn_compute
.firewalls()
1411 .insert(project
=self
.project
, body
=firewall_rule_body
)
1415 # Adding firewall rule to allow internal:
1416 self
.logger
.debug("creating firewall rule to allow internal")
1417 firewall_rule_body
= {
1418 "name": "fw-rule-internal-" + network
,
1419 "network": "global/networks/" + network
,
1421 {"IPProtocol": "tcp", "ports": ["0-65535"]},
1422 {"IPProtocol": "udp", "ports": ["0-65535"]},
1423 {"IPProtocol": "icmp"},
1426 operation_firewall
= (
1427 self
.conn_compute
.firewalls()
1428 .insert(project
=self
.project
, body
=firewall_rule_body
)
1432 # Adding firewall rule to allow microk8s:
1433 self
.logger
.debug("creating firewall rule to allow microk8s")
1434 firewall_rule_body
= {
1435 "name": "fw-rule-microk8s-" + network
,
1436 "network": "global/networks/" + network
,
1437 "allowed": [{"IPProtocol": "tcp", "ports": ["16443"]}],
1439 operation_firewall
= (
1440 self
.conn_compute
.firewalls()
1441 .insert(project
=self
.project
, body
=firewall_rule_body
)
1445 # Adding firewall rule to allow rdp:
1446 self
.logger
.debug("creating firewall rule to allow rdp")
1447 firewall_rule_body
= {
1448 "name": "fw-rule-rdp-" + network
,
1449 "network": "global/networks/" + network
,
1450 "allowed": [{"IPProtocol": "tcp", "ports": ["3389"]}],
1452 operation_firewall
= (
1453 self
.conn_compute
.firewalls()
1454 .insert(project
=self
.project
, body
=firewall_rule_body
)
1458 # Adding firewall rule to allow osm:
1459 self
.logger
.debug("creating firewall rule to allow osm")
1460 firewall_rule_body
= {
1461 "name": "fw-rule-osm-" + network
,
1462 "network": "global/networks/" + network
,
1463 "allowed": [{"IPProtocol": "tcp", "ports": ["9001", "9999"]}],
1465 operation_firewall
= (
1466 self
.conn_compute
.firewalls()
1467 .insert(project
=self
.project
, body
=firewall_rule_body
)
1472 "_create_firewall_rules Return: list_rules %s", rules_list
1475 except Exception as e
:
1477 "Unable to create google cloud firewall rules for network '{}'".format(
1481 self
._format
_vimconn
_exception
(e
)
1483 def _delete_firewall_rules(self
, network
):
1485 Deletes the associated firewall rules to the network
1487 :return: a list with the names of the firewall rules
1489 self
.logger
.debug("_delete_firewall_rules begin: network %s", network
)
1494 self
.conn_compute
.firewalls().list(project
=self
.project
).execute()
1496 for item
in rules_list
["items"]:
1497 if network
== self
._get
_resource
_name
_from
_resource
_id
(item
["network"]):
1498 operation_firewall
= (
1499 self
.conn_compute
.firewalls()
1500 .delete(project
=self
.project
, firewall
=item
["name"])
1504 self
.logger
.debug("_delete_firewall_rules Return: list_rules %s", 0)
1506 except Exception as e
:
1508 "Unable to delete google cloud firewall rules for network '{}'".format(
1512 self
._format
_vimconn
_exception
(e
)