Esempio n. 1
0
def delete_transit_gateway_connection(tg_connection):
    if not tg_connection:
        return

    current_app.logger.info(
        f"Deleting Transit Gateway Connection '{tg_connection.name}' on IBM Cloud"
    )

    try:
        ibm_manager = IBMManager(tg_connection.transit_gateway.ibm_cloud,
                                 initialize_tg_manager=True,
                                 initialize_rias_ops=False)

        tg_connection_exists = ibm_manager.tg_manager.fetch_ops.get_transit_gateway_connection(
            tg_connection)

        if not tg_connection_exists:
            doosradb.session.delete(tg_connection)
            doosradb.session.commit()

        ibm_manager.tg_manager.push_ops.delete_transit_gateway_connection(
            tg_connection)

    except (IBMAuthError, IBMConnectError, IBMExecuteError,
            IBMInvalidRequestError) as ex:
        current_app.logger.info(ex)
        if isinstance(ex, IBMAuthError):
            tg_connection.cloud.status = INVALID
        tg_connection.status = ERROR_DELETING
        doosradb.session.commit()
    else:
        tg_connection.status = DELETED
        doosradb.session.delete(tg_connection)
        doosradb.session.commit()
        return True
Esempio n. 2
0
def update_transit_gateway(transit_gateway):
    if not transit_gateway:
        return

    current_app.logger.info(
        f"Updating Transit Gateway '{transit_gateway.name}' on IBM Cloud")

    try:
        ibm_manager = IBMManager(transit_gateway.ibm_cloud,
                                 initialize_tg_manager=True,
                                 initialize_rias_ops=False)

        tg_exists = ibm_manager.tg_manager.fetch_ops.get_transit_gateway(
            transit_gateway.resource_id)

        if not tg_exists:
            return

        ibm_manager.tg_manager.push_ops.update_transit_gateway(transit_gateway)

    except (IBMAuthError, IBMConnectError, IBMExecuteError,
            IBMInvalidRequestError) as ex:
        current_app.logger.info(ex)
        if isinstance(ex, IBMAuthError):
            transit_gateway.cloud.status = INVALID
        transit_gateway.status = ERROR_DELETING
        doosradb.session.commit()
    else:
        transit_gateway.status = CREATED
        doosradb.session.commit()
        return True
Esempio n. 3
0
def list_transit_locations(ibm_cloud):
    """
    Get a list of Transit Locations where TG is available.
    :return:
    """
    try:
        ibm_manager = IBMManager(ibm_cloud,
                                 initialize_rias_ops=False,
                                 initialize_tg_manager=True)
        locations = ibm_manager.tg_manager.fetch_ops.list_transit_locations()
    except (
            IBMAuthError,
            IBMConnectError,
            IBMExecuteError,
            IBMInvalidRequestError,
    ) as ex:
        LOGGER.info(ex)
        if isinstance(ex, IBMAuthError):
            ibm_cloud.status = INVALID
            doosradb.session.commit()
    else:
        return locations
Esempio n. 4
0
def task_migrate_kubernetes_cluster(self, task_id, cloud_id, region,
                                    cluster_id, source_cluster, softlayer_id):
    cluster = doosradb.session.query(KubernetesCluster).filter_by(
        id=cluster_id).first()
    if not cluster:
        LOGGER.info("No IKS Cluster found with ID: {}".format(cluster_id))
        return Response(status=404)

    softlayer_cloud = doosradb.session.query(SoftlayerCloud).filter_by(
        id=softlayer_id).first()
    if not softlayer_cloud:
        LOGGER.info(
            "No Softlayer Cloud found with ID: {}".format(softlayer_id))
        return Response(status=404)

    source_k8s_client = ClassicKubernetesClient(softlayer_cloud.username,
                                                softlayer_cloud.api_key)
    cloud = doosradb.session.query(IBMCloud).filter_by(id=cloud_id).first()
    ibm_manager = IBMManager(cloud=cloud, region=region)
    backup_name = "backup" + str(
        datetime.utcnow().strftime("-%m-%d-%Y%H-%M-%S"))
    bucket = ibm_manager.cos_ops.create_bucket(cluster.name + cluster.id[:3],
                                               region)

    if bucket and bucket.get('ResponseMetadata', {}).get('HTTPStatusCode',
                                                         {}) == 200:
        LOGGER.info("Bucket {} created successfully".format(cluster.name +
                                                            cluster.id[:3]))

        cos = {
            "bucket_name": cluster.name + cluster.id[:3],
            "access_key_id": cloud.service_credentials.access_key_id,
            "secret_access_key": cloud.service_credentials.secret_access_key,
            "bucket_region": region
        }

        cluster_migration_task = KubernetesClusterMigrationTask(
            base_task_id=task_id,
            cos=cos,
            source_cluster=source_k8s_client.get_cluster_kube_config(
                cluster=source_cluster))

        doosradb.session.add(cluster_migration_task)
        doosradb.session.commit()

        workflow_steps = list()
        workflow_steps.append(
            task_create_workload_backup.si(
                task_id=cluster_migration_task.base_task_id,
                region=region,
                cloud_id=cloud_id,
                cluster_migration_task_id=cluster_migration_task.id,
                cluster_id=cluster_id,
                backup_name=backup_name))
        workflow_steps.append(
            task_create_kubernetes_cluster.si(
                task_id=cluster_migration_task.base_task_id,
                region=region,
                cloud_id=cloud_id,
                cluster_migration_task_id=cluster_migration_task.id,
                cluster_id=cluster_id))
        workflow_steps.append(
            task_restore_workload_backup.si(
                task_id=cluster_migration_task.base_task_id,
                region=region,
                cloud_id=cloud_id,
                cluster_migration_task_id=cluster_migration_task.id,
                cluster_id=cluster_id,
                backup_name=backup_name))

        chain(workflow_steps).delay()

    else:
        doosradb.session.commit()
        LOGGER.info(BUCKET_CREATION_ERROR)
        raise WorkflowTerminated(BUCKET_CREATION_ERROR)
