Skip to content

Commit 35a7819

Browse files
authored
Merge pull request #449 from BellionBastien/dev/bbellion/add-database-kafka-schema-registry
Add Database kafka schema registry
2 parents 485f340 + 0711c98 commit 35a7819

10 files changed

+750
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package ovh
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"log"
7+
"net/url"
8+
9+
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
10+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
11+
"github.com/ovh/terraform-provider-ovh/ovh/helpers"
12+
)
13+
14+
func dataSourceCloudProjectDatabaseKafkaSchemaRegistryAcl() *schema.Resource {
15+
return &schema.Resource{
16+
ReadContext: dataSourceCloudProjectDatabaseKafkaSchemaregistryaclRead,
17+
Schema: map[string]*schema.Schema{
18+
"service_name": {
19+
Type: schema.TypeString,
20+
Required: true,
21+
DefaultFunc: schema.EnvDefaultFunc("OVH_CLOUD_PROJECT_SERVICE", nil),
22+
},
23+
"cluster_id": {
24+
Type: schema.TypeString,
25+
Description: "Id of the database cluster",
26+
Required: true,
27+
},
28+
"id": {
29+
Type: schema.TypeString,
30+
Description: "Shema registry ACL ID",
31+
Required: true,
32+
},
33+
34+
// Computed
35+
"permission": {
36+
Type: schema.TypeString,
37+
Description: "Permission to give to this username on this resource",
38+
Computed: true,
39+
},
40+
"resource": {
41+
Type: schema.TypeString,
42+
Description: "Resource affected by this ACL",
43+
Computed: true,
44+
},
45+
"username": {
46+
Type: schema.TypeString,
47+
Description: "Username affected by this ACL",
48+
Computed: true,
49+
},
50+
},
51+
}
52+
}
53+
54+
func dataSourceCloudProjectDatabaseKafkaSchemaregistryaclRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
55+
config := meta.(*Config)
56+
serviceName := d.Get("service_name").(string)
57+
clusterID := d.Get("cluster_id").(string)
58+
id := d.Get("id").(string)
59+
60+
endpoint := fmt.Sprintf("/cloud/project/%s/database/kafka/%s/schemaRegistryAcl/%s",
61+
url.PathEscape(serviceName),
62+
url.PathEscape(clusterID),
63+
url.PathEscape(id),
64+
)
65+
res := &CloudProjectDatabaseKafkaAclResponse{}
66+
67+
log.Printf("[DEBUG] Will read schema registry ACL %s from cluster %s from project %s", id, clusterID, serviceName)
68+
if err := config.OVHClient.Get(endpoint, res); err != nil {
69+
return diag.FromErr(helpers.CheckDeleted(d, err, endpoint))
70+
}
71+
72+
for k, v := range res.ToMap() {
73+
if k != "id" {
74+
d.Set(k, v)
75+
} else {
76+
d.SetId(fmt.Sprint(v))
77+
}
78+
}
79+
80+
log.Printf("[DEBUG] Read ACL %+v", res)
81+
return nil
82+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package ovh
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"log"
7+
"net/url"
8+
"sort"
9+
10+
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
11+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
12+
"github.com/ovh/terraform-provider-ovh/ovh/helpers"
13+
"github.com/ovh/terraform-provider-ovh/ovh/helpers/hashcode"
14+
)
15+
16+
func dataSourceCloudProjectDatabaseKafkaSchemaRegistryAcls() *schema.Resource {
17+
return &schema.Resource{
18+
ReadContext: dataSourceCloudProjectDatabaseKafkaSchemaregistryclsRead,
19+
Schema: map[string]*schema.Schema{
20+
"service_name": {
21+
Type: schema.TypeString,
22+
Required: true,
23+
DefaultFunc: schema.EnvDefaultFunc("OVH_CLOUD_PROJECT_SERVICE", nil),
24+
},
25+
"cluster_id": {
26+
Type: schema.TypeString,
27+
Description: "Id of the database cluster",
28+
Required: true,
29+
},
30+
31+
// Computed
32+
"acl_ids": {
33+
Type: schema.TypeList,
34+
Description: "List of schema registry acl ids",
35+
Computed: true,
36+
Elem: &schema.Schema{Type: schema.TypeString},
37+
},
38+
},
39+
}
40+
}
41+
42+
func dataSourceCloudProjectDatabaseKafkaSchemaregistryclsRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
43+
config := meta.(*Config)
44+
serviceName := d.Get("service_name").(string)
45+
clusterId := d.Get("cluster_id").(string)
46+
47+
endpoint := fmt.Sprintf("/cloud/project/%s/database/kafka/%s/schemaRegistryAcl",
48+
url.PathEscape(serviceName),
49+
url.PathEscape(clusterId),
50+
)
51+
res := make([]string, 0)
52+
53+
log.Printf("[DEBUG] Will read schema registry ACLs from cluster %s from project %s", clusterId, serviceName)
54+
if err := config.OVHClient.Get(endpoint, &res); err != nil {
55+
return diag.FromErr(helpers.CheckDeleted(d, err, endpoint))
56+
}
57+
58+
// sort.Strings sorts in place, returns nothing
59+
sort.Strings(res)
60+
61+
d.SetId(hashcode.Strings(res))
62+
d.Set("acl_ids", res)
63+
return nil
64+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
package ovh
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"testing"
7+
8+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
9+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
10+
)
11+
12+
const testAccCloudProjectDatabaseKafkaSchemaregistryaclsDatasourceConfig_Basic = `
13+
resource "ovh_cloud_project_database" "db" {
14+
service_name = "%s"
15+
description = "%s"
16+
engine = "kafka"
17+
version = "%s"
18+
plan = "business"
19+
nodes {
20+
region = "%s"
21+
}
22+
nodes {
23+
region = "%s"
24+
}
25+
nodes {
26+
region = "%s"
27+
}
28+
flavor = "%s"
29+
}
30+
31+
resource "ovh_cloud_project_database_kafka_schemaregistryacl" "schemaRegistryAcl" {
32+
service_name = ovh_cloud_project_database.db.service_name
33+
cluster_id = ovh_cloud_project_database.db.id
34+
permission = "%s"
35+
resource = "%s"
36+
username = "%s"
37+
}
38+
39+
data "ovh_cloud_project_database_kafka_schemaregistryacls" "schemaRegistryAcls" {
40+
service_name = ovh_cloud_project_database_kafka_acl.acl.service_name
41+
cluster_id = ovh_cloud_project_database_kafka_acl.acl.cluster_id
42+
}
43+
`
44+
45+
func TestAccCloudProjectDatabaseKafkaSchemaregistryaclsDataSource_basic(t *testing.T) {
46+
serviceName := os.Getenv("OVH_CLOUD_PROJECT_SERVICE_TEST")
47+
version := os.Getenv("OVH_CLOUD_PROJECT_DATABASE_KAFKA_VERSION_TEST")
48+
if version == "" {
49+
version = os.Getenv("OVH_CLOUD_PROJECT_DATABASE_VERSION_TEST")
50+
}
51+
region := os.Getenv("OVH_CLOUD_PROJECT_DATABASE_REGION_TEST")
52+
flavor := os.Getenv("OVH_CLOUD_PROJECT_DATABASE_FLAVOR_TEST")
53+
description := acctest.RandomWithPrefix(test_prefix)
54+
permission := "schema_registry_read"
55+
aclResource := "Subject:myResource"
56+
username := "johnDoe"
57+
58+
config := fmt.Sprintf(
59+
testAccCloudProjectDatabaseKafkaAclsDatasourceConfig_Basic,
60+
serviceName,
61+
description,
62+
version,
63+
region,
64+
region,
65+
region,
66+
flavor,
67+
permission,
68+
aclResource,
69+
username,
70+
)
71+
72+
resource.Test(t, resource.TestCase{
73+
PreCheck: func() { testAccPreCheckCloudDatabaseNoEngine(t) },
74+
Providers: testAccProviders,
75+
Steps: []resource.TestStep{
76+
{
77+
Config: config,
78+
Check: resource.ComposeTestCheckFunc(
79+
resource.TestCheckResourceAttrSet(
80+
"data.ovh_cloud_project_database_kafka_schemaregistryacls.schemaRegistryAcls",
81+
"acl_ids.#",
82+
),
83+
),
84+
},
85+
},
86+
})
87+
}

