github.com/diggerhq/digger/libs@v0.0.0-20240604170430-9d61cdf01cc5/orchestrator/models.go (about)

     1  package orchestrator
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  	"slices"
     7  
     8  	"github.com/aws/aws-sdk-go-v2/credentials/stscreds"
     9  	"github.com/diggerhq/digger/libs/digger_config"
    10  	configuration "github.com/diggerhq/digger/libs/digger_config"
    11  )
    12  
    13  type Job struct {
    14  	ProjectName        string
    15  	ProjectDir         string
    16  	ProjectWorkspace   string
    17  	ProjectWorkflow    string
    18  	Terragrunt         bool
    19  	OpenTofu           bool
    20  	Commands           []string
    21  	ApplyStage         *Stage
    22  	PlanStage          *Stage
    23  	PullRequestNumber  *int
    24  	EventName          string
    25  	RequestedBy        string
    26  	Namespace          string
    27  	RunEnvVars         map[string]string
    28  	StateEnvVars       map[string]string
    29  	CommandEnvVars     map[string]string
    30  	StateEnvProvider   *stscreds.WebIdentityRoleProvider
    31  	CommandEnvProvider *stscreds.WebIdentityRoleProvider
    32  }
    33  
    34  type Step struct {
    35  	Action    string
    36  	Value     string
    37  	ExtraArgs []string
    38  	Shell     string
    39  }
    40  
    41  type Stage struct {
    42  	Steps []Step
    43  }
    44  
    45  func ToConfigStep(configState configuration.Step) Step {
    46  	return Step{
    47  		Action:    configState.Action,
    48  		Value:     configState.Value,
    49  		ExtraArgs: configState.ExtraArgs,
    50  		Shell:     configState.Shell,
    51  	}
    52  
    53  }
    54  
    55  func ToConfigStage(configStage *configuration.Stage) *Stage {
    56  	if configStage == nil {
    57  		return nil
    58  	}
    59  	steps := make([]Step, 0)
    60  	for _, step := range configStage.Steps {
    61  		steps = append(steps, ToConfigStep(step))
    62  	}
    63  	return &Stage{
    64  		Steps: steps,
    65  	}
    66  }
    67  
    68  func (j *Job) IsPlan() bool {
    69  	return slices.Contains(j.Commands, "digger plan")
    70  }
    71  
    72  func (j *Job) IsApply() bool {
    73  	return slices.Contains(j.Commands, "digger apply")
    74  }
    75  
    76  func IsPlanJobs(jobs []Job) bool {
    77  	isPlan := true
    78  	for _, job := range jobs {
    79  		isPlan = isPlan && job.IsPlan()
    80  	}
    81  	return isPlan
    82  }
    83  
    84  func IsApplyJobs(jobs []JobJson) bool {
    85  	isApply := true
    86  	for _, job := range jobs {
    87  		isApply = isApply && job.IsApply()
    88  	}
    89  	return isApply
    90  }
    91  
    92  func ConvertProjectsToJobs(actor string, repoNamespace string, command string, prNumber int, impactedProjects []digger_config.Project, requestedProject *digger_config.Project, workflows map[string]digger_config.Workflow) ([]Job, bool, error) {
    93  	jobs := make([]Job, 0)
    94  
    95  	log.Printf("ConvertToCommands, command: %s\n", command)
    96  	for _, project := range impactedProjects {
    97  		workflow, ok := workflows[project.Workflow]
    98  		if !ok {
    99  			return nil, true, fmt.Errorf("failed to find workflow digger_config '%s' for project '%s'", project.Workflow, project.Name)
   100  		}
   101  
   102  		stateEnvVars, commandEnvVars := digger_config.CollectTerraformEnvConfig(workflow.EnvVars)
   103  		StateEnvProvider, CommandEnvProvider := GetStateAndCommandProviders(project)
   104  		jobs = append(jobs, Job{
   105  			ProjectName:      project.Name,
   106  			ProjectDir:       project.Dir,
   107  			ProjectWorkspace: project.Workspace,
   108  			Terragrunt:       project.Terragrunt,
   109  			OpenTofu:         project.OpenTofu,
   110  			// TODO: expose lower level api per command configuration
   111  			Commands:   []string{command},
   112  			ApplyStage: ToConfigStage(workflow.Apply),
   113  			PlanStage:  ToConfigStage(workflow.Plan),
   114  			// TODO:
   115  			PullRequestNumber:  &prNumber,
   116  			EventName:          "manual_run",
   117  			RequestedBy:        actor,
   118  			Namespace:          repoNamespace,
   119  			StateEnvVars:       stateEnvVars,
   120  			CommandEnvVars:     commandEnvVars,
   121  			StateEnvProvider:   StateEnvProvider,
   122  			CommandEnvProvider: CommandEnvProvider,
   123  		})
   124  	}
   125  	return jobs, true, nil
   126  }