/*
Copyright (c) 2019-2022 Digital Energy Cloud Solutions LLC. All Rights Reserved.
Authors:
Petr Krutov, <petr.krutov@digitalenergy.online>
Stanislav Solovev, <spsolovev@digitalenergy.online>
Kasim Baybikov, <kmbaybikov@basistech.ru>
Tim Tkachev, <tvtkachev@basistech.ru>

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.
*/

/*
Terraform DECORT provider - manage resources provided by DECORT (Digital Energy Cloud
Orchestration Technology) with Terraform by Hashicorp.

Source code: https://repository.basistech.ru/BASIS/terraform-provider-decort

Please see README.md to learn where to place source code so that it
builds seamlessly.

Documentation: https://repository.basistech.ru/BASIS/terraform-provider-decort/wiki
*/

package k8s

import (
	"context"
	"fmt"
	"strconv"
	"strings"
	"time"

	"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
	"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
	"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
	log "github.com/sirupsen/logrus"
	"repository.basistech.ru/BASIS/decort-golang-sdk/pkg/cloudapi/compute"
	"repository.basistech.ru/BASIS/decort-golang-sdk/pkg/cloudapi/k8s"
	"repository.basistech.ru/BASIS/decort-golang-sdk/pkg/cloudapi/lb"
	"repository.basistech.ru/BASIS/decort-golang-sdk/pkg/cloudapi/tasks"
	"repository.basistech.ru/BASIS/terraform-provider-decort/internal/constants"
	"repository.basistech.ru/BASIS/terraform-provider-decort/internal/controller"
	"repository.basistech.ru/BASIS/terraform-provider-decort/internal/dc"
	"repository.basistech.ru/BASIS/terraform-provider-decort/internal/status"
	"repository.basistech.ru/BASIS/terraform-provider-decort/internal/validators"
)

