예제 #1
0
def task_validate_ibm_ssh_key(self, task_id, cloud_id, region, ssh_key):
    """Check if ssh key already exists"""

    self.resource_name = ssh_key["name"]
    self.resource_type = 'ssh_keys'
    self.report_utils.update_reporting(task_id=task_id,
                                       resource_name=self.resource_name,
                                       resource_type=self.resource_type,
                                       stage=VALIDATION,
                                       status=IN_PROGRESS)
    existing_ssh_keys = self.ibm_manager.rias_ops.fetch_ops.get_all_ssh_keys()

    for existing_ssh_key in existing_ssh_keys:
        if existing_ssh_key.name == ssh_key[
                "name"] and existing_ssh_key.public_key != ssh_key[
                    "public_key"]:
            raise IBMInvalidRequestError(
                "IBM SSH Key with name '{}' already configured on IBM with a different public key."
                .format(ssh_key["name"]))
        elif existing_ssh_key.public_key == ssh_key[
                "public_key"] and existing_ssh_key.name != ssh_key["name"]:
            raise IBMInvalidRequestError(
                "IBM SSH Key with public key '{}' already configured with a different name '{}'."
                .format(ssh_key["name"], existing_ssh_key.name,
                        ssh_key["name"], existing_ssh_key.name))

    self.report_utils.update_reporting(task_id=task_id,
                                       resource_name=self.resource_name,
                                       resource_type=self.resource_type,
                                       stage=VALIDATION,
                                       status=SUCCESS)
    LOGGER.info("IBM SSH key with name '{}' validated successfully".format(
        ssh_key["name"]))
예제 #2
0
def get_cos_buckets(ibm_cloud, region, get_objects, primary_objects=True):
    """
    This request lists all COS buckets available in the region
    :return:
    """
    try:
        if not ibm_cloud.service_credentials:
            raise IBMInvalidRequestError(
                "IBM Service Credential for COS is required")

        ibm_manager = IBMManager(ibm_cloud, region)
        buckets = ibm_manager.cos_ops.fetch_ops.get_buckets(
            get_objects=get_objects, primary_objects=primary_objects)

    except (
            IBMAuthError,
            IBMConnectError,
            IBMExecuteError,
            IBMInvalidRequestError,
    ) as ex:
        current_app.logger.info(ex)
        if isinstance(ex, IBMAuthError):
            ibm_cloud.status = INVALID
            doosradb.session.commit()
        return None, False
    else:
        return buckets, True
예제 #3
0
def task_validate_kubernetes_cluster(self, task_id, cloud_id, region,
                                     cluster_id):
    """ Check if Cluster already exists """

    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)

    self.resource = cluster
    self.resource_type = "iks_clusters"
    self.report_utils.update_reporting(task_id=task_id,
                                       resource_name=self.resource.name,
                                       resource_type=self.resource_type,
                                       stage=VALIDATION,
                                       status=IN_PROGRESS)

    kubernetes_client = VPCKubernetesClient(cloud_id)
    existing_cluster = kubernetes_client.get_cluster_status(cluster.name)
    if existing_cluster:
        raise IBMInvalidRequestError(
            "IKS Cluster with name '{cluster}'"
            " already provisioned".format(cluster=cluster.name))

    self.report_utils.update_reporting(task_id=task_id,
                                       resource_name=self.resource.name,
                                       resource_type=self.resource_type,
                                       stage=VALIDATION,
                                       status=SUCCESS)
    LOGGER.info(
        "IKS Cluster with name '{cluster}' validated successfully".format(
            cluster=cluster.name))
예제 #4
0
def task_validate_ibm_resource_group(self, task_id, cloud_id, region,
                                     resource_group):
    """Check if resource group is configured"""
    self.resource_type = 'resource_group'
    self.resource_name = resource_group
    self.report_utils.update_reporting(task_id=task_id,
                                       resource_name=self.resource_name,
                                       resource_type=self.resource_type,
                                       stage=VALIDATION,
                                       status=IN_PROGRESS)
    existing_resource_group = self.ibm_manager.resource_ops.fetch_ops.get_resource_groups(
        resource_group)
    if not existing_resource_group:
        raise IBMInvalidRequestError(
            "Resource Group with name '{name}' not configured".format(
                name=resource_group))

    self.report_utils.update_reporting(task_id=task_id,
                                       resource_name=self.resource_name,
                                       resource_type=self.resource_type,
                                       stage=VALIDATION,
                                       status=SUCCESS)
    LOGGER.info(
        "IBM Resource group with name '{name}' validated successfully".format(
            name=resource_group))
예제 #5
0
def configure_and_save_obj_confs(ibm_manager, obj):
    """
    This method pushes the config objects on IBM, and validates these objects.
    :return:
    """
    if not ibm_manager:
        return

    ibm_manager.rias_ops.push_obj_confs(obj)
    existing_obj = ibm_manager.rias_ops.fetch_ops.list_obj_method_mapper(obj)
    if not existing_obj:
        raise IBMInvalidRequestError(
            "Failed to configure '{obj}' with name '{name}'".format(
                obj=obj.__class__.__name__, name=obj.name))

    return existing_obj[0]
