def wait_for_operation(operation: compute_v1.Operation, project_id: str) -> compute_v1.Operation: """ This method waits for an operation to be completed. Calling this function will block until the operation is finished. Args: operation: The Operation object representing the operation you want to wait on. project_id: ID or number of the project owning the operation. Returns: Finished Operation object. """ kwargs = {"project": project_id, "operation": operation.name} if operation.zone: client = compute_v1.ZoneOperationsClient() # Operation.zone is a full URL address of a zone, so we need to extract just the name kwargs["zone"] = operation.zone.rsplit("/", maxsplit=1)[1] elif operation.region: client = compute_v1.RegionOperationsClient() # Operation.region is a full URL address of a zone, so we need to extract just the name kwargs["region"] = operation.region.rsplit("/", maxsplit=1)[1] else: client = compute_v1.GlobalOperationsClient() return client.wait(**kwargs)
def set_usage_export_bucket(project_id: str, bucket_name: str, report_name_prefix: str = "") -> None: """ Set Compute Engine usage export bucket for the Cloud project. This sample presents how to interpret the default value for the report name prefix parameter. Args: project_id: project ID or project number of the project to update. bucket_name: Google Cloud Storage bucket used to store Compute Engine usage reports. An existing Google Cloud Storage bucket is required. report_name_prefix: Prefix of the usage report name which defaults to an empty string to showcase default values behaviour. """ usage_export_location = compute_v1.UsageExportLocation( bucket_name=bucket_name, report_name_prefix=report_name_prefix) if not report_name_prefix: # Sending an empty value for report_name_prefix results in the # next usage report being generated with the default prefix value # "usage_gce". (ref: https://cloud.google.com/compute/docs/reference/rest/v1/projects/setUsageExportBucket) print("Setting report_name_prefix to empty value causes the report " "to have the default prefix of `usage_gce`.") projects_client = compute_v1.ProjectsClient() operation = projects_client.set_usage_export_bucket( project=project_id, usage_export_location_resource=usage_export_location) op_client = compute_v1.GlobalOperationsClient() while operation.status != compute_v1.Operation.Status.DONE: operation = op_client.wait(operation=operation.name, project=project_id)
def firewall_rule(): firewall_rule = compute_v1.Firewall() firewall_rule.name = "firewall-sample-test" + uuid.uuid4().hex[:10] firewall_rule.direction = "INGRESS" allowed_ports = compute_v1.Allowed() allowed_ports.I_p_protocol = "tcp" allowed_ports.ports = ["80"] firewall_rule.allowed = [allowed_ports] firewall_rule.source_ranges = ["0.0.0.0/0"] firewall_rule.network = "global/networks/default" firewall_rule.description = "Rule generated by Python sample test fixture." firewall_rule.target_tags = ["web"] firewall_client = compute_v1.FirewallsClient() op = firewall_client.insert_unary(project=PROJECT, firewall_resource=firewall_rule) op_client = compute_v1.GlobalOperationsClient() op_client.wait(project=PROJECT, operation=op.name) yield firewall_client.get(project=PROJECT, firewall=firewall_rule.name) try: op = firewall_client.delete_unary(project=PROJECT, firewall=firewall_rule.name) op_client.wait(project=PROJECT, operation=op.name) except google.api_core.exceptions.BadRequest as err: if err.code == 400 and "is not ready" in err.message: # This means GCE enforcer has already deleted that rule. pass else: raise err
def instance_template(): disk = compute_v1.AttachedDisk() initialize_params = compute_v1.AttachedDiskInitializeParams() initialize_params.source_image = ( "projects/debian-cloud/global/images/family/debian-11") initialize_params.disk_size_gb = 25 disk.initialize_params = initialize_params disk.auto_delete = True disk.boot = True network_interface = compute_v1.NetworkInterface() network_interface.name = "global/networks/default" template = compute_v1.InstanceTemplate() template.name = "test-template-" + uuid.uuid4().hex[:10] template.properties.disks = [disk] template.properties.machine_type = "e2-standard-4" template.properties.network_interfaces = [network_interface] template_client = compute_v1.InstanceTemplatesClient() operation_client = compute_v1.GlobalOperationsClient() op = template_client.insert_unary(project=PROJECT, instance_template_resource=template) operation_client.wait(project=PROJECT, operation=op.name) template = template_client.get(project=PROJECT, instance_template=template.name) yield template op = template_client.delete_unary(project=PROJECT, instance_template=template.name) operation_client.wait(project=PROJECT, operation=op.name)
def create_template(project_id: str, template_name: str) -> compute_v1.InstanceTemplate: """ Create a new instance template with the provided name and a specific instance configuration. Args: project_id: project ID or project number of the Cloud project you use. template_name: name of the new template to create. Returns: InstanceTemplate object that represents the new instance template. """ # The template describes the size and source image of the boot disk # to attach to the instance. disk = compute_v1.AttachedDisk() initialize_params = compute_v1.AttachedDiskInitializeParams() initialize_params.source_image = ( "projects/debian-cloud/global/images/family/debian-11") initialize_params.disk_size_gb = 250 disk.initialize_params = initialize_params disk.auto_delete = True disk.boot = True # The template connects the instance to the `default` network, # without specifying a subnetwork. network_interface = compute_v1.NetworkInterface() network_interface.name = "global/networks/default" # The template lets the instance use an external IP address. access_config = compute_v1.AccessConfig() access_config.name = "External NAT" access_config.type_ = "ONE_TO_ONE_NAT" access_config.network_tier = "PREMIUM" network_interface.access_configs = [access_config] template = compute_v1.InstanceTemplate() template.name = template_name template.properties.disks = [disk] template.properties.machine_type = "e2-standard-4" template.properties.network_interfaces = [network_interface] template_client = compute_v1.InstanceTemplatesClient() operation_client = compute_v1.GlobalOperationsClient() op = template_client.insert_unary(project=project_id, instance_template_resource=template) operation_client.wait(project=project_id, operation=op.name) return template_client.get(project=project_id, instance_template=template_name)
def delete_instance_template(project_id: str, template_name: str): """ Delete an instance template. Args: project_id: project ID or project number of the Cloud project you use. template_name: name of the template to delete. """ template_client = compute_v1.InstanceTemplatesClient() operation_client = compute_v1.GlobalOperationsClient() op = template_client.delete_unary(project=project_id, instance_template=template_name) operation_client.wait(project=project_id, operation=op.name) return
def delete_firewall_rule(project_id: str, firewall_rule_name: str): """ Deleted a firewall rule from the project. Args: project_id: project ID or project number of the Cloud project you want to use. firewall_rule_name: name of the firewall rule you want to delete. """ firewall_client = compute_v1.FirewallsClient() operation = firewall_client.delete_unary(project=project_id, firewall=firewall_rule_name) operation_client = compute_v1.GlobalOperationsClient() operation_client.wait(project=project_id, operation=operation.name) return
def create_template_with_subnet( project_id: str, network: str, subnetwork: str, template_name: str) -> compute_v1.InstanceTemplate: """ Create an instance template that uses a provided subnet. Args: project_id: project ID or project number of the Cloud project you use. network: the network to be used in the new template. This value uses the following format: "projects/{project}/global/networks/{network}" subnetwork: the subnetwork to be used in the new template. This value uses the following format: "projects/{project}/regions/{region}/subnetworks/{subnetwork}" template_name: name of the new template to create. Returns: InstanceTemplate object that represents the new instance template. """ # The template describes the size and source image of the book disk to # attach to the instance. disk = compute_v1.AttachedDisk() initialize_params = compute_v1.AttachedDiskInitializeParams() initialize_params.source_image = ( "projects/debian-cloud/global/images/family/debian-11") initialize_params.disk_size_gb = 250 disk.initialize_params = initialize_params disk.auto_delete = True disk.boot = True template = compute_v1.InstanceTemplate() template.name = template_name template.properties = compute_v1.InstanceProperties() template.properties.disks = [disk] template.properties.machine_type = "e2-standard-4" # The template connects the instance to the specified network and subnetwork. network_interface = compute_v1.NetworkInterface() network_interface.network = network network_interface.subnetwork = subnetwork template.properties.network_interfaces = [network_interface] template_client = compute_v1.InstanceTemplatesClient() operation_client = compute_v1.GlobalOperationsClient() op = template_client.insert_unary(project=project_id, instance_template_resource=template) operation_client.wait(project=project_id, operation=op.name) return template_client.get(project=project_id, instance_template=template_name)
def __init__(self, infra_spec, options): super().__init__(infra_spec, options) self.project = 'couchbase-qe' self.credentials, _ = google.auth.default() self.storage_client = storage.Client(project=self.project, credentials=self.credentials) self.instance_client = compute.InstancesClient() self.network_client = compute.NetworksClient() self.subnet_client = compute.SubnetworksClient() self.firewall_client = compute.FirewallsClient() self.zone_ops_client = compute.ZoneOperationsClient() self.region_ops_client = compute.RegionOperationsClient() self.global_ops_client = compute.GlobalOperationsClient() with open(self.generated_cloud_config_path) as f: self.deployed_infra = json.load(f) self.zone = self.deployed_infra['zone'] self.region = self.zone.rsplit('-', 1)[0]
def create_firewall_rule(project_id: str, firewall_rule_name: str, network: str = "global/networks/default"): """ Creates a simple firewall rule allowing for incoming HTTP and HTTPS access from the entire Internet. Args: project_id: project ID or project number of the Cloud project you want to use. firewall_rule_name: name of the rule that is created. network: name of the network the rule will be applied to. Available name formats: * https://www.googleapis.com/compute/v1/projects/{project_id}/global/networks/{network} * projects/{project_id}/global/networks/{network} * global/networks/{network} """ firewall_rule = compute_v1.Firewall() firewall_rule.name = firewall_rule_name firewall_rule.direction = "INGRESS" allowed_ports = compute_v1.Allowed() allowed_ports.I_p_protocol = "tcp" allowed_ports.ports = ["80", "443"] firewall_rule.allowed = [allowed_ports] firewall_rule.source_ranges = ["0.0.0.0/0"] firewall_rule.network = network firewall_rule.description = "Allowing TCP traffic on port 80 and 443 from Internet." firewall_rule.target_tags = ["web"] # Note that the default value of priority for the firewall API is 1000. # If you check the value of `firewall_rule.priority` at this point it # will be equal to 0, however it is not treated as "set" by the library and thus # the default will be applied to the new rule. If you want to create a rule that # has priority == 0, you need to explicitly set it so: # firewall_rule.priority = 0 firewall_client = compute_v1.FirewallsClient() op = firewall_client.insert_unary(project=project_id, firewall_resource=firewall_rule) op_client = compute_v1.GlobalOperationsClient() op_client.wait(project=project_id, operation=op.name) return
def disable_usage_export(project_id: str) -> None: """ Disable Compute Engine usage export bucket for the Cloud Project. Args: project_id: project ID or project number of the project to update. """ projects_client = compute_v1.ProjectsClient() # Updating the setting with None will disable the # usage report generation. operation = projects_client.set_usage_export_bucket( project=project_id, usage_export_location_resource=None) op_client = compute_v1.GlobalOperationsClient() while operation.status != compute_v1.Operation.Status.DONE: operation = op_client.wait(operation=operation.name, project=project_id)
def create_template_from_instance( project_id: str, instance: str, template_name: str) -> compute_v1.InstanceTemplate: """ Create a new instance template based on an existing instance. This new template specifies a different boot disk. Args: project_id: project ID or project number of the Cloud project you use. instance: the instance to base the new template on. This value uses the following format: "projects/{project}/zones/{zone}/instances/{instance_name}" template_name: name of the new template to create. Returns: InstanceTemplate object that represents the new instance template. """ disk = compute_v1.DiskInstantiationConfig() # Device name must match the name of a disk attached to the instance you are # basing your template on. disk.device_name = "disk-1" # Replace the original boot disk image used in your instance with a Rocky Linux image. disk.instantiate_from = "CUSTOM_IMAGE" disk.custom_image = "projects/rocky-linux-cloud/global/images/family/rocky-linux-8" # Override the auto_delete setting. disk.auto_delete = True template = compute_v1.InstanceTemplate() template.name = template_name template.source_instance = instance template.source_instance_params = compute_v1.SourceInstanceParams() template.source_instance_params.disk_configs = [disk] template_client = compute_v1.InstanceTemplatesClient() operation_client = compute_v1.GlobalOperationsClient() op = template_client.insert_unary(project=project_id, instance_template_resource=template) operation_client.wait(project=project_id, operation=op.name) return template_client.get(project=project_id, instance_template=template_name)
def patch_firewall_priority(project_id: str, firewall_rule_name: str, priority: int): """ Modifies the priority of a given firewall rule. Args: project_id: project ID or project number of the Cloud project you want to use. firewall_rule_name: name of the rule you want to modify. priority: the new priority to be set for the rule. """ firewall_rule = compute_v1.Firewall() firewall_rule.priority = priority # The patch operation doesn't require the full definition of a Firewall object. It will only update # the values that were set in it, in this case it will only change the priority. firewall_client = compute_v1.FirewallsClient() operation = firewall_client.patch_unary(project=project_id, firewall=firewall_rule_name, firewall_resource=firewall_rule) operation_client = compute_v1.GlobalOperationsClient() operation_client.wait(project=project_id, operation=operation.name) return