func resourceK8sCPCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
	log.Debugf("resourceK8sControlPlaneCreate: called with name %s, rg %d", d.Get("name").(string), d.Get("rg_id").(int))

	haveRGID, err := existRGID(ctx, d, m)
	if err != nil {
		return diag.FromErr(err)
	}

	if !haveRGID {
		return diag.Errorf("resourceK8sCreate: can't create k8s cluster because RGID %d is not allowed or does not exist", d.Get("rg_id").(int))
	}

	haveK8sciID, err := existK8sCIID(ctx, d, m)
	if err != nil {
		return diag.FromErr(err)
	}

	if !haveK8sciID {
		return diag.Errorf("resourceK8sCreate: can't create k8s cluster because K8sCIID %d is not allowed or does not exist", d.Get("k8sci_id").(int))
	}

	if _, ok := d.GetOk("extnet_id"); ok {
		haveExtNetID, err := existExtNetID(ctx, d, m)
		if err != nil {
			return diag.FromErr(err)
		}

		if !haveExtNetID {
			return diag.Errorf("resourceK8sCreate: can't create k8s cluster because ExtNetID %d is not allowed or does not exist", d.Get("extnet_id").(int))
		}
	}

	c := m.(*controller.ControllerCfg)
	createReq := k8s.CreateRequest{}

	createReq.Name = d.Get("name").(string)
	createReq.RGID = uint64(d.Get("rg_id").(int))
	createReq.K8SCIID = uint64(d.Get("k8sci_id").(int))
	createReq.WorkerGroupName = "temp"
	createReq.NetworkPlugin = d.Get("network_plugin").(string)
	createReq.StoragePolicyID = uint64(d.Get("storage_policy_id").(int))

	if zoneID, ok := d.GetOk("zone_id"); ok {
		createReq.ZoneID = uint64(zoneID.(int))
	}

	if num, ok := d.GetOk("num"); ok {
		createReq.MasterNum = uint(num.(int))
	} else {
		createReq.MasterNum = 1
	}

	if cpu, ok := d.GetOk("cpu"); ok {
		createReq.MasterCPU = uint(cpu.(int))
	} else {
		createReq.MasterCPU = 2
	}

	if ram, ok := d.GetOk("ram"); ok {
		createReq.MasterRAM = uint64(ram.(int))
	} else {
		createReq.MasterRAM = 2048
	}

	if disk, ok := d.GetOk("disk"); ok {
		createReq.MasterDisk = uint(disk.(int))
	} else {
		createReq.MasterDisk = 0
	}

	if sepId, ok := d.GetOk("sep_id"); ok {
		createReq.MasterSEPID = uint64(sepId.(int))
	}

	if chipset, ok := d.GetOk("chipset"); ok {
		createReq.Chipset = chipset.(string)
	}

	if sepPool, ok := d.GetOk("sep_pool"); ok {
		createReq.MasterSEPPool = sepPool.(string)
	}

	createReq.WithLB = d.Get("with_lb").(bool)

	///4.4.0
	createReq.HighlyAvailable = d.Get("ha_mode").(bool)

	if additionalSans, ok := d.GetOk("additional_sans"); ok {
		addSans := additionalSans.([]interface{})
		resSans := make([]string, 0)
		for _, san := range addSans {
			resSans = append(resSans, san.(string))
		}

		createReq.AdditionalSANs = resSans
	}

	if clusterConfig, ok := d.GetOk("cluster_config"); ok {
		createReq.ClusterConfiguration = clusterConfig.(string)
	}

	if kubeletConfig, ok := d.GetOk("kubelet_config"); ok {
		createReq.KubeletConfiguration = kubeletConfig.(string)
	}

	if kubeProxyConfig, ok := d.GetOk("kube_proxy_config"); ok {
		createReq.KubeProxyConfiguration = kubeProxyConfig.(string)
	}

	if joinConfig, ok := d.GetOk("join_config"); ok {
		createReq.JoinConfiguration = joinConfig.(string)
	}

	// if cloudInit, ok := d.GetOk("cloud_init"); ok {
	// 	createReq.UserData = cloudInit.(string)
	// }

	if initConfig, ok := d.GetOk("init_config"); ok {
		createReq.InitConfiguration = initConfig.(string)
	}

	if lbSysctlParams, ok := d.GetOk("lb_sysctl_params"); ok {
		syscrlSliceMaps := lbSysctlParams.([]interface{})
		res := make([]map[string]interface{}, 0, len(syscrlSliceMaps))
		for _, syscrlMap := range syscrlSliceMaps {
			tempMap := make(map[string]interface{})
			for k, v := range syscrlMap.(map[string]interface{}) {
				if intVal, err := strconv.Atoi(v.(string)); err == nil {
					tempMap[k] = intVal
					continue
				}
				tempMap[k] = v.(string)
			}
			res = append(res, tempMap)
		}
		createReq.LbSysctlParams = res
	}

	if oidcCertificate, ok := d.GetOk("oidc_cert"); ok {
		createReq.OidcCertificate = oidcCertificate.(string)
		log.Debug(createReq.OidcCertificate)
	}

	///

	createReq.ExtNetOnly = d.Get("extnet_only").(bool)

	if extNet, ok := d.GetOk("extnet_id"); ok {
		createReq.ExtNetID = uint64(extNet.(int))
	} else {
		createReq.ExtNetID = 0
	}

	if vins, ok := d.GetOk("vins_id"); ok {
		createReq.VinsId = uint64(vins.(int))
	} else {
		createReq.VinsId = 0
	}

	if desc, ok := d.GetOk("desc"); ok {
		createReq.Description = desc.(string)
	}

	resp, err := c.CloudAPI().K8S().Create(ctx, createReq)
	if err != nil {
		d.SetId("")
		return diag.FromErr(err)
	}

	taskReq := tasks.GetRequest{
		AuditID: strings.Trim(resp, `"`),
	}

	for {
		task, err := c.CloudAPI().Tasks().Get(ctx, taskReq)
		if err != nil {
			return diag.FromErr(err)
		}

		log.Debugf("resourceK8sControlPlaneCreate: instance creating - %s", task.Stage)

		if task.Completed {
			if task.Error != "" {
				return diag.FromErr(fmt.Errorf("cannot create k8s instance: %v", task.Error))
			}

			id, err := task.Result.ID()
			if err != nil {
				return diag.FromErr(err)
			}
			d.SetId(strconv.Itoa(id))
			break
		}

		time.Sleep(time.Second * 20)
	}

	cluster, err := utilityK8sCheckPresence(ctx, d, m)
	if err != nil {
		return diag.FromErr(err)
	}

	delWGReq := k8s.WorkersGroupDeleteRequest{
		K8SID:          cluster.ID,
		WorkersGroupID: cluster.K8SGroups.Workers[0].ID,
	}

	_, err = c.CloudAPI().K8S().WorkersGroupDelete(ctx, delWGReq)
	if err != nil {
		return diag.FromErr(err)
	}

	return resourceK8sCPRead(ctx, d, m)
}

