Skip to content

Commit 363b22f

Browse files
committed
fix(certificates): plugin crashes because of invalid certificate resource layout
1 parent b9d959f commit 363b22f

File tree

3 files changed

+85
-11
lines changed

3 files changed

+85
-11
lines changed

provider/data_source_iis_certificates.go

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -69,16 +69,21 @@ func dataSourceIisCertificatesRead(ctx context.Context, d *schema.ResourceData,
6969
}
7070

7171
func mapCertificatesToSet(certificates []iis.Certificate) *schema.Set {
72-
result := schema.NewSet(schema.HashString, []interface{}{})
72+
var set []interface{}
7373
for _, certificate := range certificates {
74-
mapped := map[string]interface{}{
74+
set = append(set, map[string]interface{}{
7575
AliasKey: certificate.Alias,
7676
IdKey: certificate.ID,
7777
IssuedByKey: certificate.IssuedBy,
7878
SubjectKey: certificate.Subject,
7979
ThumbprintKey: certificate.Thumbprint,
80-
}
81-
result.Add(mapped)
80+
})
8281
}
83-
return result
82+
return schema.NewSet(hashCertificate, set)
83+
}
84+
85+
func hashCertificate(v interface{}) int {
86+
certificateMap := v.(map[string]interface{})
87+
88+
return schema.HashString(certificateMap[IdKey].(string))
8489
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package provider
2+
3+
import (
4+
"os"
5+
"testing"
6+
7+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
8+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
9+
)
10+
11+
func testAccPreCheck(t *testing.T) {
12+
if v := os.Getenv("IIS_HOST"); v == "" {
13+
t.Skip("IIS_HOST must be set for acceptance tests")
14+
}
15+
if v := os.Getenv("IIS_ACCESS_KEY"); v == "" {
16+
t.Skip("IIS_ACCESS_KEY must be set for acceptance tests")
17+
}
18+
}
19+
20+
func TestAccDataSourceIisCertificates_basic(t *testing.T) {
21+
resource.Test(t, resource.TestCase{
22+
PreCheck: func() { testAccPreCheck(t) },
23+
ProviderFactories: map[string]func() (*schema.Provider, error){
24+
"iis": func() (*schema.Provider, error) {
25+
return Provider(), nil
26+
},
27+
},
28+
Steps: []resource.TestStep{
29+
{
30+
Config: testAccDataSourceIisCertificatesConfig,
31+
Check: resource.ComposeTestCheckFunc(
32+
resource.TestCheckResourceAttrSet("data.iis_certificates.test", "certificates.#"),
33+
),
34+
},
35+
},
36+
})
37+
}
38+
39+
const testAccDataSourceIisCertificatesConfig = `
40+
data "iis_certificates" "test" {}
41+
`

provider/provider.go

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,16 @@ func Provider() *schema.Provider {
1515
return &schema.Provider{
1616
Schema: map[string]*schema.Schema{
1717
"access_key": {
18-
Type: schema.TypeString,
19-
Required: true,
18+
Type: schema.TypeString,
19+
Optional: true,
20+
DefaultFunc: schema.EnvDefaultFunc("IIS_ACCESS_KEY", nil),
21+
Description: "The access key for IIS authentication. Can also be sourced from the IIS_ACCESS_KEY environment variable.",
2022
},
2123
"host": {
22-
Type: schema.TypeString,
23-
Required: true,
24+
Type: schema.TypeString,
25+
Optional: true,
26+
DefaultFunc: schema.EnvDefaultFunc("IIS_HOST", nil),
27+
Description: "The IIS host URL. Can also be sourced from the IIS_HOST environment variable.",
2428
},
2529
},
2630
ResourcesMap: map[string]*schema.Resource{
@@ -38,6 +42,30 @@ func Provider() *schema.Provider {
3842
}
3943

4044
func providerConfigure(ctx context.Context, d *schema.ResourceData) (interface{}, diag.Diagnostics) {
45+
var diags diag.Diagnostics
46+
47+
host := d.Get("host").(string)
48+
if host == "" {
49+
diags = append(diags, diag.Diagnostic{
50+
Severity: diag.Error,
51+
Summary: "Missing IIS Host",
52+
Detail: "The IIS host must be configured via the 'host' argument or IIS_HOST environment variable.",
53+
})
54+
}
55+
56+
accessKey := d.Get("access_key").(string)
57+
if accessKey == "" {
58+
diags = append(diags, diag.Diagnostic{
59+
Severity: diag.Error,
60+
Summary: "Missing IIS Access Key",
61+
Detail: "The IIS access key must be configured via the 'access_key' argument or IIS_ACCESS_KEY environment variable.",
62+
})
63+
}
64+
65+
if diags.HasError() {
66+
return nil, diags
67+
}
68+
4169
transport := &http.Transport{
4270
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
4371
}
@@ -46,8 +74,8 @@ func providerConfigure(ctx context.Context, d *schema.ResourceData) (interface{}
4674
HttpClient: http.Client{
4775
Transport: loggingTransport,
4876
},
49-
Host: d.Get("host").(string),
50-
AccessKey: d.Get("access_key").(string),
77+
Host: host,
78+
AccessKey: accessKey,
5179
}
5280

5381
return client, nil

0 commit comments

Comments
 (0)