<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Rahul Kashyap</title>
    <description>The latest articles on DEV Community by Rahul Kashyap (@grkashyap).</description>
    <link>https://dev.to/grkashyap</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F443090%2F5bfae5b6-5164-486e-91c2-d02f363f9d3d.png</url>
      <title>DEV Community: Rahul Kashyap</title>
      <link>https://dev.to/grkashyap</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/grkashyap"/>
    <language>en</language>
    <item>
      <title>Read cloud specific configuration from configuration files</title>
      <dc:creator>Rahul Kashyap</dc:creator>
      <pubDate>Thu, 10 Oct 2024 05:54:09 +0000</pubDate>
      <link>https://dev.to/grkashyap/read-cloud-specific-configuration-from-configuration-files-5db0</link>
      <guid>https://dev.to/grkashyap/read-cloud-specific-configuration-from-configuration-files-5db0</guid>
      <description>&lt;p&gt;Configuration specific to cloud provider can be separated in a config file by setting cloud provider name as a section.&lt;/p&gt;

&lt;h2&gt;
  
  
  Define a configuration file
&lt;/h2&gt;

&lt;p&gt;Create a configuration file - config.ini in the src package and define cloud provider specific configuration similar to below.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[aws]
bucket_name: test-aws-bucket

[gcp]
bucket_name: test-gcp-bucket
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Read the configuration in the code
&lt;/h2&gt;

&lt;p&gt;Read cloud provider from environment variable.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;cloud_provider = os.environ.get('CLOUD_PROVIDER')&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Declare a config parser in python and read the configuration file&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;config = configparser.ConfigParser()
config_path = os.path.join(os.path.dirname(__file__), 'config.ini')
config.read(config_path)
bucket_name = config.get(cloud_provider,'bucket_name')
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this way, we can separate cloud provider specific configuration in config files.&lt;/p&gt;

&lt;p&gt;Please feel free to comment with any suggestions/feedback.&lt;/p&gt;

</description>
      <category>aws</category>
      <category>cloudcomputing</category>
      <category>python</category>
    </item>
    <item>
      <title>Developing cloud agnostic application</title>
      <dc:creator>Rahul Kashyap</dc:creator>
      <pubDate>Thu, 10 Oct 2024 05:44:05 +0000</pubDate>
      <link>https://dev.to/grkashyap/developing-cloud-agnostic-application-part-1-4ff8</link>
      <guid>https://dev.to/grkashyap/developing-cloud-agnostic-application-part-1-4ff8</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;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. &lt;/p&gt;

&lt;p&gt;In this post, i want to share the approach which was followed.&lt;/p&gt;

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

&lt;h2&gt;
  
  
  Code
&lt;/h2&gt;

&lt;p&gt;Below code uses python&lt;/p&gt;

&lt;h3&gt;
  
  
  Interface with abstract methods
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;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_

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Create cloud provider specific implementation
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;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_
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Create a method to get the specifc cloud provider implementation class object - &lt;em&gt;Factory method&lt;/em&gt;
&lt;/h3&gt;

&lt;p&gt;This method takes a cloud provider variable which will be passed from the calling method&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;def get_object_storage(cloud_provider, bucket_name) -&amp;gt; IObjectStorage:

    if cloud_provider == 'aws':
        return AWSObjectStorageConnector(bucket_name=bucket_name)
    else:
        raise ValueError(f'Unsupported cloud provider: {cloud_provider}')
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Call the factory method to get an instance of the object
&lt;/h3&gt;

&lt;p&gt;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.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;object_storage_connector = get_object_storage(cloud_provider=provider, bucket_name=bucket_name)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Please feel free to comment with any suggestions or feedback.&lt;/p&gt;

</description>
      <category>cloudcomputing</category>
      <category>aws</category>
      <category>cloudskills</category>
      <category>python</category>
    </item>
  </channel>
</rss>
