Skip to content

Commit

Permalink
[feat]: [DBOPS-307]: Onboard DBDevOps harness_platform_db_instance (#…
Browse files Browse the repository at this point in the history
…1001)

* test fix

* update optional field data source

* update documentation

* [feat]: [DBOPS-307]: Onboard DBDevOps harness_platform_db_instance

* add example for resource and data source

* added documentation

* update tests

* undo schema changes after rebase

* undo schema chanages after rebase

* address PR comments

* update documentation
  • Loading branch information
Abhishek-CDC committed Jul 3, 2024
1 parent 09b1568 commit 6f4ca6f
Show file tree
Hide file tree
Showing 12 changed files with 611 additions and 0 deletions.
45 changes: 45 additions & 0 deletions docs/data-sources/platform_db_instance.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "harness_platform_db_instance Data Source - terraform-provider-harness"
subcategory: "Next Gen"
description: |-
Data source for retrieving a Harness DBDevOps Instance.
---

# harness_platform_db_instance (Data Source)

Data source for retrieving a Harness DBDevOps Instance.

## Example Usage

```terraform
data "harness_platform_db_instance" "example" {
identifier = "identifier"
org_id = "org_id"
project_id = "project_id"
schema = "schema1"
}
```

<!-- schema generated by tfplugindocs -->
## Schema

### Required

- `identifier` (String) Unique identifier of the resource.
- `org_id` (String) Unique identifier of the organization.
- `project_id` (String) Unique identifier of the project.
- `schema` (String) The identifier of the parent database schema

### Optional

- `name` (String) Name of the resource.

### Read-Only

- `branch` (String) The branch of changeSet repository
- `connector` (String) The connector to database
- `context` (String) The liquibase context
- `description` (String) Description of the resource.
- `id` (String) The ID of this resource.
- `tags` (Set of String) Tags to associate with the resource.
59 changes: 59 additions & 0 deletions docs/resources/platform_db_instance.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "harness_platform_db_instance Resource - terraform-provider-harness"
subcategory: "Next Gen"
description: |-
Resource for creating a Harness DBDevOps Instance.
---

# harness_platform_db_instance (Resource)

Resource for creating a Harness DBDevOps Instance.

## Example Usage

```terraform
resource "harness_platform_db_instance" "test" {
identifier = "identifier"
org_id = "org_id"
project_id = "project_id"
name = "name"
tags = ["foo:bar", "bar:foo"]
schema = "schema1"
branch = "main"
connector = "jdbcConnector"
context = "ctx"
}
```

<!-- schema generated by tfplugindocs -->
## Schema

### Required

- `connector` (String) The connector to database
- `identifier` (String) Unique identifier of the resource.
- `name` (String) Name of the resource.
- `org_id` (String) Unique identifier of the organization.
- `project_id` (String) Unique identifier of the project.
- `schema` (String) The identifier of the parent database schema

### Optional

- `branch` (String) The branch of changeSet repository
- `context` (String) The liquibase context
- `description` (String) Description of the resource.
- `tags` (Set of String) Tags to associate with the resource.

### Read-Only

- `id` (String) The ID of this resource.

## Import

Import is supported using the following syntax:

