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


import jingrow


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