Skip to content

Add MongoDB and Redis user provider #283

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

Merged
merged 1 commit into from
Aug 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
97 changes: 97 additions & 0 deletions ovh/data_cloud_project_database_mongodb_user.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package ovh

import (
"fmt"
"log"
"net/url"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

func dataSourceCloudProjectDatabaseMongodbUser() *schema.Resource {
return &schema.Resource{
Read: dataSourceCloudProjectDatabaseMongodbUserRead,
Schema: map[string]*schema.Schema{
"service_name": {
Type: schema.TypeString,
Required: true,
DefaultFunc: schema.EnvDefaultFunc("OVH_CLOUD_PROJECT_SERVICE", nil),
},
"cluster_id": {
Type: schema.TypeString,
Description: "Cluster ID",
Required: true,
},
"name": {
Type: schema.TypeString,
Description: "Name of the user with the authentication database in the format name@authDB",
Required: true,
},

//Computed
"created_at": {
Type: schema.TypeString,
Description: "Date of the creation of the user",
Computed: true,
},
"roles": {
Type: schema.TypeList,
Description: "Roles the user belongs to",
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"status": {
Type: schema.TypeString,
Description: "Current status of the user",
Computed: true,
},
},
}
}

func dataSourceCloudProjectDatabaseMongodbUserRead(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
serviceName := d.Get("service_name").(string)
clusterId := d.Get("cluster_id").(string)

listEndpoint := fmt.Sprintf("/cloud/project/%s/database/mongodb/%s/user",
url.PathEscape(serviceName),
url.PathEscape(clusterId),
)

listRes := make([]string, 0)

log.Printf("[DEBUG] Will read users from cluster %s from project %s", clusterId, serviceName)
if err := config.OVHClient.Get(listEndpoint, &listRes); err != nil {
return fmt.Errorf("Error calling GET %s:\n\t %q", listEndpoint, err)
}

name := d.Get("name").(string)
for _, id := range listRes {
endpoint := fmt.Sprintf("/cloud/project/%s/database/mongodb/%s/user/%s",
url.PathEscape(serviceName),
url.PathEscape(clusterId),
url.PathEscape(id),
)
res := &CloudProjectDatabaseMongodbUserResponse{}

log.Printf("[DEBUG] Will read user %s from cluster %s from project %s", id, clusterId, serviceName)
if err := config.OVHClient.Get(endpoint, res); err != nil {
return fmt.Errorf("Error calling GET %s:\n\t %q", endpoint, err)
}

if res.Username == name {
for k, v := range res.ToMap() {
if k != "id" {
d.Set(k, v)
} else {
d.SetId(fmt.Sprint(v))
}
}
log.Printf("[DEBUG] Read user %+v", res)
return nil
}
}

return fmt.Errorf("User name %s not found for cluster %s from project %s", name, clusterId, serviceName)
}
83 changes: 83 additions & 0 deletions ovh/data_cloud_project_database_mongodb_user_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package ovh

import (
"fmt"
"os"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
)

const testAccCloudProjectDatabaseMongodbUserDatasourceConfig_Basic = `
resource "ovh_cloud_project_database" "db" {
service_name = "%s"
engine = "mongodb"
version = "%s"
plan = "essential"
nodes {
region = "%s"
}
flavor = "%s"
}

resource "ovh_cloud_project_database_mongodb_user" "user" {
service_name = ovh_cloud_project_database.db.service_name
cluster_id = ovh_cloud_project_database.db.id
name = "%s"
roles = ["%s", "%s"]
}

data "ovh_cloud_project_database_mongodb_user" "user" {
service_name = ovh_cloud_project_database_mongodb_user.user.service_name
cluster_id = ovh_cloud_project_database_mongodb_user.user.cluster_id
name = ovh_cloud_project_database_mongodb_user.user.name
}
`

func TestAccCloudProjectDatabaseMongodbUserDataSource_basic(t *testing.T) {
serviceName := os.Getenv("OVH_CLOUD_PROJECT_SERVICE_TEST")
version := os.Getenv("OVH_CLOUD_PROJECT_DATABASE_MONGODB_VERSION_TEST")
if version == "" {
version = os.Getenv("OVH_CLOUD_PROJECT_DATABASE_VERSION_TEST")
}
region := os.Getenv("OVH_CLOUD_PROJECT_DATABASE_REGION_TEST")
flavor := os.Getenv("OVH_CLOUD_PROJECT_DATABASE_FLAVOR_TEST")
name := "johndoe"
rolesBackup := "backup"
rolesReadAnyDatabase := "readAnyDatabase"

config := fmt.Sprintf(
testAccCloudProjectDatabaseMongodbUserDatasourceConfig_Basic,
serviceName,
version,
region,
flavor,
name,
rolesBackup,
rolesReadAnyDatabase,
)

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheckCloudDatabaseNoEngine(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: config,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrSet(
"data.ovh_cloud_project_database_mongodb_user.user", "created_at"),
resource.TestCheckResourceAttr(
"data.ovh_cloud_project_database_mongodb_user.user", "name", name+"@admin"),
resource.TestCheckResourceAttrSet(
"data.ovh_cloud_project_database_mongodb_user.user", "roles.#"),
resource.TestCheckResourceAttr(
"data.ovh_cloud_project_database_mongodb_user.user", "roles.0", rolesBackup),
resource.TestCheckResourceAttr(
"data.ovh_cloud_project_database_mongodb_user.user", "roles.1", rolesReadAnyDatabase),
resource.TestCheckResourceAttrSet(
"data.ovh_cloud_project_database_mongodb_user.user", "status"),
),
},
},
})
}
6 changes: 5 additions & 1 deletion ovh/data_cloud_project_database_postgresql_user_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ resource "ovh_cloud_project_database_postgresql_user" "user" {
service_name = ovh_cloud_project_database.db.service_name
cluster_id = ovh_cloud_project_database.db.id
name = "%s"
roles = ["replication"]
roles = ["%s"]
}

