Skip to content

Commit 7bf2536

Browse files
authored
Fix a few bugs (#134)
1 parent 70601e0 commit 7bf2536

File tree

3 files changed

+42
-15
lines changed

3 files changed

+42
-15
lines changed

managed/resource_allow_list.go

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -332,13 +332,24 @@ func findNetworkAllowList(nals []openapiclient.NetworkAllowListData, name string
332332
}
333333

334334
func getNetworkAllowListIdByName(ctx context.Context, accountId string, projectId string, networkAllowListName string, apiClient openapiclient.APIClient) (string, error) {
335-
nalResp, resp, err := apiClient.NetworkApi.ListNetworkAllowLists(ctx, accountId, projectId).Execute()
336-
if err != nil {
337-
errMsg := getErrorMessage(resp, err)
338-
return "", fmt.Errorf("Unable to read the Network allow list %s: %s", networkAllowListName, errMsg)
339-
}
340-
if nalData, ok := findNetworkAllowList(nalResp.Data, networkAllowListName); ok {
341-
return nalData.Info.GetId(), nil
335+
var continuationToken string
336+
for {
337+
request := apiClient.NetworkApi.ListNetworkAllowLists(ctx, accountId, projectId)
338+
if continuationToken != "" {
339+
request = request.ContinuationToken(continuationToken)
340+
}
341+
nalResp, resp, err := request.Execute()
342+
if err != nil {
343+
errMsg := getErrorMessage(resp, err)
344+
return "", fmt.Errorf("Unable to read the Network allow list %s: %s", networkAllowListName, errMsg)
345+
}
346+
if nalData, ok := findNetworkAllowList(nalResp.Data, networkAllowListName); ok {
347+
return nalData.Info.GetId(), nil
348+
}
349+
continuationToken = nalResp.Metadata.GetContinuationToken()
350+
if continuationToken == "" {
351+
break
352+
}
342353
}
343354

344355
return "", fmt.Errorf("NetworkAllowList %s not found", networkAllowListName)

managed/resource_associate_me_cluster.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,6 @@ func getTaskState(accountId string, projectId string, entityId string, entityTyp
294294

295295
if v, ok := taskList.GetDataOk(); ok && v != nil {
296296
c := taskList.GetData()
297-
298297
if len(c) == 0 {
299298
tflog.Info(ctx, "No task found for this operation")
300299
return "TASK_NOT_FOUND", true, ""

managed/resource_cluster.go

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -610,6 +610,7 @@ func createClusterSpec(ctx context.Context, apiClient *openapiclient.APIClient,
610610
clusterRegionInfo := []openapiclient.ClusterRegionInfo{}
611611
totalNodes := 0
612612
clusterType := plan.ClusterType.Value
613+
isDefaultSet := false
613614
for _, regionInfo := range plan.ClusterRegionInfo {
614615
regionNodes := regionInfo.NumNodes.Value
615616
totalNodes += int(regionNodes)
@@ -694,7 +695,11 @@ func createClusterSpec(ctx context.Context, apiClient *openapiclient.APIClient,
694695
info.SetIsAffinitized(regionInfo.IsPreferred.Value)
695696
}
696697
if !regionInfo.IsDefault.IsUnknown() && !regionInfo.IsDefault.IsNull() {
698+
if isDefaultSet {
699+
return nil, false, "Cluster must have exactly one default region."
700+
}
697701
info.SetIsDefault(regionInfo.IsDefault.Value)
702+
isDefaultSet = true
698703
}
699704
clusterRegionInfo = append(clusterRegionInfo, info)
700705
}
@@ -2013,7 +2018,6 @@ func (r resourceCluster) Update(ctx context.Context, req tfsdk.UpdateResourceReq
20132018
return
20142019
}
20152020
clusterSpec.ClusterInfo.SetVersion(int32(clusterVersion))
2016-
20172021
_, response, err := apiClient.ClusterApi.EditCluster(context.Background(), accountId, projectId, clusterId).ClusterSpec(*clusterSpec).Execute()
20182022
if err != nil {
20192023
errMsg := getErrorMessage(response, err)
@@ -2041,6 +2045,7 @@ func (r resourceCluster) Update(ctx context.Context, req tfsdk.UpdateResourceReq
20412045
// Something similar will happen if changing the backup schedule or the CMK spec.
20422046
retries := 0
20432047
readClusterRetries := 0
2048+
checkNewTaskSpawned := true
20442049
retryPolicy := retry.NewConstant(10 * time.Second)
20452050
retryPolicy = retry.WithMaxDuration(3600*time.Second, retryPolicy)
20462051
err = retry.Do(ctx, retryPolicy, func(ctx context.Context) error {
@@ -2049,12 +2054,6 @@ func (r resourceCluster) Update(ctx context.Context, req tfsdk.UpdateResourceReq
20492054
tflog.Info(ctx, "Cluster edit operation in progress, state: "+asState)
20502055

20512056
if readInfoOK {
2052-
if asState == string(openapiclient.TASKACTIONSTATEENUM_SUCCEEDED) {
2053-
return nil
2054-
}
2055-
if asState == string(openapiclient.TASKACTIONSTATEENUM_FAILED) {
2056-
return ErrFailedTask
2057-
}
20582057
if asState == "TASK_NOT_FOUND" {
20592058
// We try for a minute waiting for the tasks to be spawned. If edit cluster responded with a success
20602059
// without creating a task for about a minute, we can safely assume that a task is not required to be spawned.
@@ -2068,6 +2067,24 @@ func (r resourceCluster) Update(ctx context.Context, req tfsdk.UpdateResourceReq
20682067
return nil
20692068
}
20702069
}
2070+
// There are cases this code flow checks for the state of previously spawned tasks instead of checking for new tasks.
2071+
// Hence, we check whether a new task is spawned.
2072+
if checkNewTaskSpawned {
2073+
if asState == string(openapiclient.TASKACTIONSTATEENUM_IN_PROGRESS) {
2074+
checkNewTaskSpawned = false
2075+
return retry.RetryableError(errors.New("Cluster edit operation in progress"))
2076+
} else {
2077+
tflog.Info(ctx, "Cluster edit task not found, the change would not have required a task creation")
2078+
return nil
2079+
}
2080+
}
2081+
if asState == string(openapiclient.TASKACTIONSTATEENUM_SUCCEEDED) {
2082+
return nil
2083+
}
2084+
if asState == string(openapiclient.TASKACTIONSTATEENUM_FAILED) {
2085+
return ErrFailedTask
2086+
}
2087+
20712088
} else {
20722089
return handleReadFailureWithRetries(ctx, &readClusterRetries, 2, message)
20732090
}

0 commit comments

Comments
 (0)