Skip to content

Commit

Permalink
Merge pull request #99 from cvbarros/cvbarros/97/template-build-count…
Browse files Browse the repository at this point in the history
…er-bug

(fix):#97 disregard build_counter when using build config template
  • Loading branch information
cvbarros authored Oct 14, 2020
2 parents 2ecd449 + 2810704 commit 9ed994e
Show file tree
Hide file tree
Showing 2 changed files with 149 additions and 20 deletions.
42 changes: 29 additions & 13 deletions teamcity/resource_build_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package teamcity

import (
"bytes"
"errors"
"fmt"
"log"
"reflect"
Expand Down Expand Up @@ -56,7 +57,7 @@ func resourceBuildConfig() *schema.Resource {
}
if setComputed {
computed := flattenBuildConfigOptionsRaw(nsi)
diff.SetNew("settings", []map[string]interface{}{computed})
_ = diff.SetNew("settings", []map[string]interface{}{computed})
}
}
}
Expand Down Expand Up @@ -280,6 +281,7 @@ func resourceBuildConfigCreate(d *schema.ResourceData, meta interface{}) error {
}
if opt != nil {
bt.Options = opt
opt.Template = isTemplate
}

created, err := client.BuildTypes.Create(projectID, bt)
Expand All @@ -292,7 +294,6 @@ func resourceBuildConfigCreate(d *schema.ResourceData, meta interface{}) error {

d.MarkNewResource()
d.SetId(created.ID)
d.Partial(true)

log.Printf("[DEBUG] resourceBuildConfigCreate: initial creation finished. Calling resourceBuildConfigUpdate to update the rest of resource.")

Expand All @@ -308,6 +309,13 @@ func resourceBuildConfigUpdate(d *schema.ResourceData, meta interface{}) error {
return err
}

if d.HasChange("is_template") {
err := validateBuildConfig(d)
if err != nil {
return err
}
}

var changed bool
if d.HasChange("sys_params") || d.HasChange("config_params") || d.HasChange("env_params") {
log.Printf("[DEBUG] resourceBuildConfigUpdate: change detected for params")
Expand All @@ -325,20 +333,22 @@ func resourceBuildConfigUpdate(d *schema.ResourceData, meta interface{}) error {
}
}
if d.HasChange("settings") {
isTemplate := false
if v, ok := d.GetOk("is_template"); ok {
log.Printf("[DEBUG] resourceBuildConfigCreate: setting is_template = '%v'.", v.(bool))
isTemplate = v.(bool)
}

log.Printf("[DEBUG] resourceBuildConfigUpdate: change detected for settings")
if _, ok := d.GetOk("settings"); ok {
dt.Options, err = expandBuildConfigOptions(d)
dt.Options.Template = isTemplate
changed = true
}
}

if changed {
_, err := client.BuildTypes.Update(dt)
d.SetPartial("settings")
d.SetPartial("description")
d.SetPartial("config_params")
d.SetPartial("sys_params")
d.SetPartial("env_params")
if err != nil {
return err
}
Expand All @@ -355,7 +365,6 @@ func resourceBuildConfigUpdate(d *schema.ResourceData, meta interface{}) error {
}
log.Printf("[DEBUG] resourceBuildConfigUpdate: attached vcsRoot '%v' to build configuration", toAttach.ID)
}
d.SetPartial("vcs_root")
}

if d.HasChange("step") {
Expand Down Expand Up @@ -386,7 +395,6 @@ func resourceBuildConfigUpdate(d *schema.ResourceData, meta interface{}) error {
}
}
}
d.SetPartial("step")
}

if d.HasChange("templates") {
Expand All @@ -407,10 +415,8 @@ func resourceBuildConfigUpdate(d *schema.ResourceData, meta interface{}) error {
}
log.Printf("[DEBUG] resourceBuildConfigUpdate: detached template '%v' from build configuration", r)
}
d.SetPartial("templates")
}

d.Partial(false)
log.Printf("[DEBUG] resourceBuildConfigUpdate: updated finished. Calling 'read' to refresh state.")
return resourceBuildConfigRead(d, meta)
}
Expand Down Expand Up @@ -501,9 +507,19 @@ func validateBuildConfig(d *schema.ResourceData) error {
isTemplate := v.(bool)

if isTemplate {
if _, isSet := d.GetOkExists("description"); isSet {
if _, ok := d.GetOk("description"); ok {
return fmt.Errorf("'description' field is not supported for Build Configuration Templates. See issue https://youtrack.jetbrains.com/issue/TW-63617 for details")
}
if _, ok := d.GetOk("settings"); ok {
opt, err := expandBuildConfigOptions(d)
if err != nil {
return err
}
// If there's build counter specified in the configuration
if opt.BuildCounter != 0 {
return errors.New("'settings.build_counter' field is not supported for Build Configuration Templates, is_template = true")
}
}
}
}
return nil
Expand Down Expand Up @@ -1027,7 +1043,7 @@ func resourceBuildConfigInstanceResourceV0() *schema.Resource {
}
}

