Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ENG-13251: Add optional MongoDBSettings 'flavor' field to repository resource #503

Merged
merged 5 commits into from
Jan 31, 2024

Conversation

juniocezar
Copy link
Member

@juniocezar juniocezar commented Jan 29, 2024

Description of the change

ENG-13251: Add Flavor as an Optional MongoDBSettings field. Repository resource.

Type of change

  • Bug fix (non-breaking change that fixes an issue)
  • New feature (non-breaking change that adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)

Checklists

Development

  • Lint rules pass locally
  • The code changed/added as part of this pull request has been covered with tests
  • All tests related to the changed code pass in development

Code review

  • This pull request has a descriptive title and information useful to a reviewer. There may be a screenshot or screencast attached
  • Jira issue referenced in commit message and/or PR title

Testing

  • The flavor field should be optional:
resource "cyral_repository" "document_db_standard" {
  connection_draining {
    auto      = false
    wait_time = 0
  }
  name = "mongodb_terraform_test"
  repo_node {
    dynamic = false
    host    = "www.value.com"
    port    = 27017
  }
  type = "mongodb"
  mongodb_settings {
    server_type      = "replicaset"
    replica_set_name = "rs1"
  }
}
└─ $ ▶ terraform apply --auto-approve
cyral_repository.document_db_standard: Refreshing state... [id=2bgc3gkCWVrBDpR8NM4nynSj3M7]

Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
  ~ update in-place

Terraform will perform the following actions:

  # cyral_repository.document_db_standard will be updated in-place
  ~ resource "cyral_repository" "document_db_standard" {
        id     = "2bgc3gkCWVrBDpR8NM4nynSj3M7"
        name   = "mongodb_terraform_test"
        # (2 unchanged attributes hidden)

      - mongodb_settings {
          - replica_set_name = "rs0" -> null
          - server_type      = "replicaset" -> null
        }
      + mongodb_settings {
          + replica_set_name = "rs1"
          + server_type      = "replicaset"
        }

        # (2 unchanged blocks hidden)
    }

Plan: 0 to add, 1 to change, 0 to destroy.
cyral_repository.document_db_standard: Modifying... [id=2bgc3gkCWVrBDpR8NM4nynSj3M7]
cyral_repository.document_db_standard: Modifications complete after 2s [id=2bgc3gkCWVrBDpR8NM4nynSj3M7]

Apply complete! Resources: 0 added, 1 changed, 0 destroyed.

  • Ensure that invalid flavors are not allowed:
resource "cyral_repository" "document_db_standard" {
  connection_draining {
    auto      = false
    wait_time = 0
  }
  name = "mongodb_terraform_test"
  repo_node {
    dynamic = false
    host    = "www.value.com"
    port    = 27017
  }
  type = "mongodb"
  mongodb_settings {
    server_type      = "replicaset"
    replica_set_name = "rs0"
    flavor = "cosmosdb"
  }
}

