-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
update importer to output .tf code files for job
- Loading branch information
Showing
31 changed files
with
777 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,6 @@ sonar/ | |
|
||
.envrc | ||
terraform-provider-jenkins | ||
importer | ||
/importer/importer | ||
report.json | ||
coverage.out |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
|
||
"github.com/jgramoll/terraform-provider-jenkins/client" | ||
) | ||
|
||
type GenerateTerraformCodeService struct { | ||
jobService *client.JobService | ||
} | ||
|
||
func NewGenerateTerraformCodeService(jobService *client.JobService) *GenerateTerraformCodeService { | ||
return &GenerateTerraformCodeService{ | ||
jobService: jobService, | ||
} | ||
} | ||
|
||
func (s *GenerateTerraformCodeService) GenerateCode(job *client.Job, outputDir string) error { | ||
if err := ensureOutputDir(outputDir); err != nil { | ||
return err | ||
} | ||
|
||
if err := s.generateProviderCode(outputDir); err != nil { | ||
return err | ||
} | ||
|
||
if err := s.generatePipelineCode(outputDir, job); err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (s *GenerateTerraformCodeService) generateProviderCode(outputDir string) error { | ||
tfCodeFile, err := os.Create(fmt.Sprintf("%s/provider.tf", outputDir)) | ||
if err != nil { | ||
return err | ||
} | ||
defer tfCodeFile.Close() | ||
|
||
_, err = tfCodeFile.Write([]byte(providerCode(s.jobService.Config.Address))) | ||
return err | ||
} | ||
|
||
func (s *GenerateTerraformCodeService) generatePipelineCode(outputDir string, job *client.Job) error { | ||
tfCodeFile, err := os.Create(fmt.Sprintf("%s/pipeline.tf", outputDir)) | ||
if err != nil { | ||
return err | ||
} | ||
defer tfCodeFile.Close() | ||
|
||
_, err = tfCodeFile.Write([]byte(jobCode(job))) | ||
return err | ||
} | ||
|
||
func ensureOutputDir(outputDir string) error { | ||
if err := os.Mkdir(outputDir, os.ModePerm); err != nil { | ||
if os.IsNotExist(err) { | ||
return err | ||
} | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
importer/resource_git_scm_clean_before_checkout_extension.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,17 @@ | ||
package main | ||
|
||
import ( | ||
"github.com/jgramoll/terraform-provider-jenkins/client" | ||
) | ||
|
||
func init() { | ||
jobGitScmExtensionCodeFuncs["*client.GitScmCleanBeforeCheckoutExtension"] = jobGitScmCleanBeforeCheckoutExtensionCode | ||
} | ||
|
||
func jobGitScmCleanBeforeCheckoutExtensionCode(client.GitScmExtension) string { | ||
return ` | ||
resource "jenkins_job_git_scm_clean_before_checkout_extension" "main" { | ||
scm = "${jenkins_job_git_scm.main.id}" | ||
} | ||
` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/google/uuid" | ||
"github.com/jgramoll/terraform-provider-jenkins/client" | ||
) | ||
|
||
func ensureJob(job *client.Job) error { | ||
if job.Id == "" { | ||
id, err := uuid.NewRandom() | ||
if err != nil { | ||
return err | ||
} | ||
job.Id = id.String() | ||
} | ||
if err := ensureJobActions(job.Actions); err != nil { | ||
return err | ||
} | ||
if err := ensureJobProperties(job.Properties); err != nil { | ||
return err | ||
} | ||
return ensureJobDefinition(job.Definition) | ||
} | ||
|
||
func jobCode(job *client.Job) string { | ||
return strings.TrimSpace(fmt.Sprintf(` | ||
resource "jenkins_job" "main" { | ||
name = "%v" | ||
plugin = "%v" | ||
disabled = %v | ||
} | ||
`, job.Name, job.Plugin, job.Disabled)+ | ||
jobActionsCode(job.Actions)+ | ||
jobDefinitionCode(job.Definition)+ | ||
jobPropertiesCode(job.Properties)) + "\n" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package main | ||
|
||
import ( | ||
"github.com/jgramoll/terraform-provider-jenkins/client" | ||
) | ||
|
||
func init() { | ||
jobPropertyCodeFuncs["*client.JobBuildDiscarderProperty"] = jobBuildDiscarderPropertyCode | ||
} | ||
|
||
func jobBuildDiscarderPropertyCode(propertyInterface client.JobProperty) string { | ||
property := propertyInterface.(*client.JobBuildDiscarderProperty) | ||
return ` | ||
resource "jenkins_job_build_discarder_property" "main" { | ||
job = "${jenkins_job.main.id}" | ||
} | ||
` + jobBuildDiscarderPropertyStrategyCode(property.Strategy.Item) | ||
} |
Oops, something went wrong.