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

import (
	"context"
	storage "github.com/Azure/azure-service-operator/v2/api/insights/v1api20230311/storage"
	"github.com/Azure/azure-service-operator/v2/internal/genericarmclient"
	"github.com/Azure/azure-service-operator/v2/pkg/genruntime"
	"github.com/Azure/azure-service-operator/v2/pkg/genruntime/conditions"
	"github.com/Azure/azure-service-operator/v2/pkg/genruntime/configmaps"
	"github.com/Azure/azure-service-operator/v2/pkg/genruntime/core"
	"github.com/Azure/azure-service-operator/v2/pkg/genruntime/secrets"
	"github.com/go-logr/logr"
	"github.com/rotisserie/eris"
	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
	"k8s.io/apimachinery/pkg/runtime/schema"
	"sigs.k8s.io/controller-runtime/pkg/client"
)

// +kubebuilder:rbac:groups=insights.azure.com,resources=components,verbs=get;list;watch;create;update;patch;delete
// +kubebuilder:rbac:groups=insights.azure.com,resources={components/status,components/finalizers},verbs=get;update;patch

// +kubebuilder:object:root=true
// +kubebuilder:resource:categories={azure,insights}
// +kubebuilder:subresource:status
// +kubebuilder:storageversion
// +kubebuilder:printcolumn:name="Ready",type="string",JSONPath=".status.conditions[?(@.type=='Ready')].status"
// +kubebuilder:printcolumn:name="Severity",type="string",JSONPath=".status.conditions[?(@.type=='Ready')].severity"
// +kubebuilder:printcolumn:name="Reason",type="string",JSONPath=".status.conditions[?(@.type=='Ready')].reason"
// +kubebuilder:printcolumn:name="Message",type="string",JSONPath=".status.conditions[?(@.type=='Ready')].message"
// Storage version of v1api20200202.Component
// Generator information:
// - Generated from: /applicationinsights/resource-manager/Microsoft.Insights/stable/2020-02-02/components_API.json
// - ARM URI: /subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Insights/components/{resourceName}
type Component struct {
	metav1.TypeMeta   `json:",inline"`
	metav1.ObjectMeta `json:"metadata,omitempty"`
	Spec              Component_Spec   `json:"spec,omitempty"`
	Status            Component_STATUS `json:"status,omitempty"`
}

var _ conditions.Conditioner = &Component{}

// GetConditions returns the conditions of the resource
func (component *Component) GetConditions() conditions.Conditions {
	return component.Status.Conditions
}

// SetConditions sets the conditions on the resource status
func (component *Component) SetConditions(conditions conditions.Conditions) {
	component.Status.Conditions = conditions
}

var _ configmaps.Exporter = &Component{}

// ConfigMapDestinationExpressions returns the Spec.OperatorSpec.ConfigMapExpressions property
func (component *Component) ConfigMapDestinationExpressions() []*core.DestinationExpression {
	if component.Spec.OperatorSpec == nil {
		return nil
	}
	return component.Spec.OperatorSpec.ConfigMapExpressions
}

var _ secrets.Exporter = &Component{}

// SecretDestinationExpressions returns the Spec.OperatorSpec.SecretExpressions property
func (component *Component) SecretDestinationExpressions() []*core.DestinationExpression {
	if component.Spec.OperatorSpec == nil {
		return nil
	}
	return component.Spec.OperatorSpec.SecretExpressions
}

var _ genruntime.KubernetesConfigExporter = &Component{}

