Example #1
0
    def delete(self, yaml_file=None, wait=False):
        """
        Delete resource

        Args:
            yaml_file (str): Path to yaml file to delete from yaml.
            wait (bool): True to wait for pod to be deleted.

        Returns:
            True if delete succeeded, False otherwise.
        """
        if yaml_file:
            with open(yaml_file, 'r') as stream:
                data = yaml.full_load(stream)

            self._extract_data_from_yaml(yaml_data=data)
            res = utils.run_oc_command(command=f'delete -f {yaml_file}',
                                       namespace=self.namespace)[0]
            if wait and res:
                return self.wait_until_gone()
            return res

        resource_list = self.client.resources.get(api_version=self.api_version,
                                                  kind=self.kind)
        try:
            res = resource_list.delete(name=self.name,
                                       namespace=self.namespace)
            if wait and res:
                return self.wait_until_gone()
            return res
        except NotFoundError:
            return False
Example #2
0
    def work_on(self):
        """
        Switch to name space

        Returns:
            bool: True f switched , False otherwise
        """
        return utils.run_oc_command(command=f"project {self.name}")[0]
Example #3
0
    def run_command(self, command, container):
        """
        Run command on pod.

        Args:
            command (str): Command to run.
            container (str): Container name if pod has more then one.

        Returns:
            tuple: True, out if command succeeded, False, err otherwise.
        """
        cmd = f"exec -i {self.name}"
        if self.namespace:
            cmd += f" -n {self.namespace}"

        if container:
            cmd += f" -c {container}"

        cmd += f" -- {command}"
        return utils.run_oc_command(command=cmd, namespace=self.namespace)
Example #4
0
    def create(self, yaml_file=None, resource_dict=None, wait=False):
        """
        Create resource from given yaml file or from dict

        Args:
            yaml_file (str): Path to yaml file.
            resource_dict (dict): Dict to create resource from.
            wait (bool) : True to wait for resource status.

        Returns:
            bool: True if create succeeded, False otherwise.
        """
        if yaml_file:
            with open(yaml_file, 'r') as stream:
                data = yaml.full_load(stream)

            self._extract_data_from_yaml(yaml_data=data)
            res = utils.run_oc_command(command=f'create -f {yaml_file}',
                                       namespace=self.namespace)[0]
            if wait and res:
                return self.wait()
            return res

        if not resource_dict:
            resource_dict = {
                'apiVersion': self.api_version,
                'kind': self.kind,
                'metadata': {
                    'name': self.namespace
                }
            }

        resource_list = self.client.resources.get(api_version=self.api_version,
                                                  kind=self.kind)
        res = resource_list.create(body=resource_dict,
                                   namespace=self.namespace)
        if wait and res:
            return self.wait()
        return res