"""
Provider implementation based on boto library for AWS-compatible clouds.
"""

import os

import boto
from boto.ec2.regioninfo import RegionInfo

from cloudbridge.providers.base import BaseCloudProvider

from .services import AWSBlockStoreService
from .services import AWSComputeService
from .services import AWSImageService
from .services import AWSSecurityService


class AWSCloudProviderV1(BaseCloudProvider):

    def __init__(self, config):
        super(AWSCloudProviderV1, self).__init__(config)
        self.cloud_type = 'aws'

        # Initialize cloud connection fields
        self.a_key = self._get_config_value(
            'access_key', os.environ.get('AWS_ACCESS_KEY', None))
        self.s_key = self._get_config_value(
            'secret_key', os.environ.get('AWS_SECRET_KEY', None))
        self.is_secure = self._get_config_value('is_secure', True)
        self.region_name = self._get_config_value('region_name', 'us-east-1')
        self.region_endpoint = self._get_config_value(
            'region_endpoint', 'ec2.us-east-1.amazonaws.com')
        self.ec2_port = self._get_config_value('ec2_port', '')
        self.ec2_conn_path = self._get_config_value('ec2_port', '/')

        # Create a connection object
        self.ec2_conn = self._connect_ec2()

        # Initialize provider services
        self._compute = AWSComputeService(self)
        self._images = AWSImageService(self)
        self._security = AWSSecurityService(self)
        self._block_store = AWSBlockStoreService(self)
        self._object_store = None  # AWSObjectStore(self)

    @property
    def compute(self):
        return self._compute

    @property
    def images(self):
        return self._images

    @property
    def security(self):
        return self._security

    @property
    def block_store(self):
        return self._block_store

    @property
    def object_store(self):
        return self._object_store

    def _connect_ec2(self):
        """
        Get a boto connection object for the given cloud.
        """
        r = RegionInfo(name=self.region_name, endpoint=self.region_endpoint)
        ec2_conn = boto.connect_ec2(
            aws_access_key_id=self.a_key,
            aws_secret_access_key=self.s_key,
            # api_version is needed for availability
            # zone support for EC2
            api_version='2012-06-01' if self.cloud_type == 'aws' else None,
            is_secure=self.is_secure,
            region=r,
            port=self.ec2_port,
            path=self.ec2_conn_path,
            validate_certs=False)
        return ec2_conn

    def _connect_s3(self):
        """
        Get a boto connection object for the given cloud.
        """
        r = RegionInfo(name=self.region_name, endpoint=self.region_endpoint)
        ec2_conn = boto.connect_ec2(
            aws_access_key_id=self.a_key,
            aws_secret_access_key=self.s_key,
            # api_version is needed for availability
            # zone support for EC2
            api_version='2012-06-01' if self.cloud_type == 'aws' else None,
            is_secure=self.is_secure,
            region=r,
            port=self.ec2_port,
            path=self.ec2_conn_path,
            validate_certs=False)
        return ec2_conn

@@ -1274,16 +1274,58 @@
 ct_ec2()
+%0A        self.s3_conn = self._connect_s3()
 %0A%0A      
@@ -2018,32 +2018,36 @@
      Get a boto 
+ec2 
 connection objec
@@ -2047,36 +2047,16 @@
 n object
- for the given cloud
 .%0A      
@@ -2673,16 +2673,19 @@
  a boto 
+S3 
 connecti
@@ -2697,28 +2697,8 @@
 ject
- for the given cloud
 .%0A  
@@ -2719,88 +2719,10 @@
     
-r = RegionInfo(name=self.region_name, endpoint=self.region_endpoint)%0A        ec2
+s3
 _con
@@ -2738,33 +2738,19 @@
 connect_
-ec2(%0A            
+s3(
 aws_acce
@@ -2775,32 +2775,54 @@
 ey,%0A            
+                      
 aws_secret_acces
@@ -2841,335 +2841,8 @@
 _key
-,%0A            # api_version is needed for availability%0A            # zone support for EC2%0A            api_version='2012-06-01' if self.cloud_type == 'aws' else None,%0A            is_secure=self.is_secure,%0A            region=r,%0A            port=self.ec2_port,%0A            path=self.ec2_conn_path,%0A            validate_certs=False
 )%0A  
@@ -2842,29 +2842,28 @@
 key)%0A        return 
-ec2
+s3
 _conn%0A

Fix how connection to S3 is established