Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pr77 fixup #101

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,16 @@ GIT_COMMIT=$(shell git rev-parse HEAD)
GIT_DIRTY=$(shell test -n "`git status --porcelain`" && echo "+CHANGES" || true)
BUILD_DATE=$(shell date '+%Y-%m-%d-%H:%M:%S')
BUILDER_IMAGE=cvbarros/terraform-provider-teamcity-builder
PLUGIN_REGISTRY_PATH=registry.terraform.io/cvbarros/teamcity/${VERSION}/linux_amd64

default: test

build:
GO111MODULE=on go build -o ./bin/terraform-provider-teamcity_${VERSION}

install: build
cp ./bin/terraform-provider-teamcity_${VERSION} ~/.terraform.d/plugins/
mkdir -p ~/.terraform.d/plugins/${PLUGIN_REGISTRY_PATH}/
cp ./bin/terraform-provider-teamcity_v${VERSION} ~/.terraform.d/plugins/${PLUGIN_REGISTRY_PATH}/terraform-provider-teamcity_v${VERSION}

clean:
rm -rf ./bin
Expand All @@ -28,3 +30,14 @@ clean_samples:

fmt_samples:
terraform fmt -recursive examples/

start-teamcity:
integration_tests/start_teamcity.sh

stop-teamcity:
docker-compose --file integration_tests/docker-compose.yml rm --stop --force

acceptance-test:
TEAMCITY_ADDR=http://localhost:8112 TEAMCITY_USER=admin TEAMCITY_PASSWORD=admin TF_ACC=1 go test -v ./teamcity/

