# -*- coding: utf-8 -*-
# Copyright (c) 2021, JINGROW
# For license information, please see license.txt


import jingrow


def execute():
	jingrow.reload_pg("jcloude", "doctype", "root_domain")
	jcloude_settings = jingrow.get_pg("Press Settings", "Press Settings")
	if (
		jcloude_settings.domain
		and jcloude_settings.aws_secret_access_key
		and not jingrow.db.exists("Root Domain", jcloude_settings.domain)
	):
		default_cluster = jingrow.db.get_value("Cluster", {"default": True})
		jingrow.get_pg(
			{
				"doctype": "Root Domain",
				"name": jcloude_settings.domain,
				"default_cluster": default_cluster,
				"dns_provider": jcloude_settings.dns_provider,
				"aws_access_key_id": jcloude_settings.aws_access_key_id,
				"aws_secret_access_key": jcloude_settings.get_password("aws_secret_access_key"),
			}
		).insert()