Esempio n. 5
0
    def __call__(self, *args, **kwargs):
        with app.app_context():
            try:
                LOGGER.info("Running '{0}'".format(self.name))
                self.region = kwargs["region"]
                self.cloud = doosradb.session.query(IBMCloud).filter_by(
                    id=kwargs["cloud_id"]).first()
                if not self.cloud:
                    raise WorkflowTerminated(
                        "IBM Cloud with ID '{id}' not found".format(
                            id=kwargs["cloud_id"]))

                self.ibm_task = doosradb.session.query(IBMTask).filter_by(
                    id=kwargs["task_id"]).first()
                if not self.ibm_task:
                    raise WorkflowTerminated(
                        "IBM Task with ID '{id}' has failed".format(
                            id=kwargs["task_id"]))

                self.ibm_manager = IBMManager(self.cloud, self.region)
                self.sub_task = IBMSubTask(task_id=self.request.id,
                                           resource_id=self.ibm_task.id)
                doosradb.session.add(self.sub_task)
                doosradb.session.commit()
                self.run(*args, **kwargs)
                self.sub_task.status = SUCCESS
                doosradb.session.commit()

            except StaleDataError:
                LOGGER.debug(
                    "SubTask for current task with ID '{id}' workflow removed".
                    format(id=self.ibm_task.id))

            except (IBMAuthError, IBMConnectError, IBMExecuteError,
                    IBMInvalidRequestError, SLInvalidRequestError, SLAuthError,
                    SLExecuteError, SLRateLimitExceededError, TaskFailureError,
                    WorkflowTerminated, VolumeAttachmentException,
                    ClientIBMExecuteError) as ex:

                if isinstance(ex, TaskFailureError):
                    LOGGER.error(ex)
                else:
                    LOGGER.info(ex)

                try:
                    if self.sub_task:
                        self.sub_task.status = FAILED
                        doosradb.session.commit()

                    if not self.ibm_task:
                        LOGGER.info(
                            "Task Failure: IBMTask with ID '{id}' not found".
                            format(id=kwargs["task_id"]))
                        return

                    fail_message = ex
                    if isinstance(ex, TaskFailureError):
                        if self.resource_type == "dedicated_hosts":
                            LOGGER.info(
                                "IBM Workflow Task Terminated for ID: '{id}'".
                                format(id=self.ibm_task.id))
                            self.ibm_task.status = FAILED
                            doosradb.session.merge(self.ibm_task)
                            doosradb.session.commit()
                            update_ibm_task(task_id=self.ibm_task.id)
                            self.request.chain = self.request.callbacks = None
                            return
                        fail_message = "Internal Server Error"

                    if self.resource:
                        self.report_utils.update_reporting(
                            task_id=self.ibm_task.id,
                            resource_name=self.vpn_cidr
                            if self.vpn_cidr else self.resource.name,
                            resource_type=self.resource_type,
                            stage=PROVISIONING,
                            status=FAILED,
                            message=fail_message,
                            path=self.report_path if self.report_path else "")

                        if self.resource.status == DELETING:
                            self.resource.status = ERROR_DELETING

                        elif self.resource.status in [CREATING, UPDATING]:
                            self.resource.status = ERROR_CREATING

                        doosradb.session.merge(self.resource)
                        doosradb.session.commit()
                    else:
                        self.resource = doosradb.session.query(
                            IBMVpcNetwork).filter_by(
                                id=self.ibm_task.resource_id).first()
                        if not self.resource:
                            self.resource = doosradb.session.query(
                                IBMInstance).filter_by(
                                    id=self.ibm_task.resource_id).first()
                        if not self.resource.status == CREATED:
                            self.resource.status = ERROR_CREATING
                            doosradb.session.commit()

                        if self.resource_name:
                            self.report_utils.update_reporting(
                                task_id=self.ibm_task.id,
                                resource_name=self.resource_name,
                                resource_type=self.resource_type,
                                stage=VALIDATION,
                                status=FAILED,
                                message=fail_message)

                    if kwargs.get("ibm_instance_task_id"):
                        ibm_instance_task = doosradb.session.query(
                            IBMInstanceTasks).filter_by(
                                id=kwargs["ibm_instance_task_id"]).first()
                        if ibm_instance_task:
                            ibm_instance_task.status = FAILED
                            ibm_instance_task.in_focus = False
                            doosradb.session.commit()

                    if hasattr(ex, 'trace_id'):
                        self.ibm_task.trace_id = ex.trace_id
                        doosradb.session.commit()

                    if not isinstance(ex, WorkflowTerminated):
                        self.ibm_task.message = str(ex)
                        doosradb.session.commit()
                        return

                    LOGGER.info(
                        "IBM Workflow Task Terminated for ID: '{id}'".format(
                            id=self.ibm_task.id))
                    self.ibm_task.status = FAILED
                    doosradb.session.merge(self.ibm_task)
                    doosradb.session.commit()
                    update_ibm_task(task_id=self.ibm_task.id)
                    self.request.chain = self.request.callbacks = None

                except DetachedInstanceError as e:
                    LOGGER.info(
                        "Instance detached error occurred but this can be ignored \n {}"
                        .format(str(e)))
