Skip to content

Commit

Permalink
Merge branch 'main' into 65758_User_resource_acceptance_test
Browse files Browse the repository at this point in the history
* main:
  [AV-65762]Fix allowlist acceptance tests (#107)
  [AV-67976] CREATE USER should not require optional fields (#99)
  [AV-68063] Create Backup & DB Creds should not required optional fields (#101)
  • Loading branch information
matty271828 committed Nov 29, 2023
2 parents 1b14ff8 + 593cafc commit 3229b4a
Show file tree
Hide file tree
Showing 13 changed files with 82 additions and 67 deletions.
1 change: 1 addition & 0 deletions examples/backup/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,5 @@ variable "restore" {
replace_ttl_with = optional(string)
restore_times = number
})
default = null
}
4 changes: 2 additions & 2 deletions examples/database_credential/create_database_credential.tf
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ output "database_credential_id" {
}

resource "capella_database_credential" "new_database_credential" {
name = var.database_credential_name
name = var.database_credential.database_credential_name
organization_id = var.organization_id
project_id = var.project_id
cluster_id = var.cluster_id
password = var.password
password = var.database_credential.password
access = var.access
}

8 changes: 6 additions & 2 deletions examples/database_credential/terraform.template.tfvars
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@ auth_token = "<v4-api-key-secret>"
organization_id = "<organization_id>"
project_id = "<project_id>"
host = "https://cloudapi.cloud.couchbase.com"
database_credential_name = "test_db_user"
cluster_id = "<cluster_id>"
password = "Secret12$#"

database_credential = {
database_credential_name = "test_db_user"
password = "Secret12$#"
}

access = [
{
privileges = ["data_writer"]
Expand Down
21 changes: 14 additions & 7 deletions examples/database_credential/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,28 @@ variable "project_id" {
description = "Capella Project ID"
}

variable "database_credential_name" {
description = "Database Credentials Name"
}

variable "cluster_id" {
description = "Capella Cluster ID"
}

variable "password" {
description = "password for database credential"
sensitive = true
variable "database_credential" {
type = object({
database_credential_name = string
password = optional(string)
})
}

variable "access" {
type = list(object({
privileges = list(string)
resources = optional(object({
buckets = list(object({
name = string
scopes = optional(list(object({
name = string
collections = optional(list(string))
})))
}))
}))
}))
}
19 changes: 4 additions & 15 deletions examples/user/create_user.tf
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,8 @@ output "user_id" {
resource "capella_user" "new_user" {
organization_id = var.organization_id

name = var.user_name
email = var.email

organization_roles = var.organization_roles

resources = [
{
type = "project"
id = var.project_id
roles = [
"projectViewer",
"projectDataReaderWriter"
]
}
]
name = var.user.name
email = var.user.email
organization_roles = var.user.organization_roles
resources = var.resources
}
19 changes: 16 additions & 3 deletions examples/user/terraform.template.tfvars
Original file line number Diff line number Diff line change
@@ -1,6 +1,19 @@
auth_token = "<v4-api-key-secret>"
organization_id = "<organization_id>"
project_id = "<project_id>"
user_name = "John"
email = "[email protected]"
host = "https://cloudapi.cloud.couchbase.com"


user = {
name = "John ABC"
email = "[email protected]"
organization_roles = ["organizationMember"]
}

resources = [{
type = "project"
id = "<project_id>"
roles = [
"projectViewer",
"projectDataReaderWriter"
]
}]
26 changes: 13 additions & 13 deletions examples/user/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,24 @@ variable "organization_id" {
description = "Capella Organization ID"
}

variable "project_id" {
description = "Capella Project ID"
}

variable "auth_token" {
description = "Authentication API Key"
sensitive = true
}

variable "user_name" {
description = "Capella User Name"
}

variable "email" {
description = "Capella Email Address"
variable "user" {
type = object({
name = optional(string)
email = string
organization_roles = list(string)
})
}

variable "organization_roles" {
description = "Capella Organization Roles"
type = list(string)
variable "resources" {
type = list(object({
type = optional(string)
id = string
roles = list(string)
}))
default = null
}
2 changes: 1 addition & 1 deletion internal/api/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (
// To learn more, see https://docs.couchbase.com/cloud/organizations/organization-user-roles.html
type CreateUserRequest struct {
// Name represents the name of the user.
Name string `json:"name"`
Name *string `json:"name"`

// Email represents the email of the user.
Email string `json:"email"`
Expand Down
24 changes: 5 additions & 19 deletions internal/resources/acceptance_tests/allowlist_acceptance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func TestAccAllowListTestCases(t *testing.T) {
resourceReference := "capella_cluster." + resourceName
projectResourceName := "terraform_project"
projectResourceReference := "capella_project." + projectResourceName
cidr := "10.0.2.0/23"
cidr := "10.250.250.0/23"

testCfg := acctest.Cfg
resource.Test(t, resource.TestCase{
Expand Down Expand Up @@ -70,7 +70,7 @@ func TestAccAllowListTestCases(t *testing.T) {

//Add SameIP (this ip is same as the one added with required fields teststep and the config of that test step is retained)
{
Config: testAccAddIPSameIP(testCfg, "add_allowlist_sameIP", "10.1.4.1/32"),
Config: testAccAddIPSameIP(testCfg, "add_allowlist_sameIP", "10.1.1.1/32"),
ExpectError: regexp.MustCompile("CIDR provided already exists for the cluster"),
},
//Delete expired IP
Expand All @@ -93,7 +93,7 @@ func TestAccAllowedIPDeleteIP(t *testing.T) {
clusterResourceReference := "capella_cluster." + clusterName
projectResourceName := "terraform_project"
projectResourceReference := "capella_project." + projectResourceName
cidr := "10.3.2.0/23"
cidr := "10.250.250.0/23"

testCfg := acctest.Cfg
resource.Test(t, resource.TestCase{
Expand Down Expand Up @@ -172,7 +172,7 @@ resource "capella_cluster" "%[2]s" {
organization_id = var.organization_id
project_id = %[4]s.id
name = "Terraform Acceptance Test Cluster"
description = "My first test cluster for multiple services"
description = "terraform acceptance test cluster"
couchbase_server = {
version = "7.1"
}
Expand Down Expand Up @@ -288,20 +288,6 @@ resource "capella_allowlist" "%[2]s_1" {
comment = "terraform allow list acceptance test"
expires_at = "%[4]s"
}
output "%[2]s_2"{
value = capella_allowlist.%[2]s_2
}
resource "capella_allowlist" "%[2]s_2" {
organization_id = var.organization_id
project_id = capella_project.terraform_project.id
cluster_id = capella_cluster.new_cluster.id
cidr = "%[3]s"
comment = "terraform allow list acceptance test"
expires_at = "%[4]s"
}
`, cfg, resourceName, cidr, expiryTime)
}

Expand Down Expand Up @@ -376,7 +362,7 @@ func testAccDeleteAllowIP(clusterResourceReference, projectResourceReference, al
host := os.Getenv("TF_VAR_host")
orgid := os.Getenv("TF_VAR_organization_id")
authToken := os.Getenv("TF_VAR_auth_token")
url := fmt.Sprintf("%s/v4/organizations/%s/projects/%s/clusters/%s//allowedcidrs/%s", host, orgid, projectState["id"], clusterState["id"], allowListState["id"])
url := fmt.Sprintf("%s/v4/organizations/%s/projects/%s/clusters/%s/allowedcidrs/%s", host, orgid, projectState["id"], clusterState["id"], allowListState["id"])
cfg := api.EndpointCfg{Url: url, Method: http.MethodDelete, SuccessStatus: http.StatusNoContent}
_, err = data.Client.Execute(
cfg,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1575,7 +1575,16 @@ func testAccDeleteCluster(clusterResourceReference, projectResourceReference str
if err != nil {
return err
}

//get the cluster state
err = checkClusterStatus(data, context.Background(), orgid, projectState["id"], clusterState["id"])
resourceNotFound, errString := api.CheckResourceNotFoundError(err)
if !resourceNotFound {
return fmt.Errorf(errString)
}
fmt.Printf("successfully deleted")
return nil

}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@ import (
"net/http"
"os"
"terraform-provider-capella/internal/api"
"testing"

"terraform-provider-capella/internal/provider"
acctest "terraform-provider-capella/internal/testing"
"testing"

"github.com/hashicorp/terraform-plugin-framework/providerserver"
"github.com/hashicorp/terraform-plugin-go/tfprotov6"
Expand Down
11 changes: 9 additions & 2 deletions internal/resources/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,17 @@ func (r *User) Create(ctx context.Context, req resource.CreateRequest, resp *res
var organizationId = plan.OrganizationId.ValueString()

createUserRequest := api.CreateUserRequest{
Name: plan.Name.ValueString(),
Email: plan.Email.ValueString(),
OrganizationRoles: providerschema.ConvertRoles(plan.OrganizationRoles),
Resources: providerschema.ConvertResources(plan.Resources),
}

// check for optional fields
if !plan.Name.IsNull() && !plan.Name.IsUnknown() {
createUserRequest.Name = plan.Name.ValueStringPointer()
}

if len(plan.Resources) != 0 {
createUserRequest.Resources = providerschema.ConvertResources(plan.Resources)
}

// Execute request
Expand Down
2 changes: 1 addition & 1 deletion internal/resources/user_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ func UserSchema() schema.Schema {
return schema.Schema{
Attributes: map[string]schema.Attribute{
"id": stringAttribute(computed),
"name": stringAttribute(optional),
"name": stringAttribute(optional, computed),
"status": stringAttribute(computed),
"inactive": boolAttribute(computed),
"email": stringAttribute(required),
Expand Down

0 comments on commit 3229b4a

Please sign in to comment.