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

     1  package orchestrator
     2  
     3  import (
     4  	"slices"
     5  
     6  	"github.com/diggerhq/digger/libs/digger_config"
     7  )
     8  
     9  type StepJson struct {
    10  	Action    string   `json:"action"`
    11  	Value     string   `json:"value"`
    12  	ExtraArgs []string `json:"extraArgs"`
    13  	Shell     string   `json:"shell"`
    14  }
    15  
    16  type StageJson struct {
    17  	Steps []StepJson `json:"steps"`
    18  }
    19  
    20  type JobJson struct {
    21  	JobType                 string            `json:"job_type"`
    22  	ProjectName             string            `json:"projectName"`
    23  	ProjectDir              string            `json:"projectDir"`
    24  	ProjectWorkspace        string            `json:"projectWorkspace"`
    25  	Terragrunt              bool              `json:"terragrunt"`
    26  	OpenTofu                bool              `json:"opentofu"`
    27  	Commands                []string          `json:"commands"`
    28  	ApplyStage              StageJson         `json:"applyStage"`
    29  	PlanStage               StageJson         `json:"planStage"`
    30  	PullRequestNumber       *int              `json:"pullRequestNumber"`
    31  	Commit                  string            `json:"commit"`
    32  	Branch                  string            `json:"branch"`
    33  	EventName               string            `json:"eventName"`
    34  	RequestedBy             string            `json:"requestedBy"`
    35  	Namespace               string            `json:"namespace"`
    36  	RunEnvVars              map[string]string `json:"runEnvVars"`
    37  	StateEnvVars            map[string]string `json:"stateEnvVars"`
    38  	CommandEnvVars          map[string]string `json:"commandEnvVars"`
    39  	AwsRoleRegion           string            `json:"aws_role_region"`
    40  	StateRoleName           string            `json:"state_role_name"`
    41  	CommandRoleName         string            `json:"command_role_name"`
    42  	BackendHostname         string            `json:"backend_hostname"`
    43  	BackendOrganisationName string            `json:"backend_organisation_hostname"`
    44  	BackendJobToken         string            `json:"backend_job_token"`
    45  }
    46  
    47  func (j *JobJson) IsPlan() bool {
    48  	return slices.Contains(j.Commands, "digger plan")
    49  }
    50  
    51  func (j *JobJson) IsApply() bool {
    52  	return slices.Contains(j.Commands, "digger apply")
    53  }
    54  
    55  func JobToJson(job Job, jobType DiggerCommand, organisationName string, branch string, commitSha string, jobToken string, backendHostname string, project digger_config.Project) JobJson {
    56  	stateRole, commandRole, region := "", "", ""
    57  
    58  	if project.AwsRoleToAssume != nil {
    59  		region = project.AwsRoleToAssume.AwsRoleRegion
    60  		stateRole = project.AwsRoleToAssume.State
    61  		commandRole = project.AwsRoleToAssume.Command
    62  	}
    63  	return JobJson{
    64  		JobType:                 string(jobType),
    65  		ProjectName:             job.ProjectName,
    66  		ProjectDir:              job.ProjectDir,
    67  		ProjectWorkspace:        job.ProjectWorkspace,
    68  		OpenTofu:                job.OpenTofu,
    69  		Terragrunt:              job.Terragrunt,
    70  		Commands:                job.Commands,
    71  		ApplyStage:              stageToJson(job.ApplyStage),
    72  		PlanStage:               stageToJson(job.PlanStage),
    73  		PullRequestNumber:       job.PullRequestNumber,
    74  		Commit:                  commitSha,
    75  		Branch:                  branch,
    76  		EventName:               job.EventName,
    77  		RequestedBy:             job.RequestedBy,
    78  		Namespace:               job.Namespace,
    79  		RunEnvVars:              job.RunEnvVars,
    80  		StateEnvVars:            job.StateEnvVars,
    81  		CommandEnvVars:          job.CommandEnvVars,
    82  		AwsRoleRegion:           region,
    83  		StateRoleName:           stateRole,
    84  		CommandRoleName:         commandRole,
    85  		BackendHostname:         backendHostname,
    86  		BackendJobToken:         jobToken,
    87  		BackendOrganisationName: organisationName,
    88  	}
    89  }
    90  
    91  func JsonToJob(jobJson JobJson) Job {
    92  	return Job{
    93  		ProjectName:        jobJson.ProjectName,
    94  		ProjectDir:         jobJson.ProjectDir,
    95  		ProjectWorkspace:   jobJson.ProjectWorkspace,
    96  		OpenTofu:           jobJson.OpenTofu,
    97  		Terragrunt:         jobJson.Terragrunt,
    98  		Commands:           jobJson.Commands,
    99  		ApplyStage:         jsonToStage(jobJson.ApplyStage),
   100  		PlanStage:          jsonToStage(jobJson.PlanStage),
   101  		PullRequestNumber:  jobJson.PullRequestNumber,
   102  		EventName:          jobJson.EventName,
   103  		RequestedBy:        jobJson.RequestedBy,
   104  		Namespace:          jobJson.Namespace,
   105  		RunEnvVars:         jobJson.RunEnvVars,
   106  		StateEnvVars:       jobJson.StateEnvVars,
   107  		CommandEnvVars:     jobJson.CommandEnvVars,
   108  		StateEnvProvider:   GetProviderFromRole(jobJson.StateRoleName, jobJson.AwsRoleRegion),
   109  		CommandEnvProvider: GetProviderFromRole(jobJson.CommandRoleName, jobJson.AwsRoleRegion),
   110  	}
   111  }
   112  
   113  func jsonToStage(stageJson StageJson) *Stage {
   114  	if len(stageJson.Steps) == 0 {
   115  		return nil
   116  	}
   117  	steps := make([]Step, len(stageJson.Steps))
   118  	for i, step := range stageJson.Steps {
   119  		steps[i] = Step{
   120  			Action:    step.Action,
   121  			Value:     step.Value,
   122  			ExtraArgs: step.ExtraArgs,
   123  			Shell:     step.Shell,
   124  		}
   125  	}
   126  	return &Stage{
   127  		Steps: steps,
   128  	}
   129  }
   130  
   131  func stageToJson(stage *Stage) StageJson {
   132  	if stage == nil {
   133  		return StageJson{}
   134  	}
   135  	steps := make([]StepJson, len(stage.Steps))
   136  	for i, step := range stage.Steps {
   137  		steps[i] = StepJson{
   138  			Action:    step.Action,
   139  			Value:     step.Value,
   140  			ExtraArgs: step.ExtraArgs,
   141  			Shell:     step.Shell,
   142  		}
   143  	}
   144  	return StageJson{
   145  		Steps: steps,
   146  	}
   147  }
   148  
   149  func IsPlanJobSpecs(jobs []JobJson) bool {
   150  	isPlan := true
   151  	for _, job := range jobs {
   152  		isPlan = isPlan && job.IsPlan()
   153  	}
   154  	return isPlan
   155  }
   156  
   157  func IsApplyJobSpecs(jobs []JobJson) bool {
   158  	isApply := true
   159  	for _, job := range jobs {
   160  		isApply = isApply && job.IsApply()
   161  	}
   162  	return isApply
   163  }
   164  
   165  func JobsSpecsToProjectMap(jobSpecs []JobJson) (map[string]JobJson, error) {
   166  	res := make(map[string]JobJson)
   167  	for _, jobSpec := range jobSpecs {
   168  		res[jobSpec.ProjectName] = jobSpec
   169  	}
   170  	return res, nil
   171  }