"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > Developing cloud agnostic application

Developing cloud agnostic application

Published on 2024-11-06
Browse:307

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_

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_

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}')

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)

Please feel free to comment with any suggestions or feedback.

Release Statement This article is reproduced at: https://dev.to/grkashyap/developing-cloud-agnostic-application-part-1-4ff8?1 If there is any infringement, please contact [email protected] to delete it
Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3