Skip to content

Commit

Permalink
Kafka and opensearch lifecycle
Browse files Browse the repository at this point in the history
  • Loading branch information
lpatte committed Sep 6, 2022
1 parent 3a82e77 commit 1f45272
Show file tree
Hide file tree
Showing 87 changed files with 4,064 additions and 108 deletions.
4 changes: 2 additions & 2 deletions ovh/data_cloud_project_database.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func dataSourceCloudProjectDatabase() *schema.Resource {
Description: "Name of the engine of the service",
Required: true,
},
"cluster_id": {
"id": {
Type: schema.TypeString,
Description: "Cluster ID",
Required: true,
Expand Down Expand Up @@ -155,7 +155,7 @@ func dataSourceCloudProjectDatabaseRead(d *schema.ResourceData, meta interface{}
config := meta.(*Config)
serviceName := d.Get("service_name").(string)
engine := d.Get("engine").(string)
id := d.Get("cluster_id").(string)
id := d.Get("id").(string)

serviceEndpoint := fmt.Sprintf("/cloud/project/%s/database/%s/%s",
url.PathEscape(serviceName),
Expand Down
4 changes: 4 additions & 0 deletions ovh/data_cloud_project_database_ip_restrictions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ import (
"os"
"testing"

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

const testAccCloudProjectDatabaseIpRestrictionsDatasourceConfig_Basic = `
resource "ovh_cloud_project_database" "db" {
service_name = "%s"
description = "%s"
engine = "%s"
version = "%s"
plan = "essential"
Expand Down Expand Up @@ -41,10 +43,12 @@ func TestAccCloudProjectDatabaseIpRestrictionsDataSource_basic(t *testing.T) {
region := os.Getenv("OVH_CLOUD_PROJECT_DATABASE_REGION_TEST")
flavor := os.Getenv("OVH_CLOUD_PROJECT_DATABASE_FLAVOR_TEST")
ip := os.Getenv("OVH_CLOUD_PROJECT_DATABASE_IP_RESTRICTION_IP_TEST")
description := acctest.RandomWithPrefix(test_prefix)

config := fmt.Sprintf(
testAccCloudProjectDatabaseIpRestrictionsDatasourceConfig_Basic,
serviceName,
description,
engine,
version,
region,
Expand Down
80 changes: 80 additions & 0 deletions ovh/data_cloud_project_database_kafka_acl.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package ovh

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

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

func dataSourceCloudProjectDatabaseKafkaAcl() *schema.Resource {
return &schema.Resource{
Read: dataSourceCloudProjectDatabaseKafkaAclRead,
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,
},
"id": {
Type: schema.TypeString,
Description: "Acl ID",
Required: true,
},

// Computed
"permission": {
Type: schema.TypeString,
Description: "Permission to give to this username on this topic",
Computed: true,
},
"topic": {
Type: schema.TypeString,
Description: "Topic affected by this acl",
Computed: true,
},
"username": {
Type: schema.TypeString,
Description: "Username affected by this acl",
Computed: true,
},
},
}
}

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

endpoint := fmt.Sprintf("/cloud/project/%s/database/kafka/%s/acl/%s",
url.PathEscape(serviceName),
url.PathEscape(clusterId),
url.PathEscape(id),
)
res := &CloudProjectDatabaseKafkaAclResponse{}

log.Printf("[DEBUG] Will read acl %s from cluster %s from project %s", id, clusterId, serviceName)
if err := config.OVHClient.Get(endpoint, res); err != nil {
return helpers.CheckDeleted(d, err, endpoint)
}

for k, v := range res.ToMap() {
if k != "id" {
d.Set(k, v)
} else {
d.SetId(fmt.Sprint(v))
}
}

log.Printf("[DEBUG] Read acl %+v", res)
return nil
}
90 changes: 90 additions & 0 deletions ovh/data_cloud_project_database_kafka_acl_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package ovh

import (
"fmt"
"os"
"testing"

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

const testAccCloudProjectDatabaseKafkaAclDatasourceConfig_Basic = `
resource "ovh_cloud_project_database" "db" {
service_name = "%s"
description = "%s"
engine = "kafka"
version = "%s"
plan = "business"
nodes {
region = "%s"
}
nodes {
region = "%s"
}
nodes {
region = "%s"
}
flavor = "%s"
}
resource "ovh_cloud_project_database_kafka_acl" "acl" {
service_name = ovh_cloud_project_database.db.service_name
cluster_id = ovh_cloud_project_database.db.id
permission = "%s"
topic = "%s"
username = "%s"
}
data "ovh_cloud_project_database_kafka_acl" "acl" {
service_name = ovh_cloud_project_database_kafka_acl.acl.service_name
cluster_id = ovh_cloud_project_database_kafka_acl.acl.cluster_id
id = ovh_cloud_project_database_kafka_acl.acl.id
}
`

func TestAccCloudProjectDatabaseKafkaAclDataSource_basic(t *testing.T) {
serviceName := os.Getenv("OVH_CLOUD_PROJECT_SERVICE_TEST")
version := os.Getenv("OVH_CLOUD_PROJECT_DATABASE_KAFKA_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")
description := acctest.RandomWithPrefix(test_prefix)
permission := "read"
topic := "myTopic"
username := "johnDoe"

config := fmt.Sprintf(
testAccCloudProjectDatabaseKafkaAclDatasourceConfig_Basic,
serviceName,
description,
version,
region,
region,
region,
flavor,
permission,
topic,
username,
)

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheckCloudDatabaseNoEngine(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: config,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(
"data.ovh_cloud_project_database_kafka_acl.acl", "permission", permission),
resource.TestCheckResourceAttr(
"data.ovh_cloud_project_database_kafka_acl.acl", "topic", topic),
resource.TestCheckResourceAttr(
"data.ovh_cloud_project_database_kafka_acl.acl", "username", username),
),
},
},
})
}
62 changes: 62 additions & 0 deletions ovh/data_cloud_project_database_kafka_acls.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package ovh

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

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/ovh/terraform-provider-ovh/ovh/helpers"
"github.com/ovh/terraform-provider-ovh/ovh/helpers/hashcode"
)

func dataSourceCloudProjectDatabaseKafkaAcls() *schema.Resource {
return &schema.Resource{
Read: dataSourceCloudProjectDatabaseKafkaAclsRead,
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,
},

// Computed
"acl_ids": {
Type: schema.TypeList,
Description: "List of acl ids",
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
},
}
}

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

endpoint := fmt.Sprintf("/cloud/project/%s/database/kafka/%s/acl",
url.PathEscape(serviceName),
url.PathEscape(clusterId),
)
res := make([]string, 0)

log.Printf("[DEBUG] Will read acls from cluster %s from project %s", clusterId, serviceName)
if err := config.OVHClient.Get(endpoint, res); err != nil {
return helpers.CheckDeleted(d, err, endpoint)
}

// sort.Strings sorts in place, returns nothing
sort.Strings(res)

d.SetId(hashcode.Strings(res))
d.Set("acl_ids", res)
return nil
}
87 changes: 87 additions & 0 deletions ovh/data_cloud_project_database_kafka_acls_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package ovh

import (
"fmt"
"os"
"testing"

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

const testAccCloudProjectDatabaseKafkaAclsDatasourceConfig_Basic = `
resource "ovh_cloud_project_database" "db" {
service_name = "%s"
description = "%s"
engine = "kafka"
version = "%s"
plan = "business"
nodes {
region = "%s"
}
nodes {
region = "%s"
}
nodes {
region = "%s"
}
flavor = "%s"
}
resource "ovh_cloud_project_database_kafka_acl" "acl" {
service_name = ovh_cloud_project_database.db.service_name
cluster_id = ovh_cloud_project_database.db.id
permission = "%s"
topic = "%s"
username = "%s"
}
data "ovh_cloud_project_database_kafka_acls" "acls" {
service_name = ovh_cloud_project_database_kafka_acl.acl.service_name
cluster_id = ovh_cloud_project_database_kafka_acl.acl.cluster_id
}
`

func TestAccCloudProjectDatabaseKafkaAclsDataSource_basic(t *testing.T) {
serviceName := os.Getenv("OVH_CLOUD_PROJECT_SERVICE_TEST")
version := os.Getenv("OVH_CLOUD_PROJECT_DATABASE_KAFKA_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")
description := acctest.RandomWithPrefix(test_prefix)
permission := "read"
topic := "myTopic"
username := "johnDoe"

config := fmt.Sprintf(
testAccCloudProjectDatabaseKafkaAclsDatasourceConfig_Basic,
serviceName,
description,
version,
region,
region,
region,
flavor,
permission,
topic,
username,
)

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_kafka_acls.acls",
"acl_ids.#",
),
),
},
},
})
}
Loading

0 comments on commit 1f45272

Please sign in to comment.