test: start-teamcity acceptance-test stop-teamcity
8 changes: 4 additions & 4 deletions teamcity/data_source_project.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,11 @@ func dataSourceProjectRead(d *schema.ResourceData, meta interface{}) error {
d.SetId(dt.ID)
d.Set("name", dt.Name)
d.Set("project_id", dt.ID)
parentProjectId := dt.ParentProjectID
if parentProjectId == "_Root" {
parentProjectId = ""
parentProjectID := dt.ParentProjectID
if parentProjectID == "_Root" {
parentProjectID = ""
}
d.Set("parent_project_id", parentProjectId)
d.Set("parent_project_id", parentProjectID)
d.Set("url", dt.WebURL)
return nil
}
7 changes: 7 additions & 0 deletions teamcity/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package teamcity

import "strings"

func isNotFoundError(err error) bool {
return strings.Contains(err.Error(), "404")
}
8 changes: 8 additions & 0 deletions teamcity/resource_agent_requirement.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package teamcity

import (
"fmt"
"log"

"github.com/hashicorp/terraform-plugin-sdk/helper/validation"

Expand Down Expand Up @@ -92,6 +93,13 @@ func resourceAgentRequirementRead(d *schema.ResourceData, meta interface{}) erro

dt, err := getAgentRequirement(client, d.Id())
if err != nil {
// handles this being deleted outside of TF
if isNotFoundError(err) {
log.Printf("[DEBUG] Agent Requirement was not found - removing from state!")
d.SetId("")
return nil
}

return err
}

Expand Down
8 changes: 8 additions & 0 deletions 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"
"log"

api "github.com/cvbarros/go-teamcity/teamcity"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
Expand Down Expand Up @@ -101,6 +102,13 @@ func resourceArtifactDependencyRead(d *schema.ResourceData, meta interface{}) er

dt, err := getArtifactDependency(client, d.Id())
if err != nil {
// handles this being deleted outside of TF
if isNotFoundError(err) {
log.Printf("[DEBUG] Build Trigger Artifact Dependency was not found - removing from state!")
d.SetId("")
return nil
}

return err
}

Expand Down
59 changes: 32 additions & 27 deletions teamcity/resource_build_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -285,14 +285,12 @@ func resourceBuildConfigCreate(d *schema.ResourceData, meta interface{}) error {
}

created, err := client.BuildTypes.Create(projectID, bt)

if err != nil {
return err
}

log.Printf("[DEBUG] resourceBuildConfigCreate: sucessfully created build configuration with id = '%v'. Marking new resource.", created.ID)

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

log.Printf("[DEBUG] resourceBuildConfigCreate: initial creation finished. Calling resourceBuildConfigUpdate to update the rest of resource.")
Expand Down Expand Up @@ -433,6 +431,13 @@ func resourceBuildConfigRead(d *schema.ResourceData, meta interface{}) error {
log.Printf("[DEBUG] resourceBuildConfigRead started for resouceId: %v", d.Id())
dt, err := getBuildConfiguration(client, d.Id())
if err != nil {
// handles this being deleted outside of TF
if isNotFoundError(err) {
log.Printf("[DEBUG] Build Configuration was not found - removing from state!")
d.SetId("")
return nil
}

return err
}
log.Printf("[DEBUG] BuildConfiguration '%v' retrieved successfully", dt.Name)
Expand Down Expand Up @@ -556,23 +561,21 @@ func flattenTemplates(d *schema.ResourceData, templates *api.Templates) error {
}

func flattenParameterCollection(d *schema.ResourceData, params *api.Parameters) error {
var configParams, sysParams, envParams = flattenParameters(params)

if len(envParams) > 0 {
if err := d.Set("env_params", envParams); err != nil {
return err
}
configParams := flattenParameters(params, api.ParameterTypes.Configuration)
if err := d.Set("config_params", configParams); err != nil {
return err
}
if len(sysParams) > 0 {
if err := d.Set("sys_params", sysParams); err != nil {
return err
}

envParams := flattenParameters(params, api.ParameterTypes.EnvironmentVariable)
if err := d.Set("env_params", envParams); err != nil {
return err
}
if len(configParams) > 0 {
if err := d.Set("config_params", configParams); err != nil {
return err
}

systemParams := flattenParameters(params, api.ParameterTypes.System)
if err := d.Set("sys_params", systemParams); err != nil {
return err
}

return nil
}

Expand Down Expand Up @@ -616,19 +619,21 @@ func expandParameterCollection(d *schema.ResourceData) (*api.Parameters, error)
return out, nil
}

func flattenParameters(dt *api.Parameters) (config map[string]string, sys map[string]string, env map[string]string) {
env, sys, config = make(map[string]string), make(map[string]string), make(map[string]string)
for _, p := range dt.Items {
switch p.Type {
case api.ParameterTypes.Configuration:
config[p.Name] = p.Value
case api.ParameterTypes.EnvironmentVariable:
env[p.Name] = p.Value
case api.ParameterTypes.System:
sys[p.Name] = p.Value
func flattenParameters(input *api.Parameters, paramType string) map[string]string {
output := make(map[string]string)
if input == nil {
return output
}

for _, p := range input.Items {
if p.Type != paramType {
continue
}

output[p.Name] = p.Value
}
return config, sys, env

return output
}

func expandParameters(raw map[string]interface{}, paramType string) (*api.Parameters, error) {
Expand Down
7 changes: 7 additions & 0 deletions teamcity/resource_build_trigger_build_finish.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,13 @@ func resourceBuildTriggerBuildFinishRead(d *schema.ResourceData, meta interface{

ret, err := getTrigger(client, d.Id())
if err != nil {
// handles this being deleted outside of TF
if isNotFoundError(err) {
log.Printf("[DEBUG] Build Trigger Build Finish was not found - removing from state!")
d.SetId("")
return nil
}

return err
}
dt, ok := ret.(*api.TriggerBuildFinish)
Expand Down
8 changes: 8 additions & 0 deletions teamcity/resource_build_trigger_schedule.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package teamcity

import (
"fmt"
"log"

api "github.com/cvbarros/go-teamcity/teamcity"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
Expand Down Expand Up @@ -177,6 +178,13 @@ func resourceBuildTriggerScheduleRead(d *schema.ResourceData, meta interface{})

ret, err := getTrigger(client, d.Id())
if err != nil {
// handles this being deleted outside of TF
if isNotFoundError(err) {
log.Printf("[DEBUG] Build Trigger Schedule was not found - removing from state!")
d.SetId("")
return nil
}

return err
}
dt, ok := ret.(*api.TriggerSchedule)
Expand Down
8 changes: 8 additions & 0 deletions teamcity/resource_build_trigger_vcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package teamcity

import (
"fmt"
"log"

api "github.com/cvbarros/go-teamcity/teamcity"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
Expand Down Expand Up @@ -82,6 +83,13 @@ func resourceBuildTriggerVcsRead(d *schema.ResourceData, meta interface{}) error

ret, err := getTrigger(client, d.Id())
if err != nil {
// handles this being deleted outside of TF
if isNotFoundError(err) {
log.Printf("[DEBUG] Build Trigger VCS was not found - removing from state!")
d.SetId("")
return nil
}

return err
}
dt, ok := ret.(*api.TriggerVcs)
Expand Down
8 changes: 8 additions & 0 deletions teamcity/resource_feature_commit_status_publisher.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package teamcity
import (
"bytes"
"fmt"
"log"
"strings"

api "github.com/cvbarros/go-teamcity/teamcity"
Expand Down Expand Up @@ -115,6 +116,13 @@ func resourceFeatureCommitStatusPublisherRead(d *schema.ResourceData, meta inter

dt, err := getBuildFeatureCommitPublisher(client, d.Id())
if err != nil {
// handles this being deleted outside of TF
if isNotFoundError(err) {
log.Printf("[DEBUG] Build Feature Commit Status Publisher was not found - removing from state!")
d.SetId("")
return nil
}

return err
}

Expand Down
7 changes: 5 additions & 2 deletions teamcity/resource_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package teamcity
import (
"fmt"
"hash/crc32"
"log"
"regexp"
"strings"

Expand Down Expand Up @@ -87,7 +88,6 @@ func resourceGroupCreate(d *schema.ResourceData, meta interface{}) error {
}

if created != nil {
d.MarkNewResource()
d.SetId(created.Key)
} else {
d.SetId(key)
Expand All @@ -113,10 +113,13 @@ func resourceGroupRead(d *schema.ResourceData, meta interface{}) error {

dt, err := client.Groups.GetByKey(d.Id())
if err != nil {
if strings.Contains(err.Error(), "404") {
// handles this being deleted outside of TF
if isNotFoundError(err) {
log.Printf("[DEBUG] User Group was not found - removing from state!")
d.SetId("")
return nil
}

return err
}
if err := d.Set("key", dt.Key); err != nil {
Expand Down
Loading