// ExportKubernetesConfigMaps defines a resource which can create ConfigMaps in Kubernetes.
func (component *Component) ExportKubernetesConfigMaps(_ context.Context, _ genruntime.MetaObject, _ *genericarmclient.GenericClient, _ logr.Logger) ([]client.Object, error) {
	collector := configmaps.NewCollector(component.Namespace)
	if component.Spec.OperatorSpec != nil && component.Spec.OperatorSpec.ConfigMaps != nil {
		if component.Status.ConnectionString != nil {
			collector.AddValue(component.Spec.OperatorSpec.ConfigMaps.ConnectionString, *component.Status.ConnectionString)
		}
	}
	if component.Spec.OperatorSpec != nil && component.Spec.OperatorSpec.ConfigMaps != nil {
		if component.Status.InstrumentationKey != nil {
			collector.AddValue(component.Spec.OperatorSpec.ConfigMaps.InstrumentationKey, *component.Status.InstrumentationKey)
		}
	}
	result, err := collector.Values()
	if err != nil {
		return nil, err
	}
	return configmaps.SliceToClientObjectSlice(result), nil
}

var _ genruntime.KubernetesResource = &Component{}

// AzureName returns the Azure name of the resource
func (component *Component) AzureName() string {
	return component.Spec.AzureName
}

// GetAPIVersion returns the ARM API version of the resource. This is always "2020-02-02"
func (component Component) GetAPIVersion() string {
	return "2020-02-02"
}

// GetResourceScope returns the scope of the resource
func (component *Component) GetResourceScope() genruntime.ResourceScope {
	return genruntime.ResourceScopeResourceGroup
}

// GetSpec returns the specification of this resource
func (component *Component) GetSpec() genruntime.ConvertibleSpec {
	return &component.Spec
}

// GetStatus returns the status of this resource
func (component *Component) GetStatus() genruntime.ConvertibleStatus {
	return &component.Status
}

// GetSupportedOperations returns the operations supported by the resource
func (component *Component) GetSupportedOperations() []genruntime.ResourceOperation {
	return []genruntime.ResourceOperation{
		genruntime.ResourceOperationDelete,
		genruntime.ResourceOperationGet,
		genruntime.ResourceOperationPut,
	}
}

// GetType returns the ARM Type of the resource. This is always "Microsoft.Insights/components"
func (component *Component) GetType() string {
	return "Microsoft.Insights/components"
}

// NewEmptyStatus returns a new empty (blank) status
func (component *Component) NewEmptyStatus() genruntime.ConvertibleStatus {
	return &Component_STATUS{}
}

// Owner returns the ResourceReference of the owner
func (component *Component) Owner() *genruntime.ResourceReference {
	if component.Spec.Owner == nil {
		return nil
	}

	group, kind := genruntime.LookupOwnerGroupKind(component.Spec)
	return component.Spec.Owner.AsResourceReference(group, kind)
}

// SetStatus sets the status of this resource
func (component *Component) SetStatus(status genruntime.ConvertibleStatus) error {
	// If we have exactly the right type of status, assign it
	if st, ok := status.(*Component_STATUS); ok {
		component.Status = *st
		return nil
	}

	// Convert status to required version
	var st Component_STATUS
	err := status.ConvertStatusTo(&st)
	if err != nil {
		return eris.Wrap(err, "failed to convert status")
	}

	component.Status = st
	return nil
}

// Hub marks that this Component is the hub type for conversion
func (component *Component) Hub() {}

// OriginalGVK returns a GroupValueKind for the original API version used to create the resource
func (component *Component) OriginalGVK() *schema.GroupVersionKind {
	return &schema.GroupVersionKind{
		Group:   GroupVersion.Group,
		Version: component.Spec.OriginalVersion,
		Kind:    "Component",
	}
}

// +kubebuilder:object:root=true
// Storage version of v1api20200202.Component
// Generator information:
// - Generated from: /applicationinsights/resource-manager/Microsoft.Insights/stable/2020-02-02/components_API.json
// - ARM URI: /subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Insights/components/{resourceName}
type ComponentList struct {
	metav1.TypeMeta `json:",inline"`
	metav1.ListMeta `json:"metadata,omitempty"`
	Items           []Component `json:"items"`
}

// Storage version of v1api20200202.APIVersion
// +kubebuilder:validation:Enum={"2020-02-02"}
type APIVersion string

const APIVersion_Value = APIVersion("2020-02-02")

