Skip to content

Commit 47641d6

Browse files
fix(datastore): send revision in updates (#65)
* fix(datastore): send revision in updates * formatting
1 parent 63688b8 commit 47641d6

File tree

4 files changed

+128
-54
lines changed

4 files changed

+128
-54
lines changed

resources/datastore_test.go

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package resources
33
import (
44
"errors"
55
"fmt"
6+
"os"
67
"regexp"
78
"testing"
89

@@ -29,7 +30,7 @@ func TestDatastore_Create(t *testing.T) {
2930

3031
// terraform acceptance test case
3132
resource.Test(t, resource.TestCase{
32-
PreCheck: func() { checkEnv(t) },
33+
PreCheck: func() { checkDatastoreEnv(t) },
3334
CheckDestroy: testDatastoreDestroy,
3435
Providers: testAccProviders,
3536
Steps: []resource.TestStep{
@@ -49,6 +50,21 @@ func TestDatastore_Create(t *testing.T) {
4950
})
5051
}
5152

53+
func checkDatastoreEnv(t *testing.T) {
54+
if v := os.Getenv("TF_CLI_CONFIG_FILE"); v == "" {
55+
t.Fatal("TF_CLI_CONFIG_FILE must be set for acceptance tests, it should contain the dev_overrides config that points to local instance of the provider")
56+
}
57+
if v := os.Getenv("TF_VAR_zedcloud_token"); v == "" {
58+
t.Fatal("TF_VAR_zedcloud_token must be set for acceptance tests to access the zedcloud API")
59+
}
60+
// if v := os.Getenv("TF_VAR_api_key"); v == "" {
61+
// t.Fatal("TF_VAR_api_key must be set for acceptance tests to access the zedcloud API")
62+
// }
63+
// if v := os.Getenv("TF_VAR_api_password"); v == "" {
64+
// t.Fatal("TF_VAR_api_password must be set for acceptance tests to access the zedcloud API")
65+
// }
66+
}
67+
5268
func testDatastoreExists(resourceName string, datastoreModel *models.Datastore) resource.TestCheckFunc {
5369
return func(s *terraform.State) error {
5470
// retrieve the resource by name from state
@@ -88,6 +104,7 @@ func testDatastoreAttributes(t *testing.T, got, expected *models.Datastore) reso
88104
ignoredFields := []string{
89105
"ID",
90106
"Revision",
107+
"EncryptedSecrets",
91108
}
92109
// if expected.Proxy != nil && expected.Proxy.Proxies != nil {
93110
// ignoredFields = append(ignoredFields, "Proxy.Proxies")
Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,35 @@
11
resource "zedcloud_datastore" "test_datastore" {
2-
# required
3-
ds_fqdn = "my-datastore.my-company.com"
4-
ds_path = "download/AMD64"
5-
ds_type = "DATASTORE_TYPE_HTTPS"
6-
ds_status = "DATASTORE_STATUS_ACTIVE"
7-
name = "test"
8-
title = "title"
2+
# required
3+
ds_fqdn = "my-datastore.my-company.com"
4+
ds_path = "download/AMD64"
5+
ds_type = "DATASTORE_TYPE_AZUREBLOB"
6+
name = "test"
7+
title = "title"
8+
description = "description"
9+
region = "eu"
910

10-
# optional
11-
# api_key = "1234abcd"
12-
description = "description"
13-
need_clear_text = false
14-
region = "eu"
11+
# we cannot test for this, the API does not return any values
12+
# here so the plan command reports a diff after successfull test always
13+
# secret {
14+
# api_key = "api_key"
15+
# api_passwd = "api_password"
16+
# }
1517
}
18+
19+
# resource "zedcloud_datastore" "test_datastore" {
20+
# # required
21+
# ds_fqdn = "my-datastore.my-company.com"
22+
# ds_path = "download/AMD64"
23+
# ds_type = "DATASTORE_TYPE_AZUREBLOB"
24+
# # ds_status = "DATASTORE_STATUS_ACTIVE"
25+
# name = "test"
26+
# title = "title"
27+
28+
# # optional
29+
# description = "description"
30+
# region = "eu"
31+
# secret {
32+
# api_key = var.api_key
33+
# api_passwd = var.api_password
34+
# }
35+
# }
Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,29 @@
1-
dsfqdn: "https://my-datastore.my-company.com"
1+
dsfqdn: "my-datastore.my-company.com"
22
dspath: "download/AMD64"
3-
dstype: "DATASTORE_TYPE_HTTPS"
3+
dstype: "DATASTORE_TYPE_AZUREBLOB"
44
dsstatus: "DATASTORE_STATUS_ACTIVE"
55
name: "test"
66
title: "title"
77
# api_key: "1234abcd"
88
description: "description"
9-
enterprise_id: "1234"
109
need_clear_text: false
1110
region: "eu"
1211
encryptedsecrets: {}
12+
enterprise_id: "123"
1313
origintype: "ORIGIN_LOCAL"
1414
projectaccesslist: []
15+
16+
# dsfqdn: "https://my-datastore.my-company.com"
17+
# dspath: "download/AMD64"
18+
# dstype: "DATASTORE_TYPE_HTTPS"
19+
# dsstatus: "DATASTORE_STATUS_ACTIVE"
20+
# name: "test"
21+
# title: "title"
22+
# # api_key: "1234abcd"
23+
# description: "description"
24+
# enterprise_id: "1234"
25+
# need_clear_text: false
26+
# region: "eu"
27+
# encryptedsecrets: {}
28+
# origintype: "ORIGIN_LOCAL"
29+
# projectaccesslist: []

schemas/datastore.go

Lines changed: 60 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ import (
77
"github.com/zededa/terraform-provider/models"
88
)
99

10+
// Note, when providing secrets as variable instead of hard-coding them, the fields api_key, ds_err and enterprise_id
11+
// reports a diff in a subsequent plan command. This seems to be a bug in TF SDKv2 transition of nil values or similar.
12+
// So these fields are removed (commented) from the schema.
13+
1014
func DatastoreModel(d *schema.ResourceData) *models.Datastore {
1115
aPIKey, _ := d.Get("api_key").(string)
1216
var certificateChain *models.CertificateChain // CertificateChain
@@ -32,7 +36,7 @@ func DatastoreModel(d *schema.ResourceData) *models.Datastore {
3236
dsTypeModel := dsTypeInterface.(string)
3337
dsType = models.NewDatastoreType(models.DatastoreType(dsTypeModel))
3438
}
35-
enterpriseID, _ := d.Get("enterprise_id").(string)
39+
// enterpriseID, _ := d.Get("enterprise_id").(string)
3640
id, _ := d.Get("id").(string)
3741
name, _ := d.Get("name").(string)
3842
needClearText, _ := d.Get("need_clear_text").(bool)
@@ -62,22 +66,29 @@ func DatastoreModel(d *schema.ResourceData) *models.Datastore {
6266
}
6367
}
6468
title, _ := d.Get("title").(string)
69+
var revision *models.ObjectRevision // ObjectRevision
70+
revisionInterface, revisionIsSet := d.GetOk("revision")
71+
if revisionIsSet {
72+
revisionMap := revisionInterface.([]interface{})[0].(map[string]interface{})
73+
revision = ObjectRevisionModelFromMap(revisionMap)
74+
}
6575
return &models.Datastore{
66-
APIKey: aPIKey,
67-
CertificateChain: certificateChain,
68-
Description: description,
69-
DsFQDN: &dsFQDN, // string true false false
70-
DsPath: &dsPath, // string true false false
71-
DsStatus: dsStatus,
72-
DsType: dsType,
73-
EnterpriseID: enterpriseID,
76+
APIKey: aPIKey,
77+
CertificateChain: certificateChain,
78+
Description: description,
79+
DsFQDN: &dsFQDN,
80+
DsPath: &dsPath,
81+
DsStatus: dsStatus,
82+
DsType: dsType,
83+
// EnterpriseID: enterpriseID,
7484
ID: id,
75-
Name: &name, // string true false false
85+
Name: &name,
7686
NeedClearText: needClearText,
7787
ProjectAccessList: projectAccessList,
7888
Region: region,
7989
Secret: secret,
80-
Title: &title, // string true false false
90+
Title: &title,
91+
Revision: revision,
8192
}
8293
}
8394

@@ -107,7 +118,7 @@ func DatastoreModelFromMap(m map[string]interface{}) *models.Datastore {
107118
dsTypeModel := dsTypeInterface.(string)
108119
dsType = models.NewDatastoreType(models.DatastoreType(dsTypeModel))
109120
}
110-
enterpriseID := m["enterprise_id"].(string)
121+
// enterpriseID := m["enterprise_id"].(string)
111122
id := m["id"].(string)
112123
name := m["name"].(string)
113124
needClearText := m["need_clear_text"].(bool)
@@ -138,22 +149,31 @@ func DatastoreModelFromMap(m map[string]interface{}) *models.Datastore {
138149
}
139150
//
140151
title := m["title"].(string)
152+
var revision *models.ObjectRevision // ObjectRevision
153+
revisionInterface, revisionIsSet := m["revision"]
154+
if revisionIsSet && revisionInterface != nil {
155+
revisionMap := revisionInterface.([]interface{})
156+
if len(revisionMap) > 0 {
157+
revision = ObjectRevisionModelFromMap(revisionMap[0].(map[string]interface{}))
158+
}
159+
}
141160
return &models.Datastore{
142-
APIKey: aPIKey,
143-
CertificateChain: certificateChain,
144-
Description: description,
145-
DsFQDN: &dsFQDN,
146-
DsPath: &dsPath,
147-
DsStatus: dsStatus,
148-
DsType: dsType,
149-
EnterpriseID: enterpriseID,
161+
APIKey: aPIKey,
162+
CertificateChain: certificateChain,
163+
Description: description,
164+
DsFQDN: &dsFQDN,
165+
DsPath: &dsPath,
166+
DsStatus: dsStatus,
167+
DsType: dsType,
168+
// EnterpriseID: enterpriseID,
150169
ID: id,
151170
Name: &name,
152171
NeedClearText: needClearText,
153172
ProjectAccessList: projectAccessList,
154173
Region: region,
155174
Secret: secret,
156175
Title: &title,
176+
Revision: revision,
157177
}
158178
}
159179

@@ -162,13 +182,13 @@ func SetDatastoreResourceData(d *schema.ResourceData, m *models.Datastore) {
162182
d.Set("certificate_chain", SetCertificateChainSubResourceData([]*models.CertificateChain{m.CertificateChain}))
163183
d.Set("crypto_key", m.CryptoKey)
164184
d.Set("description", m.Description)
165-
d.Set("ds_err", m.DsErr)
185+
// d.Set("ds_err", m.DsErr)
166186
d.Set("ds_fqdn", m.DsFQDN)
167187
d.Set("ds_path", m.DsPath)
168188
d.Set("ds_status", m.DsStatus)
169189
d.Set("ds_type", m.DsType)
170190
d.Set("encrypted_secrets", m.EncryptedSecrets)
171-
d.Set("enterprise_id", m.EnterpriseID)
191+
// d.Set("enterprise_id", m.EnterpriseID)
172192
d.Set("id", m.ID)
173193
d.Set("name", m.Name)
174194
d.Set("need_clear_text", m.NeedClearText)
@@ -180,7 +200,6 @@ func SetDatastoreResourceData(d *schema.ResourceData, m *models.Datastore) {
180200
d.Set("title", m.Title)
181201
}
182202

183-
// Iterate through and update the DatastoreInfo resource data within a pagination response (typically defined in the items array field) retrieved from a READ operation for multiple LM resources
184203
func SetDatastoreInfoSubResourceData(m []*models.Datastore) (d []*map[string]interface{}) {
185204
for _, DatastoreInfoModel := range m {
186205
if DatastoreInfoModel != nil {
@@ -189,13 +208,13 @@ func SetDatastoreInfoSubResourceData(m []*models.Datastore) (d []*map[string]int
189208
properties["certificate_chain"] = SetCertificateChainSubResourceData([]*models.CertificateChain{DatastoreInfoModel.CertificateChain})
190209
properties["crypto_key"] = DatastoreInfoModel.CryptoKey
191210
properties["description"] = DatastoreInfoModel.Description
192-
properties["ds_err"] = DatastoreInfoModel.DsErr
211+
// properties["ds_err"] = DatastoreInfoModel.DsErr
193212
properties["ds_fqdn"] = DatastoreInfoModel.DsFQDN
194213
properties["ds_path"] = DatastoreInfoModel.DsPath
195214
properties["ds_status"] = DatastoreInfoModel.DsStatus
196215
properties["ds_type"] = DatastoreInfoModel.DsType
197216
properties["encrypted_secrets"] = DatastoreInfoModel.EncryptedSecrets
198-
properties["enterprise_id"] = DatastoreInfoModel.EnterpriseID
217+
// properties["enterprise_id"] = DatastoreInfoModel.EnterpriseID
199218
properties["id"] = DatastoreInfoModel.ID
200219
properties["name"] = DatastoreInfoModel.Name
201220
properties["need_clear_text"] = DatastoreInfoModel.NeedClearText
@@ -211,7 +230,6 @@ func SetDatastoreInfoSubResourceData(m []*models.Datastore) (d []*map[string]int
211230
return
212231
}
213232

214-
// Schema mapping representing the DatastoreInfo resource defined in the Terraform configuration
215233
func Datastore() map[string]*schema.Schema {
216234
return map[string]*schema.Schema{
217235
"api_key": {
@@ -244,11 +262,11 @@ func Datastore() map[string]*schema.Schema {
244262
Optional: true,
245263
},
246264

247-
"ds_err": {
248-
Description: `Datastore validation detailed error/status message`,
249-
Type: schema.TypeString,
250-
Computed: true,
251-
},
265+
// "ds_err": {
266+
// Description: `Datastore validation detailed error/status message`,
267+
// Type: schema.TypeString,
268+
// Computed: true,
269+
// },
252270

253271
"ds_fqdn": {
254272
Description: `Datastore Fully Qualified Domain Name`,
@@ -272,6 +290,7 @@ func Datastore() map[string]*schema.Schema {
272290
Description: `Datastore status`,
273291
Type: schema.TypeString,
274292
Optional: true,
293+
Computed: true,
275294
},
276295

277296
"ds_type": {
@@ -290,11 +309,11 @@ func Datastore() map[string]*schema.Schema {
290309
Sensitive: true,
291310
},
292311

293-
"enterprise_id": {
294-
Description: ``,
295-
Type: schema.TypeString,
296-
Optional: true,
297-
},
312+
// "enterprise_id": {
313+
// Description: ``,
314+
// Type: schema.TypeString,
315+
// Optional: true,
316+
// },
298317

299318
"id": {
300319
Description: `System defined universally unique Id of the datastore.`,
@@ -312,6 +331,9 @@ func Datastore() map[string]*schema.Schema {
312331
Description: `knob for sending creds in clear text`,
313332
Type: schema.TypeBool,
314333
Optional: true,
334+
DiffSuppressFunc: func(k, oldValue, newValue string, d *schema.ResourceData) bool {
335+
return true
336+
},
315337
},
316338

317339
"origin_type": {
@@ -336,11 +358,12 @@ func Datastore() map[string]*schema.Schema {
336358
},
337359

338360
"revision": {
339-
Description: `system defined info`,
361+
Description: `Object revision details`,
340362
Type: schema.TypeList, //GoType: ObjectRevision
341363
Elem: &schema.Resource{
342364
Schema: ObjectRevision(),
343365
},
366+
Optional: true,
344367
Computed: true,
345368
},
346369

@@ -352,7 +375,6 @@ func Datastore() map[string]*schema.Schema {
352375
},
353376
Optional: true,
354377
Sensitive: true,
355-
Computed: true,
356378
},
357379

358380
"title": {

0 commit comments

Comments
 (0)