Skip to content

Commit

Permalink
[feat]: [DBOPS-306]: Onboard DBDevOps schema entity (#998)
Browse files Browse the repository at this point in the history
* [feat]: [DBOPS-306]: Onboard DBDevOps schema entity

* test fix

* update optional fields data source

* update optional field data source

* add documentation

* update harness-go-sdk version

* add to changelog

* add data source example

* Address documentation comments

* update documentation

* address PR comments
  • Loading branch information
Abhishek-CDC committed Jul 3, 2024
1 parent 9dfaa1e commit 09b1568
Show file tree
Hide file tree
Showing 17 changed files with 701 additions and 15 deletions.
3 changes: 3 additions & 0 deletions .changelog/998.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:new-resource
harness_platform_db_schema
```
51 changes: 51 additions & 0 deletions docs/data-sources/platform_db_schema.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "harness_platform_db_schema Data Source - terraform-provider-harness"
subcategory: "Next Gen"
description: |-
Data source for retrieving a Harness DBDevOps Schema.
---

# harness_platform_db_schema (Data Source)

Data source for retrieving a Harness DBDevOps Schema.

## Example Usage

```terraform
data "harness_platform_db_schema" "example" {
identifier = "identifier"
org_id = "org_id"
project_id = "project_id"
}
```

<!-- 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.

### Optional

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

### Read-Only

- `description` (String) Description of the resource.
- `id` (String) The ID of this resource.
- `schema_source` (List of Object) Provides a connector and path at which to find the database schema representation (see [below for nested schema](#nestedatt--schema_source))
- `service` (String) The service associated with schema
- `tags` (Set of String) Tags to associate with the resource.

<a id="nestedatt--schema_source"></a>
### Nested Schema for `schema_source`

Read-Only:

- `connector` (String)
- `location` (String)
- `repo` (String)
71 changes: 71 additions & 0 deletions docs/resources/platform_db_schema.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "harness_platform_db_schema Resource - terraform-provider-harness"
subcategory: "Next Gen"
description: |-
Resource for creating a Harness DBDevOps Schema.
---

# harness_platform_db_schema (Resource)

Resource for creating a Harness DBDevOps Schema.

## Example Usage

```terraform
resource "harness_platform_db_schema" "test" {
identifier = "identifier"
org_id = "org_id"
project_id = "project_id"
name = "name"
service = "service1"
tags = ["foo:bar", "bar:foo"]
schema_source {
connector = "gitConnector"
repo = "TestRepo"
location = "db/example-changelog.yaml"
}
}
```

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

### Required

- `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_source` (Block List, Min: 1, Max: 1) Provides a connector and path at which to find the database schema representation (see [below for nested schema](#nestedblock--schema_source))

### Optional

- `description` (String) Description of the resource.
- `service` (String) The service associated with schema
- `tags` (Set of String) Tags to associate with the resource.

### Read-Only

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

<a id="nestedblock--schema_source"></a>
### Nested Schema for `schema_source`

Required:

- `connector` (String) Connector to repository at which to find details about the database schema
- `location` (String) The path within the specified repository at which to find details about the database schema

Optional:

- `repo` (String) If connector url is of account, which repository to connect to using the connector

## Import

Import is supported using the following syntax:

```shell
# Import project level db schema
terraform import harness_platform_db_schema.example <org_id>/<project_id>/<db_schema_id>
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
data "harness_platform_db_schema" "example" {
identifier = "identifier"
org_id = "org_id"
project_id = "project_id"
}
2 changes: 2 additions & 0 deletions examples/resources/harness_platform_db_schema/import.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Import project level db schema
terraform import harness_platform_db_schema.example <org_id>/<project_id>/<db_schema_id>
13 changes: 13 additions & 0 deletions examples/resources/harness_platform_db_schema/resource.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
resource "harness_platform_db_schema" "test" {
identifier = "identifier"
org_id = "org_id"
project_id = "project_id"
name = "name"
service = "service1"
tags = ["foo:bar", "bar:foo"]
schema_source {
connector = "gitConnector"
repo = "TestRepo"
location = "db/example-changelog.yaml"
}
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ require (
github.com/antihax/optional v1.0.0
github.com/aws/aws-sdk-go v1.46.4
github.com/docker/docker v24.0.5+incompatible
github.com/harness/harness-go-sdk v0.3.92
github.com/harness/harness-go-sdk v0.3.93
github.com/harness/harness-openapi-go-client v0.0.19
github.com/hashicorp/go-cleanhttp v0.5.2
github.com/hashicorp/go-cty v1.4.1-0.20200414143053-d3edf31b6320
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMyw
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/harness/harness-go-sdk v0.3.92 h1:XkMdvcXzppn0WpakSENagKVZOl52XOUr2dMYznWEFh8=
github.com/harness/harness-go-sdk v0.3.92/go.mod h1:CPXydorp4zd5Dz2u2FXiHyWL4yd5PQafOMN69cgPSvk=
github.com/harness/harness-go-sdk v0.3.93 h1:D9KK7savwonhl4lUEV1uvZmYsxxxCS3nZxHR/JI7wmo=
github.com/harness/harness-go-sdk v0.3.93/go.mod h1:CPXydorp4zd5Dz2u2FXiHyWL4yd5PQafOMN69cgPSvk=
github.com/harness/harness-openapi-go-client v0.0.19 h1:8XuZvSPZrNqKRLh7Qksdz78WvRMRzRf88LgzxoT5u7k=
github.com/harness/harness-openapi-go-client v0.0.19/go.mod h1:u0vqYb994BJGotmEwJevF4L3BNAdU9i8ui2d22gmLPA=
github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
Expand Down
44 changes: 44 additions & 0 deletions helpers/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"net/http"

"github.com/harness/harness-go-sdk/harness/dbops"
"github.com/harness/harness-go-sdk/harness/nextgen"
openapi_client_nextgen "github.com/harness/harness-openapi-go-client/nextgen"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
Expand Down Expand Up @@ -58,6 +59,27 @@ func HandleApiError(err error, d *schema.ResourceData, httpResp *http.Response)
return diag.Errorf(err.Error())
}

func HandleDBOpsApiError(err error, d *schema.ResourceData, httpResp *http.Response) diag.Diagnostics {
erro, ok := err.(dbops.GenericSwaggerError)
if ok && httpResp != nil {
if httpResp.StatusCode == 401 {
return diag.Errorf(httpResp.Status + "\n" + "Hint:\n" +
"1) Please check if token has expired or is wrong.\n" +
"2) Harness Provider is misconfigured. For firstgen resources please give the correct api_key and for nextgen resources please give the correct platform_api_key.")
}
if httpResp.StatusCode == 403 {
return diag.Errorf(httpResp.Status + "\n" + "Hint:\n" +
"1) Please check if the token has required permission for this operation.\n" +
"2) Please check if the token has expired or is wrong.")
}
if httpResp.StatusCode == 404 {
return diag.Errorf("resource with ID %s not found: %v", d.Id(), erro.Error())
}
}

return diag.Errorf(err.Error())
}

func HandleReadApiError(err error, d *schema.ResourceData, httpResp *http.Response) diag.Diagnostics {
erro, ok := err.(nextgen.GenericSwaggerError)
if ok {
Expand Down Expand Up @@ -107,3 +129,25 @@ func HandleReadApiError(err error, d *schema.ResourceData, httpResp *http.Respon

return diag.Errorf(err.Error())
}

func HandleDBOpsReadApiError(err error, d *schema.ResourceData, httpResp *http.Response) diag.Diagnostics {
_, ok := err.(dbops.GenericSwaggerError)
if ok && httpResp != nil {
if httpResp.StatusCode == 401 {
return diag.Errorf(httpResp.Status + "\n" + "Hint:\n" +
"1) Please check if token has expired or is wrong.\n" +
"2) Harness Provider is misconfigured. For firstgen resources please give the correct api_key and for nextgen resources please give the correct platform_api_key.")
}
if httpResp.StatusCode == 403 {
return diag.Errorf(httpResp.Status + "\n" + "Hint:\n" +
"1) Please check if the token has required permission for this operation.\n" +
"2) Please check if the token has expired or is wrong.")
}
if httpResp.StatusCode == 404 {
d.SetId("")
d.MarkNewResource()
return nil
}
}
return diag.Errorf(err.Error())
}
10 changes: 10 additions & 0 deletions helpers/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,11 +134,21 @@ func SetOrgLevelDataSourceSchema(s map[string]*schema.Schema) {
s["org_id"] = GetOrgIdSchema(SchemaFlagTypes.Required)
}

func SetOrgLevelDataSourceSchemaIdentifierRequired(s map[string]*schema.Schema) {
SetCommonDataSourceSchemaIdentifierRequired(s)
s["org_id"] = GetOrgIdSchema(SchemaFlagTypes.Required)
}

func SetProjectLevelDataSourceSchema(s map[string]*schema.Schema) {
SetOrgLevelDataSourceSchema(s)
s["project_id"] = GetProjectIdSchema(SchemaFlagTypes.Required)
}

func SetProjectLevelDataSourceSchemaIdentifierRequired(s map[string]*schema.Schema) {
SetOrgLevelDataSourceSchemaIdentifierRequired(s)
s["project_id"] = GetProjectIdSchema(SchemaFlagTypes.Required)
}

func SetOptionalOrgAndProjectLevelDataSourceSchema(s map[string]*schema.Schema) {
s["org_id"] = GetOrgIdSchema(SchemaFlagTypes.Optional)
s["project_id"] = GetProjectIdSchema(SchemaFlagTypes.Optional)
Expand Down
5 changes: 5 additions & 0 deletions internal/acctest/acctest.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

"github.com/harness/harness-go-sdk/harness/cd/graphql"
"github.com/harness/harness-go-sdk/harness/code"
"github.com/harness/harness-go-sdk/harness/dbops"
"github.com/harness/harness-go-sdk/harness/helpers"
"github.com/harness/harness-go-sdk/harness/nextgen"
"github.com/harness/harness-go-sdk/harness/policymgmt"
Expand Down Expand Up @@ -64,6 +65,10 @@ func TestAccGetPlatformClientWithContext() (*nextgen.APIClient, context.Context)
return TestAccProvider.Meta().(*internal.Session).GetPlatformClientWithContext(context.Background())
}

func TestAccGetDBOpsClientWithContext() (*dbops.APIClient, context.Context) {
return TestAccProvider.Meta().(*internal.Session).GetDBOpsClientWithContext(context.Background())
}

func TestAccGetClientWithContext() (*openapi_client_nextgen.APIClient, context.Context) {
return TestAccProvider.Meta().(*internal.Session).GetClientWithContext(context.Background())
}
Expand Down
29 changes: 23 additions & 6 deletions internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"fmt"
"log"

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

"github.com/harness/terraform-provider-harness/internal/service/platform/feature_flag"
"github.com/harness/terraform-provider-harness/internal/service/platform/feature_flag_target"
feature_flag_target_group "github.com/harness/terraform-provider-harness/internal/service/platform/feature_flag_target_group"
Expand All @@ -22,6 +24,7 @@ import (
"github.com/harness/harness-go-sdk/harness"
"github.com/harness/harness-go-sdk/harness/cd"
"github.com/harness/harness-go-sdk/harness/code"
"github.com/harness/harness-go-sdk/harness/dbops"
"github.com/harness/harness-go-sdk/harness/helpers"
"github.com/harness/harness-go-sdk/harness/nextgen"
"github.com/harness/harness-go-sdk/harness/utils"
Expand Down Expand Up @@ -183,6 +186,7 @@ func Provider(version string) func() *schema.Provider {
"harness_platform_current_user": pl_current_user.DataSourceCurrentUser(),
"harness_platform_user": pl_user.DataSourceUser(),
"harness_platform_environment": pl_environment.DataSourceEnvironment(),
"harness_platform_db_schema": dbschema.DataSourceDBSchema(),
"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 @@ -305,6 +309,7 @@ func Provider(version string) func() *schema.Provider {
"harness_platform_connector_sumologic": connector.ResourceConnectorSumologic(),
"harness_platform_connector_pdc": connector.ResourceConnectorPdc(),
"harness_platform_environment": pl_environment.ResourceEnvironment(),
"harness_platform_db_schema": dbschema.ResourceDBSchema(),
"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 Expand Up @@ -455,6 +460,17 @@ func getPLClient(d *schema.ResourceData, version string) *nextgen.APIClient {
return client
}

func getDBOpsClient(d *schema.ResourceData, version string) *dbops.APIClient {
client := dbops.NewAPIClient(&dbops.Configuration{
AccountId: d.Get("account_id").(string),
BasePath: d.Get("endpoint").(string),
ApiKey: d.Get("platform_api_key").(string),
UserAgent: fmt.Sprintf("terraform-provider-harness-platform-%s", version),
})

return client
}

func getClient(d *schema.ResourceData, version string) *openapi_client_nextgen.APIClient {
cfg := openapi_client_nextgen.NewConfiguration()
client := openapi_client_nextgen.NewAPIClient(&openapi_client_nextgen.Configuration{
Expand Down Expand Up @@ -487,12 +503,13 @@ func getCodeClient(d *schema.ResourceData, version string) *code.APIClient {
func configure(version string, p *schema.Provider) func(context.Context, *schema.ResourceData) (interface{}, diag.Diagnostics) {
return func(ctx context.Context, d *schema.ResourceData) (interface{}, diag.Diagnostics) {
return &internal.Session{
AccountId: d.Get("account_id").(string),
Endpoint: d.Get("endpoint").(string),
CDClient: getCDClient(d, version),
PLClient: getPLClient(d, version),
Client: getClient(d, version),
CodeClient: getCodeClient(d, version),
AccountId: d.Get("account_id").(string),
Endpoint: d.Get("endpoint").(string),
CDClient: getCDClient(d, version),
PLClient: getPLClient(d, version),
Client: getClient(d, version),
CodeClient: getCodeClient(d, version),
DBOpsClient: getDBOpsClient(d, version),
}, nil
}
}
Loading

0 comments on commit 09b1568

Please sign in to comment.