Skip to content

Commit 2901e92

Browse files
committed
Add MongoDB and Redis user provider
1 parent cc9f0a8 commit 2901e92

27 files changed

+1921
-93
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
package ovh
2+
3+
import (
4+
"fmt"
5+
"log"
6+
"net/url"
7+
8+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
9+
)
10+
11+
func dataSourceCloudProjectDatabaseMongodbUser() *schema.Resource {
12+
return &schema.Resource{
13+
Read: dataSourceCloudProjectDatabaseMongodbUserRead,
14+
Schema: map[string]*schema.Schema{
15+
"service_name": {
16+
Type: schema.TypeString,
17+
Required: true,
18+
DefaultFunc: schema.EnvDefaultFunc("OVH_CLOUD_PROJECT_SERVICE", nil),
19+
},
20+
"cluster_id": {
21+
Type: schema.TypeString,
22+
Description: "Cluster ID",
23+
Required: true,
24+
},
25+
"username": {
26+
Type: schema.TypeString,
27+
Description: "Name of the user with the authentication database in the format name@authDB",
28+
Required: true,
29+
},
30+
31+
//Computed
32+
"created_at": {
33+
Type: schema.TypeString,
34+
Description: "Date of the creation of the user",
35+
Computed: true,
36+
},
37+
"name": {
38+
Type: schema.TypeString,
39+
Description: "Name of the user",
40+
Computed: true,
41+
},
42+
"roles": {
43+
Type: schema.TypeList,
44+
Description: "Roles the user belongs to",
45+
Computed: true,
46+
Elem: &schema.Schema{Type: schema.TypeString},
47+
},
48+
"roles_auth_db": {
49+
Type: schema.TypeList,
50+
Description: "Roles the user belongs to with the authentication database in the format role@authDB",
51+
Computed: true,
52+
Elem: &schema.Schema{Type: schema.TypeString},
53+
},
54+
"status": {
55+
Type: schema.TypeString,
56+
Description: "Current status of the user",
57+
Computed: true,
58+
},
59+
},
60+
}
61+
}
62+
63+
func dataSourceCloudProjectDatabaseMongodbUserRead(d *schema.ResourceData, meta interface{}) error {
64+
config := meta.(*Config)
65+
serviceName := d.Get("service_name").(string)
66+
clusterId := d.Get("cluster_id").(string)
67+
68+
listEndpoint := fmt.Sprintf("/cloud/project/%s/database/mongodb/%s/user",
69+
url.PathEscape(serviceName),
70+
url.PathEscape(clusterId),
71+
)
72+
73+
listRes := make([]string, 0)
74+
75+
log.Printf("[DEBUG] Will read users from cluster %s from project %s", clusterId, serviceName)
76+
if err := config.OVHClient.Get(listEndpoint, &listRes); err != nil {
77+
return fmt.Errorf("Error calling GET %s:\n\t %q", listEndpoint, err)
78+
}
79+
80+
username := d.Get("username").(string)
81+
for _, id := range listRes {
82+
endpoint := fmt.Sprintf("/cloud/project/%s/database/mongodb/%s/user/%s",
83+
url.PathEscape(serviceName),
84+
url.PathEscape(clusterId),
85+
url.PathEscape(id),
86+
)
87+
res := &CloudProjectDatabaseMongodbUserResponse{}
88+
89+
log.Printf("[DEBUG] Will read user %s from cluster %s from project %s", id, clusterId, serviceName)
90+
if err := config.OVHClient.Get(endpoint, res); err != nil {
91+
return fmt.Errorf("Error calling GET %s:\n\t %q", endpoint, err)
92+
}
93+
94+
if res.Username == username {
95+
for k, v := range res.ToMap() {
96+
if k != "id" {
97+
d.Set(k, v)
98+
} else {
99+
d.SetId(fmt.Sprint(v))
100+
}
101+
}
102+
log.Printf("[DEBUG] Read user %+v", res)
103+
return nil
104+
}
105+
}
106+
107+
return fmt.Errorf("User name %s not found for cluster %s from project %s", username, clusterId, serviceName)
108+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package ovh
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"testing"
7+
8+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
9+
)
10+
11+
const testAccCloudProjectDatabaseMongodbUserDatasourceConfig_Basic = `
12+
resource "ovh_cloud_project_database" "db" {
13+
service_name = "%s"
14+
engine = "mongodb"
15+
version = "%s"
16+
plan = "essential"
17+
nodes {
18+
region = "%s"
19+
}
20+
flavor = "%s"
21+
}
22+
23+
resource "ovh_cloud_project_database_mongodb_user" "user" {
24+
service_name = ovh_cloud_project_database.db.service_name
25+
cluster_id = ovh_cloud_project_database.db.id
26+
name = "%s"
27+
roles = ["backup", "readAnyDatabase"]
28+
}
29+
30+
data "ovh_cloud_project_database_mongodb_user" "user" {
31+
service_name = ovh_cloud_project_database_mongodb_user.user.service_name
32+
cluster_id = ovh_cloud_project_database_mongodb_user.user.cluster_id
33+
username = ovh_cloud_project_database_mongodb_user.user.username
34+
}
35+
`
36+
37+
func TestAccCloudProjectDatabaseMongodbUserDataSource_basic(t *testing.T) {
38+
serviceName := os.Getenv("OVH_CLOUD_PROJECT_SERVICE_TEST")
39+
version := os.Getenv("OVH_CLOUD_PROJECT_DATABASE_POSTGRESQL_VERSION_TEST")
40+
if version == "" {
41+
version = os.Getenv("OVH_CLOUD_PROJECT_DATABASE_VERSION_TEST")
42+
}
43+
region := os.Getenv("OVH_CLOUD_PROJECT_DATABASE_REGION_TEST")
44+
flavor := os.Getenv("OVH_CLOUD_PROJECT_DATABASE_FLAVOR_TEST")
45+
name := "johndoe"
46+
47+
config := fmt.Sprintf(
48+
testAccCloudProjectDatabaseMongodbUserDatasourceConfig_Basic,
49+
serviceName,
50+
version,
51+
region,
52+
flavor,
53+
name,
54+
)
55+
56+
resource.Test(t, resource.TestCase{
57+
PreCheck: func() { testAccPreCheckCloudDatabaseNoEngine(t) },
58+
Providers: testAccProviders,
59+
Steps: []resource.TestStep{
60+
{
61+
Config: config,
62+
Check: resource.ComposeTestCheckFunc(
63+
resource.TestCheckResourceAttrSet(
64+
"data.ovh_cloud_project_database_mongodb_user.user", "created_at"),
65+
resource.TestCheckResourceAttr(
66+
"data.ovh_cloud_project_database_mongodb_user.user", "name", name),
67+
resource.TestCheckResourceAttrSet(
68+
"data.ovh_cloud_project_database_mongodb_user.user", "roles.#"),
69+
resource.TestCheckResourceAttrSet(
70+
"data.ovh_cloud_project_database_mongodb_user.user", "roles_auth_db.#"),
71+
resource.TestCheckResourceAttrSet(
72+
"data.ovh_cloud_project_database_mongodb_user.user", "status"),
73+
resource.TestCheckResourceAttr(
74+
"data.ovh_cloud_project_database_mongodb_user.user", "username", name+"@admin"),
75+
),
76+
},
77+
},
78+
})
79+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
package ovh
2+
3+
import (
4+
"fmt"
5+
"log"
6+
"net/url"
7+
8+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
9+
)
10+
11+
func dataSourceCloudProjectDatabaseRedisUser() *schema.Resource {
12+
return &schema.Resource{
13+
Read: dataSourceCloudProjectDatabaseRedisUserRead,
14+
Schema: map[string]*schema.Schema{
15+
"service_name": {
16+
Type: schema.TypeString,
17+
Required: true,
18+
DefaultFunc: schema.EnvDefaultFunc("OVH_CLOUD_PROJECT_SERVICE", nil),
19+
},
20+
"cluster_id": {
21+
Type: schema.TypeString,
22+
Description: "Id of the database cluster",
23+
Required: true,
24+
},
25+
"name": {
26+
Type: schema.TypeString,
27+
Description: "Name of the user",
28+
Required: true,
29+
},
30+
31+
//Computed
32+
"categories": {
33+
Type: schema.TypeList,
34+
Description: "Categories of the user",
35+
Computed: true,
36+
Elem: &schema.Schema{Type: schema.TypeString},
37+
},
38+
"channels": {
39+
Type: schema.TypeList,
40+
Description: "Channels of the user",
41+
Computed: true,
42+
Elem: &schema.Schema{Type: schema.TypeString},
43+
},
44+
"commands": {
45+
Type: schema.TypeList,
46+
Description: "Commands of the user",
47+
Computed: true,
48+
Elem: &schema.Schema{Type: schema.TypeString},
49+
},
50+
"created_at": {
51+
Type: schema.TypeString,
52+
Description: "Date of the creation of the user",
53+
Computed: true,
54+
},
55+
"keys": {
56+
Type: schema.TypeList,
57+
Description: "Keys of the user",
58+
Computed: true,
59+
Elem: &schema.Schema{Type: schema.TypeString},
60+
},
61+
"status": {
62+
Type: schema.TypeString,
63+
Description: "Current status of the user",
64+
Computed: true,
65+
},
66+
},
67+
}
68+
}
69+
70+
func dataSourceCloudProjectDatabaseRedisUserRead(d *schema.ResourceData, meta interface{}) error {
71+
config := meta.(*Config)
72+
serviceName := d.Get("service_name").(string)
73+
clusterId := d.Get("cluster_id").(string)
74+
75+
listEndpoint := fmt.Sprintf("/cloud/project/%s/database/redis/%s/user",
76+
url.PathEscape(serviceName),
77+
url.PathEscape(clusterId),
78+
)
79+
80+
listRes := make([]string, 0)
81+
82+
log.Printf("[DEBUG] Will read users from cluster %s from project %s", clusterId, serviceName)
83+
if err := config.OVHClient.Get(listEndpoint, &listRes); err != nil {
84+
return fmt.Errorf("Error calling GET %s:\n\t %q", listEndpoint, err)
85+
}
86+
87+
name := d.Get("name").(string)
88+
for _, id := range listRes {
89+
endpoint := fmt.Sprintf("/cloud/project/%s/database/redis/%s/user/%s",
90+
url.PathEscape(serviceName),
91+
url.PathEscape(clusterId),
92+
url.PathEscape(id),
93+
)
94+
res := &CloudProjectDatabaseRedisUserResponse{}
95+
96+
log.Printf("[DEBUG] Will read user %s from cluster %s from project %s", id, clusterId, serviceName)
97+
if err := config.OVHClient.Get(endpoint, res); err != nil {
98+
return fmt.Errorf("Error calling GET %s:\n\t %q", endpoint, err)
99+
}
100+
101+
if res.Username == name {
102+
for k, v := range res.ToMap() {
103+
if k != "id" {
104+
d.Set(k, v)
105+
} else {
106+
d.SetId(fmt.Sprint(v))
107+
}
108+
}
109+
log.Printf("[DEBUG] Read user %+v", res)
110+
return nil
111+
}
112+
}
113+
114+
return fmt.Errorf("User name %s not found for cluster %s from project %s", name, clusterId, serviceName)
115+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package ovh
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"testing"
7+
8+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
9+
)
10+
11+
const testAccCloudProjectDatabaseRedisUserDatasourceConfig = `
12+
resource "ovh_cloud_project_database" "db" {
13+
service_name = "%s"
14+
engine = "redis"
15+
version = "%s"
16+
plan = "essential"
17+
nodes {
18+
region = "%s"
19+
}
20+
flavor = "%s"
21+
}
22+
23+
resource "ovh_cloud_project_database_redis_user" "user" {
24+
service_name = ovh_cloud_project_database.db.service_name
25+
cluster_id = ovh_cloud_project_database.db.id
26+
categories = ["+@set", "+@sortedset"]
27+
channels = ["*"]
28+
commands = ["+get", "-set"]
29+
keys = ["data", "properties"]
30+
name = "%s"
31+
}
32+
33+
data "ovh_cloud_project_database_redis_user" "user" {
34+
service_name = ovh_cloud_project_database_redis_user.user.service_name
35+
cluster_id = ovh_cloud_project_database_redis_user.user.cluster_id
36+
name = ovh_cloud_project_database_redis_user.user.name
37+
}
38+
`
39+
40+
func TestAccCloudProjectDatabaseRedisUserDataSource_basic(t *testing.T) {
41+
serviceName := os.Getenv("OVH_CLOUD_PROJECT_SERVICE_TEST")
42+
version := os.Getenv("OVH_CLOUD_PROJECT_DATABASE_POSTGRESQL_VERSION_TEST")
43+
if version == "" {
44+
version = os.Getenv("OVH_CLOUD_PROJECT_DATABASE_VERSION_TEST")
45+
}
46+
region := os.Getenv("OVH_CLOUD_PROJECT_DATABASE_REGION_TEST")
47+
flavor := os.Getenv("OVH_CLOUD_PROJECT_DATABASE_FLAVOR_TEST")
48+
name := "johndoe"
49+
50+
config := fmt.Sprintf(
51+
testAccCloudProjectDatabaseRedisUserDatasourceConfig,
52+
serviceName,
53+
version,
54+
region,
55+
flavor,
56+
name,
57+
)
58+
59+
resource.Test(t, resource.TestCase{
60+
PreCheck: func() { testAccPreCheckCloudDatabaseNoEngine(t) },
61+
Providers: testAccProviders,
62+
Steps: []resource.TestStep{
63+
{
64+
Config: config,
65+
Check: resource.ComposeTestCheckFunc(
66+
resource.TestCheckResourceAttrSet(
67+
"data.ovh_cloud_project_database_redis_user.user", "categories.#"),
68+
resource.TestCheckResourceAttrSet(
69+
"data.ovh_cloud_project_database_redis_user.user", "channels.#"),
70+
resource.TestCheckResourceAttrSet(
71+
"data.ovh_cloud_project_database_redis_user.user", "commands.#"),
72+
resource.TestCheckResourceAttrSet(
73+
"data.ovh_cloud_project_database_redis_user.user", "created_at"),
74+
resource.TestCheckResourceAttrSet(
75+
"data.ovh_cloud_project_database_redis_user.user", "keys.#"),
76+
resource.TestCheckResourceAttr(
77+
"data.ovh_cloud_project_database_redis_user.user", "name", name),
78+
resource.TestCheckResourceAttrSet(
79+
"data.ovh_cloud_project_database_redis_user.user", "status"),
80+
),
81+
},
82+
},
83+
})
84+
}

0 commit comments

Comments
 (0)