Skip to content

Commit

Permalink
Refactor code
Browse files Browse the repository at this point in the history
  • Loading branch information
ducvm29 committed Oct 7, 2024
1 parent 4623478 commit cd3d25c
Show file tree
Hide file tree
Showing 6 changed files with 187 additions and 142 deletions.
10 changes: 7 additions & 3 deletions fptcloud/database/resource_database.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ var (
}
)

const (
errorCallingApi = "Error calling API"
)

type resourceDatabase struct {
client *common.Client
}
Expand Down Expand Up @@ -104,7 +108,7 @@ func (r *resourceDatabase) Create(ctx context.Context, request resource.CreateRe
tflog.Info(ctx, "Response: "+string(a))

if err != nil {
response.Diagnostics.Append(diag2.NewErrorDiagnostic("Error calling API", err.Error()))
response.Diagnostics.Append(diag2.NewErrorDiagnostic(errorCallingApi, err.Error()))
return
}

Expand Down Expand Up @@ -147,7 +151,7 @@ func (r *resourceDatabase) Read(ctx context.Context, request resource.ReadReques

err := r.internalRead(ctx, state.Id.ValueString(), &state)
if err != nil {
response.Diagnostics.Append(diag2.NewErrorDiagnostic("Error calling API", err.Error()))
response.Diagnostics.Append(diag2.NewErrorDiagnostic(errorCallingApi, err.Error()))
return
}

Expand All @@ -174,7 +178,7 @@ func (r *resourceDatabase) Delete(ctx context.Context, request resource.DeleteRe
path := common.ApiPath.DatabaseDelete(state.Id.ValueString())
_, err := r.client.SendDeleteRequest(path)
if err != nil {
response.Diagnostics.Append(diag2.NewErrorDiagnostic("Error calling API", err.Error()))
response.Diagnostics.Append(diag2.NewErrorDiagnostic(errorCallingApi, err.Error()))
return
}
}
Expand Down
6 changes: 3 additions & 3 deletions fptcloud/database/resource_database_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ func (r *resourceDatabaseStatus) getDatabaseCurrentStatus(ctx context.Context, d
}
cluster = d.Data.Cluster

tflog.Info(ctx, "Waiting for database to be provisioned. Time waited: "+strconv.Itoa(int(time.Since(timeStart).Seconds()))+" seconds.")
tflog.Info(ctx, "Waiting for database to be provisioned. Time waited: "+time.Since(timeStart).String())
time.Sleep(30 * time.Second)
}

Expand Down Expand Up @@ -269,7 +269,7 @@ func (r *resourceDatabaseStatus) stopDatabase(ctx context.Context, databaseId st
return nil
}

tflog.Info(ctx, "Waiting for nodes to be stopped. Time waited: "+strconv.Itoa(int(time.Since(timeStart).Seconds()))+" seconds.")
tflog.Info(ctx, "Waiting for nodes to be stopped. Time waited: "+time.Since(timeStart).String())
time.Sleep(60 * time.Second)
}

Expand Down Expand Up @@ -297,7 +297,7 @@ func (r *resourceDatabaseStatus) startDatabase(ctx context.Context, databaseId s
if status == "running" {
return nil
}
tflog.Info(ctx, "Waiting for nodes to be provisioned. Time waited: "+strconv.Itoa(int(time.Since(timeStart).Seconds()))+" seconds.")
tflog.Info(ctx, "Waiting for nodes to be provisioned. Time waited: "+time.Since(timeStart).String())
time.Sleep(60 * time.Second)
}

Expand Down
8 changes: 6 additions & 2 deletions fptcloud/dfke/datasource_dfke.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ var (
_ datasource.DataSourceWithConfigure = &datasourceDedicatedKubernetesEngine{}
)

const (
noSuchClusterId = "no cluster with such ID found"
)

type datasourceDedicatedKubernetesEngine struct {
client *commons.Client
dfkeClient *dfkeApiClient
Expand Down Expand Up @@ -282,11 +286,11 @@ func (d *datasourceDedicatedKubernetesEngine) findClusterUUID(_ context.Context,
}

if list.Total == 0 {
return "", errors.New("no cluster with such ID found")
return "", errors.New(noSuchClusterId)
}

if len(list.Data) == 0 {
return "", errors.New("no cluster with such ID found")
return "", errors.New(noSuchClusterId)
}

total = list.Total
Expand Down
Loading

0 comments on commit cd3d25c

Please sign in to comment.