data "ovh_cloud_project_database_postgresql_user" "user" {
Expand All @@ -43,6 +43,7 @@ func TestAccCloudProjectDatabasePostgresqlUserDataSource_basic(t *testing.T) {
region := os.Getenv("OVH_CLOUD_PROJECT_DATABASE_REGION_TEST")
flavor := os.Getenv("OVH_CLOUD_PROJECT_DATABASE_FLAVOR_TEST")
name := "johndoe"
replication := "replication"

config := fmt.Sprintf(
testAccCloudProjectDatabasePostgresqlUserDatasourceConfig_Basic,
Expand All @@ -51,6 +52,7 @@ func TestAccCloudProjectDatabasePostgresqlUserDataSource_basic(t *testing.T) {
region,
flavor,
name,
replication,
)

resource.Test(t, resource.TestCase{
Expand All @@ -64,6 +66,8 @@ func TestAccCloudProjectDatabasePostgresqlUserDataSource_basic(t *testing.T) {
"data.ovh_cloud_project_database_postgresql_user.user", "created_at"),
resource.TestCheckResourceAttrSet(
"data.ovh_cloud_project_database_postgresql_user.user", "roles.#"),
resource.TestCheckResourceAttr(
"data.ovh_cloud_project_database_postgresql_user.user", "roles.0", replication),
resource.TestCheckResourceAttrSet(
"data.ovh_cloud_project_database_postgresql_user.user", "status"),
resource.TestCheckResourceAttr(
Expand Down
115 changes: 115 additions & 0 deletions ovh/data_cloud_project_database_redis_user.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
package ovh

import (
"fmt"
"log"
"net/url"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

func dataSourceCloudProjectDatabaseRedisUser() *schema.Resource {
return &schema.Resource{
Read: dataSourceCloudProjectDatabaseRedisUserRead,
Schema: map[string]*schema.Schema{
"service_name": {
Type: schema.TypeString,
Required: true,
DefaultFunc: schema.EnvDefaultFunc("OVH_CLOUD_PROJECT_SERVICE", nil),
},
"cluster_id": {
Type: schema.TypeString,
Description: "Id of the database cluster",
Required: true,
},
"name": {
Type: schema.TypeString,
Description: "Name of the user",
Required: true,
},

//Computed
"categories": {
Type: schema.TypeList,
Description: "Categories of the user",
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"channels": {
Type: schema.TypeList,
Description: "Channels of the user",
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"commands": {
Type: schema.TypeList,
Description: "Commands of the user",
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"created_at": {
Type: schema.TypeString,
Description: "Date of the creation of the user",
Computed: true,
},
"keys": {
Type: schema.TypeList,
Description: "Keys of the user",
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"status": {
Type: schema.TypeString,
Description: "Current status of the user",
Computed: true,
},
},
}
}

func dataSourceCloudProjectDatabaseRedisUserRead(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
serviceName := d.Get("service_name").(string)
clusterId := d.Get("cluster_id").(string)

listEndpoint := fmt.Sprintf("/cloud/project/%s/database/redis/%s/user",
url.PathEscape(serviceName),
url.PathEscape(clusterId),
)

listRes := make([]string, 0)

log.Printf("[DEBUG] Will read users from cluster %s from project %s", clusterId, serviceName)
if err := config.OVHClient.Get(listEndpoint, &listRes); err != nil {
return fmt.Errorf("Error calling GET %s:\n\t %q", listEndpoint, err)
}

name := d.Get("name").(string)
for _, id := range listRes {
endpoint := fmt.Sprintf("/cloud/project/%s/database/redis/%s/user/%s",
url.PathEscape(serviceName),
url.PathEscape(clusterId),
url.PathEscape(id),
)
res := &CloudProjectDatabaseRedisUserResponse{}

log.Printf("[DEBUG] Will read user %s from cluster %s from project %s", id, clusterId, serviceName)
if err := config.OVHClient.Get(endpoint, res); err != nil {
return fmt.Errorf("Error calling GET %s:\n\t %q", endpoint, err)
}

if res.Username == name {
for k, v := range res.ToMap() {
if k != "id" {
d.Set(k, v)
} else {
d.SetId(fmt.Sprint(v))
}
}
log.Printf("[DEBUG] Read user %+v", res)
return nil
}
}

return fmt.Errorf("User name %s not found for cluster %s from project %s", name, clusterId, serviceName)
}
Loading