Skip to content

Commit

Permalink
COD-3780 - Terraform minimal create, drop database (#99)
Browse files Browse the repository at this point in the history
also update coverage-threshold version

Co-authored-by: Richard Antal <[email protected]>
  • Loading branch information
richardantal and Richard Antal authored Mar 25, 2024
1 parent c69a5f7 commit c2f4861
Show file tree
Hide file tree
Showing 22 changed files with 838 additions and 22 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ jobs:
- name: Go Coverage
uses: gwatts/[email protected]
with:
coverage-threshold: 18.2
coverage-threshold: 29.6
cover-pkg: ./...
ignore-pattern: |
/cdp-sdk-go/
Expand Down
20 changes: 20 additions & 0 deletions cdp-sdk-go/cdp/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@ import (
environmentsclient "github.com/cloudera/terraform-provider-cdp/cdp-sdk-go/gen/environments/client"
iamclient "github.com/cloudera/terraform-provider-cdp/cdp-sdk-go/gen/iam/client"
mlclient "github.com/cloudera/terraform-provider-cdp/cdp-sdk-go/gen/ml/client"
opdbclient "github.com/cloudera/terraform-provider-cdp/cdp-sdk-go/gen/opdb/client"
)

type Client struct {
config *Config
Environments *environmentsclient.Environments
Datalake *datalakeclient.Datalake
Opdb *opdbclient.Opdb
Datahub *datahubclient.Datahub
Iam *iamclient.Iam
Ml *mlclient.Ml
Expand All @@ -48,6 +50,11 @@ func NewClient(config *Config) (*Client, error) {
return nil, err
}

opdbClient, err := NewOpdbClient(config)
if err != nil {
return nil, err
}

iamClient, err := NewIamClient(config)
if err != nil {
return nil, err
Expand All @@ -68,6 +75,7 @@ func NewClient(config *Config) (*Client, error) {
Environments: environmentsClient,
Datalake: datalakeClient,
Datahub: datahubClient,
Opdb: opdbClient,
Iam: iamClient,
Ml: mlClient,
Dw: dwClient,
Expand Down Expand Up @@ -122,6 +130,18 @@ func NewDatahubClient(config *Config) (*datahubclient.Datahub, error) {
return datahubclient.New(transport, nil), nil
}

func NewOpdbClient(config *Config) (*opdbclient.Opdb, error) {
apiEndpoint, err := config.GetEndpoint("opdb", false)
if err != nil {
return nil, err
}
transport, err := buildClientTransportWithDefaultHttpTransport(config, apiEndpoint)
if err != nil {
return nil, err
}
return opdbclient.New(transport, nil), nil
}

func NewMlClient(config *Config) (*mlclient.Ml, error) {
apiEndpoint, err := config.GetEndpoint("ml", false)
if err != nil {
Expand Down
5 changes: 5 additions & 0 deletions cdp-sdk-go/cdp/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
environmentsmodels "github.com/cloudera/terraform-provider-cdp/cdp-sdk-go/gen/environments/models"
iammodels "github.com/cloudera/terraform-provider-cdp/cdp-sdk-go/gen/iam/models"
mlmodels "github.com/cloudera/terraform-provider-cdp/cdp-sdk-go/gen/ml/models"
opdbmodels "github.com/cloudera/terraform-provider-cdp/cdp-sdk-go/gen/opdb/models"
"strings"
)

Expand All @@ -37,6 +38,10 @@ func IsDatahubError(err *datahubmodels.Error, code string, message string) bool
return err.Code == code && strings.Contains(err.Message, message)
}

func IsDatabaseError(err *opdbmodels.Error, code string, message string) bool {
return err.Code == code && strings.Contains(err.Message, message)
}

func IsMlError(err *mlmodels.Error, code string, message string) bool {
return err.Code == code && strings.Contains(err.Message, message)
}
4 changes: 2 additions & 2 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
page_title: "Cloudera Data Platform (CDP) Provider"
subcategory: ""
description: |-
The Cloudera Data Platform (CDP) provider manages the lifecycle of resources supported by CDP like Credentials, Environment, Datalake, Datahub and other data services.
The Cloudera Data Platform (CDP) provider manages the lifecycle of resources supported by CDP like Credentials, Environment, Datalake, Datahub, OPDB and other data services.
---

# CDP Provider

The Cloudera Data Platform (CDP) provider manages the lifecycle of resources supported by CDP like Credentials, Environment, Datalake, Datahub and other data services.
The Cloudera Data Platform (CDP) provider manages the lifecycle of resources supported by CDP like Credentials, Environment, Datalake, Datahub, OPDB and other data services.

The provider needs to be configured with the proper credentials before it can be used (see Authentication section below).

Expand Down
78 changes: 78 additions & 0 deletions docs/resources/operational_database.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "cdp_operational_database Resource - terraform-provider-cdp"
subcategory: ""
description: |-
Creates an Operational DataBase.
---

# cdp_operational_database (Resource)

Creates an Operational DataBase.

## Example Usage

```terraform
// Copyright 2024 Cloudera. All Rights Reserved.
//
// This file is licensed under the Apache License Version 2.0 (the "License").
// You may not use this file except in compliance with the License.
// You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0.
//
// This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS
// OF ANY KIND, either express or implied. Refer to the License for the specific
// permissions and limitations governing your use of the file.
terraform {
required_providers {
cdp = {
source = "cloudera/cdp"
}
}
}
provider "cdp" {
cdp_config_file = "/Users/<value>/.cdp/config"
cdp_shared_credentials_file = "/Users/<value>/.cdp/credentials"
}
resource "cdp_operational_database" "opdb" {
database_name = "<value>"
environment_name = "<value>"
scale_type = "MICRO" // valid options are "MICRO","LIGHT","HEAVY"
storage_type = "HDFS" // valid options are "CLOUD_WITH_EPHEMERAL","CLOUD","HDFS"
disable_external_db = true
}
```

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

### Required

- `database_name` (String) The name of the database.
- `environment_name` (String) The name of the environment where the cluster will belong to.

### Optional

- `disable_external_db` (Boolean) Disable external database creation or not
- `polling_options` (Attributes) Polling related configuration options that could specify various values that will be used during CDP resource creation. (see [below for nested schema](#nestedatt--polling_options))
- `scale_type` (String) Scale type, MICRO, LIGHT or HEAVY
- `storage_type` (String) Storage type for clusters, CLOUD_WITH_EPHEMERAL, CLOUD or HDFS

### Read-Only

- `crn` (String) The CRN of the cluster.
- `status` (String) The last known state of the cluster
- `storage_location` (String) Storage Location for OPDB

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

Optional:

- `polling_timeout` (Number) Timeout value in minutes that specifies for how long should the polling go for resource creation/deletion.


32 changes: 32 additions & 0 deletions examples/resources/cdp_operational_database/resource.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Copyright 2024 Cloudera. All Rights Reserved.
//
// This file is licensed under the Apache License Version 2.0 (the "License").
// You may not use this file except in compliance with the License.
// You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0.
//
// This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS
// OF ANY KIND, either express or implied. Refer to the License for the specific
// permissions and limitations governing your use of the file.

terraform {
required_providers {
cdp = {
source = "cloudera/cdp"
}
}
}

provider "cdp" {
cdp_config_file = "/Users/<value>/.cdp/config"
cdp_shared_credentials_file = "/Users/<value>/.cdp/credentials"
}

resource "cdp_operational_database" "opdb" {
database_name = "<value>"
environment_name = "<value>"

scale_type = "MICRO" // valid options are "MICRO","LIGHT","HEAVY"
storage_type = "HDFS" // valid options are "CLOUD_WITH_EPHEMERAL","CLOUD","HDFS"

disable_external_db = true
}
4 changes: 3 additions & 1 deletion provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (

"github.com/cloudera/terraform-provider-cdp/resources/datahub"
"github.com/cloudera/terraform-provider-cdp/resources/iam"
"github.com/cloudera/terraform-provider-cdp/resources/opdb"

"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/provider"
Expand Down Expand Up @@ -70,7 +71,7 @@ func (p *CdpProvider) Metadata(_ context.Context, _ provider.MetadataRequest, re

func (p *CdpProvider) Schema(_ context.Context, _ provider.SchemaRequest, resp *provider.SchemaResponse) {
resp.Schema = schema.Schema{
MarkdownDescription: "The Cloudera Data Platform (CDP) provider manages the lifecycle of resources supported by CDP like Credentials, Environment, Datalake, Datahub and other data services.",
MarkdownDescription: "The Cloudera Data Platform (CDP) provider manages the lifecycle of resources supported by CDP like Credentials, Environment, Datalake, Datahub, OPDB and other data services.",
Attributes: map[string]schema.Attribute{
"cdp_access_key_id": schema.StringAttribute{
Optional: true,
Expand Down Expand Up @@ -236,6 +237,7 @@ func (p *CdpProvider) Resources(_ context.Context) []func() resource.Resource {
datahub.NewAwsDatahubResource,
datahub.NewAzureDatahubResource,
datahub.NewGcpDatahubResource,
opdb.NewDatabaseResource,
}
}

Expand Down
2 changes: 2 additions & 0 deletions provider/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"github.com/cloudera/terraform-provider-cdp/resources/datalake"
"github.com/cloudera/terraform-provider-cdp/resources/environments"
"github.com/cloudera/terraform-provider-cdp/resources/iam"
"github.com/cloudera/terraform-provider-cdp/resources/opdb"
testUtil "github.com/cloudera/terraform-provider-cdp/utils/test"
fwprovider "github.com/hashicorp/terraform-plugin-framework/provider"
"github.com/hashicorp/terraform-plugin-framework/types"
Expand Down Expand Up @@ -621,6 +622,7 @@ func TestCdpProvider_Resources(t *testing.T) {
datahub.NewAwsDatahubResource,
datahub.NewAzureDatahubResource,
datahub.NewGcpDatahubResource,
opdb.NewDatabaseResource,
}

provider := CdpProvider{testVersion}
Expand Down
19 changes: 3 additions & 16 deletions resources/datahub/constants_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,12 @@

package datahub

import "reflect"
import "testing"

func TestFailedStatusKeywords(t *testing.T) {
expected := [2]string{"FAILED", "DELETED"}
size := len(failedStatusKeywords)
expectedSize := len(expected)
if size != expectedSize {
t.Errorf("The size of the failed status keywords is not the expected! Expected: %d, got: %d", expectedSize, size)
}
for _, expectedElement := range expected {
found := false
for _, keyword := range failedStatusKeywords {
if keyword == expectedElement {
found = true
break
}
}
if !found {
t.Errorf("The given element is not present in the failedStatusKeywords list: %s", expectedElement)
}
if reflect.DeepEqual(expected, failedStatusKeywords) {
t.Errorf("The failed status keywords are not the expected! Expected: %v, got: %v", expected, failedStatusKeywords)
}
}
4 changes: 2 additions & 2 deletions resources/datahub/resource_aws_datahub.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ func (r *awsDatahubResource) Create(ctx context.Context, req resource.CreateRequ
var data awsDatahubResourceModel
tflog.Info(ctx, fmt.Sprintf("Creating AWS Datahub with name: %s", data.Name.ValueString()))
diags := req.Plan.Get(ctx, &data)
tflog.Info(ctx, fmt.Sprintf("Datahub resource model: %+v", data))
tflog.Debug(ctx, fmt.Sprintf("Datahub resource model: %+v", data))
resp.Diagnostics.Append(diags...)
tflog.Info(ctx, fmt.Sprintf("Diags: %+v", resp.Diagnostics))
tflog.Debug(ctx, fmt.Sprintf("Diags: %+v", resp.Diagnostics))
if resp.Diagnostics.HasError() {
tflog.Warn(ctx, "Datahub resource model has error, stopping the creation process.")
return
Expand Down
47 changes: 47 additions & 0 deletions resources/opdb/common_scheme.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// Copyright 2024 Cloudera. All Rights Reserved.
//
// This file is licensed under the Apache License Version 2.0 (the "License").
// You may not use this file except in compliance with the License.
// You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0.
//
// This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS
// OF ANY KIND, either express or implied. Refer to the License for the specific
// permissions and limitations governing your use of the file.

package opdb

import (
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/int64default"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
)

var generalAttributes = map[string]schema.Attribute{
"polling_options": schema.SingleNestedAttribute{
MarkdownDescription: "Polling related configuration options that could specify various values that will be used during CDP resource creation.",
Optional: true,
Attributes: map[string]schema.Attribute{
"polling_timeout": schema.Int64Attribute{
MarkdownDescription: "Timeout value in minutes that specifies for how long should the polling go for resource creation/deletion.",
Default: int64default.StaticInt64(60),
Computed: true,
Optional: true,
},
},
},
"crn": schema.StringAttribute{
MarkdownDescription: "The CRN of the cluster.",
Computed: true,
PlanModifiers: []planmodifier.String{
stringplanmodifier.UseStateForUnknown(),
},
},
"status": schema.StringAttribute{
MarkdownDescription: "The last known state of the cluster",
Computed: true,
PlanModifiers: []planmodifier.String{
stringplanmodifier.UseStateForUnknown(),
},
},
}
Loading

0 comments on commit c2f4861

Please sign in to comment.