Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[New Resource]: aws_finspace_kx_database #31803

Merged
merged 28 commits into from
Jun 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
3d6bd58
Adds finspace_kx_database resource
kavan-aws Jun 6, 2023
581e40b
Merge branch 'hashicorp:main' into f-finspace_kx_database
kavan-aws Jun 6, 2023
ab6d36e
Ran make servicepackages
kavan-aws Jun 6, 2023
6c11107
Merge branch 'hashicorp:main' into f-finspace_kx_database
kavan-aws Jun 7, 2023
b966536
Formatting fixes for database
kavan-aws Jun 7, 2023
8ec58ef
Merge branch 'f-finspace_kx_database' of github.com:kavan-aws/terrafo…
kavan-aws Jun 7, 2023
264d86c
Merge branch 'hashicorp:main' into f-finspace_kx_database
kavan-aws Jun 7, 2023
17b2656
Formatting fixes to database tests
kavan-aws Jun 7, 2023
502abc5
Merge branch 'hashicorp:main' into f-finspace_kx_database
kavan-aws Jun 7, 2023
ace1bd7
Minor corrections to test formatting
kavan-aws Jun 7, 2023
1ee98f0
Merge branch 'hashicorp:main' into f-finspace_kx_database
kavan-aws Jun 7, 2023
941efa7
Update kx_database_test.go
kavan-aws Jun 7, 2023
408aca3
Update kx_database_test.go
kavan-aws Jun 7, 2023
d96c734
r/aws_finspace_kx_database: alphabetize attributes
jar-b Jun 8, 2023
0efbe5b
r/aws_finspace_kx_database: crud handlers return diags
jar-b Jun 8, 2023
860716d
r/aws_finspace_kx_database: simplify update
jar-b Jun 8, 2023
43b406d
r/aws_finspace_kx_database: find by id to support import
jar-b Jun 8, 2023
ee04317
r/aws_finspace_kx_database(test): terrafmt
jar-b Jun 8, 2023
ab82ff9
Merge branch 'hashicorp:main' into f-finspace_kx_database
kavan-aws Jun 8, 2023
b33a2c4
Update kx_database_test.go
kavan-aws Jun 8, 2023
03e1a67
Update kx_database_test.go
kavan-aws Jun 8, 2023
10e7dce
r/aws_finspace_kx_database(docs): add import section, document id att…
jar-b Jun 8, 2023
3a31e13
r/aws_finspace_kx_database(docs): use standard tags/tags_all descript…
jar-b Jun 8, 2023
ddae2fe
r/aws_finspace_kx_database: import support
jar-b Jun 8, 2023
56b7260
r/aws_finspace_kx_database(test): add base config
jar-b Jun 8, 2023
3568a4d
chore: changelog
jar-b Jun 8, 2023
6073c27
r/aws_finspace_kx_database(test): add ctx to check funcs
jar-b Jun 8, 2023
bfc7d03
Merge branch 'main' into f-finspace_kx_database
jar-b Jun 8, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .changelog/31803.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:new-resource
aws_finspace_kx_database
```
224 changes: 224 additions & 0 deletions internal/service/finspace/kx_database.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,224 @@
package finspace

import (
"context"
"errors"
"log"
"time"

"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/finspace"
"github.com/aws/aws-sdk-go-v2/service/finspace/types"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/id"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
"github.com/hashicorp/terraform-provider-aws/internal/conns"
"github.com/hashicorp/terraform-provider-aws/internal/create"
"github.com/hashicorp/terraform-provider-aws/internal/flex"
tftags "github.com/hashicorp/terraform-provider-aws/internal/tags"
"github.com/hashicorp/terraform-provider-aws/internal/tfresource"
"github.com/hashicorp/terraform-provider-aws/internal/verify"
"github.com/hashicorp/terraform-provider-aws/names"
)

// @SDKResource("aws_finspace_kx_database", name="Kx Database")
// @Tags(identifierAttribute="arn")
func ResourceKxDatabase() *schema.Resource {
return &schema.Resource{
CreateWithoutTimeout: resourceKxDatabaseCreate,
ReadWithoutTimeout: resourceKxDatabaseRead,
UpdateWithoutTimeout: resourceKxDatabaseUpdate,
DeleteWithoutTimeout: resourceKxDatabaseDelete,

Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
},

Timeouts: &schema.ResourceTimeout{
Create: schema.DefaultTimeout(30 * time.Minute),
Update: schema.DefaultTimeout(30 * time.Minute),
Delete: schema.DefaultTimeout(30 * time.Minute),
},

Schema: map[string]*schema.Schema{
"arn": {
Type: schema.TypeString,
Computed: true,
},
"created_timestamp": {
Type: schema.TypeString,
Computed: true,
},
"description": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.StringLenBetween(1, 1000),
},
"environment_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validation.StringLenBetween(1, 32),
},
"last_modified_timestamp": {
Type: schema.TypeString,
Computed: true,
},
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validation.StringLenBetween(3, 63),
},
names.AttrTags: tftags.TagsSchema(),
names.AttrTagsAll: tftags.TagsSchemaComputed(),
},

CustomizeDiff: verify.SetTagsDiff,
}
}

const (
ResNameKxDatabase = "Kx Database"

kxDatabaseIDPartCount = 2
)

func resourceKxDatabaseCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
var diags diag.Diagnostics
conn := meta.(*conns.AWSClient).FinSpaceClient()

in := &finspace.CreateKxDatabaseInput{
DatabaseName: aws.String(d.Get("name").(string)),
EnvironmentId: aws.String(d.Get("environment_id").(string)),
ClientToken: aws.String(id.UniqueId()),
Tags: GetTagsIn(ctx),
}

if v, ok := d.GetOk("description"); ok {
in.Description = aws.String(v.(string))
}

out, err := conn.CreateKxDatabase(ctx, in)
if err != nil {
return append(diags, create.DiagError(names.FinSpace, create.ErrActionCreating, ResNameKxDatabase, d.Get("name").(string), err)...)
}

if out == nil || out.DatabaseArn == nil {
return append(diags, create.DiagError(names.FinSpace, create.ErrActionCreating, ResNameKxDatabase, d.Get("name").(string), errors.New("empty output"))...)
}

idParts := []string{
aws.ToString(out.EnvironmentId),
aws.ToString(out.DatabaseName),
}
id, err := flex.FlattenResourceId(idParts, kxDatabaseIDPartCount, false)
if err != nil {
return append(diags, create.DiagError(names.FinSpace, create.ErrActionFlatteningResourceId, ResNameKxDatabase, d.Get("name").(string), err)...)
}

d.SetId(id)

return append(diags, resourceKxDatabaseRead(ctx, d, meta)...)
}

func resourceKxDatabaseRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
var diags diag.Diagnostics
conn := meta.(*conns.AWSClient).FinSpaceClient()

out, err := findKxDatabaseByID(ctx, conn, d.Id())
if !d.IsNewResource() && tfresource.NotFound(err) {
log.Printf("[WARN] FinSpace KxDatabase (%s) not found, removing from state", d.Id())
d.SetId("")
return diags
}

if err != nil {
return append(diags, create.DiagError(names.FinSpace, create.ErrActionReading, ResNameKxDatabase, d.Id(), err)...)
}

d.Set("arn", out.DatabaseArn)
d.Set("name", out.DatabaseName)
d.Set("environment_id", out.EnvironmentId)
d.Set("description", out.Description)
d.Set("created_timestamp", out.CreatedTimestamp.String())
d.Set("last_modified_timestamp", out.LastModifiedTimestamp.String())

return diags
}

func resourceKxDatabaseUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
var diags diag.Diagnostics
conn := meta.(*conns.AWSClient).FinSpaceClient()

if d.HasChanges("description") {
in := &finspace.UpdateKxDatabaseInput{
EnvironmentId: aws.String(d.Get("environment_id").(string)),
DatabaseName: aws.String(d.Get("name").(string)),
Description: aws.String(d.Get("description").(string)),
}

_, err := conn.UpdateKxDatabase(ctx, in)
if err != nil {
return append(diags, create.DiagError(names.FinSpace, create.ErrActionUpdating, ResNameKxDatabase, d.Id(), err)...)
}
}

return append(diags, resourceKxDatabaseRead(ctx, d, meta)...)
}

func resourceKxDatabaseDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
var diags diag.Diagnostics
conn := meta.(*conns.AWSClient).FinSpaceClient()

log.Printf("[INFO] Deleting FinSpace KxDatabase %s", d.Id())

_, err := conn.DeleteKxDatabase(ctx, &finspace.DeleteKxDatabaseInput{
EnvironmentId: aws.String(d.Get("environment_id").(string)),
DatabaseName: aws.String(d.Get("name").(string)),
})

if err != nil {
var nfe *types.ResourceNotFoundException
if errors.As(err, &nfe) {
return diags
}

return append(diags, create.DiagError(names.FinSpace, create.ErrActionDeleting, ResNameKxDatabase, d.Id(), err)...)
}

return diags
}

func findKxDatabaseByID(ctx context.Context, conn *finspace.Client, id string) (*finspace.GetKxDatabaseOutput, error) {
parts, err := flex.ExpandResourceId(id, kxDatabaseIDPartCount, false)
if err != nil {
return nil, err
}

in := &finspace.GetKxDatabaseInput{
EnvironmentId: aws.String(parts[0]),
DatabaseName: aws.String(parts[1]),
}

out, err := conn.GetKxDatabase(ctx, in)
if err != nil {
var nfe *types.ResourceNotFoundException
if errors.As(err, &nfe) {
return nil, &retry.NotFoundError{
LastError: err,
LastRequest: in,
}
}

return nil, err
}

if out == nil || out.DatabaseArn == nil {
return nil, tfresource.NewEmptyResultError(in)
}

return out, nil
}
Loading