func resourceK8sCPRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
	cluster, err := utilityK8sCheckPresence(ctx, d, m)
	if err != nil {
		d.SetId("")
		return diag.FromErr(err)
	}

	c := m.(*controller.ControllerCfg)

	hasChanged := false

	switch cluster.Status {
	case status.Modeled:
		return diag.Errorf("The k8s cluster is in status: %s, please, contact support for more information", cluster.Status)
	case status.Creating:
	case status.Created:
	case status.Deleting:
	case status.Deleted:
		// id, _ := strconv.ParseUint(d.Id(), 10, 64)
		// restoreReq := k8s.RestoreRequest{
		// 	K8SID: id,
		// }

		// _, err := c.CloudAPI().K8S().Restore(ctx, restoreReq)
		// if err != nil {
		// 	return diag.FromErr(err)
		// }

		// enableReq := k8s.DisableEnableRequest{
		// 	K8SID: id,
		// }

		// _, err = c.CloudAPI().K8S().Enable(ctx, enableReq)
		// if err != nil {
		// 	return diag.FromErr(err)
		// }

		// hasChanged = true
	case status.Destroying:
		return diag.Errorf("The k8s cluster is in progress with status: %s", cluster.Status)
	case status.Destroyed:
		d.SetId("")
		return diag.Errorf("The resource cannot be updated because it has been destroyed")
		// return resourceK8sCreate(ctx, d, m)
	case status.Enabling:
	case status.Enabled:
	case status.Disabling:
	case status.Disabled:
		log.Debugf("The k8s cluster is in status: %s, troubles may occur with update. Please, enable compute first.", cluster.Status)
	case status.Restoring:
	}

	if hasChanged {
		cluster, err = utilityK8sCheckPresence(ctx, d, m)
		if cluster == nil {
			d.SetId("")
			if err != nil {
				return diag.FromErr(err)
			}
			return nil
		}
	}

	if d.Get("start").(bool) {
		if cluster.TechStatus == "STOPPED" {
			req := k8s.StartRequest{
				K8SID: cluster.ID,
			}
			_, err := c.CloudAPI().K8S().Start(ctx, req)
			if err != nil {
				return diag.FromErr(err)
			}
		}
	}

	k8sList, err := utilityK8sListForResourceCheckPresence(ctx, d, m)
	if err != nil {
		d.SetId("")
		return diag.FromErr(err)
	}

	curK8s := k8s.ItemK8SCluster{}
	for _, k8sCluster := range k8sList.Data {
		if k8sCluster.ID == cluster.ID {
			curK8s = k8sCluster
		}
	}
	if curK8s.ID == 0 {
		return diag.Errorf("Cluster with id %d not found", cluster.ID)
	}

	d.Set("vins_id", curK8s.VINSID)

	masterComputeList := make([]compute.RecordCompute, 0, len(cluster.K8SGroups.Masters.DetailedInfo))
	for _, masterNode := range cluster.K8SGroups.Masters.DetailedInfo {
		compute, err := utilityComputeCheckPresence(ctx, d, m, masterNode.ID)
		if err != nil {
			return diag.FromErr(err)
		}
		masterComputeList = append(masterComputeList, *compute)
	}

	var warnings dc.Warnings

	if _, ok := d.GetOk("k8s_id"); !ok {
		for _, worker := range cluster.K8SGroups.Workers {
			err := fmt.Errorf("found worker-group with ID %d. Make sure to import it to decort_k8s_wg resource if you wish to manage it", worker.ID)
			warnings.Add(err)
		}
	}

	flattenResourceK8sCP(d, *cluster, masterComputeList)

	lbGetReq := lb.GetRequest{
		LBID: cluster.LBID,
	}

	if d.Get("with_lb").(bool) || cluster.LBID != 0 {
		lb, err := c.CloudAPI().LB().Get(ctx, lbGetReq)
		if err != nil {
			return diag.FromErr(err)
		}

		d.Set("extnet_id", lb.ExtNetID)
		d.Set("lb_ip", lb.PrimaryNode.FrontendIP)
	}

	kubeconfigReq := k8s.GetConfigRequest{
		K8SID: cluster.ID,
	}

	kubeconfig, err := c.CloudAPI().K8S().GetConfig(ctx, kubeconfigReq)
	if err != nil {
		log.Warnf("could not get kubeconfig: %v", err)
	}

	d.Set("kubeconfig", kubeconfig)

	return warnings.Get()
}

