// Code generated by azure-service-operator-codegen. DO NOT EDIT.
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
package webhook

import (
	"context"
	"fmt"
	v20220301 "github.com/Azure/azure-service-operator/v2/api/web/v1api20220301"
	"github.com/Azure/azure-service-operator/v2/internal/reflecthelpers"
	"github.com/Azure/azure-service-operator/v2/pkg/genruntime"
	"github.com/Azure/azure-service-operator/v2/pkg/genruntime/configmaps"
	"github.com/Azure/azure-service-operator/v2/pkg/genruntime/secrets"
	"k8s.io/apimachinery/pkg/runtime"
	"sigs.k8s.io/controller-runtime/pkg/webhook"
	"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
)

type SitesSourcecontrol struct {
}

// +kubebuilder:webhook:path=/mutate-web-azure-com-v1api20220301-sitessourcecontrol,mutating=true,sideEffects=None,matchPolicy=Exact,failurePolicy=fail,groups=web.azure.com,resources=sitessourcecontrols,verbs=create;update,versions=v1api20220301,name=default.v1api20220301.sitessourcecontrols.web.azure.com,admissionReviewVersions=v1

var _ webhook.CustomDefaulter = &SitesSourcecontrol{}

// Default applies defaults to the SitesSourcecontrol resource
func (sourcecontrol *SitesSourcecontrol) Default(ctx context.Context, obj runtime.Object) error {
	resource, ok := obj.(*v20220301.SitesSourcecontrol)
	if !ok {
		return fmt.Errorf("expected github.com/Azure/azure-service-operator/v2/api/web/v1api20220301/SitesSourcecontrol, but got %T", obj)
	}
	err := sourcecontrol.defaultImpl(ctx, resource)
	if err != nil {
		return err
	}
	var temp any = sourcecontrol
	if runtimeDefaulter, ok := temp.(genruntime.Defaulter); ok {
		err = runtimeDefaulter.CustomDefault(ctx, resource)
		if err != nil {
			return err
		}
	}
	return nil
}

// defaultImpl applies the code generated defaults to the SitesSourcecontrol resource
func (sourcecontrol *SitesSourcecontrol) defaultImpl(ctx context.Context, obj *v20220301.SitesSourcecontrol) error {
	return nil
}

// +kubebuilder:webhook:path=/validate-web-azure-com-v1api20220301-sitessourcecontrol,mutating=false,sideEffects=None,matchPolicy=Exact,failurePolicy=fail,groups=web.azure.com,resources=sitessourcecontrols,verbs=create;update,versions=v1api20220301,name=validate.v1api20220301.sitessourcecontrols.web.azure.com,admissionReviewVersions=v1

var _ webhook.CustomValidator = &SitesSourcecontrol{}

// ValidateCreate validates the creation of the resource
func (sourcecontrol *SitesSourcecontrol) ValidateCreate(ctx context.Context, resource runtime.Object) (admission.Warnings, error) {
	obj, ok := resource.(*v20220301.SitesSourcecontrol)
	if !ok {
		return nil, fmt.Errorf("expected github.com/Azure/azure-service-operator/v2/api/web/v1api20220301/SitesSourcecontrol, but got %T", resource)
	}
	validations := sourcecontrol.createValidations()
	var temp any = sourcecontrol
	if runtimeValidator, ok := temp.(genruntime.Validator[*v20220301.SitesSourcecontrol]); ok {
		validations = append(validations, runtimeValidator.CreateValidations()...)
	}
	return genruntime.ValidateCreate(ctx, obj, validations)
}

// ValidateDelete validates the deletion of the resource
func (sourcecontrol *SitesSourcecontrol) ValidateDelete(ctx context.Context, resource runtime.Object) (admission.Warnings, error) {
	obj, ok := resource.(*v20220301.SitesSourcecontrol)
	if !ok {
		return nil, fmt.Errorf("expected github.com/Azure/azure-service-operator/v2/api/web/v1api20220301/SitesSourcecontrol, but got %T", resource)
	}
	validations := sourcecontrol.deleteValidations()
	var temp any = sourcecontrol
	if runtimeValidator, ok := temp.(genruntime.Validator[*v20220301.SitesSourcecontrol]); ok {
		validations = append(validations, runtimeValidator.DeleteValidations()...)
	}
	return genruntime.ValidateDelete(ctx, obj, validations)
}

