Home > Backend Development > Python Tutorial > Developing cloud agnostic application

Developing cloud agnostic application

Patricia Arquette
Release: 2024-10-10 14:12:30
Original
253 people have browsed it

Developing cloud agnostic application

Introduction

Recently, i started working on a personal project where i want to build an application which is cloud agnostic - i.e. it can be deployed on any cloud provider with minimal/no code changes. Primary requirement is to separate business logic with cloud provider specific logic.

In this post, i want to share the approach which was followed.

  • Create an interface which has abstract methods for performing operations in the cloud.
  • Create cloud provider specific classes which are subclasses of this interface and provide implemenation for the abstract methods.
  • Create a separate class/method which will return the cloud provider implementation based on some condition. Factory Pattern
  • The calling class will use the object from the above class/method and call its methods.

Code

Below code uses python

Interface with abstract methods

from abc import ABC, abstractmethod

class IObjectStorage(ABC):

    @abstractmethod
    def upload_object_to_bucket(self, file_name, file_content):
        _raise an error that method is not implemented_

Copy after login

Create cloud provider specific implementation

class AWSObjectStorageConnector(IObjectStorage):

    def __init__(self, bucket_name):
       _Initialize a s3 client using boto3 and initialize a variable using bucket name_

    def upload_object_to_bucket(self, file_name, file_content):
        _Implement the logic to upload the file to AWS S3 bucket_
Copy after login

Create a method to get the specifc cloud provider implementation class object - Factory method

This method takes a cloud provider variable which will be passed from the calling method

def get_object_storage(cloud_provider, bucket_name) -> IObjectStorage:

    if cloud_provider == 'aws':
        return AWSObjectStorageConnector(bucket_name=bucket_name)
    else:
        raise ValueError(f'Unsupported cloud provider: {cloud_provider}')
Copy after login

Call the factory method to get an instance of the object

cloud_provider variable will be read from an environment variable passed as input. This ensures that the same logic works fine with different cloud providers.

object_storage_connector = get_object_storage(cloud_provider=provider, bucket_name=bucket_name)
Copy after login

Please feel free to comment with any suggestions or feedback.

The above is the detailed content of Developing cloud agnostic application. For more information, please follow other related articles on the PHP Chinese website!

source:dev.to
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template