Skip to content

Commit

Permalink
Avoid destructive actions when deleting projects and buildconfigs (#19)
Browse files Browse the repository at this point in the history
Instead of deleting buildconfigs:
- Pause buildconfig
- Remove all build steps
- Rename to have an (Archived) suffix

Instead of deleting projects:
- Archive project
- Rename to have an (Archived) suffix
  • Loading branch information
andrewsheng2 authored Aug 3, 2023
1 parent 7036891 commit 9e47499
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 12 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ go 1.19
require (
github.com/hashicorp/terraform v0.12.2
github.com/robfig/cron v1.2.0
github.com/yext/go-teamcity v0.5.3-0.20230323164551-08f28b0ad89b
github.com/yext/go-teamcity v0.5.3
)

require (
Expand Down
6 changes: 2 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -344,10 +344,8 @@ github.com/vmihailenco/msgpack v4.0.4+incompatible/go.mod h1:fy3FlTQTDXWkZ7Bh6Ac
github.com/xanzy/ssh-agent v0.2.1/go.mod h1:mLlQY/MoOhWBj+gOGMQkOeiEvkx+8pJSI+0Bx9h2kr4=
github.com/xiang90/probing v0.0.0-20160813154853-07dd2e8dfe18/go.mod h1:UETIi67q53MR2AWcXfiuqkDkRtnGDLqkBTpCHuJHxtU=
github.com/xlab/treeprint v0.0.0-20161029104018-1d6e34225557/go.mod h1:ce1O1j6UtZfjr22oyGxGLbauSBp2YVXpARAosm7dHBg=
github.com/yext/go-teamcity v0.5.2 h1:SETbrxEDeK2p7bjJJOCcCFHSKUGzsowGqpu1ypeUH0E=
github.com/yext/go-teamcity v0.5.2/go.mod h1:GC2xvLY4y27DU/01mveI8LsSBerjpCEtHk6eQ7ym8oc=
github.com/yext/go-teamcity v0.5.3-0.20230323164551-08f28b0ad89b h1:EEtPT+W2T28FKBX/v3HUWpNRjoZMX0R8TgHNZnZdEWw=
github.com/yext/go-teamcity v0.5.3-0.20230323164551-08f28b0ad89b/go.mod h1:GC2xvLY4y27DU/01mveI8LsSBerjpCEtHk6eQ7ym8oc=
github.com/yext/go-teamcity v0.5.3 h1:kO8of1jNql70g9fURGs+oM0ArMX3csjGy99FAkPecEQ=
github.com/yext/go-teamcity v0.5.3/go.mod h1:GC2xvLY4y27DU/01mveI8LsSBerjpCEtHk6eQ7ym8oc=
github.com/zclconf/go-cty v0.0.0-20181129180422-88fbe721e0f8/go.mod h1:xnAOWiHeOqg2nWS62VtQ7pbOu17FtxJNW8RLEih+O3s=
github.com/zclconf/go-cty v0.0.0-20190426224007-b18a157db9e2/go.mod h1:xnAOWiHeOqg2nWS62VtQ7pbOu17FtxJNW8RLEih+O3s=
github.com/zclconf/go-cty v0.0.0-20190516203816-4fecf87372ec/go.mod h1:xnAOWiHeOqg2nWS62VtQ7pbOu17FtxJNW8RLEih+O3s=
Expand Down
24 changes: 21 additions & 3 deletions teamcity/resource_build_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func resourceBuildConfig() *schema.Resource {
Create: resourceBuildConfigCreate,
Read: resourceBuildConfigRead,
Update: resourceBuildConfigUpdate,
Delete: resourceBuildConfigDelete,
Delete: resourceBuildConfigArchive,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Expand Down Expand Up @@ -431,9 +431,27 @@ func resourceBuildConfigUpdate(d *schema.ResourceData, meta interface{}) error {
return resourceBuildConfigRead(d, meta)
}

func resourceBuildConfigDelete(d *schema.ResourceData, meta interface{}) error {
func resourceBuildConfigArchive(d *schema.ResourceData, meta interface{}) error {
client := meta.(*api.Client)
return client.BuildTypes.Delete(d.Id())

err := client.BuildTypes.Pause(d.Id())
if err != nil {
return err
}

steps, err := client.BuildTypes.GetSteps(d.Id())
if err != nil {
return err
}
for _, step := range steps {
err = client.BuildTypes.DeleteStep(d.Id(), step.GetID())
if err != nil {
return err
}
}

name := d.Get("name")
return client.BuildTypes.Rename(d.Id(), fmt.Sprintf("%s (Archived)", name.(string)))
}

func resourceBuildConfigRead(d *schema.ResourceData, meta interface{}) error {
Expand Down
16 changes: 12 additions & 4 deletions teamcity/resource_project.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
package teamcity

import (
"fmt"
"log"

api "github.com/yext/go-teamcity/teamcity"
"github.com/hashicorp/terraform/helper/schema"
api "github.com/yext/go-teamcity/teamcity"
)

func resourceProject() *schema.Resource {
return &schema.Resource{
Create: resourceProjectCreate,
Read: resourceProjectRead,
Update: resourceProjectUpdate,
Delete: resourceProjectDelete,
Delete: resourceProjectArchive,
Importer: &schema.ResourceImporter{
State: resourceProjectImport,
},
Expand Down Expand Up @@ -152,9 +153,16 @@ func resourceProjectRead(d *schema.ResourceData, meta interface{}) error {
return nil
}

func resourceProjectDelete(d *schema.ResourceData, meta interface{}) error {
func resourceProjectArchive(d *schema.ResourceData, meta interface{}) error {
client := meta.(*api.Client)
return client.Projects.Delete(d.Id())

err := client.Projects.Archive(d.Id())
if err != nil {
return err
}

name := d.Get("name")
return client.Projects.Rename(d.Id(), fmt.Sprintf("%s (Archived)", name.(string)))
}

func resourceProjectImport(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
Expand Down

0 comments on commit 9e47499

Please sign in to comment.