// ValidateUpdate validates an update of the resource
func (sourcecontrol *SitesSourcecontrol) ValidateUpdate(ctx context.Context, oldResource runtime.Object, newResource runtime.Object) (admission.Warnings, error) {
	newObj, ok := newResource.(*v20220301.SitesSourcecontrol)
	if !ok {
		return nil, fmt.Errorf("expected github.com/Azure/azure-service-operator/v2/api/web/v1api20220301/SitesSourcecontrol, but got %T", newResource)
	}
	oldObj, ok := oldResource.(*v20220301.SitesSourcecontrol)
	if !ok {
		return nil, fmt.Errorf("expected github.com/Azure/azure-service-operator/v2/api/web/v1api20220301/SitesSourcecontrol, but got %T", oldResource)
	}
	validations := sourcecontrol.updateValidations()
	var temp any = sourcecontrol
	if runtimeValidator, ok := temp.(genruntime.Validator[*v20220301.SitesSourcecontrol]); ok {
		validations = append(validations, runtimeValidator.UpdateValidations()...)
	}
	return genruntime.ValidateUpdate(
		ctx,
		oldObj,
		newObj,
		validations)
}

// createValidations validates the creation of the resource
func (sourcecontrol *SitesSourcecontrol) createValidations() []func(ctx context.Context, obj *v20220301.SitesSourcecontrol) (admission.Warnings, error) {
	return []func(ctx context.Context, obj *v20220301.SitesSourcecontrol) (admission.Warnings, error){
		sourcecontrol.validateResourceReferences,
		sourcecontrol.validateOwnerReference,
		sourcecontrol.validateSecretDestinations,
		sourcecontrol.validateConfigMapDestinations,
	}
}

// deleteValidations validates the deletion of the resource
func (sourcecontrol *SitesSourcecontrol) deleteValidations() []func(ctx context.Context, obj *v20220301.SitesSourcecontrol) (admission.Warnings, error) {
	return nil
}

// updateValidations validates the update of the resource
func (sourcecontrol *SitesSourcecontrol) updateValidations() []func(ctx context.Context, oldObj *v20220301.SitesSourcecontrol, newObj *v20220301.SitesSourcecontrol) (admission.Warnings, error) {
	return []func(ctx context.Context, oldObj *v20220301.SitesSourcecontrol, newObj *v20220301.SitesSourcecontrol) (admission.Warnings, error){
		func(ctx context.Context, oldObj *v20220301.SitesSourcecontrol, newObj *v20220301.SitesSourcecontrol) (admission.Warnings, error) {
			return sourcecontrol.validateResourceReferences(ctx, newObj)
		},
		sourcecontrol.validateWriteOnceProperties,
		func(ctx context.Context, oldObj *v20220301.SitesSourcecontrol, newObj *v20220301.SitesSourcecontrol) (admission.Warnings, error) {
			return sourcecontrol.validateOwnerReference(ctx, newObj)
		},
		func(ctx context.Context, oldObj *v20220301.SitesSourcecontrol, newObj *v20220301.SitesSourcecontrol) (admission.Warnings, error) {
			return sourcecontrol.validateSecretDestinations(ctx, newObj)
		},
		func(ctx context.Context, oldObj *v20220301.SitesSourcecontrol, newObj *v20220301.SitesSourcecontrol) (admission.Warnings, error) {
			return sourcecontrol.validateConfigMapDestinations(ctx, newObj)
		},
	}
}

// validateConfigMapDestinations validates there are no colliding genruntime.ConfigMapDestinations
func (sourcecontrol *SitesSourcecontrol) validateConfigMapDestinations(ctx context.Context, obj *v20220301.SitesSourcecontrol) (admission.Warnings, error) {
	if obj.Spec.OperatorSpec == nil {
		return nil, nil
	}
	return configmaps.ValidateDestinations(obj, nil, obj.Spec.OperatorSpec.ConfigMapExpressions)
}

// validateOwnerReference validates the owner field
func (sourcecontrol *SitesSourcecontrol) validateOwnerReference(ctx context.Context, obj *v20220301.SitesSourcecontrol) (admission.Warnings, error) {
	return genruntime.ValidateOwner(obj)
}

// validateResourceReferences validates all resource references
func (sourcecontrol *SitesSourcecontrol) validateResourceReferences(ctx context.Context, obj *v20220301.SitesSourcecontrol) (admission.Warnings, error) {
	refs, err := reflecthelpers.FindResourceReferences(&obj.Spec)
	if err != nil {
		return nil, err
	}
	return genruntime.ValidateResourceReferences(refs)
}

// validateSecretDestinations validates there are no colliding genruntime.SecretDestination's
func (sourcecontrol *SitesSourcecontrol) validateSecretDestinations(ctx context.Context, obj *v20220301.SitesSourcecontrol) (admission.Warnings, error) {
	if obj.Spec.OperatorSpec == nil {
		return nil, nil
	}
	return secrets.ValidateDestinations(obj, nil, obj.Spec.OperatorSpec.SecretExpressions)
}

// validateWriteOnceProperties validates all WriteOnce properties
func (sourcecontrol *SitesSourcecontrol) validateWriteOnceProperties(ctx context.Context, oldObj *v20220301.SitesSourcecontrol, newObj *v20220301.SitesSourcecontrol) (admission.Warnings, error) {
	return genruntime.ValidateWriteOnceProperties(oldObj, newObj)
}
