/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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.
 */

package v1alpha1

import (
	"sigs.k8s.io/yaml"

	"github.com/apache/dubbo-admin/pkg/common/constants"
	"github.com/apache/dubbo-admin/pkg/core/logger"
	coremodel "github.com/apache/dubbo-admin/pkg/core/resource/model"
)

func BuildConditionRouteResName(scopeEntity string) string {
	return scopeEntity + constants.ConditionRuleDotSuffix
}

func ToConditionRouteResource(mesh, name, data string) coremodel.Resource {
	res := NewConditionRouteResourceWithAttributes(name, mesh)
	err := yaml.Unmarshal([]byte(data), res.Spec)
	if err != nil {
		logger.Warnf("cannot unmarshal condition route %s in %s, cause: %s, raw content:\n %s, ", name, mesh, err, data)
	}
	return res
}