// Storage version of v1api20200202.Component_Spec
type Component_Spec struct {
	Application_Type *string `json:"Application_Type,omitempty"`

	// AzureName: The name of the resource in Azure. This is often the same as the name of the resource in Kubernetes but it
	// doesn't have to be.
	AzureName                       string                 `json:"azureName,omitempty"`
	DisableIpMasking                *bool                  `json:"DisableIpMasking,omitempty"`
	DisableLocalAuth                *bool                  `json:"DisableLocalAuth,omitempty"`
	Etag                            *string                `json:"etag,omitempty"`
	Flow_Type                       *string                `json:"Flow_Type,omitempty"`
	ForceCustomerStorageForProfiler *bool                  `json:"ForceCustomerStorageForProfiler,omitempty"`
	HockeyAppId                     *string                `json:"HockeyAppId,omitempty"`
	ImmediatePurgeDataOn30Days      *bool                  `json:"ImmediatePurgeDataOn30Days,omitempty"`
	IngestionMode                   *string                `json:"IngestionMode,omitempty"`
	Kind                            *string                `json:"kind,omitempty"`
	Location                        *string                `json:"location,omitempty"`
	OperatorSpec                    *ComponentOperatorSpec `json:"operatorSpec,omitempty"`
	OriginalVersion                 string                 `json:"originalVersion,omitempty"`

	// +kubebuilder:validation:Required
	// Owner: The owner of the resource. The owner controls where the resource goes when it is deployed. The owner also
	// controls the resources lifecycle. When the owner is deleted the resource will also be deleted. Owner is expected to be a
	// reference to a resources.azure.com/ResourceGroup resource
	Owner                           *genruntime.KnownResourceReference `group:"resources.azure.com" json:"owner,omitempty" kind:"ResourceGroup"`
	PropertyBag                     genruntime.PropertyBag             `json:"$propertyBag,omitempty"`
	PublicNetworkAccessForIngestion *string                            `json:"publicNetworkAccessForIngestion,omitempty"`
	PublicNetworkAccessForQuery     *string                            `json:"publicNetworkAccessForQuery,omitempty"`
	Request_Source                  *string                            `json:"Request_Source,omitempty"`
	RetentionInDays                 *int                               `json:"RetentionInDays,omitempty"`
	SamplingPercentage              *float64                           `json:"SamplingPercentage,omitempty"`
	Tags                            map[string]string                  `json:"tags,omitempty"`

	// WorkspaceResourceReference: Resource Id of the log analytics workspace which the data will be ingested to. This property
	// is required to create an application with this API version. Applications from older versions will not have this property.
	WorkspaceResourceReference *genruntime.ResourceReference `armReference:"WorkspaceResourceId" json:"workspaceResourceReference,omitempty"`
}

var _ genruntime.ConvertibleSpec = &Component_Spec{}

// ConvertSpecFrom populates our Component_Spec from the provided source
func (component *Component_Spec) ConvertSpecFrom(source genruntime.ConvertibleSpec) error {
	if source == component {
		return eris.New("attempted conversion between unrelated implementations of github.com/Azure/azure-service-operator/v2/pkg/genruntime/ConvertibleSpec")
	}

	return source.ConvertSpecTo(component)
}

// ConvertSpecTo populates the provided destination from our Component_Spec
func (component *Component_Spec) ConvertSpecTo(destination genruntime.ConvertibleSpec) error {
	if destination == component {
		return eris.New("attempted conversion between unrelated implementations of github.com/Azure/azure-service-operator/v2/pkg/genruntime/ConvertibleSpec")
	}

	return destination.ConvertSpecFrom(component)
}

