Skip to content

Commit 111f592

Browse files
authored
Add missing properties for data_git_repositories and resource_build_definition (#301)
* Add missing properties * Update docs * format * Lint * Review feedback * Unexport * Fix tests * Fix test * Docs update
1 parent 8bdb124 commit 111f592

File tree

7 files changed

+132
-35
lines changed

7 files changed

+132
-35
lines changed

azuredevops/data_git_repositories.go

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"strings"
99

1010
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
11-
"github.com/hashicorp/terraform/helper/hashcode"
1211
"github.com/microsoft/azure-devops-go-api/azuredevops/git"
1312
"github.com/microsoft/terraform-provider-azuredevops/azuredevops/utils"
1413
"github.com/microsoft/terraform-provider-azuredevops/azuredevops/utils/config"
@@ -39,9 +38,8 @@ func dataGitRepositories() *schema.Resource {
3938
Default: false,
4039
},
4140
"repositories": {
42-
Type: schema.TypeSet,
41+
Type: schema.TypeList,
4342
Computed: true,
44-
Set: getGitRepositoryHash,
4543
Elem: &schema.Resource{
4644
Schema: map[string]*schema.Schema{
4745
"name": {
@@ -76,17 +74,17 @@ func dataGitRepositories() *schema.Resource {
7674
Type: schema.TypeInt,
7775
Computed: true,
7876
},
77+
"default_branch": {
78+
Type: schema.TypeString,
79+
Computed: true,
80+
},
7981
},
8082
},
8183
},
8284
},
8385
}
8486
}
8587

86-
func getGitRepositoryHash(v interface{}) int {
87-
return hashcode.String(v.(map[string]interface{})["id"].(string))
88-
}
89-
9088
func dataSourceGitRepositoriesRead(d *schema.ResourceData, m interface{}) error {
9189
clients := m.(*config.AggregatedClient)
9290

@@ -184,6 +182,10 @@ func flattenGitRepositories(repos *[]git.GitRepository) ([]interface{}, error) {
184182
output["size"] = *element.Size
185183
}
186184

185+
if element.DefaultBranch != nil {
186+
output["default_branch"] = *element.DefaultBranch
187+
}
188+
187189
results = append(results, output)
188190
}
189191

azuredevops/data_git_repositories_test.go

