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

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 pfw

import (
	"context"
	"fmt"

	"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/terraform-provider-decort/internal/constants"
	"repository.basistech.ru/BASIS/terraform-provider-decort/internal/controller"
)

func resourcePfwCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
	log.Debugf("resourcePfwCreate: called for compute %d", d.Get("compute_id").(int))

	c := m.(*controller.ControllerCfg)
	req := compute.PFWAddRequest{
		ComputeID:       uint64(d.Get("compute_id").(int)),
		PublicPortStart: uint64(d.Get("public_port_start").(int)),
		LocalBasePort:   uint64(d.Get("local_base_port").(int)),
		Proto:           d.Get("proto").(string),
	}

	if portEnd, ok := d.GetOk("public_port_end"); ok {
		req.PublicPortEnd = int64(portEnd.(int))
	}

	pfwId, err := c.CloudAPI().Compute().PFWAdd(ctx, req)
	if err != nil {
		return diag.FromErr(err)
	}

	d.SetId(fmt.Sprintf("%d-%d", d.Get("compute_id").(int), pfwId))

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

	d.Set("local_ip", pfw.LocalIP)
	if _, ok := d.GetOk("public_port_end"); !ok {
		d.Set("public_port_end", pfw.PublicPortEnd)
	}

	return resourcePfwRead(ctx, d, m)
}

func resourcePfwRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
	log.Debugf("resourcePfwRead: called for compute %d, rule %s", d.Get("compute_id").(int), d.Id())

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

	flattenPFW(d, pfw)

	return nil
}

func resourcePfwDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
	log.Debugf("resourcePfwDelete: called for compute %d, rule %s", d.Get("compute_id").(int), d.Id())

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

	c := m.(*controller.ControllerCfg)
	req := compute.PFWDelRequest{
		ComputeID: uint64(d.Get("compute_id").(int)),
		PFWID:     pfw.ID,
	}

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

	d.SetId("")

	return nil
}

func resourcePfwSchemaMake() map[string]*schema.Schema {
	return map[string]*schema.Schema{
		"compute_id": {
			Type:        schema.TypeInt,
			Required:    true,
			ForceNew:    true,
			Description: "ID of compute instance.",
		},

		"public_port_start": {
			Type:         schema.TypeInt,
			Required:     true,
			ForceNew:     true,
			ValidateFunc: validation.IntBetween(1, 65535),
			Description:  "External start port number for the rule.",
		},

		"public_port_end": {
			Type:         schema.TypeInt,
			Optional:     true,
			Computed:     true,
			ForceNew:     true,
			ValidateFunc: validation.IntBetween(1, 65535),
			Description:  "End port number (inclusive) for the ranged rule.",
		},

		"local_ip": {
			Type:        schema.TypeString,
			Computed:    true,
			Description: "IP address of compute instance.",
		},

		"local_base_port": {
			Type:         schema.TypeInt,
			Required:     true,
			ForceNew:     true,
			ValidateFunc: validation.IntBetween(1, 65535),
			Description:  "Internal base port number.",
		},

		"proto": {
			Type:         schema.TypeString,
			Required:     true,
			ForceNew:     true,
			ValidateFunc: validation.StringInSlice([]string{"tcp", "udp"}, false),
			Description:  "Network protocol, either 'tcp' or 'udp'.",
		},
	}
}

func ResourcePfw() *schema.Resource {
	return &schema.Resource{
		SchemaVersion: 1,

		CreateContext: resourcePfwCreate,
		ReadContext:   resourcePfwRead,
		DeleteContext: resourcePfwDelete,

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

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

		Schema: resourcePfwSchemaMake(),
	}
}