```shell
# Import project level db instance
terraform import harness_platform_db_instance.example <org_id>/<project_id>/<db_schema_id>/<db_instance_id>
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
data "harness_platform_db_instance" "example" {
identifier = "identifier"
org_id = "org_id"
project_id = "project_id"
schema = "schema1"
}
2 changes: 2 additions & 0 deletions examples/resources/harness_platform_db_instance/import.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Import project level db instance
terraform import harness_platform_db_instance.example <org_id>/<project_id>/<db_schema_id>/<db_instance_id>
11 changes: 11 additions & 0 deletions examples/resources/harness_platform_db_instance/resource.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
resource "harness_platform_db_instance" "test" {
identifier = "identifier"
org_id = "org_id"
project_id = "project_id"
name = "name"
tags = ["foo:bar", "bar:foo"]
schema = "schema1"
branch = "main"
connector = "jdbcConnector"
context = "ctx"
}
13 changes: 13 additions & 0 deletions helpers/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,19 @@ var PipelineResourceImporter = &schema.ResourceImporter{
},
}

var DBInstanceResourceImporter = &schema.ResourceImporter{
State: func(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
parts := strings.Split(d.Id(), "/")
d.Set("org_id", parts[0])
d.Set("project_id", parts[1])
d.Set("schema", parts[2])
d.Set("identifier", parts[3])
d.SetId(parts[3])

return []*schema.ResourceData{d}, nil
},
}

var TriggerResourceImporter = &schema.ResourceImporter{
State: func(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
parts := strings.Split(d.Id(), "/")
Expand Down
12 changes: 12 additions & 0 deletions internal/acctest/acctest.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,18 @@ func EnvRelatedResourceImportStateIdFunc(resourceName string) resource.ImportSta
}
}

func DBInstanceResourceImportStateIdFunc(resourceName string) resource.ImportStateIdFunc {
return func(s *terraform.State) (string, error) {
primary := s.RootModule().Resources[resourceName].Primary
id := primary.ID
orgId := primary.Attributes["org_id"]
projId := primary.Attributes["project_id"]
schema := primary.Attributes["schema"]

return fmt.Sprintf("%s/%s/%s/%s", orgId, projId, schema, id), nil
}
}

func OverridesV1ResourceImportStateIdFunc(resourceName string) resource.ImportStateIdFunc {
return func(s *terraform.State) (string, error) {
primary := s.RootModule().Resources[resourceName].Primary
Expand Down
3 changes: 3 additions & 0 deletions internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"log"

dbinstance "github.com/harness/terraform-provider-harness/internal/service/platform/db_instance"
dbschema "github.com/harness/terraform-provider-harness/internal/service/platform/db_schema"

"github.com/harness/terraform-provider-harness/internal/service/platform/feature_flag"
Expand Down Expand Up @@ -187,6 +188,7 @@ func Provider(version string) func() *schema.Provider {
"harness_platform_user": pl_user.DataSourceUser(),
"harness_platform_environment": pl_environment.DataSourceEnvironment(),
"harness_platform_db_schema": dbschema.DataSourceDBSchema(),
"harness_platform_db_instance": dbinstance.DataSourceDBInstance(),
"harness_platform_environment_list": pl_environment.DataSourceEnvironmentList(),
"harness_platform_environment_group": pl_environment_group.DataSourceEnvironmentGroup(),
"harness_platform_environment_clusters_mapping": pl_environment_clusters_mapping.DataSourceEnvironmentClustersMapping(),
Expand Down Expand Up @@ -310,6 +312,7 @@ func Provider(version string) func() *schema.Provider {
"harness_platform_connector_pdc": connector.ResourceConnectorPdc(),
"harness_platform_environment": pl_environment.ResourceEnvironment(),
"harness_platform_db_schema": dbschema.ResourceDBSchema(),
"harness_platform_db_instance": dbinstance.ResourceDBInstance(),
"harness_platform_environment_group": pl_environment_group.ResourceEnvironmentGroup(),
"harness_platform_environment_clusters_mapping": pl_environment_clusters_mapping.ResourceEnvironmentClustersMapping(),
"harness_platform_environment_service_overrides": pl_environment_service_overrides.ResourceEnvironmentServiceOverrides(),
Expand Down
81 changes: 81 additions & 0 deletions internal/service/platform/db_instance/data_source_db_instance.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package dbinstance

import (
"context"
"net/http"

"github.com/antihax/optional"
"github.com/harness/harness-go-sdk/harness/dbops"
"github.com/harness/terraform-provider-harness/helpers"
"github.com/harness/terraform-provider-harness/internal"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

func DataSourceDBInstance() *schema.Resource {
resource := &schema.Resource{
Description: "Data source for retrieving a Harness DBDevOps Instance.",

ReadContext: dataSourceDBInstanceRead,

Schema: map[string]*schema.Schema{
"schema": {
Description: "The identifier of the parent database schema",
Type: schema.TypeString,
Required: true,
},
"branch": {
Description: "The branch of changeSet repository",
Type: schema.TypeString,
Computed: true,
},
"connector": {
Description: "The connector to database",
Type: schema.TypeString,
Computed: true,
},
"context": {
Description: "The liquibase context",
Type: schema.TypeString,
Computed: true,
},
},
}

helpers.SetProjectLevelDataSourceSchemaIdentifierRequired(resource.Schema)

return resource
}

func dataSourceDBInstanceRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
c, ctx := meta.(*internal.Session).GetDBOpsClientWithContext(ctx)

var err error
var dbInstance dbops.DbInstanceOut
var httpResp *http.Response

id := d.Get("identifier").(string)

localVarOptionals := dbops.DatabaseInstanceApiV1GetProjDbSchemaInstanceOpts{
HarnessAccount: optional.NewString(meta.(*internal.Session).AccountId),
}
dbInstance, httpResp, err = c.DatabaseInstanceApi.V1GetProjDbSchemaInstance(ctx, d.Get("org_id").(string), d.Get("project_id").(string), d.Get("schema").(string), id, &localVarOptionals)

if err != nil {
return helpers.HandleDBOpsApiError(err, d, httpResp)
}

readDataSourceDBInstance(d, &dbInstance)

return nil
}

func readDataSourceDBInstance(d *schema.ResourceData, dbInstance *dbops.DbInstanceOut) {
d.SetId(dbInstance.Identifier)
d.Set("identifier", dbInstance.Identifier)
d.Set("name", dbInstance.Name)
d.Set("tags", helpers.FlattenTags(dbInstance.Tags))
d.Set("branch", dbInstance.Branch)
d.Set("connector", dbInstance.Connector)
d.Set("context", dbInstance.Context)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package dbinstance_test

import (
"fmt"
"testing"

"github.com/harness/harness-go-sdk/harness/utils"
"github.com/harness/terraform-provider-harness/internal/acctest"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
)

func TestAccDataSourceDBInstance(t *testing.T) {

id := fmt.Sprintf("%s_%s", t.Name(), utils.RandStringBytes(6))
name := id
resourceName := "data.harness_platform_db_instance.test"

resource.UnitTest(t, resource.TestCase{
PreCheck: func() { acctest.TestAccPreCheck(t) },
ProviderFactories: acctest.ProviderFactories,
Steps: []resource.TestStep{
{
Config: testAccDataSourceDBInstance(id, name),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(resourceName, "identifier", id),
resource.TestCheckResourceAttr(resourceName, "org_id", id),
resource.TestCheckResourceAttr(resourceName, "name", name),
resource.TestCheckResourceAttr(resourceName, "project_id", id),
),
},
},
})
}

func testAccDataSourceDBInstance(id string, name string) string {
return fmt.Sprintf(`
resource "harness_platform_organization" "test" {
identifier = "%[1]s"
name = "%[2]s"
}
resource "harness_platform_project" "test" {
identifier = "%[1]s"
name = "%[2]s"
org_id = harness_platform_organization.test.id
color = "#472848"
}
resource "harness_platform_connector_github" "test" {
identifier = "%[1]s"
name = "%[2]s"
description = "test"
tags = ["foo:bar"]
org_id = harness_platform_project.test.org_id
project_id = harness_platform_project.test.id
url = "https://github.com/account"
connection_type = "Account"
validation_repo = "some_repo"
delegate_selectors = ["harness-delegate"]
credentials {
http {
anonymous {}
}
}
}
resource "harness_platform_db_schema" "test" {
identifier = "%[1]s"
org_id = harness_platform_project.test.org_id
project_id = harness_platform_project.test.id
name = "%[2]s"
service = "s1"
tags = ["foo:bar", "bar:foo"]
schema_source {
connector = harness_platform_connector_github.test.id
repo = "TestRepo"
location = "db/example-changelog.yaml"
}
}
resource "harness_platform_db_instance" "test" {
identifier = "%[1]s"
org_id = harness_platform_project.test.org_id
project_id = harness_platform_project.test.id
name = "%[2]s"
tags = ["foo:bar", "bar:foo"]
branch = "main"
connector = harness_platform_connector_github.test.id
schema = harness_platform_db_schema.test.id
}
data "harness_platform_db_instance" "test" {
identifier = harness_platform_db_instance.test.id
org_id = harness_platform_organization.test.id
project_id = harness_platform_project.test.id
schema = harness_platform_db_schema.test.id
}
`, id, name)
}
Loading

0 comments on commit 6f4ca6f

Please sign in to comment.