# #######
# Copyright (c) 2019 Cloudify Platform Ltd. All rights reserved
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#        http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import os
from random import random

from integration_tests.tests.test_cases import PluginsTest

PLUGIN_NAME = 'cloudify-gcp-plugin'
DEVELOPMENT_ROOT = os.path.abspath(
    os.path.join(os.path.dirname(
        os.path.realpath(__file__)), '../..'))

test_id = '{0}{1}'.format(
    os.getenv('CIRCLE_JOB', 'cfy'),
    os.getenv('CIRCLE_BUILD_NUM', str(random())[-4:-1])
)


class GCPPluginTestCase(PluginsTest):

    base_path = os.path.dirname(os.path.realpath(__file__))

    @property
    def plugin_root_directory(self):
        return os.path.abspath(os.path.join(self.base_path, '..'))

    @property
    def inputs(self):
        return {
            'region': os.getenv('gcp_region'),
            'network_name': '{0}network'.format(test_id),
            'subnet_name': '{0}subnet'.format(test_id)
        }

    def create_secrets(self):
        secrets = {
            'gcp_region': os.getenv('gcp_region'),
            'gcp_zone': os.getenv('gcp_zone'),
            'gcp_private_key': os.getenv('gcp_private_key'),
            'gcp_private_key_id': os.getenv('gcp_private_key_id'),
            'gcp_project_id': os.getenv('gcp_project_id'),
            'gcp_client_id': os.getenv('gcp_client_id'),
            'gcp_client_email': os.getenv('gcp_client_email'),
            'gcp_client_x509_cert_url': os.getenv('gcp_client_x509_cert_url'),
            'agent_key_private': os.getenv('agent_key_private'),
            'agent_key_public': os.getenv('agent_key_public'),
        }
        self._create_secrets(secrets)

    def upload_plugins(self):
        self.upload_mock_plugin(
            PLUGIN_NAME, self.plugin_root_directory)
        self.upload_mock_plugin(
            'cloudify-utilities-plugin',
            os.path.join(DEVELOPMENT_ROOT, 'cloudify-utilities-plugin'))
        self.upload_mock_plugin(
            'cloudify-ansible-plugin',
            os.path.join(DEVELOPMENT_ROOT, 'cloudify-ansible-plugin'))

    def test_blueprints(self):
        self.upload_plugins()
        self.create_secrets()
        self.check_hello_world_blueprint('gcp', self.inputs, 400)
        self.check_db_lb_app_blueprint(
            'gcp', 800,
            {
                'resource_prefix': 'dblbapp',
                'resource_suffix': test_id
            }
        )

@@ -738,19 +738,21 @@
 loudify-
-gcp
+azure
 -plugin'
@@ -869,17 +869,120 @@
 ./..'))%0A
+TEST_KEY_PATH = '/tmp/foo.rsa'%0ATEST_PUB_PATH = '/tmp/foo.rsa.pub'%0AGCP_KEY_PATH = '/tmp/gcp_private_key'
 %0A
-
 test_id 
@@ -1589,16 +1589,285 @@
 ets = %7B%0A
+            'agent_key_private': os.getenv('agent_key_private',%0A                                           open(TEST_KEY_PATH).read()),%0A            'agent_key_public': os.getenv('agent_key_public',%0A                                          open(TEST_PUB_PATH).read()),%0A
         
@@ -1992,34 +1992,31 @@
 ': o
-s.getenv('gcp_private_key'
+pen(GCP_KEY_PATH).read(
 ),%0A 
@@ -2256,24 +2256,24 @@
 nt_email'),%0A
+
             
@@ -2343,136 +2343,8 @@
 '),%0A
-            'agent_key_private': os.getenv('agent_key_private'),%0A            'agent_key_public': os.getenv('agent_key_public'),%0A
     

update test