Lines changed: 48 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,12 @@ package azuredevops
55
import (
66
"context"
77
"errors"
8+
"fmt"
89
"testing"
910

1011
"github.com/golang/mock/gomock"
12+
"github.com/hashicorp/terraform-plugin-sdk/helper/acctest"
13+
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
1114
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
1215
"github.com/microsoft/azure-devops-go-api/azuredevops/core"
1316
"github.com/microsoft/azure-devops-go-api/azuredevops/git"
@@ -111,9 +114,9 @@ func TestGitRepositoriesDataSource_Read_TestHandleError(t *testing.T) {
111114
err := dataSourceGitRepositoriesRead(resourceData, clients)
112115
require.NotNil(t, err)
113116
require.Zero(t, resourceData.Id())
114-
repos := resourceData.Get("repositories").(*schema.Set)
117+
repos := resourceData.Get("repositories").([]interface{})
115118
require.NotNil(t, repos)
116-
require.Zero(t, repos.Len())
119+
require.Zero(t, len(repos))
117120
}
118121

119122
func TestGitRepositoriesDataSource_Read_TestHandleErrorWithSpecificRepository(t *testing.T) {
@@ -146,9 +149,9 @@ func TestGitRepositoriesDataSource_Read_TestHandleErrorWithSpecificRepository(t
146149
err := dataSourceGitRepositoriesRead(resourceData, clients)
147150
require.NotNil(t, err)
148151
require.Zero(t, resourceData.Id())
149-
repos := resourceData.Get("repositories").(*schema.Set)
152+
repos := resourceData.Get("repositories").([]interface{})
150153
require.NotNil(t, repos)
151-
require.Zero(t, repos.Len())
154+
require.Zero(t, len(repos))
152155
}
153156

154157
func TestGitRepositoriesDataSource_Read_NoRepositories(t *testing.T) {
@@ -178,9 +181,9 @@ func TestGitRepositoriesDataSource_Read_NoRepositories(t *testing.T) {
178181

179182
err := dataSourceGitRepositoriesRead(resourceData, clients)
180183
require.Nil(t, err)
181-
repos := resourceData.Get("repositories").(*schema.Set)
184+
repos := resourceData.Get("repositories").([]interface{})
182185
require.NotNil(t, repos)
183-
require.Zero(t, repos.Len())
186+
require.Zero(t, len(repos))
184187
}
185188

186189
func TestGitRepositoriesDataSource_Read_AllRepositories(t *testing.T) {
@@ -210,9 +213,9 @@ func TestGitRepositoriesDataSource_Read_AllRepositories(t *testing.T) {
210213

211214
err := dataSourceGitRepositoriesRead(resourceData, clients)
212215
require.Nil(t, err)
213-
repos := resourceData.Get("repositories").(*schema.Set)
216+
repos := resourceData.Get("repositories").([]interface{})
214217
require.NotNil(t, repos)
215-
require.Equal(t, repos.Len(), 3)
218+
require.Equal(t, len(repos), 3)
216219
}
217220

218221
func TestGitRepositoriesDataSource_Read_AllRepositoriesByProject(t *testing.T) {
@@ -247,11 +250,11 @@ func TestGitRepositoriesDataSource_Read_AllRepositoriesByProject(t *testing.T) {
247250

248251
err := dataSourceGitRepositoriesRead(resourceData, clients)
249252
require.Nil(t, err)
250-
repos := resourceData.Get("repositories").(*schema.Set)
253+
repos := resourceData.Get("repositories").([]interface{})
251254
require.NotNil(t, repos)
252-
require.Equal(t, repos.Len(), 2)
255+
require.Equal(t, len(repos), 2)
253256
repoMap := make(map[string]interface{})
254-
for _, item := range repos.List() {
257+
for _, item := range repos {
255258
repoData := item.(map[string]interface{})
256259
repoMap[repoData["name"].(string)] = repoData
257260
}
@@ -293,7 +296,39 @@ func TestGitRepositoriesDataSource_Read_SingleRepository(t *testing.T) {
293296

294297
err := dataSourceGitRepositoriesRead(resourceData, clients)
295298
require.Nil(t, err)
296-
repos := resourceData.Get("repositories").(*schema.Set)
299+
repos := resourceData.Get("repositories").([]interface{})
297300
require.NotNil(t, repos)
298-
require.Equal(t, repos.Len(), 1)
301+
require.Equal(t, len(repos), 1)
302+
}
303+
304+
/**
305+
* Begin acceptance tests
306+
*/
307+
308+
// Verifies that the following sequence of events occurrs without error:
309+
// (1) TF can create a project
310+
// (2) A data source is added to the configuration, and that data source can find the created project
311+
func TestAccAzureTfsGitRepositories_DataSource(t *testing.T) {
312+
projectName := testhelper.TestAccResourcePrefix + acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum)
313+
gitRepoName := testhelper.TestAccResourcePrefix + acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum)
314+
tfConfigStep1 := testhelper.TestAccAzureGitRepoResource(projectName, gitRepoName, "Clean")
315+
tfConfigStep2 := fmt.Sprintf("%s\n%s", tfConfigStep1, testhelper.TestAccProjectGitRepositories(projectName, gitRepoName))
316+
317+
tfNode := "data.azuredevops_git_repositories.repositories"
318+
resource.Test(t, resource.TestCase{
319+
PreCheck: func() { testhelper.TestAccPreCheck(t, nil) },
320+
Providers: testAccProviders,
321+
Steps: []resource.TestStep{
322+
{
323+
Config: tfConfigStep1,
324+
}, {
325+
Config: tfConfigStep2,
326+
Check: resource.ComposeTestCheckFunc(
327+
resource.TestCheckResourceAttr(tfNode, "name", gitRepoName),
328+
resource.TestCheckResourceAttr(tfNode, "repositories.0.name", gitRepoName),
329+
resource.TestCheckResourceAttr(tfNode, "repositories.0.default_branch", "refs/heads/master"),
330+
),
331+
},
332+
},
333+
})
299334
}

azuredevops/resource_build_definition.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,10 @@ func resourceBuildDefinition() *schema.Resource {
132132
Type: schema.TypeString,
133133
Required: true,
134134
},
135+
"repo_id": {
136+
Type: schema.TypeString,
137+
Required: true,
138+
},
135139
"repo_name": {
136140
Type: schema.TypeString,
137141
Required: true,
@@ -424,6 +428,7 @@ func flattenRepository(buildDefinition *build.BuildDefinition) interface{} {
424428

425429
return []map[string]interface{}{{
426430
"yml_path": yamlFilePath,
431+
"repo_id": *buildDefinition.Repository.Id,
427432
"repo_name": *buildDefinition.Repository.Name,
428433
"repo_type": *buildDefinition.Repository.Type,
429434
"branch_name": *buildDefinition.Repository.DefaultBranch,
@@ -729,6 +734,7 @@ func expandBuildDefinition(d *schema.ResourceData) (*build.BuildDefinition, stri
729734

730735
repository := repositories[0].(map[string]interface{})
731736

737+
repoID := repository["repo_id"].(string)
732738
repoName := repository["repo_name"].(string)
733739
repoType := RepoType(repository["repo_type"].(string))
734740
repoURL := ""
@@ -768,7 +774,7 @@ func expandBuildDefinition(d *schema.ResourceData) (*build.BuildDefinition, stri
768774
Revision: converter.Int(d.Get("revision").(int)),
769775
Repository: &build.BuildRepository{
770776
Url: &repoURL,
771-
Id: &repoName,
777+
Id: &repoID,
772778
Name: &repoName,
773779
DefaultBranch: converter.String(repository["branch_name"].(string)),
774780
Type: converter.String(string(repoType)),

azuredevops/resource_build_definition_test.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,9 +362,11 @@ func TestAzureDevOpsBuildDefinition_Update_DoesNotSwallowError(t *testing.T) {
362362
// underlying terraform state.
363363
func TestAccAzureDevOpsBuildDefinition_Create_Update_Import(t *testing.T) {
364364
projectName := testhelper.TestAccResourcePrefix + acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum)
365+
gitRepoName := testhelper.TestAccResourcePrefix + acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum)
365366
buildDefinitionPathEmpty := `\`
366367
buildDefinitionNameFirst := testhelper.TestAccResourcePrefix + acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum)
367368
buildDefinitionNameSecond := testhelper.TestAccResourcePrefix + acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum)
369+
buildDefinitionNameThird := testhelper.TestAccResourcePrefix + acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum)
368370

369371
buildDefinitionPathFirst := `\` + acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum)
370372
buildDefinitionPathSecond := `\` + acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum)
@@ -433,6 +435,17 @@ func TestAccAzureDevOpsBuildDefinition_Create_Update_Import(t *testing.T) {
433435
resource.TestCheckResourceAttr(tfBuildDefNode, "name", buildDefinitionNameFirst),
434436
resource.TestCheckResourceAttr(tfBuildDefNode, "path", buildDefinitionPathFourth),
435437
),
438+
}, {
439+
Config: testhelper.TestAccBuildDefinitionResourceTfsGit(projectName, gitRepoName, buildDefinitionNameThird, buildDefinitionPathEmpty),
440+
Check: resource.ComposeTestCheckFunc(
441+
testAccCheckBuildDefinitionResourceExists(buildDefinitionNameThird),
442+
resource.TestCheckResourceAttrSet(tfBuildDefNode, "project_id"),
443+
resource.TestCheckResourceAttrSet(tfBuildDefNode, "revision"),
444+
resource.TestCheckResourceAttrSet(tfBuildDefNode, "repository.0.repo_id"),
445+
resource.TestCheckResourceAttr(tfBuildDefNode, "repository.0.repo_name", gitRepoName),
446+
resource.TestCheckResourceAttr(tfBuildDefNode, "name", buildDefinitionNameThird),
447+
resource.TestCheckResourceAttr(tfBuildDefNode, "path", buildDefinitionPathEmpty),
448+
),
436449
}, {
437450
// Resource Acceptance Testing https://www.terraform.io/docs/extend/resources/import.html#resource-acceptance-testing-implementation
438451
ResourceName: tfBuildDefNode,

azuredevops/utils/testhelper/hcl.go

Lines changed: 51 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,20 @@ import (
55
"strings"
66
)
77

8-
// TestAccAzureGitRepoResource HCL describing an AzDO GIT repository resource
9-
func TestAccAzureGitRepoResource(projectName string, gitRepoName string, initType string) string {
10-
azureGitRepoResource := fmt.Sprintf(`
8+
func getAzureGitRepoResource(gitRepoName string, initType string) string {
9+
return fmt.Sprintf(`
1110
resource "azuredevops_git_repository" "gitrepo" {
1211
project_id = azuredevops_project.project.id
1312
name = "%s"
1413
initialization {
1514
init_type = "%s"
1615
}
1716
}`, gitRepoName, initType)
17+
}
18+
19+
// TestAccAzureGitRepoResource HCL describing an AzDO GIT repository resource
20+
func TestAccAzureGitRepoResource(projectName string, gitRepoName string, initType string) string {
21+
azureGitRepoResource := getAzureGitRepoResource(gitRepoName, initType)
1822

1923
projectResource := TestAccProjectResource(projectName)
2024
return fmt.Sprintf("%s\n%s", projectResource, azureGitRepoResource)
@@ -23,15 +27,14 @@ resource "azuredevops_git_repository" "gitrepo" {
2327
// TestAccAzureForkedGitRepoResource HCL describing an AzDO GIT repository resource
2428
func TestAccAzureForkedGitRepoResource(projectName string, gitRepoName string, gitForkedRepoName string, initType string, forkedInitType string) string {
2529
azureGitRepoResource := fmt.Sprintf(`
26-
resource "azuredevops_git_repository" "gitforkedrepo" {
27-
project_id = azuredevops_project.project.id
28-
parent_repository_id = azuredevops_git_repository.gitrepo.id
29-
name = "%s"
30-
initialization {
31-
init_type = "%s"
32-
}
33-
}`, gitForkedRepoName, forkedInitType)
34-
30+
resource "azuredevops_git_repository" "gitforkedrepo" {
31+
project_id = azuredevops_project.project.id
32+
parent_repository_id = azuredevops_git_repository.gitrepo.id
33+
name = "%s"
34+
initialization {
35+
init_type = "%s"
36+
}
37+
}`, gitForkedRepoName, forkedInitType)
3538
gitRepoResource := TestAccAzureGitRepoResource(projectName, gitRepoName, initType)
3639
return fmt.Sprintf("%s\n%s", gitRepoResource, azureGitRepoResource)
3740
}
@@ -71,6 +74,19 @@ data "azuredevops_project" "project" {
7174
}`, projectName)
7275
}
7376

77+
// TestAccProjectGitRepositories HCL describing a data source for an AzDO git repo
78+
func TestAccProjectGitRepositories(projectName string, gitRepoName string) string {
79+
return fmt.Sprintf(`
80+
data "azuredevops_project" "project" {
81+
project_name = "%s"
82+
}
83+
84+
data "azuredevops_git_repositories" "repositories" {
85+
project_id = data.azuredevops_project.project.id
86+
name = "%s"
87+
}`, projectName, gitRepoName)
88+
}
89+
7490
// TestAccUserEntitlementResource HCL describing an AzDO UserEntitlement
7591
func TestAccUserEntitlementResource(principalName string) string {
7692
return fmt.Sprintf(`
@@ -206,6 +222,7 @@ func TestAccBuildDefinitionResourceGitHub(projectName string, buildDefinitionNam
206222
buildPath,
207223
"GitHub",
208224
"repoOrg/repoName",
225+
"repoOrg/repoName",
209226
"master",
210227
"path/to/yaml",
211228
"")
@@ -219,17 +236,37 @@ func TestAccBuildDefinitionResourceBitbucket(projectName string, buildDefinition
219236
buildPath,
220237
"Bitbucket",
221238
"repoOrg/repoName",
239+
"repoOrg/repoName",
222240
"master",
223241
"path/to/yaml",
224242
serviceConnectionID)
225243
}
226244

245+
// TestAccBuildDefinitionResourceTfsGit HCL describing an AzDO build definition sourced from AzDo Git Repo
246+
func TestAccBuildDefinitionResourceTfsGit(projectName string, gitRepoName string, buildDefinitionName string, buildPath string) string {
247+
buildDefinitionResource := TestAccBuildDefinitionResource(
248+
projectName,
249+
buildDefinitionName,
250+
buildPath,
251+
"TfsGit",
252+
"${azuredevops_git_repository.gitrepo.id}",
253+
"${azuredevops_git_repository.gitrepo.name}",
254+
"master",
255+
"path/to/yaml",
256+
"")
257+
258+
azureGitRepoResource := getAzureGitRepoResource(gitRepoName, "Clean")
259+
260+
return fmt.Sprintf("%s\n%s", azureGitRepoResource, buildDefinitionResource)
261+
}
262+
227263
// TestAccBuildDefinitionResource HCL describing an AzDO build definition
228264
func TestAccBuildDefinitionResource(
229265
projectName string,
230266
buildDefinitionName string,
231267
buildPath string,
232268
repoType string,
269+
repoID string,
233270
repoName string,
234271
branchName string,
235272
yamlPath string,
@@ -238,11 +275,12 @@ func TestAccBuildDefinitionResource(
238275
repositoryBlock := fmt.Sprintf(`
239276
repository {
240277
repo_type = "%s"
278+
repo_id = "%s"
241279
repo_name = "%s"
242280
branch_name = "%s"
243281
yml_path = "%s"
244282
service_connection_id = "%s"
245-
}`, repoType, repoName, branchName, yamlPath, serviceConnectionID)
283+
}`, repoType, repoID, repoName, branchName, yamlPath, serviceConnectionID)
246284

247285
buildDefinitionResource := fmt.Sprintf(`
248286
resource "azuredevops_build_definition" "build" {

website/docs/d/data_git_repositories.html.markdown

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ The following attributes are exported:
8686
- `remote_url` - HTTPS Url to clone the Git repository
8787
- `project_id` - Project identifier to which the Git repository belongs.
8888
- `size` - Compressed size (bytes) of the repository.
89+
- `default_branch` - The name of the default branch.
8990

9091
## Relevant Links
9192

website/docs/r/build_definition.html.markdown

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ resource "azuredevops_build_definition" "build" {
7070
7171
repository {
7272
repo_type = "TfsGit"
73+
repo_id = azuredevops_git_repository.repository.id
7374
repo_name = azuredevops_git_repository.repository.name
7475
branch_name = azuredevops_git_repository.repository.default_branch
7576
yml_path = "azure-pipelines.yml"
@@ -96,6 +97,7 @@ The following arguments are supported:
9697
`repository` block supports the following:
9798

9899
* `branch_name` - (Optional) The branch name for which builds are triggered. Defaults to `master`.
100+
* `repo_id` - (Required) The id of the repository.
99101
* `repo_name` - (Required) The name of the repository.
100102
* `repo_type` - (Optional) The repository type. Valid values: `GitHub` or `TfsGit` or `Bitbucket`. Defaults to `Github`.
101103
* `service_connection_id` - (Optional) The service connection ID. Used if the `repo_type` is `GitHub`.
@@ -155,7 +157,7 @@ In addition to all arguments above, the following attributes are exported:
155157

156158
## Import
157159
Azure DevOps Build Definitions can be imported using the project name/definitions Id or by the project Guid/definitions Id, e.g.
158-
160+
159161
```
160162
terraform import azuredevops_build_definition.build "Test Project"/10
161163
or

0 commit comments

Comments
 (0)