-
Notifications
You must be signed in to change notification settings - Fork 288
/
Copy pathresource_feed_permission_test.go
157 lines (136 loc) · 4.81 KB
/
resource_feed_permission_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
//go:build (all || core || data_sources || data_feed) && (!data_sources || !exclude_feed)
// +build all core data_sources data_feed
// +build !data_sources !exclude_feed
package acceptancetests
import (
"fmt"
"regexp"
"testing"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
"github.com/microsoft/azure-devops-go-api/azuredevops/v7/feed"
"github.com/microsoft/terraform-provider-azuredevops/azuredevops/internal/acceptancetests/testutils"
"github.com/microsoft/terraform-provider-azuredevops/azuredevops/internal/client"
)
func TestAccFeedPermission_basic(t *testing.T) {
name := testutils.GenerateResourceName()
tfNode := "azuredevops_feed_permission.test"
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testutils.PreCheck(t, nil) },
ProviderFactories: testutils.GetProviderFactories(),
Steps: []resource.TestStep{
{
Config: hclFeedPermissionBasic(name),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrSet(tfNode, "feed_id"),
resource.TestCheckResourceAttrSet(tfNode, "project_id"),
resource.TestCheckResourceAttrSet(tfNode, "role"),
resource.TestCheckResourceAttrSet(tfNode, "identity_descriptor"),
),
},
},
})
}
func TestAccFeedPermission_importErrorStep(t *testing.T) {
projectName := testutils.GenerateResourceName()
tfNode := "azuredevops_feed_permission.test"
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testutils.PreCheck(t, nil) },
ProviderFactories: testutils.GetProviderFactories(),
CheckDestroy: checkFeedPermissionDestroyed,
Steps: []resource.TestStep{
{
Config: hclFeedPermissionBasic(projectName),
Check: resource.ComposeTestCheckFunc(
CheckFeedPermissionExist(),
resource.TestCheckResourceAttrSet(tfNode, "feed_id"),
resource.TestCheckResourceAttrSet(tfNode, "project_id"),
resource.TestCheckResourceAttrSet(tfNode, "role"),
resource.TestCheckResourceAttrSet(tfNode, "identity_descriptor"),
),
},
{
Config: hclFeedPermissionImport(projectName),
ExpectError: feedPermissionRequiresImportError(),
},
},
})
}
func feedPermissionRequiresImportError() *regexp.Regexp {
return regexp.MustCompile(`Error: feed Permission for Feed : .* and Identity : .* already exists`)
}
func checkFeedPermissionDestroyed(s *terraform.State) error {
clients := testutils.GetProvider().Meta().(*client.AggregatedClient)
for _, res := range s.RootModule().Resources {
if res.Type != "azuredevops_feed_permission" {
continue
}
id := res.Primary.Attributes["feed_id"]
projectID := res.Primary.Attributes["project_id"]
permissions, err := clients.FeedClient.GetFeedPermissions(clients.Ctx, feed.GetFeedPermissionsArgs{
FeedId: &id,
Project: &projectID,
})
if err == nil {
if permissions != nil && len(*permissions) > 0 {
return fmt.Errorf(" Feed permissions (Feed ID: %s) should not exist", id)
}
}
}
return nil
}
func CheckFeedPermissionExist() resource.TestCheckFunc {
return func(s *terraform.State) error {
res, ok := s.RootModule().Resources["azuredevops_feed_permission.test"]
if !ok {
return fmt.Errorf(" Did not find a `azuredevops_feed_permission` in the TF state")
}
clients := testutils.GetProvider().Meta().(*client.AggregatedClient)
id := res.Primary.Attributes["feed_id"]
projectID := res.Primary.Attributes["project_id"]
_, err := clients.FeedClient.GetFeedPermissions(clients.Ctx, feed.GetFeedPermissionsArgs{
FeedId: &id,
Project: &projectID,
})
if err != nil {
return fmt.Errorf(" Feed permissions with Feed ( Feed ID=%s ) cannot be found!. Error=%v", id, err)
}
return nil
}
}
func hclFeedPermissionBasic(name string) string {
return fmt.Sprintf(`
resource "azuredevops_project" "test" {
name = "%[1]s"
description = "%[1]s-description"
visibility = "private"
version_control = "Git"
work_item_template = "Agile"
}
resource "azuredevops_feed" "test" {
name = "%[1]s"
project_id = azuredevops_project.test.id
}
resource "azuredevops_group" "test" {
scope = azuredevops_project.test.id
display_name = "%[1]s"
}
resource "azuredevops_feed_permission" "test" {
feed_id = azuredevops_feed.test.id
project_id = azuredevops_project.test.id
role = "reader"
identity_descriptor = azuredevops_group.test.descriptor
}
`, name)
}
func hclFeedPermissionImport(name string) string {
return fmt.Sprintf(`
%s
resource "azuredevops_feed_permission" "import" {
feed_id = azuredevops_feed_permission.test.feed_id
project_id = azuredevops_feed_permission.test.project_id
role = "reader"
identity_descriptor = azuredevops_feed_permission.test.identity_descriptor
}
`, hclFeedPermissionBasic(name))
}