Esempio n. 6
0
def configure_transit_gateway(name, cloud_id, data):
    """
    This request creates a new Transit Gateway from a Transit Gateway template.
    """
    transit_gateway = None

    cloud = IBMCloud.query.get(cloud_id)
    if not cloud:
        current_app.logger.info(
            "No IBM cloud found with ID {id}".format(id=data['cloud_id']))
        return

    current_app.logger.info(
        "Deploying Transit Gateway '{name}' on IBM Cloud".format(name=name))
    try:
        ibm_manager = IBMManager(cloud,
                                 initialize_tg_manager=True,
                                 initialize_rias_ops=False)
        existing_resource_group = ibm_manager.tg_manager.fetch_ops.get_resource_groups(
            data['resource_group'])

        if not existing_resource_group:
            raise IBMInvalidRequestError(
                "Resource Group with name '{}' not configured".format(
                    data['resource_group']))

        transit_gateways = ibm_manager.tg_manager.fetch_ops.get_all_transit_gateways(
        )

        existing_transit_gateway = list(
            map(lambda obj: obj.name, transit_gateways))
        if data['name'] in existing_transit_gateway:
            raise IBMInvalidRequestError(
                "Transit Gateway with name '{}' already configured for Cloud '{}'"
                .format(name, cloud.name))

        resource_group = doosradb.session.query(IBMResourceGroup).filter_by(
            name=data['resource_group'], cloud_id=cloud_id).first()

        if not resource_group:
            resource_group = existing_resource_group[0]
            resource_group.ibm_cloud = cloud
            doosradb.session.add(resource_group)

        transit_gateway = TransitGateway(
            name=data['name'],
            region=data["location"],
            is_global_route=data["is_global_route"])
        transit_gateway.ibm_cloud = cloud
        transit_gateway.ibm_resource_group = resource_group
        doosradb.session.add(transit_gateway)
        doosradb.session.commit()

        configured_transit_gateway = ibm_manager.tg_manager.push_ops.create_transit_gateway(
            transit_gateway_obj=transit_gateway)

        if not configured_transit_gateway:
            raise IBMInvalidRequestError(
                "Failed to configure Transit Gateway {transit_gateway} on region {region}"
                .format(transit_gateway=transit_gateway.name,
                        region=transit_gateway.region))

        if configured_transit_gateway["name"] == transit_gateway.name:
            transit_gateway.resource_id = configured_transit_gateway["id"]
            transit_gateway.created_at = configured_transit_gateway[
                "created_at"]
            transit_gateway.crn = configured_transit_gateway["crn"]
            transit_gateway.gateway_status = configured_transit_gateway[
                "status"]
            doosradb.session.commit()

        connections = data.get('connections', [])
        # Transit Gateway Connection Creation
        if len(connections):
            for connection in connections:
                existing_transit_gateway_connection = doosradb.session.query(
                    TransitGatewayConnection).filter_by(
                        name=connection['name'],
                        transit_gateway_id=transit_gateway.id).first()

                if existing_transit_gateway_connection:
                    raise IBMInvalidRequestError(
                        "Transit Gateway Connection with name '{}' already configured for TransitGateway '{}'"
                        .format(connection['name'], transit_gateway.name))

                configure_transit_gateway_connection(connection['name'],
                                                     transit_gateway.id,
                                                     cloud_id, connection)

    except (IBMAuthError, IBMConnectError, IBMExecuteError,
            IBMInvalidRequestError) as ex:
        current_app.logger.info(ex)
        if isinstance(ex, IBMAuthError):
            cloud.status = INVALID
        if transit_gateway:
            transit_gateway.status = ERROR_CREATING
        doosradb.session.commit()
    else:
        transit_gateway.status = CREATED
        doosradb.session.commit()

    return transit_gateway
