Skip to content

Commit

Permalink
ENG-9133: Fix issues with repository resource properties (#271)
Browse files Browse the repository at this point in the history
* (WIP) Fix issue

* Add check to demonstrate new bug found

* Give up on new bug and finish details

* Address PR comments
  • Loading branch information
Yowgf authored Aug 15, 2022
1 parent b614a7a commit cf5a3f9
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 13 deletions.
24 changes: 14 additions & 10 deletions cyral/resource_cyral_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,35 +33,39 @@ 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)
d.Set("port", data.Port)
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
rsetMap["replica_set_id"] = data.Properties.MongoDBReplicaSetName
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
Expand Down Expand Up @@ -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
Expand Down
20 changes: 17 additions & 3 deletions cyral/resource_cyral_repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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{
Expand All @@ -52,6 +60,10 @@ func TestAccRepositoryResource(t *testing.T) {
Config: testUpdateConfig,
Check: testUpdateFunc,
},
{
Config: testEmptyPropertiesConfig,
Check: testEmptyPropertiesFunc,
},
{
Config: testReplicaSetConfig,
Check: testReplicaSetFunc,
Expand All @@ -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() {
Expand Down Expand Up @@ -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"
Expand All @@ -122,4 +134,6 @@ func formatRepoDataIntoConfig(data RepoData) string {
%s
}`, data.RepoType, data.Host, data.Port, data.Name,
formatAttributes(data.Labels), propertiesStr)

return config
}

0 comments on commit cf5a3f9

Please sign in to comment.