예제 #6
0
    def __execute(self, obj, request, data=None, required_status=None):
        if not self.cloud.credentials:
            raise IBMAuthError(self.cloud.id)
        try:
            request_url = request[1].format(base_url=self.base_url,
                                            version=VERSION)
            if self.cloud.credentials.is_token_expired():
                self.cloud.credentials.update_token(
                    IBMCredentials(self.iam_ops.authenticate_cloud_account()))

            headers = {"Authorization": self.cloud.credentials.access_token}
            current_app.logger.debug("{0} : {1}".format(
                request[0], request_url))
            response = self.session.request(request[0],
                                            request_url,
                                            json=data,
                                            timeout=30,
                                            headers=headers)
        except (
                ConnectionError,
                ReadTimeout,
                RequestException,
                MaxRetryError,
                ReadTimeoutError,
        ) as ex:
            current_app.logger.debug(ex)
            raise IBMConnectError(self.cloud.id)
        else:
            if response.status_code == 401:
                raise IBMAuthError(self.cloud.id)
            elif response.status_code in [400, 408, 500]:
                raise IBMExecuteError(response)
            elif response.status_code not in [200, 201, 204, 404]:
                raise IBMExecuteError(response)

            if not request[0] == "PATCH":
                status = self.wait_for_operation(
                    obj, obj.resource_id or response.json().get("id"),
                    required_status)
                if not status:
                    raise IBMInvalidRequestError(
                        "The requested operation could not be performed:\n{0} : {1}"
                        .format(request[0], request_url))

            return response.json() if response.text else ""
예제 #7
0
def task_validate_ibm_images(self, task_id, cloud_id, region, image_name):
    """Validate if instance images exist"""
    self.resource_name = image_name
    self.resource_type = 'images'
    self.report_utils.update_reporting(
        task_id=task_id, resource_name=self.resource_name, resource_type=self.resource_type, stage=VALIDATION,
        status=IN_PROGRESS
    )

    existing_image = self.ibm_manager.rias_ops.raw_fetch_ops.get_all_images(name=image_name)
    if not existing_image:
        raise IBMInvalidRequestError(
            "IBM Image with name '{}' not found".format(image_name))

    self.report_utils.update_reporting(
        task_id=task_id, resource_name=self.resource_name, resource_type=self.resource_type, stage=VALIDATION,
        status=SUCCESS
    )
    LOGGER.info(
        "IBM Image with name '{name}' validated successfully".format(name=image_name))
예제 #8
0
def task_validate_ibm_ipsec_policy(self, task_id, cloud_id, region, ipsec_policy):
    """Check if IPSEC policy already exists"""
    self.resource_name = ipsec_policy["name"]
    self.resource_type = 'ipsec_policies'
    self.report_utils.update_reporting(
        task_id=task_id, resource_name=self.resource_name, resource_type=self.resource_type, stage=VALIDATION,
        status=IN_PROGRESS
    )

    existing_ipsec_policy = self.ibm_manager.rias_ops.raw_fetch_ops.get_all_ipsec_policies(name=ipsec_policy)
    if existing_ipsec_policy:
        raise IBMInvalidRequestError(
            "IBM IPSec Policy with name '{}' already configured".format(ipsec_policy["name"]))

    self.report_utils.update_reporting(
        task_id=task_id, resource_name=self.resource_name, resource_type=self.resource_type, stage=VALIDATION,
        status=SUCCESS
    )
    LOGGER.info(
        "IBM IPSec Policy with name '{}' validated successfully".format(
            ipsec_policy["name"]))
예제 #9
0
def task_validate_ibm_floating_ip(self, task_id, cloud_id, region,
                                  floating_ip):
    """Validate if floating IP already exists"""
    self.resource_name = floating_ip["name"]
    self.resource_type = "floating_ips"
    self.report_utils.update_reporting(task_id=task_id,
                                       resource_name=self.resource.name,
                                       resource_type=self.resource_type,
                                       stage=VALIDATION,
                                       status=IN_PROGRESS)
    existing_floating_ip = self.ibm_manager.rias_ops.raw_fetch_ops.get_all_floating_ips(
        name=floating_ip["name"])
    if existing_floating_ip:
        raise IBMInvalidRequestError(
            "Floating IP '{name}' already exists".format(
                name=floating_ip["name"]))

    self.report_utils.update_reporting(task_id=task_id,
                                       resource_name=self.resource.name,
                                       resource_type=self.resource_type,
                                       stage=VALIDATION,
                                       status=SUCCESS)
예제 #10
0
def task_validate_ibm_acl(self, task_id, cloud_id, region, vpc_name, acl):
    """Check if ACL already exists"""
    self.resource_name = acl["name"]
    self.resource_type = "acls"
    self.report_utils.update_reporting(task_id=task_id,
                                       resource_name=self.resource_name,
                                       resource_type=self.resource_type,
                                       stage=VALIDATION,
                                       status=IN_PROGRESS)
    existing_acl = self.ibm_manager.rias_ops.fetch_ops.get_all_networks_acls(
        name=acl["name"])
    if existing_acl:
        raise IBMInvalidRequestError(
            "IBM ACL with name '{acl}' already configured".format(
                acl=acl["name"]))

    self.report_utils.update_reporting(task_id=task_id,
                                       resource_name=self.resource_name,
                                       resource_type=self.resource_type,
                                       stage=VALIDATION,
                                       status=SUCCESS)
    LOGGER.info("IBM ACL with name '{acl}' validated successfully".format(
        acl=acl["name"]))