generated from hashicorp/terraform-provider-scaffolding
-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[feat]: [DBOPS-307]: Onboard DBDevOps harness_platform_db_instance (#…
…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
1 parent
09b1568
commit 6f4ca6f
Showing
12 changed files
with
611 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
``` |
6 changes: 6 additions & 0 deletions
6
examples/data-sources/harness_platform_db_instance/data-source.tf
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
11
examples/resources/harness_platform_db_instance/resource.tf
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 81 additions & 0 deletions
81
internal/service/platform/db_instance/data_source_db_instance.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
96 changes: 96 additions & 0 deletions
96
internal/service/platform/db_instance/data_source_db_instance_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
Oops, something went wrong.