// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

// Code generated by "redactgen -header-file ../../../../hack/boilerplate.txt -o redact.generated.go ."; DO NOT EDIT.

package network

import (
	"github.com/cosi-project/runtime/pkg/resource"

	"github.com/siderolabs/talos/pkg/machinery/constants"
	"github.com/siderolabs/talos/pkg/machinery/resources"
)

var (
	_ resources.RedactableSpec[AddressSpecSpec]     = AddressSpecSpec{}
	_ resources.RedactableSpec[HostnameSpecSpec]    = HostnameSpecSpec{}
	_ resources.RedactableSpec[LinkSpecSpec]        = LinkSpecSpec{}
	_ resources.RedactableSpec[OperatorSpecSpec]    = OperatorSpecSpec{}
	_ resources.RedactableSpec[ProbeSpecSpec]       = ProbeSpecSpec{}
	_ resources.RedactableSpec[ResolverSpecSpec]    = ResolverSpecSpec{}
	_ resources.RedactableSpec[RouteSpecSpec]       = RouteSpecSpec{}
	_ resources.RedactableSpec[RoutingRuleSpecSpec] = RoutingRuleSpecSpec{}
	_ resources.RedactableSpec[TimeServerSpecSpec]  = TimeServerSpecSpec{}
)

// RedactSecrets implements resources.RedactableSpec interface.
func (spec AddressSpecSpec) RedactSecrets(resource.Metadata) AddressSpecSpec {
	// no sensitive fields
	return spec
}

// RedactSecrets implements resources.RedactableSpec interface.
func (spec HostnameSpecSpec) RedactSecrets(resource.Metadata) HostnameSpecSpec {
	// no sensitive fields
	return spec
}

// RedactSecrets implements resources.RedactableSpec interface.
func (spec LinkSpecSpec) RedactSecrets(resource.Metadata) LinkSpecSpec {
	if !hasSecretsLinkSpecSpec(&spec) {
		return spec
	}

	// the spec might share the backing storage with the resource, so copy before mutating
	spec = spec.DeepCopy()

	redactLinkSpecSpec(&spec)

	return spec
}

// RedactSecrets implements resources.RedactableSpec interface.
func (spec OperatorSpecSpec) RedactSecrets(resource.Metadata) OperatorSpecSpec {
	if !hasSecretsOperatorSpecSpec(&spec) {
		return spec
	}

	// the spec might share the backing storage with the resource, so copy before mutating
	spec = spec.DeepCopy()

	redactOperatorSpecSpec(&spec)

	return spec
}

// RedactSecrets implements resources.RedactableSpec interface.
func (spec ProbeSpecSpec) RedactSecrets(resource.Metadata) ProbeSpecSpec {
	// no sensitive fields
	return spec
}

// RedactSecrets implements resources.RedactableSpec interface.
func (spec ResolverSpecSpec) RedactSecrets(resource.Metadata) ResolverSpecSpec {
	// no sensitive fields
	return spec
}

// RedactSecrets implements resources.RedactableSpec interface.
func (spec RouteSpecSpec) RedactSecrets(resource.Metadata) RouteSpecSpec {
	// no sensitive fields
	return spec
}

// RedactSecrets implements resources.RedactableSpec interface.
func (spec RoutingRuleSpecSpec) RedactSecrets(resource.Metadata) RoutingRuleSpecSpec {
	// no sensitive fields
	return spec
}

// RedactSecrets implements resources.RedactableSpec interface.
func (spec TimeServerSpecSpec) RedactSecrets(resource.Metadata) TimeServerSpecSpec {
	// no sensitive fields
	return spec
}

// hasSecretsLinkSpecSpec checks if LinkSpecSpec carries any sensitive value.
func hasSecretsLinkSpecSpec(spec *LinkSpecSpec) bool {
	if hasSecretsWireguardSpec(&spec.Wireguard) {
		return true
	}

	return false
}

// redactLinkSpecSpec redacts sensitive values in LinkSpecSpec in place.
func redactLinkSpecSpec(spec *LinkSpecSpec) {
	redactWireguardSpec(&spec.Wireguard)
}

// hasSecretsOperatorSpecSpec checks if OperatorSpecSpec carries any sensitive value.
func hasSecretsOperatorSpecSpec(spec *OperatorSpecSpec) bool {
	if hasSecretsVIPOperatorSpec(&spec.VIP) {
		return true
	}

	return false
}

// redactOperatorSpecSpec redacts sensitive values in OperatorSpecSpec in place.
func redactOperatorSpecSpec(spec *OperatorSpecSpec) {
	redactVIPOperatorSpec(&spec.VIP)
}

// hasSecretsWireguardSpec checks if WireguardSpec carries any sensitive value.
func hasSecretsWireguardSpec(spec *WireguardSpec) bool {
	if spec.PrivateKey != "" {
		return true
	}

	for idx := range spec.Peers {
		if hasSecretsWireguardPeer(&spec.Peers[idx]) {
			return true
		}
	}

	return false
}

// redactWireguardSpec redacts sensitive values in WireguardSpec in place.
func redactWireguardSpec(spec *WireguardSpec) {
	if spec.PrivateKey != "" {
		spec.PrivateKey = constants.Redacted
	}

	for idx := range spec.Peers {
		redactWireguardPeer(&spec.Peers[idx])
	}
}

// hasSecretsVIPOperatorSpec checks if VIPOperatorSpec carries any sensitive value.
func hasSecretsVIPOperatorSpec(spec *VIPOperatorSpec) bool {
	if hasSecretsVIPEquinixMetalSpec(&spec.EquinixMetal) {
		return true
	}

	if hasSecretsVIPHCloudSpec(&spec.HCloud) {
		return true
	}

	return false
}

// redactVIPOperatorSpec redacts sensitive values in VIPOperatorSpec in place.
func redactVIPOperatorSpec(spec *VIPOperatorSpec) {
	redactVIPEquinixMetalSpec(&spec.EquinixMetal)

	redactVIPHCloudSpec(&spec.HCloud)
}

// hasSecretsWireguardPeer checks if WireguardPeer carries any sensitive value.
func hasSecretsWireguardPeer(spec *WireguardPeer) bool {
	if spec.PresharedKey != "" {
		return true
	}

	return false
}

// redactWireguardPeer redacts sensitive values in WireguardPeer in place.
func redactWireguardPeer(spec *WireguardPeer) {
	if spec.PresharedKey != "" {
		spec.PresharedKey = constants.Redacted
	}
}

// hasSecretsVIPEquinixMetalSpec checks if VIPEquinixMetalSpec carries any sensitive value.
func hasSecretsVIPEquinixMetalSpec(spec *VIPEquinixMetalSpec) bool {
	if spec.APIToken != "" {
		return true
	}

	return false
}

// redactVIPEquinixMetalSpec redacts sensitive values in VIPEquinixMetalSpec in place.
func redactVIPEquinixMetalSpec(spec *VIPEquinixMetalSpec) {
	if spec.APIToken != "" {
		spec.APIToken = constants.Redacted
	}
}

// hasSecretsVIPHCloudSpec checks if VIPHCloudSpec carries any sensitive value.
func hasSecretsVIPHCloudSpec(spec *VIPHCloudSpec) bool {
	if spec.APIToken != "" {
		return true
	}

	return false
}

// redactVIPHCloudSpec redacts sensitive values in VIPHCloudSpec in place.
func redactVIPHCloudSpec(spec *VIPHCloudSpec) {
	if spec.APIToken != "" {
		spec.APIToken = constants.Redacted
	}
}
