def ociResourceManger(): if request.method == 'GET': query_string = request.query_string parsed_query_string = urllib.parse.unquote(query_string.decode()) query_json = json.loads(parsed_query_string) logJson(query_json) config_profile = query_json.get('location', {}).get('config_profile', 'DEFAULT') compartment_id = query_json.get('location', {}).get('compartment_id', None) region = query_json.get('location', {}).get('region', None) try: config = {'region': region} oci_resourcemanager = OCIResourceManagers( config=config, profile=config_profile, compartment_id=compartment_id) stacks = oci_resourcemanager.list() return json.dumps(stacks, sort_keys=False, indent=2, separators=(',', ': ')) except Exception as e: logger.exception(e) return str(e), 500 elif request.method == 'POST': logger.debug('JSON : {0:s}'.format(str(request.json))) config_profile = request.json.get('location', {}).get('config_profile', 'DEFAULT') compartment_id = request.json.get('location', {}).get('compartment_id', None) region = request.json.get('location', {}).get('region', None) plan_or_apply = request.json.get('location', {}).get('plan_or_apply', 'PLAN') create_or_update = request.json.get('location', {}).get('create_or_update', 'CREATE') stack_id = request.json.get('location', {}).get('stack_id', '') stack_name = request.json.get('location', {}).get( 'stack_name', 'okit-stack-{0!s:s}'.format(time.strftime('%Y%m%d%H%M%S'))) logger.info('Using Profile : {0!s:s}'.format(config_profile)) try: config = {'region': region} destination_dir = tempfile.mkdtemp() logger.debug(">>>>>>>>>>>>> {0!s:s}".format(destination_dir)) stack = {} stack['display_name'] = stack_name oci_compartments = OCICompartments(config=config, profile=config_profile) # Generate Resource Manager Terraform zip generator = OCIResourceManagerGenerator( template_root, destination_dir, request.json, tenancy_ocid=oci_compartments.getTenancy(), region=region, compartment_ocid=compartment_id) generator.generate() generator.writeFiles() zipname = generator.createZipArchive( os.path.join(destination_dir, 'resource-manager'), "/tmp/okit-resource-manager") logger.info('Zipfile : {0:s}'.format(str(zipname))) # Upload to Resource manager stack['compartment_id'] = compartment_id stack['zipfile'] = zipname stack['variables'] = generator.getVariables() resource_manager = OCIResourceManagers( config=config, profile=config_profile, compartment_id=compartment_id) if create_or_update == 'UPDATE': stack['id'] = stack_id stack_json = resource_manager.updateStack(stack) else: stack_json = resource_manager.createStack(stack) resource_manager.createJob(stack_json, plan_or_apply) return_code = 200 resource_manager.list() shutil.rmtree(destination_dir) return stack['display_name'], return_code except Exception as e: logger.exception(e) return str(e), 500 return
def export(destination): logger.debug('Destination : {0:s} - {1:s}'.format(str(destination), str(request.method))) logger.debug('JSON : {0:s}'.format(str(request.json))) config_profile = request.json.get('config_profile', 'DEFAULT') logger.info('Using Profile : {0!s:s}'.format(config_profile)) if request.method == 'POST': try: config = {} destination_dir = tempfile.mkdtemp() logger.debug(">>>>>>>>>>>>> {0!s:s}".format(destination_dir)) stack = {} stack['display_name'] = 'okit-stack-{0!s:s}'.format( time.strftime('%Y%m%d%H%M%S')) if destination == 'resourcemanager': # Get Compartment Information export_compartment_index = request.json.get( 'open_compartment_index', 0) export_compartment_name = request.json['compartments'][ export_compartment_index]['name'] logger.info( "Compartment Name {0!s:s}".format(export_compartment_name)) oci_compartments = OCICompartments(config=config, profile=config_profile) compartments = oci_compartments.listTenancy( filter={'name': export_compartment_name}) logger.debug("Compartments {0!s:s}".format(compartments)) # If we find a compartment if len(compartments) > 0: # Generate Resource Manager Terraform zip generator = OCIResourceManagerGenerator( template_root, destination_dir, request.json, tenancy_ocid=oci_compartments.config['tenancy'], region=oci_compartments.config['region'], compartment_ocid=compartments[0]['id']) generator.generate() generator.writeFiles() zipname = generator.createZipArchive( os.path.join(destination_dir, 'resource-manager'), "/tmp/okit-resource-manager") logger.info('Zipfile : {0:s}'.format(str(zipname))) # Upload to Resource manager stack['compartment_id'] = compartments[0]['id'] stack['zipfile'] = zipname stack['variables'] = generator.getVariables() resource_manager = OCIResourceManagers( config=config, profile=config_profile, compartment_id=compartments[0]['id']) stack_json = resource_manager.createStack(stack) resource_manager.createJob(stack_json) return_code = 200 else: logger.warn('Unknown Compartment {0!s:s}'.format( export_compartment_name)) return_code = 400 shutil.rmtree(destination_dir) return stack['display_name'], return_code except Exception as e: logger.exception(e) return str(e), 500 return