Skip to content

Commit

Permalink
Merge pull request #34220 from kavan-aws/f-finspace_kx_cluster-update…
Browse files Browse the repository at this point in the history
…-code-config

Support the update of KxCluster databases and code configuration
  • Loading branch information
jar-b authored Nov 2, 2023
2 parents e580c03 + 4f1f54e commit 6d7cde6
Show file tree
Hide file tree
Showing 6 changed files with 222 additions and 25 deletions.
3 changes: 3 additions & 0 deletions .changelog/34220.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
resource/aws_finspace_kx_cluster: In-place updates are now supported for the `code`, `database`, and `initialization_script` arguments. The update timeout has been increased to 30 minutes.
```
97 changes: 81 additions & 16 deletions internal/service/finspace/kx_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func ResourceKxCluster() *schema.Resource {

Timeouts: &schema.ResourceTimeout{
Create: schema.DefaultTimeout(45 * time.Minute),
Update: schema.DefaultTimeout(2 * time.Minute), // Tags only
Update: schema.DefaultTimeout(30 * time.Minute),
Delete: schema.DefaultTimeout(60 * time.Minute),
},

Expand Down Expand Up @@ -155,26 +155,22 @@ func ResourceKxCluster() *schema.Resource {
"code": {
Type: schema.TypeList,
Optional: true,
ForceNew: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"s3_bucket": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validation.StringLenBetween(3, 255),
},
"s3_key": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validation.StringLenBetween(3, 1024),
},
"s3_object_version": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
ValidateFunc: validation.StringLenBetween(3, 63),
},
},
Expand All @@ -184,7 +180,6 @@ func ResourceKxCluster() *schema.Resource {
Type: schema.TypeMap,
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
ForceNew: true,
ValidateDiagFunc: validation.AllDiag(
validation.MapKeyLenBetween(1, 50),
validation.MapValueLenBetween(1, 50),
Expand All @@ -197,13 +192,11 @@ func ResourceKxCluster() *schema.Resource {
"database": {
Type: schema.TypeList,
Optional: true,
ForceNew: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"cache_configurations": {
Type: schema.TypeList,
Optional: true,
ForceNew: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"cache_type": {
Expand All @@ -217,15 +210,13 @@ func ResourceKxCluster() *schema.Resource {
Type: schema.TypeString,
},
Optional: true,
ForceNew: true,
},
},
},
},
"changeset_id": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
ValidateFunc: validation.StringLenBetween(1, 26),
},
"database_name": {
Expand Down Expand Up @@ -258,7 +249,6 @@ func ResourceKxCluster() *schema.Resource {
"initialization_script": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
ValidateFunc: validation.StringLenBetween(1, 255),
},
"last_modified_timestamp": {
Expand Down Expand Up @@ -509,10 +499,8 @@ func resourceKxClusterRead(ctx context.Context, d *schema.ResourceData, meta int
return append(diags, create.DiagError(names.FinSpace, create.ErrActionSetting, ResNameKxCluster, d.Id(), err)...)
}

if d.IsNewResource() {
if err := d.Set("database", flattenDatabases(out.Databases)); err != nil {
return append(diags, create.DiagError(names.FinSpace, create.ErrActionSetting, ResNameKxCluster, d.Id(), err)...)
}
if err := d.Set("database", flattenDatabases(out.Databases)); err != nil {
return append(diags, create.DiagError(names.FinSpace, create.ErrActionSetting, ResNameKxCluster, d.Id(), err)...)
}

if err := d.Set("command_line_arguments", flattenCommandLineArguments(out.CommandLineArguments)); err != nil {
Expand All @@ -537,7 +525,66 @@ func resourceKxClusterRead(ctx context.Context, d *schema.ResourceData, meta int

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

updateDb := false
updateCode := false

CodeConfigIn := &finspace.UpdateKxClusterCodeConfigurationInput{
EnvironmentId: aws.String(d.Get("environment_id").(string)),
ClusterName: aws.String(d.Get("name").(string)),
}

DatabaseConfigIn := &finspace.UpdateKxClusterDatabasesInput{
EnvironmentId: aws.String(d.Get("environment_id").(string)),
ClusterName: aws.String(d.Get("name").(string)),
}

if v, ok := d.GetOk("database"); ok && len(v.([]interface{})) > 0 && d.HasChanges("database") {
DatabaseConfigIn.Databases = expandDatabases(d.Get("database").([]interface{}))
updateDb = true
}

if v, ok := d.GetOk("code"); ok && len(v.([]interface{})) > 0 && v.([]interface{})[0] != nil && d.HasChanges("code") {
CodeConfigIn.Code = expandCode(v.([]interface{}))
updateCode = true
}

if v, ok := d.GetOk("initialization_script"); ok && d.HasChanges("initialization_script") {
CodeConfigIn.Code = expandCode(d.Get("code").([]interface{}))
CodeConfigIn.InitializationScript = aws.String(v.(string))
updateCode = true
}

if v, ok := d.GetOk("command_line_arguments"); ok && len(v.(map[string]interface{})) > 0 && d.HasChanges("command_line_arguments") {
CodeConfigIn.Code = expandCode(d.Get("code").([]interface{}))
CodeConfigIn.CommandLineArguments = expandCommandLineArguments(v.(map[string]interface{}))
updateCode = true
}

if updateDb {
log.Printf("[DEBUG] Updating FinSpace KxClusterDatabases (%s): %#v", d.Id(), DatabaseConfigIn)
if _, err := conn.UpdateKxClusterDatabases(ctx, DatabaseConfigIn); err != nil {
return append(diags, create.DiagError(names.FinSpace, create.ErrActionUpdating, ResNameKxCluster, d.Id(), err)...)
}
if _, err := waitKxClusterUpdated(ctx, conn, d.Id(), d.Timeout(schema.TimeoutUpdate)); err != nil {
return append(diags, create.DiagError(names.FinSpace, create.ErrActionUpdating, ResNameKxCluster, d.Id(), err)...)
}
}

if updateCode {
log.Printf("[DEBUG] Updating FinSpace KxClusterCodeConfiguration (%s): %#v", d.Id(), CodeConfigIn)
if _, err := conn.UpdateKxClusterCodeConfiguration(ctx, CodeConfigIn); err != nil {
return append(diags, create.DiagError(names.FinSpace, create.ErrActionUpdating, ResNameKxCluster, d.Id(), err)...)
}
if _, err := waitKxClusterUpdated(ctx, conn, d.Id(), d.Timeout(schema.TimeoutUpdate)); err != nil {
return append(diags, create.DiagError(names.FinSpace, create.ErrActionUpdating, ResNameKxCluster, d.Id(), err)...)
}
}

if !updateCode && !updateDb {
return diags
}
return append(diags, resourceKxClusterRead(ctx, d, meta)...)
}

Expand Down Expand Up @@ -585,6 +632,24 @@ func waitKxClusterCreated(ctx context.Context, conn *finspace.Client, id string,
return nil, err
}

func waitKxClusterUpdated(ctx context.Context, conn *finspace.Client, id string, timeout time.Duration) (*finspace.GetKxClusterOutput, error) { //nolint:unparam
stateConf := &retry.StateChangeConf{
Pending: enum.Slice(types.KxClusterStatusPending, types.KxClusterStatusUpdating),
Target: enum.Slice(types.KxClusterStatusRunning),
Refresh: statusKxCluster(ctx, conn, id),
Timeout: timeout,
NotFoundChecks: 20,
ContinuousTargetOccurence: 2,
}

outputRaw, err := stateConf.WaitForStateContext(ctx)
if out, ok := outputRaw.(*finspace.GetKxClusterOutput); ok {
return out, err
}

return nil, err
}

func waitKxClusterDeleted(ctx context.Context, conn *finspace.Client, id string, timeout time.Duration) (*finspace.GetKxClusterOutput, error) {
stateConf := &retry.StateChangeConf{
Pending: enum.Slice(types.KxClusterStatusDeleting),
Expand Down
Loading

0 comments on commit 6d7cde6

Please sign in to comment.