func (factory *BuildFactory) constructPlanFromConfig( planConfig atc.PlanConfig, resources atc.ResourceConfigs, inputs []db.BuildInput, hasHooks bool, ) atc.Plan { var plan atc.Plan switch { case planConfig.Do != nil: if hasHooks { plan = factory.constructPlanHookBasedPlan( *planConfig.Do, resources, inputs, ) } else { plan = factory.constructPlanSequenceBasedPlan( *planConfig.Do, resources, inputs, ) } if plan.Location == nil { plan.Location = planConfig.Location } case planConfig.Put != "": logicalName := planConfig.Put resourceName := planConfig.Resource if resourceName == "" { resourceName = logicalName } resource, _ := resources.Lookup(resourceName) putPlan := &atc.PutPlan{ Type: resource.Type, Name: logicalName, Pipeline: factory.PipelineName, Resource: resourceName, Source: resource.Source, Params: planConfig.Params, Tags: planConfig.Tags, } dependentGetPlan := &atc.DependentGetPlan{ Type: resource.Type, Name: logicalName, Pipeline: factory.PipelineName, Resource: resourceName, Params: planConfig.GetParams, Tags: planConfig.Tags, Source: resource.Source, } stepLocation := &atc.Location{} nextLocation := &atc.Location{} if planConfig.Location != nil { stepLocation.ID = planConfig.Location.ID stepLocation.Hook = planConfig.Location.Hook if planConfig.Location.ParallelGroup != 0 { stepLocation.ParallelGroup = planConfig.Location.ParallelGroup } else { stepLocation.ParentID = planConfig.Location.ParentID } nextLocation.ID = stepLocation.ID + 1 nextLocation.ParentID = stepLocation.ID } plan = atc.Plan{ // Location: planConfig.Location, OnSuccess: &atc.OnSuccessPlan{ Step: atc.Plan{ Location: stepLocation, Put: putPlan, }, Next: atc.Plan{ Location: nextLocation, DependentGet: dependentGetPlan, }, }, } case planConfig.Get != "": resourceName := planConfig.Resource if resourceName == "" { resourceName = planConfig.Get } resource, _ := resources.Lookup(resourceName) name := planConfig.Get var version db.Version for _, input := range inputs { if input.Name == name { version = input.Version break } } plan = atc.Plan{ Location: planConfig.Location, Get: &atc.GetPlan{ Type: resource.Type, Name: name, Pipeline: factory.PipelineName, Resource: resourceName, Source: resource.Source, Params: planConfig.Params, Version: atc.Version(version), Tags: planConfig.Tags, }, } case planConfig.Task != "": plan = atc.Plan{ Location: planConfig.Location, Task: &atc.TaskPlan{ Name: planConfig.Task, Privileged: planConfig.Privileged, Config: planConfig.TaskConfig, ConfigPath: planConfig.TaskConfigPath, Tags: planConfig.Tags, }, } case planConfig.Try != nil: nextStep := factory.constructPlanFromConfig( *planConfig.Try, resources, inputs, hasHooks) plan = atc.Plan{ Location: planConfig.Location, Try: &atc.TryPlan{ Step: nextStep, }, } case planConfig.Aggregate != nil: aggregate := atc.AggregatePlan{} for _, planConfig := range *planConfig.Aggregate { nextStep := factory.constructPlanFromConfig( planConfig, resources, inputs, hasHooks) aggregate = append(aggregate, nextStep) } plan = atc.Plan{ Location: planConfig.Location, Aggregate: &aggregate, } } if planConfig.Conditions != nil { plan = atc.Plan{ Conditional: &atc.ConditionalPlan{ Conditions: *planConfig.Conditions, Plan: plan, }, } } if planConfig.Timeout != "" { plan = atc.Plan{ Timeout: &atc.TimeoutPlan{ Duration: planConfig.Timeout, Step: plan, }, } } constructionParams := factory.ensureIfPresent(factory.successIfPresent(factory.failureIfPresent( constructionParams{ plan: plan, planConfig: planConfig, resources: resources, inputs: inputs, hasHooks: hasHooks, })), ) return constructionParams.plan }
func (factory *BuildFactory) constructPlanFromConfig( planConfig atc.PlanConfig, resources atc.ResourceConfigs, inputs []db.BuildInput, hasHooks bool, ) atc.Plan { var plan atc.Plan switch { case planConfig.Do != nil: if hasHooks { plan = factory.constructPlanHookBasedPlan( *planConfig.Do, resources, inputs, ) } else { plan = factory.constructPlanSequenceBasedPlan( *planConfig.Do, resources, inputs, ) } case planConfig.Put != "": logicalName := planConfig.Put resourceName := planConfig.Resource if resourceName == "" { resourceName = logicalName } resource, _ := resources.Lookup(resourceName) putPlan := &atc.PutPlan{ Type: resource.Type, Name: logicalName, Pipeline: factory.PipelineName, Resource: resourceName, Source: resource.Source, Params: planConfig.Params, GetParams: planConfig.GetParams, Tags: planConfig.Tags, } plan = atc.Plan{ PutGet: &atc.PutGetPlan{ Head: atc.Plan{ Put: putPlan, }, Rest: atc.Plan{}, }, } case planConfig.Get != "": resourceName := planConfig.Resource if resourceName == "" { resourceName = planConfig.Get } resource, _ := resources.Lookup(resourceName) name := planConfig.Get var version db.Version for _, input := range inputs { if input.Name == name { version = input.Version break } } plan = atc.Plan{ Get: &atc.GetPlan{ Type: resource.Type, Name: name, Pipeline: factory.PipelineName, Resource: resourceName, Source: resource.Source, Params: planConfig.Params, Version: atc.Version(version), Tags: planConfig.Tags, }, } case planConfig.Task != "": plan = atc.Plan{ Task: &atc.TaskPlan{ Name: planConfig.Task, Privileged: planConfig.Privileged, Config: planConfig.TaskConfig, ConfigPath: planConfig.TaskConfigPath, Tags: planConfig.Tags, }, } case planConfig.Try != nil: plan = atc.Plan{ Try: &atc.TryPlan{ Step: factory.constructPlanFromConfig( *planConfig.Try, resources, inputs, hasHooks, ), }, } case planConfig.Aggregate != nil: aggregate := atc.AggregatePlan{} for _, planConfig := range *planConfig.Aggregate { aggregate = append(aggregate, factory.constructPlanFromConfig( planConfig, resources, inputs, hasHooks, )) } plan = atc.Plan{ Aggregate: &aggregate, } } if planConfig.Conditions != nil { plan = atc.Plan{ Conditional: &atc.ConditionalPlan{ Conditions: *planConfig.Conditions, Plan: plan, }, } } if planConfig.Timeout != 0 { plan = atc.Plan{ Timeout: &atc.TimeoutPlan{ Duration: planConfig.Timeout, Step: plan, }, } } hooks := false failurePlan := atc.Plan{} if planConfig.Failure != nil { hooks = true failurePlan = factory.constructPlanFromConfig(*planConfig.Failure, resources, inputs, hasHooks) } ensurePlan := atc.Plan{} if planConfig.Ensure != nil { hooks = true ensurePlan = factory.constructPlanFromConfig(*planConfig.Ensure, resources, inputs, hasHooks) } successPlan := atc.Plan{} if planConfig.Success != nil { hooks = true successPlan = factory.constructPlanFromConfig(*planConfig.Success, resources, inputs, hasHooks) } if hooks { plan = atc.Plan{ HookedCompose: &atc.HookedComposePlan{ Step: plan, OnFailure: failurePlan, OnCompletion: ensurePlan, OnSuccess: successPlan, }, } } return plan }
func (factory *buildFactory) constructUnhookedPlan( planConfig atc.PlanConfig, resources atc.ResourceConfigs, resourceTypes atc.ResourceTypes, inputs []db.BuildInput, ) (atc.Plan, error) { var plan atc.Plan var err error switch { case planConfig.Do != nil: plan, err = factory.do( *planConfig.Do, resources, resourceTypes, inputs, ) if err != nil { return atc.Plan{}, err } case planConfig.Put != "": logicalName := planConfig.Put resourceName := planConfig.Resource if resourceName == "" { resourceName = logicalName } resource, found := resources.Lookup(resourceName) if !found { return atc.Plan{}, ErrResourceNotFound } putPlan := atc.PutPlan{ Type: resource.Type, Name: logicalName, PipelineID: factory.PipelineID, Resource: resourceName, Source: resource.Source, Params: planConfig.Params, Tags: planConfig.Tags, ResourceTypes: resourceTypes, } dependentGetPlan := atc.DependentGetPlan{ Type: resource.Type, Name: logicalName, PipelineID: factory.PipelineID, Resource: resourceName, Params: planConfig.GetParams, Tags: planConfig.Tags, Source: resource.Source, ResourceTypes: resourceTypes, } plan = factory.planFactory.NewPlan(atc.OnSuccessPlan{ Step: factory.planFactory.NewPlan(putPlan), Next: factory.planFactory.NewPlan(dependentGetPlan), }) case planConfig.Get != "": resourceName := planConfig.Resource if resourceName == "" { resourceName = planConfig.Get } resource, found := resources.Lookup(resourceName) if !found { return atc.Plan{}, ErrResourceNotFound } name := planConfig.Get var version db.Version for _, input := range inputs { if input.Name == name { version = input.Version break } } plan = factory.planFactory.NewPlan(atc.GetPlan{ Type: resource.Type, Name: name, PipelineID: factory.PipelineID, Resource: resourceName, Source: resource.Source, Params: planConfig.Params, Version: atc.Version(version), Tags: planConfig.Tags, ResourceTypes: resourceTypes, }) case planConfig.Task != "": plan = factory.planFactory.NewPlan(atc.TaskPlan{ Name: planConfig.Task, PipelineID: factory.PipelineID, Privileged: planConfig.Privileged, Config: planConfig.TaskConfig, ConfigPath: planConfig.TaskConfigPath, Tags: planConfig.Tags, ResourceTypes: resourceTypes, Params: planConfig.Params, InputMapping: planConfig.InputMapping, OutputMapping: planConfig.OutputMapping, }) case planConfig.Try != nil: nextStep, err := factory.constructPlanFromConfig( *planConfig.Try, resources, resourceTypes, inputs, ) if err != nil { return atc.Plan{}, err } plan = factory.planFactory.NewPlan(atc.TryPlan{ Step: nextStep, }) case planConfig.Aggregate != nil: aggregate := atc.AggregatePlan{} for _, planConfig := range *planConfig.Aggregate { nextStep, err := factory.constructPlanFromConfig( planConfig, resources, resourceTypes, inputs, ) if err != nil { return atc.Plan{}, err } aggregate = append(aggregate, nextStep) } plan = factory.planFactory.NewPlan(aggregate) } if planConfig.Timeout != "" { plan = factory.planFactory.NewPlan(atc.TimeoutPlan{ Duration: planConfig.Timeout, Step: plan, }) } return plan, nil }