forked from hashicorp/terraform-provider-azurerm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnginx_api_key_resource_test.go
More file actions
193 lines (171 loc) · 6.15 KB
/
nginx_api_key_resource_test.go
File metadata and controls
193 lines (171 loc) · 6.15 KB
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
189
190
191
192
193
// Copyright IBM Corp. 2014, 2025
// SPDX-License-Identifier: MPL-2.0
package nginx_test
import (
"context"
"fmt"
"testing"
"time"
"github.com/google/uuid"
"github.com/hashicorp/go-azure-helpers/lang/pointer"
"github.com/hashicorp/go-azure-sdk/resource-manager/nginx/2024-11-01-preview/nginxapikey"
"github.com/hashicorp/terraform-provider-azurerm/internal/acceptance"
"github.com/hashicorp/terraform-provider-azurerm/internal/acceptance/check"
"github.com/hashicorp/terraform-provider-azurerm/internal/clients"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/nginx"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
)
type APIKeyResource struct{}
func (a APIKeyResource) Exists(ctx context.Context, client *clients.Client, state *pluginsdk.InstanceState) (*bool, error) {
id, err := nginxapikey.ParseApiKeyID(state.ID)
if err != nil {
return nil, err
}
resp, err := client.Nginx.NginxApiKey.ApiKeysGet(ctx, *id)
if err != nil {
return nil, fmt.Errorf("retrieving %s: %+v", id, err)
}
return pointer.To(resp.Model != nil), nil
}
func TestAccAPIKeyResource_complete(t *testing.T) {
data := acceptance.BuildTestData(t, nginx.APIKeyResource{}.ResourceType(), "test")
secretText := uuid.NewString()
endDateTime := getEndDateTime(3)
r := APIKeyResource{}
data.ResourceTest(t, r, []acceptance.TestStep{
{
Config: r.complete(data, secretText, endDateTime),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
check.That(data.ResourceName).Key("end_date_time").HasValue(endDateTime),
check.That(data.ResourceName).Key("hint").HasValue(secretText[:3]),
),
},
// The secret_text is provided by the user and never returned by the API.
data.ImportStep("secret_text"),
})
}
func TestAccAPIKeyResource_update(t *testing.T) {
data := acceptance.BuildTestData(t, nginx.APIKeyResource{}.ResourceType(), "test")
secretTextCreate := uuid.NewString()
secretTextUpdate := uuid.NewString()
endDateTimeCreate := getEndDateTime(3)
endDateTimeUpdate := getEndDateTime(6)
r := APIKeyResource{}
data.ResourceTest(t, r, []acceptance.TestStep{
{
Config: r.complete(data, secretTextCreate, endDateTimeCreate),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
check.That(data.ResourceName).Key("end_date_time").HasValue(endDateTimeCreate),
check.That(data.ResourceName).Key("hint").HasValue(secretTextCreate[:3]),
),
},
// The secret_text is provided by the user and never returned by the API.
data.ImportStep("secret_text"),
{
Config: r.complete(data, secretTextUpdate, endDateTimeUpdate),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
check.That(data.ResourceName).Key("end_date_time").HasValue(endDateTimeUpdate),
check.That(data.ResourceName).Key("hint").HasValue(secretTextUpdate[:3]),
),
},
// The secret_text is provided by the user and never returned by the API.
data.ImportStep("secret_text"),
})
}
func TestAccAPIKeyResource_requiresImport(t *testing.T) {
data := acceptance.BuildTestData(t, nginx.APIKeyResource{}.ResourceType(), "test")
secretText := uuid.NewString()
endDateTime := getEndDateTime(3)
r := APIKeyResource{}
data.ResourceTest(t, r, []acceptance.TestStep{
{
Config: r.complete(data, secretText, endDateTime),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
),
},
data.RequiresImportErrorStep(r.requiresImport),
})
}
func getEndDateTime(monthsFromNow int) string {
return time.Now().AddDate(0, monthsFromNow, 0).UTC().Format("2006-01-02T15:04:05+00:00")
}
func (a APIKeyResource) complete(data acceptance.TestData, secretText, endDateTime string) string {
return fmt.Sprintf(`
%s
resource "azurerm_nginx_api_key" "test" {
name = "acctest-%d"
nginx_deployment_id = azurerm_nginx_deployment.test.id
end_date_time = "%s"
secret_text = "%s"
}
`, a.template(data), data.RandomInteger, endDateTime, secretText)
}
func (a APIKeyResource) requiresImport(data acceptance.TestData) string {
secretText := uuid.NewString()
endDateTime := getEndDateTime(3)
return fmt.Sprintf(`
%s
resource "azurerm_nginx_api_key" "import" {
name = azurerm_nginx_api_key.test.name
nginx_deployment_id = azurerm_nginx_api_key.test.nginx_deployment_id
end_date_time = azurerm_nginx_api_key.test.end_date_time
secret_text = azurerm_nginx_api_key.test.secret_text
}
`, a.complete(data, secretText, endDateTime))
}
func (a APIKeyResource) template(data acceptance.TestData) string {
return fmt.Sprintf(`
provider "azurerm" {
features {}
}
resource "azurerm_resource_group" "test" {
name = "acctestRG-%[1]d"
location = "%[2]s"
}
resource "azurerm_public_ip" "test" {
name = "acctest%[1]d"
resource_group_name = azurerm_resource_group.test.name
location = azurerm_resource_group.test.location
allocation_method = "Static"
sku = "Standard"
}
resource "azurerm_virtual_network" "test" {
name = "acctestvirtnet%[1]d"
address_space = ["10.0.0.0/16"]
location = azurerm_resource_group.test.location
resource_group_name = azurerm_resource_group.test.name
}
resource "azurerm_subnet" "test" {
name = "acctestsubnet%[1]d"
resource_group_name = azurerm_resource_group.test.name
virtual_network_name = azurerm_virtual_network.test.name
address_prefixes = ["10.0.2.0/24"]
delegation {
name = "delegation"
service_delegation {
name = "NGINX.NGINXPLUS/nginxDeployments"
actions = [
"Microsoft.Network/virtualNetworks/subnets/join/action",
]
}
}
}
resource "azurerm_nginx_deployment" "test" {
name = "acctest-%[1]d"
resource_group_name = azurerm_resource_group.test.name
sku = "standardv3_Monthly"
capacity = 10
location = azurerm_resource_group.test.location
frontend_public {
ip_address = [azurerm_public_ip.test.id]
}
network_interface {
subnet_id = azurerm_subnet.test.id
}
}
`, data.RandomInteger, data.Locations.Primary, data.RandomString)
}