func resourceK8sCPUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
	log.Debugf("resourceK8sControlPlaneUpdate: called with id %s, rg %d", d.Id(), d.Get("rg_id").(int))

	c := m.(*controller.ControllerCfg)

	haveRGID, err := existRGID(ctx, d, m)
	if err != nil {
		return diag.FromErr(err)
	}

	if !haveRGID {
		return diag.Errorf("resourceK8sUpdate: can't update k8s cluster because RGID %d is not allowed or does not exist", d.Get("rg_id").(int))
	}

	haveK8sciID, err := existK8sCIID(ctx, d, m)
	if err != nil {
		return diag.FromErr(err)
	}

	if !haveK8sciID {
		return diag.Errorf("resourceK8sUpdate: can't update k8s cluster because K8sCIID %d is not allowed or does not exist", d.Get("k8sci_id").(int))
	}

	cluster, err := utilityK8sCheckPresence(ctx, d, m)
	if err != nil {
		return diag.FromErr(err)
	}

	hasChanged := false

	switch cluster.Status {
	case status.Modeled:
		return diag.Errorf("The k8s cluster is in status: %s, please, contact support for more information", cluster.Status)
	case status.Creating:
	case status.Created:
	case status.Deleting:
	case status.Deleted:
		id, _ := strconv.ParseUint(d.Id(), 10, 64)
		restoreReq := k8s.RestoreRequest{
			K8SID: id,
		}

		_, err := c.CloudAPI().K8S().Restore(ctx, restoreReq)
		if err != nil {
			return diag.FromErr(err)
		}

		enableReq := k8s.DisableEnableRequest{
			K8SID: id,
		}

		_, err = c.CloudAPI().K8S().Enable(ctx, enableReq)
		if err != nil {
			return diag.FromErr(err)
		}

		hasChanged = true
	case status.Destroying:
		return diag.Errorf("The k8s cluster is in progress with status: %s", cluster.Status)
	case status.Destroyed:
		d.SetId("")
		return diag.Errorf("The resource cannot be updated because it has been destroyed")
		// return resourceK8sCreate(ctx, d, m)
	case status.Enabling:
	case status.Enabled:
	case status.Disabling:
	case status.Disabled:
		log.Debugf("The k8s cluster is in status: %s, troubles may occur with update. Please, enable compute first.", cluster.Status)
	case status.Restoring:
	}

	if hasChanged {
		cluster, err = utilityK8sCheckPresence(ctx, d, m)
		if cluster == nil {
			d.SetId("")
			if err != nil {
				return diag.FromErr(err)
			}
			return nil
		}
	}

	updateReq := k8s.UpdateRequest{K8SID: cluster.ID}
	doBasicUpdate := false

	if d.HasChange("name") {
		updateReq.Name = d.Get("name").(string)
		doBasicUpdate = true
	}

	if d.HasChange("desc") {
		updateReq.Description = d.Get("desc").(string)
		doBasicUpdate = true
	}

	if doBasicUpdate {
		_, err := c.CloudAPI().K8S().Update(ctx, updateReq)
		if err != nil {
			return diag.FromErr(err)
		}
	}

	if d.HasChange("start") {
		if d.Get("start").(bool) {
			if cluster.TechStatus == "STOPPED" {
				req := k8s.StartRequest{
					K8SID: cluster.ID,
				}
				_, err := c.CloudAPI().K8S().Start(ctx, req)
				if err != nil {
					return diag.FromErr(err)
				}
			}
		} else {
			if cluster.TechStatus == "STARTED" {
				req := k8s.StopRequest{
					K8SID: cluster.ID,
				}
				_, err := c.CloudAPI().K8S().Stop(ctx, req)
				if err != nil {
					return diag.FromErr(err)
				}
			}
		}
	}

	if d.HasChange("zone_id") {
		id, _ := strconv.ParseUint(d.Id(), 10, 64)
		zoneID := uint64(d.Get("zone_id").(int))

		start := d.Get("start").(bool)

		if start {
			stopReq := k8s.StopRequest{
				K8SID: id,
			}

			_, err := c.CloudAPI().K8S().Stop(ctx, stopReq)
			if err != nil {
				return diag.FromErr(err)
			}
		}

		req := k8s.MigrateToZoneRequest{
			K8SID:  id,
			ZoneID: zoneID,
		}

		_, err := c.CloudAPI().K8S().MigrateToZone(ctx, req)
		if err != nil {
			return diag.FromErr(err)
		}

		if start {
			startReq := k8s.StartRequest{
				K8SID: id,
			}

			_, err := c.CloudAPI().K8S().Start(ctx, startReq)
			if err != nil {
				return diag.FromErr(err)
			}
		}
	}

	if d.HasChange("num") {
		oldVal, newVal := d.GetChange("num")

		if oldVal.(int) > newVal.(int) {
			ids := make([]uint64, 0)
			for i := oldVal.(int) - 1; i >= newVal.(int); i-- {
				id := cluster.K8SGroups.Masters.DetailedInfo[i].ID
				ids = append(ids, id)
			}

			req := k8s.DeleteMasterFromGroupRequest{
				K8SID:         cluster.ID,
				MasterGroupID: cluster.K8SGroups.Masters.ID,
				MasterIDs:     ids,
			}

			_, err := c.CloudAPI().K8S().DeleteMasterFromGroup(ctx, req)
			if err != nil {
				return diag.FromErr(err)
			}
		}
	}

	if d.HasChange("lb_sysctl_params") && d.Get("with_lb").(bool) {
		lbSysctlParams := d.Get("lb_sysctl_params").([]interface{})
		res := make([]map[string]interface{}, 0, len(lbSysctlParams))
		for _, syscrlMap := range lbSysctlParams {
			tempMap := make(map[string]interface{})
			for k, v := range syscrlMap.(map[string]interface{}) {
				if intVal, err := strconv.Atoi(v.(string)); err == nil {
					tempMap[k] = intVal
					continue
				}
				tempMap[k] = v.(string)
			}
			res = append(res, tempMap)
		}
		if len(res) > 0 {
			req := lb.UpdateSysctParamsRequest{
				LBID:         cluster.LBID,
				SysctlParams: res,
			}
			_, err := c.CloudAPI().LB().UpdateSysctlParams(ctx, req)
			if err != nil {
				return diag.FromErr(err)
			}
		}
	}

	return resourceK8sCPRead(ctx, d, m)
}

