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

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 rg

import (
	"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
	"repository.basistech.ru/BASIS/decort-golang-sdk/pkg/cloudbroker/rg"
)

func makeQuotaRecord(arg_list []interface{}) QuotaRecord {
	quota := QuotaRecord{
		Cpu:      -1,
		Ram:      -1., // this is float64, but may change in the future
		Disk:     -1,
		ExtIPs:   -1,
		GpuUnits: -1,
	}
	subres_data := arg_list[0].(map[string]interface{})

	if subres_data["cpu"].(int) > 0 {
		quota.Cpu = subres_data["cpu"].(float64)
	}

	if subres_data["disk"].(int) > 0 {
		quota.Disk = subres_data["disk"].(float64)
	}

	if subres_data["ram"].(float64) > 0 {
		quota.Ram = subres_data["ram"].(float64)
	}

	if subres_data["ext_ips"].(int) > 0 {
		quota.ExtIPs = subres_data["ext_ips"].(float64)
	}

	if subres_data["gpu_units"].(int) > 0 {
		quota.GpuUnits = subres_data["gpu_units"].(float64)
	}

	return quota
}

func parseQuota(quota rg.ResourceLimits) []interface{} {
	quota_map := make(map[string]interface{})

	quota_map["cpu"] = quota.CUC
	quota_map["ram"] = quota.CUM
	quota_map["disk"] = quota.CuD
	quota_map["ext_ips"] = quota.CUI
	quota_map["gpu_units"] = quota.GPUUnits

	result := make([]interface{}, 1)
	result[0] = quota_map

	return result // this result will be used to d.Set("quota,") of dataSourceResgroup schema
}

func quotaRgSubresourceSchemaMake() map[string]*schema.Schema {
	rets := map[string]*schema.Schema{
		"cpu": {
			Type:        schema.TypeFloat,
			Optional:    true,
			Default:     -1,
			Description: "Limit on the total number of CPUs in this resource group.",
		},

		"ram": {
			Type:        schema.TypeFloat, // NB: API expects and returns this as float in units of MB! This may be changed in the future.
			Optional:    true,
			Default:     -1.,
			Description: "Limit on the total amount of RAM in this resource group, specified in MB.",
		},

		"disk": {
			Type:        schema.TypeFloat,
			Optional:    true,
			Default:     -1,
			Description: "Limit on the total volume of storage resources in this resource group, specified in GB.",
		},

		"ext_traffic": {
			Type:        schema.TypeFloat,
			Optional:    true,
			Default:     -1,
			Description: "Limit on the total ingress network traffic for this resource group, specified in GB.",
		},

		"ext_ips": {
			Type:        schema.TypeFloat,
			Optional:    true,
			Default:     -1,
			Description: "Limit on the total number of external IP addresses this resource group can use.",
		},

		"gpu_units": {
			Type:        schema.TypeFloat,
			Optional:    true,
			Default:     -1,
			Description: "Limit on the total number of virtual GPUs this resource group can use.",
		},
	}
	return rets
}
