func NewPlainPipelineFromBytes()

in tooling/templatize/pkg/pipeline/types.go [42:121]


func NewPlainPipelineFromBytes(filepath string, bytes []byte) (*Pipeline, error) {
	rawPipeline := &struct {
		Schema         string `yaml:"$schema,omitempty"`
		ServiceGroup   string `yaml:"serviceGroup"`
		RolloutName    string `yaml:"rolloutName"`
		ResourceGroups []struct {
			Name         string           `yaml:"name"`
			Subscription string           `yaml:"subscription"`
			AKSCluster   string           `yaml:"aksCluster,omitempty"`
			Steps        []map[string]any `yaml:"steps"`
		} `yaml:"resourceGroups"`
	}{}
	err := yaml.Unmarshal(bytes, rawPipeline)
	if err != nil {
		return nil, fmt.Errorf("failed to unmarshal pipeline: %w", err)
	}

	// find step properties that are variableRefs
	pipelineSchema, _, err := getSchemaForRef(rawPipeline.Schema)
	if err != nil {
		return nil, fmt.Errorf("failed to get schema for pipeline: %w", err)
	}
	variableRefStepProperties, err := getVariableRefStepProperties(pipelineSchema)
	if err != nil {
		return nil, fmt.Errorf("failed to get variableRef step properties: %w", err)
	}

	pipeline := &Pipeline{
		schema:           rawPipeline.Schema,
		pipelineFilePath: filepath,
		ServiceGroup:     rawPipeline.ServiceGroup,
		RolloutName:      rawPipeline.RolloutName,
		ResourceGroups:   make([]*ResourceGroup, len(rawPipeline.ResourceGroups)),
	}

	for i, rawRg := range rawPipeline.ResourceGroups {
		rg := &ResourceGroup{}
		pipeline.ResourceGroups[i] = rg
		rg.Name = rawRg.Name
		rg.Subscription = rawRg.Subscription
		rg.AKSCluster = rawRg.AKSCluster
		rg.Steps = make([]Step, len(rawRg.Steps))
		for i, rawStep := range rawRg.Steps {
			// preprocess variableRef step properties
			for propName := range rawStep {
				if _, ok := variableRefStepProperties[propName]; ok {
					variableRef := rawStep[propName].(map[string]any)
					variableRef["name"] = propName
				}
			}

			// unmarshal the map into a StepMeta
			stepMeta := &StepMeta{}
			err := mapToStruct(rawStep, stepMeta)
			if err != nil {
				return nil, err
			}
			switch stepMeta.Action {
			case "Shell":
				rg.Steps[i] = &ShellStep{}
			case "ARM":
				rg.Steps[i] = &ARMStep{}
			default:
				rg.Steps[i] = &GenericStep{}
			}
			err = mapToStruct(rawStep, rg.Steps[i])
			if err != nil {
				return nil, err
			}
		}
	}

	// another round of validation after postprocessing
	err = ValidatePipelineSchemaForStruct(pipeline)
	if err != nil {
		return nil, fmt.Errorf("pipeline schema validation failed after postprocessing: %w", err)
	}

	return pipeline, nil
}