func resourceK8sCPDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {

	log.Debugf("resourceK8sControlPlaneDelete: called with id %s, rg %d", d.Id(), d.Get("rg_id").(int))

	cluster, err := utilityK8sCheckPresence(ctx, d, m)
	if err != nil {
		d.SetId("")
		return diag.FromErr(err)
	}

	req := k8s.DeleteRequest{K8SID: cluster.ID}

	if val, ok := d.GetOk("permanently"); ok {
		req.Permanently = val.(bool)
	}

	c := m.(*controller.ControllerCfg)

	_, err = c.CloudAPI().K8S().Delete(ctx, req)
	if err != nil {
		return diag.FromErr(err)
	}

	d.SetId("")

	return nil
}

func resourceK8sCPSchemaMake() map[string]*schema.Schema {
	return map[string]*schema.Schema{
		"name": {
			Type:        schema.TypeString,
			Required:    true,
			Description: "Name of the cluster.",
		},
		"rg_id": {
			Type:     schema.TypeInt,
			Required: true,
			//ForceNew:    true,
			Description: "Resource group ID that this instance belongs to.",
		},
		"k8sci_id": {
			Type:     schema.TypeInt,
			Required: true,
			//ForceNew:    true,
			Description: "ID of the k8s catalog item to base this instance on.",
		},
		"network_plugin": {
			Type:         schema.TypeString,
			Required:     true,
			Description:  "Network plugin to be used",
			ValidateFunc: validation.StringInSlice([]string{"flannel", "weavenet", "calico"}, true),
		},
		"storage_policy_id": {
			Type:        schema.TypeInt,
			Required:    true,
			Description: "ID of the storage policy",
		},
		"zone_id": {
			Type:        schema.TypeInt,
			Optional:    true,
			Computed:    true,
			Description: "ID of the zone to put the cluster into.",
		},
		"num": {
			Type:         schema.TypeInt,
			Optional:     true,
			Computed:     true,
			ValidateFunc: validation.IntInSlice([]int{1, 3, 5}),
			Description:  "Number of VMs to create. Can be either 1, 3 or 5",
		},
		"cpu": {
			Type:        schema.TypeInt,
			Optional:    true,
			Computed:    true,
			Description: "Node CPU count.",
		},
		"ram": {
			Type:     schema.TypeInt,
			Optional: true,
			Computed: true,
			ValidateFunc: validation.All(
				validation.IntAtLeast(constants.MIN_RAM_PER_COMPUTE),
				validators.DivisibleBy(constants.RAM_DIVISIBILITY),
			),
			Description: "Node RAM in MB.",
		},
	"chipset": {
		Type:         schema.TypeString,
		Optional:     true,
		ValidateFunc: validation.StringInSlice([]string{"Q35", "i440fx"}, false),
		Default:      "Q35",
		Description:  "Type of the emulated system. Possible values: i440fx, Q35. Default: Q35",
	},
		"disk": {
			Type:        schema.TypeInt,
			Optional:    true,
			Computed:    true,
			Description: "Node boot disk size in GB.",
		},
		"sep_id": {
			Type:        schema.TypeInt,
			Optional:    true,
			Computed:    true,
			Description: "Storage Endpoint ID",
		},
		"sep_pool": {
			Type:        schema.TypeString,
			Optional:    true,
			Computed:    true,
			Description: "Storage Endpoint Pool",
		},
		"with_lb": {
			Type:        schema.TypeBool,
			Optional:    true,
			Default:     true,
			Description: "Create k8s with load balancer if true.",
		},
		"extnet_only": {
			Type:        schema.TypeBool,
			Optional:    true,
			Default:     false,
			Description: "Use only selected ExtNet for infrastructure connections",
		},
		"join_config": {
			Type:        schema.TypeString,
			Optional:    true,
			Description: "is used to configure the behavior and settings for joining a node to a cluster. It includes parameters such as the cluster's control plane endpoint, token, and certificate key. insert a valid JSON string with all levels of nesting.",
		},
		"kube_proxy_config": {
			Type:        schema.TypeString,
			Optional:    true,
			Description: "is used to configure the behavior and settings of the Kube-proxy, which is responsible for network proxying and load balancing within the cluster. It includes parameters such as proxy mode, cluster IP ranges, and other Kube-proxy specific configurations. insert a valid JSON string with all levels of nesting.",
		},
		"kubelet_config": {
			Type:        schema.TypeString,
			Optional:    true,
			Description: "is used to configure the behavior and settings of the Kubelet, which is the primary node agent that runs on each node in the cluster. It includes parameters such as node IP address, resource allocation, pod eviction policies, and other Kubelet-specific configurations. insert a valid JSON string with all levels of nesting.",
		},
		"cluster_config": {
			Type:        schema.TypeString,
			Optional:    true,
			Description: "is used to define global settings and configurations for the entire cluster. It includes parameters such as cluster name, DNS settings, authentication methods, and other cluster-wide configurations. insert a valid JSON string with all levels of nesting.",
		},
		"init_config": {
			Type:        schema.TypeString,
			Optional:    true,
			Description: "is used to define settings and actions that should be performed before any other component in the cluster starts. It allows you to configure things like node registration, network setup, and other initialization tasks. insert a valid JSON string with all levels of nesting.",
		},
		"additional_sans": {
			Type:        schema.TypeList,
			Optional:    true,
			Description: "Optional extra Subject Alternative Names (SANs) to use for the API Server serving certificate. Can be both IP addresses and DNS names",
			Elem: &schema.Schema{
				Type: schema.TypeString,
			},
		},
		"ha_mode": {
			Type:        schema.TypeBool,
			Optional:    true,
			Description: "Use Highly Available schema for LB deploy",
		},
		"lb_sysctl_params": {
			Type:        schema.TypeList,
			Optional:    true,
			Description: "Custom sysctl values for Load Balancer instance. Applied on boot.",
			Elem: &schema.Schema{
				Type: schema.TypeMap,
				Elem: &schema.Schema{
					Type: schema.TypeString,
				},
			},
		},
		"oidc_cert": {
			Type:        schema.TypeString,
			Optional:    true,
			Description: "insert ssl certificate in x509 pem format",
		},
		"extnet_id": {
			Type:     schema.TypeInt,
			Optional: true,
			Computed: true,
			//ForceNew:    true,
			Description: "ID of the external network to connect workers to. If omitted network will be chosen by the platfom.",
		},
		"desc": {
			Type:        schema.TypeString,
			Optional:    true,
			Description: "Text description of this instance.",
		},
		"start": {
			Type:        schema.TypeBool,
			Optional:    true,
			Default:     true,
			Description: "Start k8s cluster.",
		},
		"detailed_info": {
			Type:     schema.TypeList,
			Computed: true,
			Elem: &schema.Resource{
				Schema: detailedInfoSchemaMake(),
			},
		},
		"master_id": {
			Type:        schema.TypeInt,
			Computed:    true,
			Description: "Master group ID.",
		},
		"master_name": {
			Type:        schema.TypeString,
			Computed:    true,
			Description: "Master group name.",
		},
		"permanently": {
			Type:        schema.TypeBool,
			Optional:    true,
			Default:     false,
			Description: "Determines if cluster should be destroyed",
		},
		"acl": {
			Type:     schema.TypeList,
			Computed: true,
			Elem: &schema.Resource{
				Schema: aclGroupSchemaMake(),
			},
		},
		"account_id": {
			Type:     schema.TypeInt,
			Computed: true,
		},
		"account_name": {
			Type:     schema.TypeString,
			Computed: true,
		},
		"bservice_id": {
			Type:     schema.TypeInt,
			Computed: true,
		},
		"created_by": {
			Type:     schema.TypeString,
			Computed: true,
		},
		"created_time": {
			Type:     schema.TypeInt,
			Computed: true,
		},
		"deleted_by": {
			Type:     schema.TypeString,
			Computed: true,
		},
		"deleted_time": {
			Type:     schema.TypeInt,
			Computed: true,
		},
		"k8s_ci_name": {
			Type:     schema.TypeString,
			Computed: true,
		},
		"lb_id": {
			Type:     schema.TypeInt,
			Computed: true,
		},
		"k8s_id": {
			Type:     schema.TypeInt,
			Computed: true,
		},
		"lb_ip": {
			Type:        schema.TypeString,
			Computed:    true,
			Description: "IP address of default load balancer.",
		},
		"rg_name": {
			Type:     schema.TypeString,
			Computed: true,
		},
		"status": {
			Type:     schema.TypeString,
			Computed: true,
		},
		"tech_status": {
			Type:     schema.TypeString,
			Computed: true,
		},
		"updated_by": {
			Type:     schema.TypeString,
			Computed: true,
		},
		"updated_time": {
			Type:     schema.TypeInt,
			Computed: true,
		},
		"kubeconfig": {
			Type:        schema.TypeString,
			Computed:    true,
			Description: "Kubeconfig for cluster access.",
		},
		"vins_id": {
			Type:        schema.TypeInt,
			Optional:    true,
			Computed:    true,
			Description: "ID of default vins for this instace.",
		},
	}
}

func ResourceK8sCP() *schema.Resource {
	return &schema.Resource{
		SchemaVersion: 2,

		CreateContext: resourceK8sCPCreate,
		ReadContext:   resourceK8sCPRead,
		UpdateContext: resourceK8sCPUpdate,
		DeleteContext: resourceK8sCPDelete,

		Importer: &schema.ResourceImporter{
			StateContext: schema.ImportStatePassthroughContext,
		},

		Timeouts: &schema.ResourceTimeout{
			Create:  &constants.Timeout30m,
			Read:    &constants.Timeout600s,
			Update:  &constants.Timeout600s,
			Delete:  &constants.Timeout600s,
			Default: &constants.Timeout600s,
		},

		Schema: resourceK8sCPSchemaMake(),
		StateUpgraders: []schema.StateUpgrader{
			{
				Type:    resourceK8sCPSchemaV1().CoreConfigSchema().ImpliedType(),
				Upgrade: resourceK8sCPStateUpgradeV1,
				Version: 1,
			},
		},
	}
}
