-
Notifications
You must be signed in to change notification settings - Fork 14
/
resource_contentful_apikey_test.go
153 lines (125 loc) · 4 KB
/
resource_contentful_apikey_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
package main
import (
"fmt"
"testing"
"github.com/hashicorp/terraform/helper/acctest"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
contentful "github.com/tolgaakyuz/contentful-go"
)
func TestAccContentfulAPIKey_Basic(t *testing.T) {
var apiKey contentful.APIKey
spaceName := fmt.Sprintf("space-name-%s", acctest.RandString(3))
name := fmt.Sprintf("apikey-name-%s", acctest.RandString(3))
description := fmt.Sprintf("apikey-description-%s", acctest.RandString(3))
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccContentfulAPIKeyDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccContentfulAPIKeyConfig(spaceName, name, description),
Check: resource.ComposeTestCheckFunc(
testAccCheckContentfulAPIKeyExists("contentful_apikey.myapikey", &apiKey),
testAccCheckContentfulAPIKeyAttributes(&apiKey, map[string]interface{}{
"name": name,
"description": description,
}),
),
},
resource.TestStep{
Config: testAccContentfulAPIKeyUpdateConfig(spaceName, name, description),
Check: resource.ComposeTestCheckFunc(
testAccCheckContentfulAPIKeyExists("contentful_apikey.myapikey", &apiKey),
testAccCheckContentfulAPIKeyAttributes(&apiKey, map[string]interface{}{
"name": fmt.Sprintf("%s-updated", name),
"description": fmt.Sprintf("%s-updated", description),
}),
),
},
},
})
}
func testAccCheckContentfulAPIKeyExists(n string, apiKey *contentful.APIKey) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]
if !ok {
return fmt.Errorf("Not Found: %s", n)
}
spaceID := rs.Primary.Attributes["space_id"]
if spaceID == "" {
return fmt.Errorf("No space_id is set")
}
apiKeyID := rs.Primary.ID
if apiKeyID == "" {
return fmt.Errorf("No api key ID is set")
}
client := testAccProvider.Meta().(*contentful.Contentful)
contentfulAPIKey, err := client.APIKeys.Get(spaceID, apiKeyID)
if err != nil {
return err
}
*apiKey = *contentfulAPIKey
return nil
}
}
func testAccCheckContentfulAPIKeyAttributes(apiKey *contentful.APIKey, attrs map[string]interface{}) resource.TestCheckFunc {
return func(s *terraform.State) error {
name := attrs["name"].(string)
if apiKey.Name != name {
return fmt.Errorf("APIKey name does not match: %s, %s", apiKey.Name, name)
}
description := attrs["description"].(string)
if apiKey.Description != description {
return fmt.Errorf("APIKey description does not match: %s, %s", apiKey.Description, description)
}
return nil
}
}
func testAccContentfulAPIKeyDestroy(s *terraform.State) error {
for _, rs := range s.RootModule().Resources {
if rs.Type != "contentful_apikey" {
continue
}
// get space id from resource data
spaceID := rs.Primary.Attributes["space_id"]
if spaceID == "" {
return fmt.Errorf("No space_id is set")
}
apiKeyID := rs.Primary.ID
if apiKeyID == "" {
return fmt.Errorf("No apikey ID is set")
}
client := testAccProvider.Meta().(*contentful.Contentful)
_, err := client.APIKeys.Get(spaceID, apiKeyID)
if _, ok := err.(contentful.NotFoundError); ok {
return nil
}
return fmt.Errorf("Api Key still exists with id: %s", rs.Primary.ID)
}
return nil
}
func testAccContentfulAPIKeyConfig(spaceName, name, description string) string {
return fmt.Sprintf(`
resource "contentful_space" "myspace" {
name = "%s"
}
resource "contentful_apikey" "myapikey" {
space_id = "${contentful_space.myspace.id}"
name = "%s"
description = "%s"
}
`, spaceName, name, description)
}
func testAccContentfulAPIKeyUpdateConfig(spaceName, name, description string) string {
return fmt.Sprintf(`
resource "contentful_space" "myspace" {
name = "%s"
}
resource "contentful_apikey" "myapikey" {
space_id = "${contentful_space.myspace.id}"
name = "%s-updated"
description = "%s-updated"
}
`, spaceName, name, description)
}