// Storage version of v1api20200202.Component_STATUS
type Component_STATUS struct {
	AppId                           *string                            `json:"AppId,omitempty"`
	ApplicationId                   *string                            `json:"ApplicationId,omitempty"`
	Application_Type                *string                            `json:"Application_Type,omitempty"`
	Conditions                      []conditions.Condition             `json:"conditions,omitempty"`
	ConnectionString                *string                            `json:"ConnectionString,omitempty"`
	CreationDate                    *string                            `json:"CreationDate,omitempty"`
	DisableIpMasking                *bool                              `json:"DisableIpMasking,omitempty"`
	DisableLocalAuth                *bool                              `json:"DisableLocalAuth,omitempty"`
	Etag                            *string                            `json:"etag,omitempty"`
	Flow_Type                       *string                            `json:"Flow_Type,omitempty"`
	ForceCustomerStorageForProfiler *bool                              `json:"ForceCustomerStorageForProfiler,omitempty"`
	HockeyAppId                     *string                            `json:"HockeyAppId,omitempty"`
	HockeyAppToken                  *string                            `json:"HockeyAppToken,omitempty"`
	Id                              *string                            `json:"id,omitempty"`
	ImmediatePurgeDataOn30Days      *bool                              `json:"ImmediatePurgeDataOn30Days,omitempty"`
	IngestionMode                   *string                            `json:"IngestionMode,omitempty"`
	InstrumentationKey              *string                            `json:"InstrumentationKey,omitempty"`
	Kind                            *string                            `json:"kind,omitempty"`
	LaMigrationDate                 *string                            `json:"LaMigrationDate,omitempty"`
	Location                        *string                            `json:"location,omitempty"`
	Name                            *string                            `json:"name,omitempty"`
	PrivateLinkScopedResources      []PrivateLinkScopedResource_STATUS `json:"PrivateLinkScopedResources,omitempty"`
	PropertiesName                  *string                            `json:"properties_name,omitempty"`
	PropertyBag                     genruntime.PropertyBag             `json:"$propertyBag,omitempty"`
	ProvisioningState               *string                            `json:"provisioningState,omitempty"`
	PublicNetworkAccessForIngestion *string                            `json:"publicNetworkAccessForIngestion,omitempty"`
	PublicNetworkAccessForQuery     *string                            `json:"publicNetworkAccessForQuery,omitempty"`
	Request_Source                  *string                            `json:"Request_Source,omitempty"`
	RetentionInDays                 *int                               `json:"RetentionInDays,omitempty"`
	SamplingPercentage              *float64                           `json:"SamplingPercentage,omitempty"`
	Tags                            map[string]string                  `json:"tags,omitempty"`
	TenantId                        *string                            `json:"TenantId,omitempty"`
	Type                            *string                            `json:"type,omitempty"`
	WorkspaceResourceId             *string                            `json:"WorkspaceResourceId,omitempty"`
}

var _ genruntime.ConvertibleStatus = &Component_STATUS{}

// ConvertStatusFrom populates our Component_STATUS from the provided source
func (component *Component_STATUS) ConvertStatusFrom(source genruntime.ConvertibleStatus) error {
	if source == component {
		return eris.New("attempted conversion between unrelated implementations of github.com/Azure/azure-service-operator/v2/pkg/genruntime/ConvertibleStatus")
	}

	return source.ConvertStatusTo(component)
}

// ConvertStatusTo populates the provided destination from our Component_STATUS
func (component *Component_STATUS) ConvertStatusTo(destination genruntime.ConvertibleStatus) error {
	if destination == component {
		return eris.New("attempted conversion between unrelated implementations of github.com/Azure/azure-service-operator/v2/pkg/genruntime/ConvertibleStatus")
	}

	return destination.ConvertStatusFrom(component)
}

// Storage version of v1api20200202.ComponentOperatorSpec
// Details for configuring operator behavior. Fields in this struct are interpreted by the operator directly rather than being passed to Azure
type ComponentOperatorSpec struct {
	ConfigMapExpressions []*core.DestinationExpression `json:"configMapExpressions,omitempty"`
	ConfigMaps           *ComponentOperatorConfigMaps  `json:"configMaps,omitempty"`
	PropertyBag          genruntime.PropertyBag        `json:"$propertyBag,omitempty"`
	SecretExpressions    []*core.DestinationExpression `json:"secretExpressions,omitempty"`
}