└─ $ ▶ terraform apply --auto-approve
╷
│ Error: expected mongodb_settings.0.flavor to be one of ["mongodb" "documentdb"], got cosmosdb
│ 
│   with cyral_repository.document_db_standard,
│   on main.tf line 15, in resource "cyral_repository" "document_db_standard":
│   15: resource "cyral_repository" "document_db_standard" {
│ 

  • Flavor mongodb can be used with replicaset
└─ $ ▶ terraform apply --auto-approve
cyral_repository.document_db_standard: Refreshing state... [id=2bgc3gkCWVrBDpR8NM4nynSj3M7]

Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
  ~ update in-place

Terraform will perform the following actions:

  # cyral_repository.document_db_standard will be updated in-place
  ~ resource "cyral_repository" "document_db_standard" {
        id     = "2bgc3gkCWVrBDpR8NM4nynSj3M7"
        name   = "mongodb_terraform_test"
        # (2 unchanged attributes hidden)

      - mongodb_settings {
          - replica_set_name = "rs1" -> null
          - server_type      = "replicaset" -> null
        }
      + mongodb_settings {
          + flavor           = "mongodb"
          + replica_set_name = "rs1"
          + server_type      = "replicaset"
        }

        # (2 unchanged blocks hidden)
    }

Plan: 0 to add, 1 to change, 0 to destroy.
cyral_repository.document_db_standard: Modifying... [id=2bgc3gkCWVrBDpR8NM4nynSj3M7]
cyral_repository.document_db_standard: Modifications complete after 2s [id=2bgc3gkCWVrBDpR8NM4nynSj3M7]

Apply complete! Resources: 0 added, 1 changed, 0 destroyed.
  • Flavor mongodb can be used with standalone
└─ $ ▶ terraform apply --auto-approve
cyral_repository.document_db_standard: Refreshing state... [id=2bgfOFDW4cYQ5X6LgJuQCe2Koac]

Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
  ~ update in-place

Terraform will perform the following actions:

  # cyral_repository.document_db_standard will be updated in-place
  ~ resource "cyral_repository" "document_db_standard" {
        id     = "2bgfOFDW4cYQ5X6LgJuQCe2Koac"
        name   = "mongodb_terraform_test"
        # (2 unchanged attributes hidden)

      - mongodb_settings {
          - flavor      = "documentdb" -> null
          - server_type = "standalone" -> null
        }
      + mongodb_settings {
          + flavor      = "mongodb"
          + server_type = "standalone"
        }

        # (2 unchanged blocks hidden)
    }

Plan: 0 to add, 1 to change, 0 to destroy.
cyral_repository.document_db_standard: Modifying... [id=2bgfOFDW4cYQ5X6LgJuQCe2Koac]
cyral_repository.document_db_standard: Modifications complete after 1s [id=2bgfOFDW4cYQ5X6LgJuQCe2Koac]

Apply complete! Resources: 0 added, 1 changed, 0 destroyed.

  • Flavor mongodb can be used with sharded
└─ $ ▶ terraform apply --auto-approve

Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
  + create

Terraform will perform the following actions:

  # cyral_repository.document_db_standard will be created
  + resource "cyral_repository" "document_db_standard" {
      + id   = (known after apply)
      + name = "mongodb_terraform_test"
      + type = "mongodb"

      + connection_draining {
          + auto      = false
          + wait_time = 0
        }

      + mongodb_settings {
          + flavor      = "mongodb"
          + server_type = "sharded"
        }

      + repo_node {
          + dynamic = false
          + host    = "www.value.com"
          + port    = 27017
        }
    }

Plan: 1 to add, 0 to change, 0 to destroy.
cyral_repository.document_db_standard: Creating...
cyral_repository.document_db_standard: Creation complete after 2s [id=2bgfXcRULXD6Z2QGXyZSzj8p4zr]

Apply complete! Resources: 1 added, 0 changed, 0 destroyed.

  • Flavor documentdb can be used with standalone
└─ $ ▶ terraform apply --auto-approve

Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
  + create

Terraform will perform the following actions:

  # cyral_repository.document_db_standard will be created
  + resource "cyral_repository" "document_db_standard" {
      + id   = (known after apply)
      + name = "mongodb_terraform_test"
      + type = "mongodb"

      + connection_draining {
          + auto      = false
          + wait_time = 0
        }

      + mongodb_settings {
          + flavor      = "documentdb"
          + server_type = "standalone"
        }

      + repo_node {
          + dynamic = false
          + host    = "www.value.com"
          + port    = 27017
        }
    }

Plan: 1 to add, 0 to change, 0 to destroy.
cyral_repository.document_db_standard: Creating...
cyral_repository.document_db_standard: Creation complete after 2s [id=2bgfOFDW4cYQ5X6LgJuQCe2Koac]

Apply complete! Resources: 1 added, 0 changed, 0 destroyed.

  • Flavor documentdb can be used with replicaset
└─ $ ▶ terraform apply --auto-approve
cyral_repository.document_db_standard: Refreshing state... [id=2bgc3gkCWVrBDpR8NM4nynSj3M7]

Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
  ~ update in-place

Terraform will perform the following actions:

  # cyral_repository.document_db_standard will be updated in-place
  ~ resource "cyral_repository" "document_db_standard" {
        id     = "2bgc3gkCWVrBDpR8NM4nynSj3M7"
        name   = "mongodb_terraform_test"
        # (2 unchanged attributes hidden)

      + mongodb_settings {
          + flavor           = "documentdb"
          + replica_set_name = "rs1"
          + server_type      = "replicaset"
        }
      - mongodb_settings {
          - flavor           = "mongodb" -> null
          - replica_set_name = "rs1" -> null
          - server_type      = "replicaset" -> null
        }

        # (2 unchanged blocks hidden)
    }

Plan: 0 to add, 1 to change, 0 to destroy.
cyral_repository.document_db_standard: Modifying... [id=2bgc3gkCWVrBDpR8NM4nynSj3M7]
cyral_repository.document_db_standard: Modifications complete after 1s [id=2bgc3gkCWVrBDpR8NM4nynSj3M7]

Apply complete! Resources: 0 added, 1 changed, 0 destroyed.
  • Flavor documentdb cannot be used with sharded.
└─ $ ▶ terraform apply --auto-approve
cyral_repository.document_db_standard: Refreshing state... [id=2bgfXcRULXD6Z2QGXyZSzj8p4zr]

Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
  ~ update in-place

Terraform will perform the following actions:

  # cyral_repository.document_db_standard will be updated in-place
  ~ resource "cyral_repository" "document_db_standard" {
        id     = "2bgfXcRULXD6Z2QGXyZSzj8p4zr"
        name   = "mongodb_terraform_test"
        # (2 unchanged attributes hidden)

      + mongodb_settings {
          + flavor      = "documentdb"
          + server_type = "sharded"
        }
      - mongodb_settings {
          - flavor      = "mongodb" -> null
          - server_type = "sharded" -> null
        }

        # (2 unchanged blocks hidden)
    }

Plan: 0 to add, 1 to change, 0 to destroy.
cyral_repository.document_db_standard: Modifying... [id=2bgfXcRULXD6Z2QGXyZSzj8p4zr]
╷
│ Error: Unable to update resource RepositoryUpdate
│ 
│   with cyral_repository.document_db_standard,
│   on main.tf line 15, in resource "cyral_repository" "document_db_standard":
│   15: resource "cyral_repository" "document_db_standard" {
│ 
│ "documentdb" MongoDB flavor cannot be combined with server type: "sharded". For configuring DocumentDB Elastic clusters, please use the standalone server type
╵

@juniocezar juniocezar marked this pull request as ready for review January 30, 2024 19:21
@juniocezar juniocezar changed the title ENG-13251: Add MongoDB Flavor to Repo Configuration ENG-13251: Add optional MongoDBSettings 'flavor' field to repository resource Jan 30, 2024
Copy link
Contributor

@ricardorey10 ricardorey10 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, but let's wait until we hear from Wilson

@@ -263,10 +279,18 @@ func (r *RepoInfo) MongoDBSettingsFromInterface(i []interface{}) error {
Standalone,
)
}
if serverType == Sharded && mongoFlavor == MongoDBFlavorDocumentDB {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We already have validation for this combination in crud, so I don't think we should do it here too. I feel like the terraform provider should not encode these validations, but rather they should be left to the gRPC/REST implementation.

That said, that's just my opinion, and I don't know what's the idiomatic thing to do. Let's see what @wcmjunior thinks.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am fine with both, we can wait for wilson.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is ok to have the validation only in the backend as long as it provides an error message that actually helps the user to solve the problem. If it returns something like Invalid parameter it does not help, but if it returns something in the lines of Parameter X must be 'Y' or 'Z' it is ok.

if serverType == Sharded && mongoFlavor == MongoDBFlavorDocumentDB {
return fmt.Errorf(
"%q MongoDB flavor cannot be combined with server type: %q. For configuring "+
"DocumentDB Elastic clusters, please use the %s server type",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Both standalone and replicaset work with DocumentDB, right?

Copy link
Contributor

@ricardorey10 ricardorey10 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Based on Wilson's comment I'll approve now so that you're not blocked on me whenever you make the necessary changes.

@juniocezar
Copy link
Member Author

@ricardorey10 I just removed the local validation, as requested. This is the new output:


└─ $ ▶ terraform apply --auto-approve

Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
  + create

Terraform will perform the following actions:

  # cyral_repository.document_db_standard will be created
  + resource "cyral_repository" "document_db_standard" {
      + id   = (known after apply)
      + name = "mongodb_terraform_test"
      + type = "mongodb"

      + connection_draining {
          + auto      = false
          + wait_time = 0
        }

      + mongodb_settings {
          + flavor      = "documentdb"
          + server_type = "sharded"
        }

      + repo_node {
          + dynamic = false
          + host    = "www.value.com"
          + port    = 27017
        }
    }

Plan: 1 to add, 0 to change, 0 to destroy.
cyral_repository.document_db_standard: Creating...
╷
│ Error: Unable to create resource RepositoryCreate
│ 
│   with cyral_repository.document_db_standard,
│   on main.tf line 15, in resource "cyral_repository" "document_db_standard":
│   15: resource "cyral_repository" "document_db_standard" {
│ 
│ error executing POST request; status code: 400; body: "{\"Message\":\"Rpc error: code = InvalidArgument desc = 'documentdb' flavor is not compatible with sharded clusters\"}\n"
╵


Copy link

Quality Gate Passed Quality Gate passed

Kudos, no new issues were introduced!

0 New issues
0 Security Hotspots
No data about Coverage
0.0% Duplication on New Code

See analysis details on SonarCloud

Copy link
Contributor

@wcmjunior wcmjunior left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changes LGTM. Please also point out (a comment is fine) which is the minimum version of CP required to use the new attribute so I can add to the change log.

@juniocezar
Copy link
Member Author

v4.12.3 is the minimum Control Plane version to use the new attribute.

@wcmjunior wcmjunior merged commit 068c5d9 into main Jan 31, 2024
2 checks passed
@wcmjunior wcmjunior deleted the ENG-13251 branch January 31, 2024 00:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants