diff --git a/cyral/resource_cyral_repository.go b/cyral/resource_cyral_repository.go index 91e33185..bde00885 100644 --- a/cyral/resource_cyral_repository.go +++ b/cyral/resource_cyral_repository.go @@ -33,10 +33,6 @@ type RepoData struct { Properties *RepositoryProperties `json:"properties,omitempty"` } -func (data *RepoData) IsReplicaSet() bool { - return data.Properties != nil && data.Properties.MongoDBServerType == mongodbReplicaSetServerType -} - func (data *RepoData) WriteToSchema(d *schema.ResourceData) { d.Set("type", data.RepoType) d.Set("host", data.Host) @@ -44,11 +40,16 @@ func (data *RepoData) WriteToSchema(d *schema.ResourceData) { d.Set("name", data.Name) d.Set("labels", data.Labels) + if properties := data.PropertiesAsInterface(); properties != nil { + d.Set("properties", properties) + } +} + +func (data *RepoData) PropertiesAsInterface() []interface{} { var properties []interface{} if data.Properties != nil { - propertiesMap := make(map[string]interface{}) - if data.IsReplicaSet() { + propertiesMap := make(map[string]interface{}) var rset []interface{} rsetMap := make(map[string]interface{}) rsetMap["max_nodes"] = data.MaxAllowedListeners @@ -56,12 +57,15 @@ func (data *RepoData) WriteToSchema(d *schema.ResourceData) { rset = append(rset, rsetMap) propertiesMap["mongodb_replica_set"] = rset + properties = append(properties, propertiesMap) } - - properties = append(properties, propertiesMap) } - d.Set("properties", properties) + return properties +} + +func (data *RepoData) IsReplicaSet() bool { + return data.Properties != nil && data.Properties.MongoDBServerType == mongodbReplicaSetServerType } // RepositoryProperties relates to the field "properties" of the v1/repos @@ -294,8 +298,8 @@ func getRepoDataFromResource(c *client.Client, d *schema.ResourceData) (RepoData var maxAllowedListeners uint32 var properties *RepositoryProperties if propertiesIface, ok := d.Get("properties").(*schema.Set); ok { - properties = new(RepositoryProperties) for _, propertiesMap := range propertiesIface.List() { + properties = new(RepositoryProperties) propertiesMap := propertiesMap.(map[string]interface{}) // Replica set properties diff --git a/cyral/resource_cyral_repository_test.go b/cyral/resource_cyral_repository_test.go index c16e95c3..6b15f846 100644 --- a/cyral/resource_cyral_repository_test.go +++ b/cyral/resource_cyral_repository_test.go @@ -23,12 +23,19 @@ var updatedRepoConfig RepoData = RepoData{ Labels: []string{"rds", "us-east-1"}, } +var emptyPropertiesRepoConfig RepoData = RepoData{ + Name: "repo-test-empty-properties", + Host: "mongo-cluster.local", + Port: 27017, + RepoType: "mongodb", + Properties: &RepositoryProperties{}, +} + var replicaSetRepoConfig RepoData = RepoData{ Name: "repo-test-replica-set", Host: "mongo-cluster.local", Port: 27017, RepoType: "mongodb", - Labels: []string{"rds", "us-east-1"}, MaxAllowedListeners: 2, Properties: &RepositoryProperties{ MongoDBReplicaSetName: "replica-set-1", @@ -39,6 +46,7 @@ var replicaSetRepoConfig RepoData = RepoData{ func TestAccRepositoryResource(t *testing.T) { testConfig, testFunc := setupRepositoryTest(initialRepoConfig) testUpdateConfig, testUpdateFunc := setupRepositoryTest(updatedRepoConfig) + testEmptyPropertiesConfig, testEmptyPropertiesFunc := setupRepositoryTest(emptyPropertiesRepoConfig) testReplicaSetConfig, testReplicaSetFunc := setupRepositoryTest(replicaSetRepoConfig) resource.Test(t, resource.TestCase{ @@ -52,6 +60,10 @@ func TestAccRepositoryResource(t *testing.T) { Config: testUpdateConfig, Check: testUpdateFunc, }, + { + Config: testEmptyPropertiesConfig, + Check: testEmptyPropertiesFunc, + }, { Config: testReplicaSetConfig, Check: testReplicaSetFunc, @@ -73,7 +85,7 @@ func setupRepositoryTest(repoData RepoData) (string, resource.TestCheckFunc) { resource.TestCheckResourceAttr("cyral_repository.test_repo_repository", "name", repoData.Name), resource.TestCheckResourceAttr("cyral_repository.test_repo_repository", - "labels.#", "2"), + "labels.#", fmt.Sprintf("%d", len(repoData.Labels))), } if repoData.IsReplicaSet() { @@ -112,7 +124,7 @@ func formatRepoDataIntoConfig(data RepoData) string { }`, rsetStr) } - return fmt.Sprintf(` + config := fmt.Sprintf(` resource "cyral_repository" "test_repo_repository" { type = "%s" host = "%s" @@ -122,4 +134,6 @@ func formatRepoDataIntoConfig(data RepoData) string { %s }`, data.RepoType, data.Host, data.Port, data.Name, formatAttributes(data.Labels), propertiesStr) + + return config }