Skip to content

Commit

Permalink
Merge branch 'fix-dependency-imports' into yext
Browse files Browse the repository at this point in the history
  • Loading branch information
atavakoliyext committed Oct 3, 2019
2 parents da69163 + b74d170 commit 5bf2838
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 16 deletions.
32 changes: 32 additions & 0 deletions teamcity/importer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package teamcity

import (
"fmt"

"github.com/hashicorp/terraform/helper/schema"
)


func subresourceImporter(readFunc schema.ReadFunc) schema.StateFunc {
return func(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
var buildConfigID, resourceID string

if n, err := fmt.Sscanf(d.Id(), "%s %s", &buildConfigID, &resourceID); err != nil {
return nil, fmt.Errorf("invalid import ID '%s' - %v", d.Id(), err)
} else if n != 2 {
return nil, fmt.Errorf("invalid import ID '%s' - Unrecognized format", d.Id())
}

d.SetId(resourceID)

if err := d.Set("build_config_id", buildConfigID); err != nil {
return nil, err
}

if err := readFunc(d, meta); err != nil {
return nil, err
}

return []*schema.ResourceData{d}, nil
}
}
8 changes: 7 additions & 1 deletion teamcity/resource_artifact_dependency.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package teamcity

import (
"fmt"
"strings"

api "github.com/cvbarros/go-teamcity-sdk/teamcity"
"github.com/hashicorp/terraform/helper/schema"
Expand All @@ -14,7 +15,7 @@ func resourceArtifactDependency() *schema.Resource {
Read: resourceArtifactDependencyRead,
Delete: resourceArtifactDependencyDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
State: subresourceImporter(resourceArtifactDependencyRead),
},

Schema: map[string]*schema.Schema{
Expand Down Expand Up @@ -101,6 +102,11 @@ func resourceArtifactDependencyRead(d *schema.ResourceData, meta interface{}) er

dt, err := getArtifactDependency(client, d.Id())
if err != nil {
if strings.Contains(err.Error(), "404") {
// This dependency was deleted out-of-band
d.SetId("")
return nil
}
return err
}

Expand Down
2 changes: 1 addition & 1 deletion teamcity/resource_build_trigger_build_finish.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func resourceBuildTriggerBuildFinish() *schema.Resource {
Read: resourceBuildTriggerBuildFinishRead,
Delete: resourceBuildTriggerBuildFinishDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
State: subresourceImporter(resourceBuildTriggerBuildFinishRead),
},

Schema: map[string]*schema.Schema{
Expand Down
2 changes: 1 addition & 1 deletion teamcity/resource_build_trigger_schedule.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func resourceBuildTriggerSchedule() *schema.Resource {
Read: resourceBuildTriggerScheduleRead,
Delete: resourceBuildTriggerScheduleDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
State: subresourceImporter(resourceBuildTriggerScheduleRead),
},

Schema: map[string]*schema.Schema{
Expand Down
2 changes: 1 addition & 1 deletion teamcity/resource_build_trigger_vcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ func resourceBuildTriggerVcs() *schema.Resource {
Read: resourceBuildTriggerVcsRead,
Delete: resourceBuildTriggerVcsDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
State: subresourceImporter(resourceBuildTriggerVcsRead),
},

Schema: map[string]*schema.Schema{
Expand Down
28 changes: 16 additions & 12 deletions teamcity/resource_snapshot_dependency.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,10 +168,13 @@ func setSnapshotDependencyBoolProperty(d *schema.ResourceData, p *api.Properties
}

func resourceSnapshotDependencyRead(d *schema.ResourceData, meta interface{}) error {
depService := meta.(*api.Client).DependencyService(d.Get("build_config_id").(string))

dt, err := getSnapshotDependency(depService, d.Id())
_, dt, err := getSnapshotDependency(meta.(*api.Client), d.Id())
if err != nil {
if strings.Contains(err.Error(), "404") {
// This dependency was deleted out-of-band
d.SetId("")
return nil
}
return err
}

Expand All @@ -187,11 +190,9 @@ func resourceSnapshotDependencyRead(d *schema.ResourceData, meta interface{}) er
}

func resourceSnapshotDependencyDelete(d *schema.ResourceData, meta interface{}) error {
depService := meta.(*api.Client).DependencyService(d.Get("build_config_id").(string))

dt, err := getSnapshotDependency(depService, d.Id())
depService, dt, err := getSnapshotDependency(meta.(*api.Client), d.Id())
if err != nil {
if strings.Contains(err.Error(), "404 Not Found") {
if strings.Contains(err.Error(), "404") {
// This dependency was deleted out-of-band
return nil
}
Expand All @@ -201,17 +202,20 @@ func resourceSnapshotDependencyDelete(d *schema.ResourceData, meta interface{})
return depService.DeleteSnapshot(dt.ID)
}

func getSnapshotDependency(depService *api.DependencyService, id string) (*api.SnapshotDependency, error) {
func getSnapshotDependency(client *api.Client, id string) (*api.DependencyService, *api.SnapshotDependency, error) {
var buildConfigID, snapshotID string
if n, err := fmt.Sscanf(id, snapshotIDFormat, &buildConfigID, &snapshotID); err != nil {
return nil, fmt.Errorf("invalid snapshot_dependency ID '%s' - %v", buildConfigID, err)
return nil, nil, fmt.Errorf("invalid snapshot_dependency ID '%s' - %v", id, err)
} else if n != 2 {
return nil, fmt.Errorf("invalid snapshot_dependency ID '%s' - Unrecognized format", buildConfigID)
return nil, nil, fmt.Errorf("invalid snapshot_dependency ID '%s' - Unrecognized format", id)
}

depService := client.DependencyService(buildConfigID)

dt, err := depService.GetSnapshotByID(snapshotID)
if err != nil {
return nil, err
return nil, nil, err
}

return dt, nil
return depService, dt, nil
}

0 comments on commit 5bf2838

Please sign in to comment.