func resourceBuildConfigInstanceStateUpgradeV0(rawState map[string]interface{}, meta interface{}) (map[string]interface{}, error) {
func resourceBuildConfigInstanceStateUpgradeV0(rawState map[string]interface{}, _ interface{}) (map[string]interface{}, error) {
if raw, ok := rawState["steps"]; ok {
s := raw.(*schema.Set)
list := s.List()
Expand Down
127 changes: 120 additions & 7 deletions teamcity/resource_build_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,27 @@ func TestAccBuildConfig_Basic(t *testing.T) {
})
}

func TestAccBuildConfig_Template(t *testing.T) {
var bc api.BuildType
resName := "teamcity_build_config.build_configuration_test"
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckBuildConfigDestroy,
Steps: []resource.TestStep{
{
Config: TestAccBuildConfigTemplate,
Check: resource.ComposeTestCheckFunc(
testAccCheckBuildConfigExists(resName, &bc),
resource.TestCheckResourceAttr(resName, "name", "build config template"),
resource.TestCheckResourceAttr(resName, "project_id", "BuildConfigProjectTest"),
resource.TestCheckResourceAttr(resName, "is_template", "true"),
),
},
},
})
}

func TestAccBuildConfig_TemplateDoesNotSupportDescription(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Expand All @@ -47,21 +68,48 @@ func TestAccBuildConfig_TemplateDoesNotSupportDescription(t *testing.T) {
})
}

func TestAccBuildConfig_Template(t *testing.T) {
func TestAccBuildConfig_TemplateDisregardBuildCounter(t *testing.T) {
var bc api.BuildType
resName := "teamcity_build_config.build_configuration_test"
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckBuildConfigDestroy,
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: TestAccBuildConfigTemplate,
Config: TestAccBuildConfigTemplateBuildCounter,
Check: resource.ComposeTestCheckFunc(
testAccCheckBuildConfigExists(resName, &bc),
resource.TestCheckResourceAttr(resName, "name", "build config template"),
resource.TestCheckResourceAttr(resName, "project_id", "BuildConfigProjectTest"),
resource.TestCheckNoResourceAttr(resName, "settings.0.build_counter"),
),
},
},
})
}

func TestAccBuildConfig_ChangeToTemplate(t *testing.T) {
var bc api.BuildType
resName := "teamcity_build_config.build_configuration_test"
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: TestAccBuildConfigChangeToTemplate,
Check: resource.ComposeTestCheckFunc(
testAccCheckBuildConfigExists(resName, &bc),
resource.TestCheckResourceAttr(resName, "is_template", "false"),
),
Destroy: false,
},
{
Config: TestAccBuildConfigChangeToTemplateUpdatedError,
ExpectError: regexp.MustCompile("'settings.build_counter' field is not supported for Build Configuration Templates, is_template = true"),
},
{
Config: TestAccBuildConfigChangeToTemplateUpdated,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(resName, "is_template", "true"),
resource.TestCheckNoResourceAttr(resName, "settings.0.build_counter"),
),
},
},
Expand Down Expand Up @@ -843,6 +891,71 @@ resource "teamcity_build_config" "build_configuration_test" {
}
`

const TestAccBuildConfigTemplateBuildCounter = `
resource "teamcity_project" "build_config_project_test" {
name = "build_config_project_test"
}
resource "teamcity_build_config" "build_configuration_test" {
name = "build config template with counter"
is_template = "true"
project_id = "${teamcity_project.build_config_project_test.id}"
settings {
build_number_format = "1.0.%build.counter%-%teamcity.build.branch%"
concurrent_limit = 1
}
}
`

const TestAccBuildConfigChangeToTemplate = `
resource "teamcity_project" "build_config_project_test" {
name = "build_config_project_test"
}
resource "teamcity_build_config" "build_configuration_test" {
name = "build config not template with counter"
project_id = "${teamcity_project.build_config_project_test.id}"
settings {
build_number_format = "1.0.%build.counter%-%teamcity.build.branch%"
build_counter = "10"
concurrent_limit = 1
}
}
`

const TestAccBuildConfigChangeToTemplateUpdatedError = `
resource "teamcity_project" "build_config_project_test" {
name = "build_config_project_test"
}
resource "teamcity_build_config" "build_configuration_test" {
name = "build config template with counter"
is_template = true
project_id = "${teamcity_project.build_config_project_test.id}"
settings {
build_number_format = "1.0.%build.counter%-%teamcity.build.branch%"
build_counter = "10"
concurrent_limit = 1
}
}
`

const TestAccBuildConfigChangeToTemplateUpdated = `
resource "teamcity_project" "build_config_project_test" {
name = "build_config_project_test"
}
resource "teamcity_build_config" "build_configuration_test" {
name = "build config template with counter"
is_template = true
project_id = "${teamcity_project.build_config_project_test.id}"
settings {
build_number_format = "1.0.%build.counter%-%teamcity.build.branch%"
concurrent_limit = 1
}
}
`

const TestAccBuildConfigTemplate = `
resource "teamcity_project" "build_config_project_test" {
name = "build_config_project_test"
Expand Down

0 comments on commit 9ed994e

Please sign in to comment.