Diff for: ovh/provider.go

+3
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ func Provider() *schema.Provider {
6363
"ovh_cloud_project_database_ip_restrictions": dataSourceCloudProjectDatabaseIPRestrictions(),
6464
"ovh_cloud_project_database_kafka_acl": dataSourceCloudProjectDatabaseKafkaACL(),
6565
"ovh_cloud_project_database_kafka_acls": dataSourceCloudProjectDatabaseKafkaAcls(),
66+
"ovh_cloud_project_database_kafka_schemaregistryacl": dataSourceCloudProjectDatabaseKafkaSchemaRegistryAcl(),
67+
"ovh_cloud_project_database_kafka_schemaregistryacls": dataSourceCloudProjectDatabaseKafkaSchemaRegistryAcls(),
6668
"ovh_cloud_project_database_kafka_topic": dataSourceCloudProjectDatabaseKafkaTopic(),
6769
"ovh_cloud_project_database_kafka_topics": dataSourceCloudProjectDatabaseKafkaTopics(),
6870
"ovh_cloud_project_database_kafka_user_access": dataSourceCloudProjectDatabaseKafkaUserAccess(),
@@ -146,6 +148,7 @@ func Provider() *schema.Provider {
146148
"ovh_cloud_project_database_integration": resourceCloudProjectDatabaseIntegration(),
147149
"ovh_cloud_project_database_ip_restriction": resourceCloudProjectDatabaseIpRestriction(),
148150
"ovh_cloud_project_database_kafka_acl": resourceCloudProjectDatabaseKafkaAcl(),
151+
"ovh_cloud_project_database_kafka_schemaregistryacl": resourceCloudProjectDatabaseKafkaSchemaregistryacl(),
149152
"ovh_cloud_project_database_kafka_topic": resourceCloudProjectDatabaseKafkaTopic(),
150153
"ovh_cloud_project_database_m3db_namespace": resourceCloudProjectDatabaseM3dbNamespace(),
151154
"ovh_cloud_project_database_m3db_user": resourceCloudProjectDatabaseM3dbUser(),

0 commit comments

Comments
 (0)