// Storage version of v1api20200202.PrivateLinkScopedResource_STATUS
// The private link scope resource reference.
type PrivateLinkScopedResource_STATUS struct {
	PropertyBag genruntime.PropertyBag `json:"$propertyBag,omitempty"`
	ResourceId  *string                `json:"ResourceId,omitempty"`
	ScopeId     *string                `json:"ScopeId,omitempty"`
}

// AssignProperties_From_PrivateLinkScopedResource_STATUS populates our PrivateLinkScopedResource_STATUS from the provided source PrivateLinkScopedResource_STATUS
func (resource *PrivateLinkScopedResource_STATUS) AssignProperties_From_PrivateLinkScopedResource_STATUS(source *storage.PrivateLinkScopedResource_STATUS) error {
	// Clone the existing property bag
	propertyBag := genruntime.NewPropertyBag(source.PropertyBag)

	// ResourceId
	resource.ResourceId = genruntime.ClonePointerToString(source.ResourceId)

	// ScopeId
	resource.ScopeId = genruntime.ClonePointerToString(source.ScopeId)

	// Update the property bag
	if len(propertyBag) > 0 {
		resource.PropertyBag = propertyBag
	} else {
		resource.PropertyBag = nil
	}

	// Invoke the augmentConversionForPrivateLinkScopedResource_STATUS interface (if implemented) to customize the conversion
	var resourceAsAny any = resource
	if augmentedResource, ok := resourceAsAny.(augmentConversionForPrivateLinkScopedResource_STATUS); ok {
		err := augmentedResource.AssignPropertiesFrom(source)
		if err != nil {
			return eris.Wrap(err, "calling augmented AssignPropertiesFrom() for conversion")
		}
	}

	// No error
	return nil
}

// AssignProperties_To_PrivateLinkScopedResource_STATUS populates the provided destination PrivateLinkScopedResource_STATUS from our PrivateLinkScopedResource_STATUS
func (resource *PrivateLinkScopedResource_STATUS) AssignProperties_To_PrivateLinkScopedResource_STATUS(destination *storage.PrivateLinkScopedResource_STATUS) error {
	// Clone the existing property bag
	propertyBag := genruntime.NewPropertyBag(resource.PropertyBag)

	// ResourceId
	destination.ResourceId = genruntime.ClonePointerToString(resource.ResourceId)

	// ScopeId
	destination.ScopeId = genruntime.ClonePointerToString(resource.ScopeId)

	// Update the property bag
	if len(propertyBag) > 0 {
		destination.PropertyBag = propertyBag
	} else {
		destination.PropertyBag = nil
	}

	// Invoke the augmentConversionForPrivateLinkScopedResource_STATUS interface (if implemented) to customize the conversion
	var resourceAsAny any = resource
	if augmentedResource, ok := resourceAsAny.(augmentConversionForPrivateLinkScopedResource_STATUS); ok {
		err := augmentedResource.AssignPropertiesTo(destination)
		if err != nil {
			return eris.Wrap(err, "calling augmented AssignPropertiesTo() for conversion")
		}
	}

	// No error
	return nil
}

type augmentConversionForPrivateLinkScopedResource_STATUS interface {
	AssignPropertiesFrom(src *storage.PrivateLinkScopedResource_STATUS) error
	AssignPropertiesTo(dst *storage.PrivateLinkScopedResource_STATUS) error
}

// Storage version of v1api20200202.ComponentOperatorConfigMaps
type ComponentOperatorConfigMaps struct {
	ConnectionString   *genruntime.ConfigMapDestination `json:"connectionString,omitempty"`
	InstrumentationKey *genruntime.ConfigMapDestination `json:"instrumentationKey,omitempty"`
	PropertyBag        genruntime.PropertyBag           `json:"$propertyBag,omitempty"`
}

func init() {
	SchemeBuilder.Register(&Component{}, &ComponentList{})
}
