-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathresource_source_test.go
188 lines (166 loc) · 5.86 KB
/
resource_source_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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
package main
import (
"context"
"fmt"
"github.com/hashicorp/terraform-plugin-sdk/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/terraform"
"os"
"testing"
)
const (
testAccIdentitynowSourceType = "identitynow_source"
)
var (
testAccSource string
testAccSourceUpdate string
rName string
)
func init() {
ownerId := os.Getenv("IDENTITYNOW_EXTERNAL_OWNER_ID")
ownerName := os.Getenv("IDENTITYNOW_OWNER_NAME")
clusterId := os.Getenv("IDENTITYNOW_CLUSTER_ID")
clusterName := os.Getenv("IDENTITYNOW_CLUSTER_NAME")
rName = acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum)
testAccSource = `
resource "` + testAccIdentitynowSourceType + `" "foo" {
name = "` + rName + `"
description = "The Azure Active Directory acceptance test"
connector = "azure-active-directory"
authoritative = false
delete_threshold = 10
owner {
id = "` + ownerId + `"
name = "` + ownerName + `"
type = "IDENTITY"
}
cluster {
id = "` + clusterId + `"
name = "` + clusterName + `"
type = "CLUSTER"
}
connector_attributes {
grant_type = "CLIENT_CREDENTIALS"
client_id = "test-client-id"
client_secret = "test-client-secret"
domain_name = "us1.axonengineering.io"
ms_graph_resource_base = "https://graph.microsoft.us"
ms_graph_token_base = "https://login.microsoftonline.us"
azure_ad_graph_resource_base = "https://graph.microsoftazure.us"
azure_ad_graph_token_base = "https://login.microsoftonline.us"
}
}
`
testAccSourceUpdate = `
resource "identitynow_source" "foo" {
name = "` + rName + `"
description = "The Azure Active Directory acceptance test update"
connector = "azure-active-directory"
delete_threshold = 10
authoritative = false
owner {
id = "` + ownerId + `"
name = "` + ownerName + `"
type = "IDENTITY"
}
cluster {
id = "` + clusterId + `"
name = "` + clusterName + `"
type = "CLUSTER"
}
connector_attributes {
grant_type = "CLIENT_CREDENTIALS"
client_id = "test-client-id"
client_secret = "test-client-secret"
domain_name = "us1.axonengineering.io"
ms_graph_resource_base = "https://graph.microsoft.us"
ms_graph_token_base = "https://login.microsoftonline.us"
azure_ad_graph_resource_base = "https://graph.microsoftazure.us"
azure_ad_graph_token_base = "https://login.microsoftonline.us"
}
}
`
}
func TestAccSource_basic(t *testing.T) {
var source Source
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccIdentitynowSourceDestroy,
Steps: []resource.TestStep{
{
Config: testAccSource,
Check: resource.ComposeTestCheckFunc(
testAccCheckSourceExist(testAccIdentitynowSourceType+".foo", source),
resource.TestCheckResourceAttr(testAccIdentitynowSourceType+".foo", "name", rName),
resource.TestCheckResourceAttr(testAccIdentitynowSourceType+".foo", "description", "The Azure Active Directory acceptance test"),
resource.TestCheckResourceAttr(testAccIdentitynowSourceType+".foo", "connector", "azure-active-directory"),
resource.TestCheckResourceAttr(testAccIdentitynowSourceType+".foo", "authoritative", "false"),
resource.TestCheckResourceAttr(testAccIdentitynowSourceType+".foo", "delete_threshold", "10"),
),
},
{
Config: testAccSourceUpdate,
Check: resource.ComposeTestCheckFunc(
testAccCheckSourceExist(testAccIdentitynowSourceType+".foo", source),
resource.TestCheckResourceAttr(testAccIdentitynowSourceType+".foo", "name", rName),
resource.TestCheckResourceAttr(testAccIdentitynowSourceType+".foo", "description", "The Azure Active Directory acceptance test update"),
resource.TestCheckResourceAttr(testAccIdentitynowSourceType+".foo", "connector", "azure-active-directory"),
resource.TestCheckResourceAttr(testAccIdentitynowSourceType+".foo", "authoritative", "false"),
resource.TestCheckResourceAttr(testAccIdentitynowSourceType+".foo", "delete_threshold", "10"),
),
},
{
Config: testAccSource,
Check: resource.ComposeTestCheckFunc(
testAccCheckSourceExist(testAccIdentitynowSourceType+".foo", source),
resource.TestCheckResourceAttr(testAccIdentitynowSourceType+".foo", "name", rName),
resource.TestCheckResourceAttr(testAccIdentitynowSourceType+".foo", "description", "The Azure Active Directory acceptance test"),
resource.TestCheckResourceAttr(testAccIdentitynowSourceType+".foo", "connector", "azure-active-directory"),
resource.TestCheckResourceAttr(testAccIdentitynowSourceType+".foo", "authoritative", "false"),
),
},
},
})
}
func testAccIdentitynowSourceDestroy(state *terraform.State) error {
for _, rs := range state.RootModule().Resources {
if rs.Type != testAccIdentitynowSourceType {
continue
}
client, err := testAccProvider.Meta().(*Config).IdentityNowClient()
if err != nil {
return err
}
foundSource, _ := client.GetSource(context.Background(), rs.Primary.ID)
if len(foundSource.ID) > 0 &&
foundSource.ID != rs.Primary.ID {
return fmt.Errorf("Source still exist")
}
}
return nil
}
func testAccCheckSourceExist(name string, source Source) resource.TestCheckFunc {
return func(state *terraform.State) error {
rs, ok := state.RootModule().Resources[name]
if !ok {
return fmt.Errorf("Not found: %s", name)
}
if rs.Primary.ID == "" {
return fmt.Errorf("No source ID is set")
}
client, err := testAccProvider.Meta().(*Config).IdentityNowClient()
if err != nil {
return err
}
foundSource, err := client.GetSource(context.Background(), rs.Primary.ID)
// we expect a single Source by this ID. If we find zero
// then we consider this an error
if len(foundSource.ID) == 0 ||
foundSource.ID != rs.Primary.ID || err != nil {
return fmt.Errorf("Source not found.")
}
source = *foundSource
return nil
}
}