@@ -2,159 +2,92 @@ package provider
2
2
3
3
import (
4
4
"fmt"
5
- "strconv"
6
- "strings"
7
5
"testing"
8
6
9
- "github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
10
7
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
11
8
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
12
- "github.com/xanzy/go-gitlab"
13
9
)
14
10
15
11
func TestAccGitlabProjectBadge_basic (t * testing.T ) {
16
- var badge gitlab.ProjectBadge
17
- rInt := acctest .RandInt ()
12
+ testAccCheck (t )
13
+
14
+ testProject := testAccCreateProject (t )
18
15
19
16
resource .Test (t , resource.TestCase {
20
17
PreCheck : func () { testAccPreCheck (t ) },
21
18
ProviderFactories : providerFactories ,
22
19
CheckDestroy : testAccCheckGitlabProjectBadgeDestroy ,
23
20
Steps : []resource.TestStep {
24
- // Create a project and badge
21
+ // Create a project badge
25
22
{
26
- Config : testAccGitlabProjectBadgeConfig (rInt ),
23
+ Config : fmt .Sprintf (`
24
+ resource "gitlab_project_badge" "this" {
25
+ project = "%d"
26
+ link_url = "https://example.com/badge"
27
+ image_url = "https://example.com/badge.svg"
28
+ name = "badge"
29
+ }
30
+ ` , testProject .ID ),
27
31
Check : resource .ComposeTestCheckFunc (
28
- testAccCheckGitlabProjectBadgeExists ("gitlab_project_badge.foo" , & badge ),
29
- testAccCheckGitlabProjectBadgeAttributes (& badge , & testAccGitlabProjectBadgeExpectedAttributes {
30
- LinkURL : fmt .Sprintf ("https://example.com/badge-%d" , rInt ),
31
- ImageURL : fmt .Sprintf ("https://example.com/badge-%d.svg" , rInt ),
32
- }),
32
+ resource .TestCheckResourceAttrSet ("gitlab_project_badge.this" , "rendered_link_url" ),
33
+ resource .TestCheckResourceAttrSet ("gitlab_project_badge.this" , "rendered_image_url" ),
33
34
),
34
35
},
35
- // Test ImportState
36
+ // Verify Import
36
37
{
37
- ResourceName : "gitlab_project_badge.foo " ,
38
+ ResourceName : "gitlab_project_badge.this " ,
38
39
ImportState : true ,
39
40
ImportStateVerify : true ,
40
41
},
41
42
// Update the project badge
42
43
{
43
- Config : testAccGitlabProjectBadgeUpdateConfig (rInt ),
44
+ Config : fmt .Sprintf (`
45
+ resource "gitlab_project_badge" "this" {
46
+ project = "%d"
47
+ link_url = "https://example.com/badge-updated"
48
+ image_url = "https://example.com/badge-updated.svg"
49
+ name = "badge-updated"
50
+ }
51
+ ` , testProject .ID ),
44
52
Check : resource .ComposeTestCheckFunc (
45
- testAccCheckGitlabProjectBadgeExists ("gitlab_project_badge.foo" , & badge ),
46
- testAccCheckGitlabProjectBadgeAttributes (& badge , & testAccGitlabProjectBadgeExpectedAttributes {
47
- LinkURL : fmt .Sprintf ("https://example.com/badge-%d" , rInt ),
48
- ImageURL : fmt .Sprintf ("https://example.com/badge-%d.svg" , rInt ),
49
- }),
53
+ resource .TestCheckResourceAttrSet ("gitlab_project_badge.this" , "rendered_link_url" ),
54
+ resource .TestCheckResourceAttrSet ("gitlab_project_badge.this" , "rendered_image_url" ),
50
55
),
51
56
},
57
+ // Verify Import
58
+ {
59
+ ResourceName : "gitlab_project_badge.this" ,
60
+ ImportState : true ,
61
+ ImportStateVerify : true ,
62
+ },
52
63
},
53
64
})
54
65
}
55
66
56
- func testAccCheckGitlabProjectBadgeExists (n string , badge * gitlab.ProjectBadge ) resource.TestCheckFunc {
57
- return func (s * terraform.State ) error {
58
- rs , ok := s .RootModule ().Resources [n ]
59
- if ! ok {
60
- return fmt .Errorf ("Not Found: %s" , n )
67
+ func testAccCheckGitlabProjectBadgeDestroy (s * terraform.State ) error {
68
+ for _ , rs := range s .RootModule ().Resources {
69
+ if rs .Type != "gitlab_project_badge" {
70
+ continue
61
71
}
62
72
63
- splitID := strings .Split (rs .Primary .ID , ":" )
64
-
65
- badgeID , err := strconv .Atoi (splitID [len (splitID )- 1 ])
73
+ projectID , badgeID , err := resourceGitlabProjectBadgeParseID (rs .Primary .ID )
66
74
if err != nil {
67
75
return err
68
76
}
69
- repoName := rs .Primary .Attributes ["project" ]
70
- if repoName == "" {
71
- return fmt .Errorf ("No project ID is set" )
72
- }
73
77
74
- gotBadge , _ , err := testGitlabClient .ProjectBadges .GetProjectBadge (repoName , badgeID )
78
+ gotBadge , _ , err := testGitlabClient .ProjectBadges .GetProjectBadge (projectID , badgeID )
75
79
if err != nil {
76
- return err
77
- }
78
- * badge = * gotBadge
79
- return nil
80
- }
81
- }
82
-
83
- type testAccGitlabProjectBadgeExpectedAttributes struct {
84
- LinkURL string
85
- ImageURL string
86
- }
87
-
88
- func testAccCheckGitlabProjectBadgeAttributes (badge * gitlab.ProjectBadge , want * testAccGitlabProjectBadgeExpectedAttributes ) resource.TestCheckFunc {
89
- return func (s * terraform.State ) error {
90
- if badge .LinkURL != want .LinkURL {
91
- return fmt .Errorf ("got link_url %q; want %q" , badge .LinkURL , want .LinkURL )
92
- }
93
-
94
- if badge .ImageURL != want .ImageURL {
95
- return fmt .Errorf ("got image_url %s; want %s" , badge .ImageURL , want .ImageURL )
80
+ if ! is404 (err ) {
81
+ return err
82
+ }
83
+ return nil
96
84
}
97
85
98
- return nil
99
- }
100
- }
101
-
102
- func testAccCheckGitlabProjectBadgeDestroy (s * terraform.State ) error {
103
- for _ , rs := range s .RootModule ().Resources {
104
- if rs .Type != "gitlab_project" {
105
- continue
86
+ if gotBadge != nil {
87
+ return fmt .Errorf ("Badge still exists" )
106
88
}
107
89
108
- gotRepo , resp , err := testGitlabClient .Projects .GetProject (rs .Primary .ID , nil )
109
- if err == nil {
110
- if gotRepo != nil && fmt .Sprintf ("%d" , gotRepo .ID ) == rs .Primary .ID {
111
- if gotRepo .MarkedForDeletionAt == nil {
112
- return fmt .Errorf ("Repository still exists" )
113
- }
114
- }
115
- }
116
- if resp .StatusCode != 404 {
117
- return err
118
- }
119
90
return nil
120
91
}
121
92
return nil
122
93
}
123
-
124
- func testAccGitlabProjectBadgeConfig (rInt int ) string {
125
- return fmt .Sprintf (`
126
- resource "gitlab_project" "foo" {
127
- name = "foo-%d"
128
- description = "Terraform acceptance tests"
129
-
130
- # So that acceptance tests can be run in a gitlab organization
131
- # with no billing
132
- visibility_level = "public"
133
- }
134
-
135
- resource "gitlab_project_badge" "foo" {
136
- project = "${gitlab_project.foo.id}"
137
- link_url = "https://example.com/badge-%d"
138
- image_url = "https://example.com/badge-%d.svg"
139
- }
140
- ` , rInt , rInt , rInt )
141
- }
142
-
143
- func testAccGitlabProjectBadgeUpdateConfig (rInt int ) string {
144
- return fmt .Sprintf (`
145
- resource "gitlab_project" "foo" {
146
- name = "foo-%d"
147
- description = "Terraform acceptance tests"
148
-
149
- # So that acceptance tests can be run in a gitlab organization
150
- # with no billing
151
- visibility_level = "public"
152
- }
153
-
154
- resource "gitlab_project_badge" "foo" {
155
- project = "${gitlab_project.foo.id}"
156
- link_url = "https://example.com/badge-%d"
157
- image_url = "https://example.com/badge-%d.svg"
158
- }
159
- ` , rInt , rInt , rInt )
160
- }
0 commit comments