Skip to content

Commit 1102c82

Browse files
lornakellyLorna-Kelly
andauthored
feat(Cloud Databases): In place upgrade (IBM-Cloud#6275)
* In place upgrade * Check if task is running * Update isMatchingTask function * Make UTC * Check task is running in update function * Add location * Update expiration to strfmt datetime type and enable new version endpoint * PR fixes * Updates, remove existing task logic * Add acceptance test * Update capability call * Bump go-sdk version --------- Co-authored-by: Lorna-Kelly <[email protected]>
1 parent e999947 commit 1102c82

File tree

11 files changed

+1145
-15
lines changed

11 files changed

+1145
-15
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ require (
88
github.com/IBM-Cloud/power-go-client v1.11.0
99
github.com/IBM/appconfiguration-go-admin-sdk v0.4.4
1010
github.com/IBM/appid-management-go-sdk v0.0.0-20210908164609-dd0e0eaf732f
11-
github.com/IBM/cloud-databases-go-sdk v0.7.1
11+
github.com/IBM/cloud-databases-go-sdk v0.8.0
1212
github.com/IBM/cloud-db2-go-sdk v0.2.3
1313
github.com/IBM/cloudant-go-sdk v0.8.0
1414
github.com/IBM/code-engine-go-sdk v0.0.0-20241217191651-e1821f8c58c3

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,8 @@ github.com/IBM/appconfiguration-go-admin-sdk v0.4.4 h1:VQ+mVavoo3/xrBaVSyxE+B6Ue
102102
github.com/IBM/appconfiguration-go-admin-sdk v0.4.4/go.mod h1:E9cXu9w0EClhZq+E9wBw8QzNLKZd2bNdMrTXwVSWJ1s=
103103
github.com/IBM/appid-management-go-sdk v0.0.0-20210908164609-dd0e0eaf732f h1:4c1kqY4GqmkQ+tO03rneDb74Tv7BhTj8jDiDB1p8mdM=
104104
github.com/IBM/appid-management-go-sdk v0.0.0-20210908164609-dd0e0eaf732f/go.mod h1:d22kTYY7RYBWcQlZpqrSdshpB/lJ16viWS5Sbjtlc8s=
105-
github.com/IBM/cloud-databases-go-sdk v0.7.1 h1:5kK4/3NUsGxZzmuUe+1ftajpOQbeDVh5VeemrPgROP4=
106-
github.com/IBM/cloud-databases-go-sdk v0.7.1/go.mod h1:JYucI1PdwqbAd8XGdDAchxzxRP7bxOh1zUnseovHKsc=
105+
github.com/IBM/cloud-databases-go-sdk v0.8.0 h1:uMFqhnc/roVTzfCaUsJ23eaHKjChhGpM1F7Mpxik0bo=
106+
github.com/IBM/cloud-databases-go-sdk v0.8.0/go.mod h1:JYucI1PdwqbAd8XGdDAchxzxRP7bxOh1zUnseovHKsc=
107107
github.com/IBM/cloud-db2-go-sdk v0.2.3 h1:R+8uFXSZFOsBDC7nKqW9DgyGgT9Pop589ziUIaw2Tek=
108108
github.com/IBM/cloud-db2-go-sdk v0.2.3/go.mod h1:jEJMNXX2KOE+Hhk3M8CBq0qi9CZ0eRK36xmhuBPSiNA=
109109
github.com/IBM/cloudant-go-sdk v0.8.0 h1:XzaqZFy5fm1Q9+iK52X5zRW39SHaahT9pf5SRgVTsTY=

ibm/service/database/deployment.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// Copyright IBM Corp. 2017, 2022 All Rights Reserved.
2+
// Licensed under the Mozilla Public License v2.0
3+
4+
package database
5+
6+
import (
7+
"fmt"
8+
9+
"github.com/IBM-Cloud/terraform-provider-ibm/ibm/conns"
10+
"github.com/IBM/cloud-databases-go-sdk/clouddatabasesv5"
11+
"github.com/IBM/go-sdk-core/v5/core"
12+
)
13+
14+
/* Deployment api calls
15+
TODO Move other deployment api endpoints in here
16+
*/
17+
18+
func getDeploymentCapability(capabilityId string, deploymentId string, platform string, location string, meta interface{}) (*clouddatabasesv5.Capability, error) {
19+
cloudDatabasesClient, err := meta.(conns.ClientSession).CloudDatabasesV5()
20+
if err != nil {
21+
return nil, err
22+
}
23+
24+
getDeploymentCapabilityOptions := &clouddatabasesv5.GetDeploymentCapabilityOptions{
25+
ID: core.StringPtr(deploymentId),
26+
CapabilityID: core.StringPtr(capabilityId),
27+
TargetPlatform: core.StringPtr(fmt.Sprintf("target_platform=%s", platform)),
28+
TargetLocation: core.StringPtr(fmt.Sprintf("target_location=%s", location)),
29+
}
30+
getDeploymentCapabilityResponse, response, err := cloudDatabasesClient.GetDeploymentCapability(getDeploymentCapabilityOptions)
31+
32+
if getDeploymentCapabilityResponse == nil || getDeploymentCapabilityResponse.Capability == nil {
33+
return nil, fmt.Errorf("capability '%s' field is nil in response %s", capabilityId, response)
34+
}
35+
36+
return getDeploymentCapabilityResponse.Capability, nil
37+
}

ibm/service/database/helpers.go

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
// Copyright IBM Corp. 2017, 2022 All Rights Reserved.
2+
// Licensed under the Mozilla Public License v2.0
3+
4+
package database
5+
6+
import (
7+
"fmt"
8+
"log"
9+
"time"
10+
11+
"github.com/IBM/cloud-databases-go-sdk/clouddatabasesv5"
12+
"github.com/IBM/go-sdk-core/v5/core"
13+
"github.com/go-openapi/strfmt"
14+
)
15+
16+
/* TODO Move other helper functions here */
17+
type TimeoutHelper struct {
18+
Now time.Time
19+
}
20+
21+
// Allows mocking
22+
type DeploymentTaskFetcher interface {
23+
ListDeploymentTasks(opts *clouddatabasesv5.ListDeploymentTasksOptions) (*clouddatabasesv5.Tasks, *core.DetailedResponse, error)
24+
}
25+
type TaskManager struct {
26+
Client DeploymentTaskFetcher
27+
InstanceID string
28+
}
29+
30+
func (t *TimeoutHelper) isMoreThan24Hours(duration time.Duration) bool {
31+
return duration > 24*time.Hour
32+
}
33+
34+
func (t *TimeoutHelper) futureTimeToISO(duration time.Duration) strfmt.DateTime {
35+
utcTime := t.Now.Add(duration).UTC()
36+
return strfmt.DateTime(utcTime)
37+
}
38+
39+
func (t *TimeoutHelper) calculateExpirationDatetime(timeoutDuration time.Duration) strfmt.DateTime {
40+
if t.isMoreThan24Hours(timeoutDuration) {
41+
return t.futureTimeToISO(24 * time.Hour)
42+
}
43+
44+
return t.futureTimeToISO(timeoutDuration)
45+
}
46+
47+
func (tm *TaskManager) matchingTaskInProgress(taskType string) (bool, *clouddatabasesv5.Task, error) {
48+
opts := &clouddatabasesv5.ListDeploymentTasksOptions{
49+
ID: core.StringPtr(tm.InstanceID),
50+
}
51+
52+
resp, _, err := tm.Client.ListDeploymentTasks(opts)
53+
if err != nil {
54+
return false, nil, fmt.Errorf("failed to list tasks for instance: %w", err)
55+
}
56+
57+
for _, task := range resp.Tasks {
58+
if task.Status == nil || task.ResourceType == nil {
59+
continue
60+
}
61+
id := *task.ID
62+
createdAt := *task.CreatedAt
63+
status := *task.Status
64+
progress := *task.ProgressPercent
65+
description := *task.Description
66+
resourceType := *task.ResourceType
67+
68+
if (status == databaseTaskRunningStatus || status == databaseTaskQueuedStatus) && resourceType == taskType {
69+
log.Printf("[INFO] Found matching task in progress:\n"+
70+
" Type: %s\n"+
71+
" Created at: %s\n"+
72+
" Status: %s\n"+
73+
" Current progress percent: %d\n"+
74+
" Description: %s\n"+
75+
" ID: %s\n",
76+
resourceType, createdAt, status, progress, description, id)
77+
return true, &task, nil
78+
}
79+
}
80+
81+
return false, nil, nil
82+
}

0 commit comments

Comments
 (0)