Open-source News

Talk to your cluster with this open source Python API wrapper

opensource.com - Tue, 04/18/2023 - 15:00
Talk to your cluster with this open source Python API wrapper rnetser1 Tue, 04/18/2023 - 03:00

Open source projects that create a wrapper around an API are becoming increasingly popular. These projects make it easier for developers to interact with APIs and use them in their applications. The openshift-python-wrapper project is a wrapper around openshift-restclient-python. What began as an internal package to help our team work with the OpenShift API became an open source project (Apache License 2.0).

This article discusses what an API wrapper is, why it's useful, and some examples from the wrapper.

Why use an API wrapper?

An API wrapper is a layer of code that sits between an application and an API. It simplifies the API access process by abstracting away some of the complexities involved in making requests and parsing responses. A wrapper can also provide additional functionality beyond what the API itself offers, such as caching or error handling.

Using an API wrapper makes the code more modular and easier to maintain. Instead of writing custom code for every API, you can use a wrapper that provides a consistent interface for interacting with APIs. It saves time, avoids code duplications, and reduces the chance of errors.

Another benefit of using an API wrapper is that it can shield your code from changes to the API. If an API changes its interface, you can update the wrapper code without modifying your application code. This can reduce the work required to maintain your application over time.

Install

The application is on PyPi, so install openshift-python-wrapper using the pip command:

$ python3 -m pip install openshift-python-wrapperPython wrapper

The OpenShift REST API provides programmatic access to many of the features of the OpenShift platform. The wrapper offers a simple and intuitive interface for interacting with the API using the openshift-restclient-python library. It standardizes how to work with cluster resources and offers unified resource CRUD (Create, Read, Update, and Delete) flows. It also provides additional capabilities, such as resource-specific functionality that otherwise needs to be implemented by users. The wrapper makes code easier to read and maintain over time.

One example of simplified usage is interacting with a container. Running a command inside a container requires using Kubernetes stream, handling errors, and more. The wrapper handles it all and provides simple and intuitive functionality.

>>> from ocp_resources.pod import Pod >>> from ocp_utilities.infra import get_client >>> client = get_client() ocp_utilities.infra INFO Trying to get client via new_client_from_config >>> pod = Pod(client=client, name="nginx-deployment-7fb96c846b-b48mv", namespace="default") >>> pod.execute("ls") ocp_resources Pod INFO Execute ls on nginx-deployment-7fb96c846b-b48mv (ip-10-0-155-108.ec2.internal) 'bin\nboot\ndev\netc\nhome\nlib\nlib64\nmedia\nmnt\nopt\nproc\nroot\nrun\nsbin\nsrv\nsys\ntmp\nusr\nvar\n'

Explore the open source cloud Free online course: Developing cloud-native applications with microservices eBook: Modernize your IT with managed cloud services Try for 60 days: Red Hat OpenShift Dedicated Free online course: Containers, Kubernetes and Red Hat OpenShift What is Kubernetes? Understanding edge computing Register for your free Red Hat account Latest articles for IT architects

Developers or testers can use this wrapper—our team wrote the code while keeping testing in mind. Using Python capabilities, context managers can provide out-of-the-box resource creation and deletion, and inheritance can be used to extend functionality for specific use cases. Pytest fixtures can utilize the code for setup and teardown, leaving no leftovers. Resources can even be saved for debugging. Resource manifests and logs can be easily collected.

Here's an example of a context manager:

@pytest.fixture(scope="module") def namespace(): admin_client = get_client() with Namespace(client=admin_client, name="test-ns",) as ns: ns.wait_for_status(status=Namespace.Status.ACTIVE, timeout=240) yield ns def test_ns(namespace): print(namespace.name)

Generators iterate over resources, as seen below:

>>> from ocp_resources.node import Node >>> from ocp_utilities.infra import get_client >>> admin_client = get_client() # This returns a generator >>> for node in Node.get(dyn_client=admin_client): print(node.name) ip-10-0-128-213.ec2.internalOpen source code for open source communities

To paraphrase a popular saying, "If you love your code, set it free." The openshift-python-wrapper project started as utility modules for OpenShift Virtualization. As more and more projects benefitted from the code, we decided to extract those utilities into a separate repository and have it open sourced. To paraphrase another common saying, "If the code does not return to you, it means it was never yours." We like saying that once that happens, it's truly open source.

More contributors and maintainers mean that the code belongs to the community. Everyone is welcome to contribute.

Combine the power of an open API and the Python programming language.

Image by:

Ron on Flickr. CC BY-NC-SA 2.0

Python Cloud Kubernetes What to read next This work is licensed under a Creative Commons Attribution-Share Alike 4.0 International License. 33 points Open Enthusiast Author Register or Login to post a comment.

Debian vs Ubuntu: What’s the Difference?

Tecmint - Tue, 04/18/2023 - 13:04
The post Debian vs Ubuntu: What’s the Difference? first appeared on Tecmint: Linux Howtos, Tutorials & Guides .

There are hundreds of Linux distributions, each unique in its own way. Some of the popular and widely used Linux distributions include Ubuntu, Linux Mint, Red Hat Enterprise Linux, Fedora, CentOS Stream, Debian, and

The post Debian vs Ubuntu: What’s the Difference? first appeared on Tecmint: Linux Howtos, Tutorials & Guides.

Intel's OpenGL & Vulkan Linux Drivers Now To Build On ARM

Phoronix - Tue, 04/18/2023 - 07:30
Intel's open-source OpenGL "Iris" and Vulkan "ANV" Linux drivers are now part of the auto-generated set of drivers set to be built for 64-bit ARM (AArch64) when compiling this code inside Mesa...

RadeonSI Change Allows For Balancing RDNA3 Video Transcoding Between Multiple Engines

Phoronix - Tue, 04/18/2023 - 04:40
A change merged today for the Mesa 23.2 graphics driver stack benefits video transcoding performance for new Radeon RX 7000 series "RDNA3" graphics cards...

Proton 8.0-1 Published With More Games Now Running On Steam Play

Phoronix - Tue, 04/18/2023 - 03:19
Valve and CodeWeavers have made available Proton 8.0-1 as their newest version of this downstream of Wine that powers Steam Play for enjoying Windows games on Linux...

Pages