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

New Resource: teamcity_feature_golang #81

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
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
1 change: 1 addition & 0 deletions teamcity/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ func Provider() terraform.ResourceProvider {
"teamcity_agent_requirement": resourceAgentRequirement(),
"teamcity_feature_commit_status_publisher": resourceFeatureCommitStatusPublisher(),
"teamcity_group": resourceGroup(),
"teamcity_feature_golang": resourceFeatureGolang(),
},
DataSourcesMap: map[string]*schema.Resource{
"teamcity_project": dataSourceProject(),
Expand Down
115 changes: 115 additions & 0 deletions teamcity/resource_feature_golang.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
package teamcity

import (
"fmt"
"log"
"strings"

api "github.com/cvbarros/go-teamcity/teamcity"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
)

func resourceFeatureGolang() *schema.Resource {
return &schema.Resource{
Create: resourceFeatureGolangCreate,
Read: resourceFeatureGolangRead,
Delete: resourceFeatureGolangDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},

Schema: map[string]*schema.Schema{
"build_config_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
},
}
}

func resourceFeatureGolangCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*api.Client)

buildConfigId := d.Get("build_config_id").(string)

// validates the Build Configuration exists
if _, err := client.BuildTypes.GetByID(buildConfigId); err != nil {
return fmt.Errorf("invalid build_config_id %q - Build configuration does not exist", buildConfigId)
}

service := client.BuildFeatureService(buildConfigId)
feature := api.NewFeatureGolang()
feature.SetBuildTypeID(buildConfigId)
createdService, err := service.Create(feature)
if err != nil {
return err
}

id := fmt.Sprintf("%s|%s", buildConfigId, createdService.ID())
d.SetId(id)

return resourceFeatureGolangRead(d, meta)
}

func resourceFeatureGolangRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*api.Client)

id, err := ParseFeatureGolangID(d.Id())
if err != nil {
return err
}

service := client.BuildFeatureService(id.BuildConfigID)
if _, err := service.GetByID(id.FeatureID); err != nil {
// handles this being deleted outside of TF
if isNotFoundError(err) {
log.Printf("[DEBUG] Build Feature Golang was not found - removing from state!")
d.SetId("")
return nil
}

return err
}

d.Set("build_config_id", id.BuildConfigID)

return nil
}

func resourceFeatureGolangDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*api.Client)

id, err := ParseFeatureGolangID(d.Id())
if err != nil {
return err
}

service := client.BuildFeatureService(id.BuildConfigID)
if err := service.Delete(id.FeatureID); err != nil {
if !isNotFoundError(err) {
return err
}
}

return nil
}

type FeatureGolangId struct {
BuildConfigID string
FeatureID string
}

func ParseFeatureGolangID(input string) (*FeatureGolangId, error) {
// Format: 'BuildConfigID|FeatureID'
segments := strings.Split(input, "|")
if len(segments) != 2 {
return nil, fmt.Errorf("Expected 2 segments but got %d", len(segments))
}

id := FeatureGolangId{
BuildConfigID: segments[0],
FeatureID: segments[1],
}
return &id, nil
}
98 changes: 98 additions & 0 deletions teamcity/resource_feature_golang_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package teamcity_test

import (
"fmt"
"strings"
"testing"

api "github.com/cvbarros/go-teamcity/teamcity"
"github.com/cvbarros/terraform-provider-teamcity/teamcity"
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/terraform"
)

func TestAccTeamcityFeatureGolang_Basic(t *testing.T) {
resName := "teamcity_feature_golang.test"

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckBuildFeatureGolangDestroy,
Steps: []resource.TestStep{
{
Config: TestAccBuildFeatureGolang_basic,
Check: resource.ComposeTestCheckFunc(
testAccCheckBuildFeatureGolangExists(resName),
),
},
{
ResourceName: resName,
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func testAccCheckBuildFeatureGolangDestroy(s *terraform.State) error {
client := testAccProvider.Meta().(*api.Client)
for _, rs := range s.RootModule().Resources {
if rs.Type != "teamcity_feature_github" {
continue
}

id, err := teamcity.ParseFeatureGolangID(rs.Primary.ID)
if err != nil {
return err
}

srv := client.BuildFeatureService(id.BuildConfigID)
if _, err := srv.GetByID(id.FeatureID); err != nil {
if strings.Contains(err.Error(), "404") {
continue
}

return fmt.Errorf("Received an error retrieving the Golang Build Feature: %s", err)
}

return fmt.Errorf("Golang Build Feature still exists")
}
return nil
}

func testAccCheckBuildFeatureGolangExists(resourceName string) resource.TestCheckFunc {
return func(s *terraform.State) error {
client := testAccProvider.Meta().(*api.Client)
rs, ok := s.RootModule().Resources[resourceName]
if !ok {
return fmt.Errorf("Not found: %s", resourceName)
}

id, err := teamcity.ParseFeatureGolangID(rs.Primary.ID)
if err != nil {
return err
}

srv := client.BuildFeatureService(id.BuildConfigID)
if _, err := srv.GetByID(id.FeatureID); err != nil {
return fmt.Errorf("Received an error retrieving Golang Build Feature: %s", err)
}

return nil
}
}

const TestAccBuildFeatureGolang_basic = `
resource "teamcity_project" "test" {
name = "Build Feature"
}

resource "teamcity_build_config" "test" {
name = "BuildConfig"
project_id = teamcity_project.test.id
}

resource "teamcity_feature_golang" "test" {
build_config_id = teamcity_build_config.test.id
}
`
50 changes: 50 additions & 0 deletions website/docs/r/feature_golang.html.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
---
subcategory: "Build Configurations"
layout: teamcity
page_title: "TeamCity: Resource - teamcity_feature_golang"
description: |-
Manages an Golang Build Feature for a Build Configuration
---

# teamcity_feature_golang

Manages an Golang Build Feature for a Build Configuration

## Example Usage

```hcl
resource "teamcity_project" "example" {
name = "Example Project"
}

resource "teamcity_build_config" "example" {
name = "Example Build"
project_id = teamcity_project.example.id
}

resource "teamcity_feature_golang" "example" {
build_config_id = teamcity_build_config.example.id
}
```

## Argument Reference

The following arguments are supported:

* `build_config_id` - (Required) Specifies the ID of the Build Configuration for which a Golang Build Feature should be configured.

## Attributes Reference

In addition to all arguments above, the following attributes are exported:

* `id` - The auto-generated ID of the Golang Build Feature.

## Import

Golang Build Features can be imported using their ID, e.g.

```
$ terraform import teamcity_feature_golang.example "ProjectID|golang"
```

-> **Note:** This is a Terraform specific ID comprised of "ProjectID|FeatureID" - where featureID is likely `golang`
4 changes: 4 additions & 0 deletions website/teamcity.erb
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@
<a href="/docs/providers/teamcity/r/build_trigger_vcs.html">teamcity_build_trigger_vcs</a>
</li>

<li>
<a href="/docs/providers/teamcity/r/feature_golang.html">teamcity_feature_golang</a>
</li>

<li>
<a href="/docs/providers/teamcity/r/project.html">teamcity_project</a>
</li>
Expand Down