Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Toxophilist/sprint 9/okit 104 #93

Merged
merged 3 commits into from
Aug 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
## Version 0.10.2
**Release Date**: 27th August 2020
### Bug Fixes
1. Resolve issue where deleting an artefact would cause an addition OKE cluster to be created if one exists on the canvas. In addition deleteing OKE caused it to be removed from the model but not the view.
1. Resolve issue where deleting an artefact would cause an addition OKE cluster to be created if one exists on the canvas. In addition deleting OKE caused it to be removed from the model but not the view.


## Version 0.10.1
Expand Down
23 changes: 14 additions & 9 deletions okitweb/okitImport.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#

import os
import urllib
from flask import Blueprint
from flask import request

import json
from common.okitCommon import logJson
from common.okitLogging import getLogger
Expand All @@ -22,17 +22,22 @@
# Configure logging
logger = getLogger()

bp = Blueprint('parsers', __name__, url_prefix='/okit/import', static_folder='static/okit')
bp = Blueprint('parsers', __name__, url_prefix='/okit/parse', static_folder='static/okit')

debug_mode = bool(str(os.getenv('DEBUG_MODE', 'False')).title())

@bp.route('hcljson', methods=(['POST']))
def importHclJson():
logger.debug('JSON : {0:s}'.format(str(request.json)))
if request.method == 'POST':
logJson(request.json)
@bp.route('hcljson', methods=(['GET']))
def parseHclJson():
#logger.debug('JSON : {0:s}'.format(str(request.json)))
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)
# Import HCL
parser = OkitHclJsonParser(request.json)
return json.dumps(parser.parse(), sort_keys=False, indent=2, separators=(',', ': '))
parser = OkitHclJsonParser(query_json)
response_json = parser.parse()
logJson(response_json)
return json.dumps(response_json, sort_keys=False, indent=2, separators=(',', ': '))
else:
return '404'
5 changes: 5 additions & 0 deletions okitweb/static/okit/js/okit_designer.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,11 @@ function newRegionsModel() {
function handleLoad(evt) {
hideNavMenu();
resetDesigner();
/*
** Add Load File Handling
*/
$('#files').off('change').on('change', handleFileSelect);
// Click Files Element
let fileinput = document.getElementById("files");
fileinput.click();
}
Expand Down
2 changes: 1 addition & 1 deletion okitweb/static/okit/js/okit_designer_ready.js
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ $(document).ready(function() {
/*
** Add Load File Handling
*/
document.getElementById('files').addEventListener('change', handleFileSelect, false);
//document.getElementById('files').addEventListener('change', handleFileSelect, false);

/*
** Load Empty Properties Sheet
Expand Down
59 changes: 59 additions & 0 deletions okitweb/static/okit/js/okit_import.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
** Copyright (c) 2020, Oracle and/or its affiliates.
** Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.
*/
console.info('Loaded Import Javascript');

function handleImportFromHCLJson(e) {
hideNavMenu();
/*
** Add Load File Handling
*/
$('#files').off('change').on('change', handleFileImportSelect);
// Click Files Element
let fileinput = document.getElementById("files");
fileinput.click();
}
function handleFileImportSelect(evt) {
let files = evt.target.files; // FileList object
getHclJson(files[0]);
}
function getHclJson(readFile) {
let reader = new FileReader();
reader.onload = hclLoaded;
reader.onerror = errorHandler;
reader.readAsText(readFile);
}
function hclLoaded(evt) {
// Clear Existing Region
regionOkitJson = {};
okitJsonModel = null
hideRegionTabBar();
clearRegionTabBar();
// Obtain the read file data
let fileString = evt.target.result;
let fileJson = JSON.parse(fileString);
console.info(fileJson);
$.ajax({
type: 'get',
url: 'parse/hcljson',
dataType: 'text',
contentType: 'application/json',
data: JSON.stringify(fileJson),
success: function(resp) {
let response_json = JSON.parse(resp);
okitJsonModel = new OkitJson(JSON.stringify(response_json.okit_json));
newDesignerView();
displayOkitJson();
displayDesignerView();
displayTreeView();
},
error: function(xhr, status, error) {
console.info('Status : '+ status)
console.info('Error : '+ error)
},
complete: function() {
console.info('Parsing Complete');
}
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,22 @@ class VirtualCloudNetworkView extends OkitContainerDesignerArtefactView {
getSvgDefinition() {
let definition = this.newSVGDefinition(this, VirtualCloudNetwork.getArtifactReference());
//let parent_first_child = getCompartmentFirstChildContainerOffset(this.compartment_id);
let parent_first_child = this.getParent().getChildOffset(this.getArtifactReference());
definition['svg']['x'] = parent_first_child.dx;
definition['svg']['y'] = parent_first_child.dy;
definition['svg']['width'] = this.dimensions['width'];
definition['svg']['height'] = this.dimensions['height'];
definition['rect']['stroke']['colour'] = stroke_colours.orange;
definition['rect']['stroke']['dash'] = 5;
definition['rect']['stroke']['width'] = 2;
definition['icon']['x_translation'] = icon_translate_x_start;
definition['icon']['y_translation'] = icon_translate_y_start;
definition['name']['show'] = true;
definition['label']['show'] = true;
definition['info']['show'] = true;
definition['info']['text'] = this.cidr_block;
if (this.getParent()) {
let parent_first_child = this.getParent().getChildOffset(this.getArtifactReference());
definition['svg']['x'] = parent_first_child.dx;
definition['svg']['y'] = parent_first_child.dy;
definition['svg']['width'] = this.dimensions['width'];
definition['svg']['height'] = this.dimensions['height'];
definition['rect']['stroke']['colour'] = stroke_colours.orange;
definition['rect']['stroke']['dash'] = 5;
definition['rect']['stroke']['width'] = 2;
definition['icon']['x_translation'] = icon_translate_x_start;
definition['icon']['y_translation'] = icon_translate_y_start;
definition['name']['show'] = true;
definition['label']['show'] = true;
definition['info']['show'] = true;
definition['info']['text'] = this.cidr_block;
}
return definition;
}

Expand Down
3 changes: 2 additions & 1 deletion okitweb/templates/okit/okit_designer.html
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
<script type="text/javascript" src="{{ url_for('static', filename='okit/js/okit_designer_svg.js') }}"></script>
<script type="text/javascript" src="{{ url_for('static', filename='okit/js/okit_designer_drag_drop.js') }}"></script>
<script type="text/javascript" src="{{ url_for('static', filename='okit/js/okit_designer_generation.js') }}"></script>
<script type="text/javascript" src="{{ url_for('static', filename='okit/js/okit_import.js') }}"></script>
<script type="text/javascript" src="{{ url_for('static', filename='okit/js/okit_designer_properties.js') }}"></script>
{% for model_js in artefact_model_js_files %}
<script type="text/javascript" src="{{ url_for('static', filename='okit/model/js/artefacts/' + model_js) }}"></script>
Expand Down Expand Up @@ -160,7 +161,7 @@
</span>
</div>
</li>
<li><a id="import-hcl-json-menu-item" class="parent-item" href="javascript:void(0)">Terraform Json</a></li>
<li><a id="import-hcl-json-menu-item" class="parent-item" href="javascript:void(0)" onclick="handleImportFromHCLJson()">Terraform Json</a></li>
</ul-->
</ul>
{% endblock %}
Expand Down
9 changes: 9 additions & 0 deletions visualiser/parsers/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Copyright (c) 2020, Oracle and/or its affiliates.
# Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.

# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#
__author__ = ["Andrew Hopkinson (Oracle Cloud Solutions A-Team)"]
__version__ = "1.0.0"
__module__ = "__init__.py"
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#

53 changes: 43 additions & 10 deletions visualiser/parsers/okitHclJsonParser.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,32 @@

class OkitHclJsonParser(object):
tf_map = {
"oci_core_vcn": "virtual_cloud_networks",
"oci_core_subnet": "subnets",
"oci_core_route_table": "route_tables",
"oci_core_default_security_list": "security_lists",
"oci_core_security_list": "security_lists",
"oci_core_internet_gateway": "internet_gateways",
"oci_core_network_security_group": "network_security_groups",
"oci_core_instance": "instances"
"oci_database_autonomous_database": "autonomous_databases",
"oci_core_volume": "block_storage_volumes",
"oci_identity_compartment": "compartments",
"oci_database_db_system": "database_systems",
"oci_core_drg": "dynamic_routing_gateways",
"oci_file_storage_file_system": "file_storage_systems",
"oci_core_instance": "instances",
"oci_core_internet_gateway": "internet_gateways",
"oci_load_balancer_load_balancer": "load_balancers",
"oci_core_local_peering_gateway": "local_peering_gateways",
"oci_core_nat_gateway": "nat_gateways",
"oci_core_network_security_group": "network_security_groups",
"oci_objectstorage_bucket": "object_storage_buckets",
"oci_core_route_table": "route_tables",
"oci_core_default_security_list": "security_lists",
"oci_core_security_list": "security_lists",
"oci_core_service_gateway": "service_gateways",
"oci_core_subnet": "subnets",
"oci_core_vcn": "virtual_cloud_networks"
}

def __init__(self, hcl_json=None):
self.hcl_json = hcl_json
self.okit_json = self.initialiseOkitJson()
if self.hcl_json is not None:
self.convert()
self.parse()


def initialiseOkitJson(self):
Expand Down Expand Up @@ -108,5 +119,27 @@ def parse(self, hcl_json=None):
else:
logger.warn("Unknown Resource {0!s:s}".format(resource_key))
result_json["warnings"][resource_key] = "Unknown Resource {0!s:s}".format(resource_key)
result_json["okit_json"] = self.okit_json
result_json["okit_json"] = self.standardiseIds(self.okit_json)
return result_json


def standardiseIds(self, json_data={}):
if isinstance(json_data, dict):
for key, val in json_data.items():
logger.debug('{0!s:s} : {1!s:s}'.format(key, val))
if isinstance(val, dict):
json_data[key] = self.standardiseIds(val)
elif key == 'id' or key.endswith('_id') or key.endswith('_ids'):
if isinstance(val, list):
json_data[key] = [self.replaceReferenceSyntax(id) for id in val]
elif val is not None:
json_data[key] = self.replaceReferenceSyntax(val)
elif isinstance(val, list):
json_data[key] = [self.standardiseIds(element) for element in val]
elif isinstance(json_data, list):
json_data = [self.standardiseIds(element) for element in json_data]
return json_data


def replaceReferenceSyntax(self, value):
return value.replace('$', '').replace('{', '').replace('}', '')