Esempio n. 7
0
def configure_transit_gateway_connection(name, transit_gateway_id, cloud_id,
                                         data):
    """
    This request creates a new Transit Gateway Connection from a Transit Gateway Connection template.
    """
    transit_gateway_connection = None
    cloud = IBMCloud.query.get(cloud_id)
    if not cloud:
        current_app.logger.debug(
            "IBM Cloud with ID '{id}' not found".format(id=cloud_id))
        return

    if data.get('vpc_id'):
        vpc = IBMVpcNetwork.query.get(data["vpc_id"])
        if not vpc:
            current_app.logger.debug(
                "IBM VPC Network with ID '{id}' not found".format(
                    id=data['vpc_id']))
            return

    transit_gateway = TransitGateway.query.get(transit_gateway_id)
    if not transit_gateway:
        current_app.logger.debug(
            "Transit Gateway with ID '{id}' not found".format(
                id=data['transit_gateway_id']))
        return

    current_app.logger.info(
        "Creating Transit Gateway Connection '{}' on IBM Cloud".format(name))
    try:
        ibm_manager = IBMManager(transit_gateway.ibm_cloud,
                                 initialize_tg_manager=True,
                                 initialize_rias_ops=False)

        existing_transit_gateway = ibm_manager.tg_manager.fetch_ops.get_transit_gateway(
            transit_gateway.resource_id)
        if not existing_transit_gateway:
            raise IBMInvalidRequestError(
                "Transit Gateway with '{}' not found".format(
                    transit_gateway.name))

        existing_transit_gateway_connections = ibm_manager.tg_manager.fetch_ops.get_all_transit_gateway_connections(
            name=data['name'],
            transit_gateway_id=existing_transit_gateway.resource_id)
        if existing_transit_gateway_connections:
            raise IBMInvalidRequestError(
                "Connection already exists with name '{}' for Transit Gateway '{}'"
                .format(name, transit_gateway.name))

        transit_gateway_connection = TransitGatewayConnection(
            name=data['name'],
            network_type=data['network_type'],
            network_id=vpc.crn if data['network_type'] == "vpc" else None,
            region=vpc.region if data['network_type'] == "vpc" else None)
        transit_gateway_connection.ibm_vpc_network = vpc if data[
            'network_type'] == "vpc" else None
        transit_gateway_connection.transit_gateway = transit_gateway
        doosradb.session.add(transit_gateway_connection)
        doosradb.session.commit()

        configured_transit_gateway_connection = ibm_manager.tg_manager.push_ops.create_transit_gateway_connection(
            connection_obj=transit_gateway_connection,
            transit_gateway_obj=existing_transit_gateway)

        if not configured_transit_gateway_connection:
            raise IBMInvalidRequestError(
                "Failed to configure Transit Gateway Connection with name {}".
                format(data['name']))

        transit_gateway_connection.resource_id = configured_transit_gateway_connection[
            "id"]
        transit_gateway_connection.created_at = configured_transit_gateway_connection[
            "created_at"]
        transit_gateway_connection.connection_status = configured_transit_gateway_connection[
            "status"]

        doosradb.session.commit()

    except (IBMAuthError, IBMConnectError, IBMExecuteError,
            IBMInvalidRequestError) as ex:
        current_app.logger.info(ex)
        if isinstance(ex, IBMAuthError):
            cloud.status = INVALID
        if transit_gateway_connection:
            transit_gateway_connection.status = ERROR_CREATING
        doosradb.session.commit()
    else:
        transit_gateway_connection.status = CREATED
        doosradb.session.commit()

    return transit_gateway_connection