Skip to content

Commit

Permalink
Rollback diags change
Browse files Browse the repository at this point in the history
Signed-off-by: Peter Broadhurst <peter.broadhurst@kaleido.io>
  • Loading branch information
peterbroadhurst committed Mar 5, 2024
1 parent c977e2c commit 3b57377
Show file tree
Hide file tree
Showing 9 changed files with 41 additions and 41 deletions.
2 changes: 1 addition & 1 deletion kaleido/kaleidobase/providerdata.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ type ProviderModel struct {
PlatformPassword types.String `tfsdk:"platform_password"`
}

func ConfigureProviderData(providerData any, diagnostics *diag.Diagnostics) *ProviderData {
func ConfigureProviderData(providerData any, diagnostics diag.Diagnostics) *ProviderData {
kaleidoProviderData, ok := providerData.(*ProviderData)
if !ok {
diagnostics.AddError(
Expand Down
6 changes: 3 additions & 3 deletions kaleido/platform/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,10 @@ type commonResource struct {
}

func (r *commonResource) Configure(_ context.Context, req resource.ConfigureRequest, resp *resource.ConfigureResponse) {
r.ProviderData = kaleidobase.ConfigureProviderData(req.ProviderData, &resp.Diagnostics)
r.ProviderData = kaleidobase.ConfigureProviderData(req.ProviderData, resp.Diagnostics)
}

func (r *commonResource) apiRequest(ctx context.Context, method, path string, body, result interface{}, diagnostics *diag.Diagnostics, options ...HttpOptions) (bool, int) {
func (r *commonResource) apiRequest(ctx context.Context, method, path string, body, result interface{}, diagnostics diag.Diagnostics, options ...HttpOptions) (bool, int) {
tflog.Debug(ctx, fmt.Sprintf("--> %s%s %s", method, r.Platform.BaseURL, path))
res, err := r.Platform.R().
SetContext(ctx).
Expand Down Expand Up @@ -109,7 +109,7 @@ func (r *commonResource) apiRequest(ctx context.Context, method, path string, bo
return ok, res.StatusCode()
}

func (r *commonResource) waitForReadyStatus(ctx context.Context, path string, diagnostics *diag.Diagnostics) {
func (r *commonResource) waitForReadyStatus(ctx context.Context, path string, diagnostics diag.Diagnostics) {
type statusResponse struct {
Status string `json:"status"`
}
Expand Down
12 changes: 6 additions & 6 deletions kaleido/platform/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,12 +162,12 @@ func (r *runtimeResource) Create(ctx context.Context, req resource.CreateRequest
resp.Diagnostics.Append(req.Plan.Get(ctx, &data)...)

api := data.toAPI()
ok, _ := r.apiRequest(ctx, http.MethodPost, "/api/v1/runtimes", api, &api, &resp.Diagnostics)
ok, _ := r.apiRequest(ctx, http.MethodPost, "/api/v1/runtimes", api, &api, resp.Diagnostics)
if !ok {
return
}

r.waitForReadyStatus(ctx, fmt.Sprintf("/api/v1/runtimes/%s", api.ID), &resp.Diagnostics)
r.waitForReadyStatus(ctx, fmt.Sprintf("/api/v1/runtimes/%s", api.ID), resp.Diagnostics)

resp.Diagnostics.Append(resp.State.Set(ctx, api.toData())...)

Expand All @@ -179,12 +179,12 @@ func (r *runtimeResource) Update(ctx context.Context, req resource.UpdateRequest
resp.Diagnostics.Append(req.Plan.Get(ctx, &data)...)

api := data.toAPI()
ok, _ := r.apiRequest(ctx, http.MethodPut, fmt.Sprintf("/api/v1/runtimes/%s", api.ID), api, &api, &resp.Diagnostics)
ok, _ := r.apiRequest(ctx, http.MethodPut, fmt.Sprintf("/api/v1/runtimes/%s", api.ID), api, &api, resp.Diagnostics)
if !ok {
return
}

r.waitForReadyStatus(ctx, fmt.Sprintf("/api/v1/runtimes/%s", api.ID), &resp.Diagnostics)
r.waitForReadyStatus(ctx, fmt.Sprintf("/api/v1/runtimes/%s", api.ID), resp.Diagnostics)

resp.Diagnostics.Append(resp.State.Set(ctx, api.toData())...)

Expand All @@ -195,7 +195,7 @@ func (r *runtimeResource) Read(ctx context.Context, req resource.ReadRequest, re
resp.Diagnostics.Append(req.State.Get(ctx, &data)...)

api := data.toAPI()
ok, status := r.apiRequest(ctx, http.MethodPut, fmt.Sprintf("/api/v1/runtimes/%s", api.ID), nil, &api, &resp.Diagnostics, Allow404)
ok, status := r.apiRequest(ctx, http.MethodPut, fmt.Sprintf("/api/v1/runtimes/%s", api.ID), nil, &api, resp.Diagnostics, Allow404)
if !ok {
return
}
Expand All @@ -211,6 +211,6 @@ func (r *runtimeResource) Delete(ctx context.Context, req resource.DeleteRequest
var data RuntimeResourceModel
resp.Diagnostics.Append(req.State.Get(ctx, &data)...)

_, _ = r.apiRequest(ctx, http.MethodDelete, fmt.Sprintf("/api/v1/runtimes/%s", data.ID.ValueString()), nil, nil, &resp.Diagnostics, Allow404)
_, _ = r.apiRequest(ctx, http.MethodDelete, fmt.Sprintf("/api/v1/runtimes/%s", data.ID.ValueString()), nil, nil, resp.Diagnostics, Allow404)

}
6 changes: 3 additions & 3 deletions kaleido/platform_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,10 @@ type commonResource struct {
}

func (r *commonResource) Configure(_ context.Context, req resource.ConfigureRequest, resp *resource.ConfigureResponse) {
r.ProviderData = kaleidobase.ConfigureProviderData(req.ProviderData, &resp.Diagnostics)
r.ProviderData = kaleidobase.ConfigureProviderData(req.ProviderData, resp.Diagnostics)
}

func (r *commonResource) apiRequest(ctx context.Context, method, path string, body, result interface{}, diagnostics *diag.Diagnostics, options ...HttpOptions) (bool, int) {
func (r *commonResource) apiRequest(ctx context.Context, method, path string, body, result interface{}, diagnostics diag.Diagnostics, options ...HttpOptions) (bool, int) {
tflog.Debug(ctx, fmt.Sprintf("--> %s%s %s", method, r.Platform.BaseURL, path))
res, err := r.Platform.R().
SetContext(ctx).
Expand Down Expand Up @@ -109,7 +109,7 @@ func (r *commonResource) apiRequest(ctx context.Context, method, path string, bo
return ok, res.StatusCode()
}

func (r *commonResource) waitForReadyStatus(ctx context.Context, path string, diagnostics *diag.Diagnostics) {
func (r *commonResource) waitForReadyStatus(ctx context.Context, path string, diagnostics diag.Diagnostics) {
type statusResponse struct {
Status string `json:"status"`
}
Expand Down
12 changes: 6 additions & 6 deletions kaleido/platform_resource_runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,12 +162,12 @@ func (r *runtimeResource) Create(ctx context.Context, req resource.CreateRequest
resp.Diagnostics.Append(req.Plan.Get(ctx, &data)...)

api := data.toAPI()
ok, _ := r.apiRequest(ctx, http.MethodPost, "/api/v1/runtimes", api, &api, &resp.Diagnostics)
ok, _ := r.apiRequest(ctx, http.MethodPost, "/api/v1/runtimes", api, &api, resp.Diagnostics)
if !ok {
return
}

r.waitForReadyStatus(ctx, fmt.Sprintf("/api/v1/runtimes/%s", api.ID), &resp.Diagnostics)
r.waitForReadyStatus(ctx, fmt.Sprintf("/api/v1/runtimes/%s", api.ID), resp.Diagnostics)

resp.Diagnostics.Append(resp.State.Set(ctx, api.toData())...)

Expand All @@ -179,12 +179,12 @@ func (r *runtimeResource) Update(ctx context.Context, req resource.UpdateRequest
resp.Diagnostics.Append(req.Plan.Get(ctx, &data)...)

api := data.toAPI()
ok, _ := r.apiRequest(ctx, http.MethodPut, fmt.Sprintf("/api/v1/runtimes/%s", api.ID), api, &api, &resp.Diagnostics)
ok, _ := r.apiRequest(ctx, http.MethodPut, fmt.Sprintf("/api/v1/runtimes/%s", api.ID), api, &api, resp.Diagnostics)
if !ok {
return
}

r.waitForReadyStatus(ctx, fmt.Sprintf("/api/v1/runtimes/%s", api.ID), &resp.Diagnostics)
r.waitForReadyStatus(ctx, fmt.Sprintf("/api/v1/runtimes/%s", api.ID), resp.Diagnostics)

resp.Diagnostics.Append(resp.State.Set(ctx, api.toData())...)

Expand All @@ -195,7 +195,7 @@ func (r *runtimeResource) Read(ctx context.Context, req resource.ReadRequest, re
resp.Diagnostics.Append(req.State.Get(ctx, &data)...)

api := data.toAPI()
ok, status := r.apiRequest(ctx, http.MethodPut, fmt.Sprintf("/api/v1/runtimes/%s", api.ID), nil, &api, &resp.Diagnostics, Allow404)
ok, status := r.apiRequest(ctx, http.MethodPut, fmt.Sprintf("/api/v1/runtimes/%s", api.ID), nil, &api, resp.Diagnostics, Allow404)
if !ok {
return
}
Expand All @@ -211,6 +211,6 @@ func (r *runtimeResource) Delete(ctx context.Context, req resource.DeleteRequest
var data RuntimeResourceModel
resp.Diagnostics.Append(req.State.Get(ctx, &data)...)

_, _ = r.apiRequest(ctx, http.MethodDelete, fmt.Sprintf("/api/v1/runtimes/%s", data.ID.ValueString()), nil, nil, &resp.Diagnostics, Allow404)
_, _ = r.apiRequest(ctx, http.MethodDelete, fmt.Sprintf("/api/v1/runtimes/%s", data.ID.ValueString()), nil, nil, resp.Diagnostics, Allow404)

}
4 changes: 2 additions & 2 deletions kaleido/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ type baasBaseDatasource struct {
}

func (r *baasBaseResource) Configure(_ context.Context, req resource.ConfigureRequest, resp *resource.ConfigureResponse) {
r.ProviderData = kaleidobase.ConfigureProviderData(req.ProviderData, &resp.Diagnostics)
r.ProviderData = kaleidobase.ConfigureProviderData(req.ProviderData, resp.Diagnostics)
}

func (d *baasBaseDatasource) Configure(_ context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) {
d.ProviderData = kaleidobase.ConfigureProviderData(req.ProviderData, &resp.Diagnostics)
d.ProviderData = kaleidobase.ConfigureProviderData(req.ProviderData, resp.Diagnostics)
}

func newTestProviderData() *kaleidobase.ProviderData {
Expand Down
14 changes: 7 additions & 7 deletions kaleido/resource_configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,13 +92,13 @@ func (r *resourceConfiguration) Schema(_ context.Context, _ resource.SchemaReque
}
}

func (r *resourceConfiguration) copyConfigurationData(_ context.Context, apiModel *kaleido.Configuration, data *ConfigurationResourceModel, _ *diag.Diagnostics) {
func (r *resourceConfiguration) copyConfigurationData(_ context.Context, apiModel *kaleido.Configuration, data *ConfigurationResourceModel, _ diag.Diagnostics) {
data.ID = types.StringValue(apiModel.ID)
data.Name = types.StringValue(apiModel.Name)
data.LastUpdated = types.Int64Value(time.Now().UnixNano())
}

func (r *resourceConfiguration) dataToAPIModel(_ context.Context, data *ConfigurationResourceModel, apiModel *kaleido.Configuration, diagnostics *diag.Diagnostics) {
func (r *resourceConfiguration) dataToAPIModel(_ context.Context, data *ConfigurationResourceModel, apiModel *kaleido.Configuration, diagnostics diag.Diagnostics) {
apiModel.MembershipID = data.MembershipID.ValueString()
apiModel.Name = data.Name.ValueString()
apiModel.Type = data.Type.ValueString()
Expand All @@ -123,7 +123,7 @@ func (r *resourceConfiguration) Create(ctx context.Context, req resource.CreateR
apiModel := kaleido.Configuration{}
consortiumID := data.ConsortiumID.ValueString()
environmentID := data.EnvironmentID.ValueString()
r.dataToAPIModel(ctx, &data, &apiModel, &resp.Diagnostics)
r.dataToAPIModel(ctx, &data, &apiModel, resp.Diagnostics)

res, err := r.BaaS.CreateConfiguration(consortiumID, environmentID, &apiModel)
if err != nil {
Expand All @@ -138,7 +138,7 @@ func (r *resourceConfiguration) Create(ctx context.Context, req resource.CreateR
return
}

r.copyConfigurationData(ctx, &apiModel, &data, &resp.Diagnostics)
r.copyConfigurationData(ctx, &apiModel, &data, resp.Diagnostics)

resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
}
Expand All @@ -150,7 +150,7 @@ func (r *resourceConfiguration) Update(ctx context.Context, req resource.UpdateR
apiModel := kaleido.Configuration{}
consortiumID := data.ConsortiumID.ValueString()
environmentID := data.EnvironmentID.ValueString()
r.dataToAPIModel(ctx, &data, &apiModel, &resp.Diagnostics)
r.dataToAPIModel(ctx, &data, &apiModel, resp.Diagnostics)
configID := data.ID.String()

res, err := r.BaaS.UpdateConfiguration(consortiumID, environmentID, configID, &apiModel)
Expand All @@ -166,7 +166,7 @@ func (r *resourceConfiguration) Update(ctx context.Context, req resource.UpdateR
return
}

r.copyConfigurationData(ctx, &apiModel, &data, &resp.Diagnostics)
r.copyConfigurationData(ctx, &apiModel, &data, resp.Diagnostics)

resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
}
Expand Down Expand Up @@ -197,7 +197,7 @@ func (r *resourceConfiguration) Read(ctx context.Context, req resource.ReadReque
return
}

r.copyConfigurationData(ctx, &apiModel, &data, &resp.Diagnostics)
r.copyConfigurationData(ctx, &apiModel, &data, resp.Diagnostics)

resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
}
Expand Down
10 changes: 5 additions & 5 deletions kaleido/resource_node.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ func (r *resourceNode) Schema(_ context.Context, _ resource.SchemaRequest, resp
}
}

func (r *resourceNode) waitUntilNodeStarted(ctx context.Context, op, consortiumID, environmentID, nodeID string, apiModel *kaleido.Node, data *NodeResourceModel, diagnostics *diag.Diagnostics) error {
func (r *resourceNode) waitUntilNodeStarted(ctx context.Context, op, consortiumID, environmentID, nodeID string, apiModel *kaleido.Node, data *NodeResourceModel, diagnostics diag.Diagnostics) error {
return kaleidobase.Retry.Do(ctx, op, func(attempt int) (retry bool, err error) {
res, getErr := r.BaaS.GetNode(consortiumID, environmentID, nodeID, apiModel)
if getErr != nil {
Expand All @@ -171,7 +171,7 @@ func (r *resourceNode) waitUntilNodeStarted(ctx context.Context, op, consortiumI
})
}

func (r *resourceNode) copyNodeData(ctx context.Context, apiModel *kaleido.Node, data *NodeResourceModel, diagnostics *diag.Diagnostics) {
func (r *resourceNode) copyNodeData(ctx context.Context, apiModel *kaleido.Node, data *NodeResourceModel, diagnostics diag.Diagnostics) {
data.ID = types.StringValue(apiModel.ID)
data.Name = types.StringValue(apiModel.Name)
data.Role = types.StringValue(apiModel.Role)
Expand Down Expand Up @@ -229,7 +229,7 @@ func (r *resourceNode) Create(ctx context.Context, req resource.CreateRequest, r

if !isRemote {
// Do not wait for remote PrivateStack nodes to initialize
err = r.waitUntilNodeStarted(ctx, "Create", consortiumID, environmentID, apiModel.ID, &apiModel, &data, &resp.Diagnostics)
err = r.waitUntilNodeStarted(ctx, "Create", consortiumID, environmentID, apiModel.ID, &apiModel, &data, resp.Diagnostics)
if err != nil {
resp.Diagnostics.AddError("failed to query node status", err.Error())
return
Expand Down Expand Up @@ -281,7 +281,7 @@ func (r *resourceNode) Update(ctx context.Context, req resource.UpdateRequest, r

if !isRemote {
// Do not wait for remote PrivateStack nodes to initialize
err = r.waitUntilNodeStarted(ctx, "Update", consortiumID, environmentID, nodeID, &apiModel, &data, &resp.Diagnostics)
err = r.waitUntilNodeStarted(ctx, "Update", consortiumID, environmentID, nodeID, &apiModel, &data, resp.Diagnostics)
if err != nil {
resp.Diagnostics.AddError("failed to query node status", err.Error())
return
Expand Down Expand Up @@ -317,7 +317,7 @@ func (r *resourceNode) Read(ctx context.Context, req resource.ReadRequest, resp
return
}

r.copyNodeData(ctx, &apiModel, &data, &resp.Diagnostics)
r.copyNodeData(ctx, &apiModel, &data, resp.Diagnostics)

resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)

Expand Down
16 changes: 8 additions & 8 deletions kaleido/resource_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ func (r *resourceService) Schema(_ context.Context, _ resource.SchemaRequest, re
}
}

func (r *resourceService) waitUntilServiceStarted(ctx context.Context, op, consortiumID, environmentID, serviceID string, apiModel *kaleido.Service, data *ServiceResourceModel, diagnostics *diag.Diagnostics) error {
func (r *resourceService) waitUntilServiceStarted(ctx context.Context, op, consortiumID, environmentID, serviceID string, apiModel *kaleido.Service, data *ServiceResourceModel, diagnostics diag.Diagnostics) error {
return kaleidobase.Retry.Do(ctx, op, func(attempt int) (retry bool, err error) {
res, getErr := r.BaaS.GetService(consortiumID, environmentID, serviceID, apiModel)
if getErr != nil {
Expand All @@ -153,7 +153,7 @@ func (r *resourceService) waitUntilServiceStarted(ctx context.Context, op, conso
})
}

func (r *resourceService) copyServiceData(ctx context.Context, apiModel *kaleido.Service, data *ServiceResourceModel, diagnostics *diag.Diagnostics) {
func (r *resourceService) copyServiceData(ctx context.Context, apiModel *kaleido.Service, data *ServiceResourceModel, diagnostics diag.Diagnostics) {
data.ID = types.StringValue(apiModel.ID)
data.Name = types.StringValue(apiModel.Name)
mapValue, diag := types.MapValueFrom(ctx, types.StringType, apiModel.Urls)
Expand All @@ -177,7 +177,7 @@ func (r *resourceService) copyServiceData(ctx context.Context, apiModel *kaleido
data.HybridPortAllocation = types.Int64Value(apiModel.HybridPortAllocation)
}

func (r *resourceService) dataToAPIModel(_ context.Context, data *ServiceResourceModel, apiModel *kaleido.Service, diagnostics *diag.Diagnostics) {
func (r *resourceService) dataToAPIModel(_ context.Context, data *ServiceResourceModel, apiModel *kaleido.Service, diagnostics diag.Diagnostics) {
apiModel.Name = data.Name.ValueString()
apiModel.Service = data.ServiceType.ValueString()
apiModel.MembershipID = data.MembershipID.ValueString()
Expand All @@ -204,7 +204,7 @@ func (r *resourceService) Create(ctx context.Context, req resource.CreateRequest
apiModel := kaleido.Service{}
consortiumID := data.ConsortiumID.ValueString()
environmentID := data.EnvironmentID.ValueString()
r.dataToAPIModel(ctx, &data, &apiModel, &resp.Diagnostics)
r.dataToAPIModel(ctx, &data, &apiModel, resp.Diagnostics)

sharedExisting := false
if data.SharedDeployment.ValueBool() {
Expand Down Expand Up @@ -245,7 +245,7 @@ func (r *resourceService) Create(ctx context.Context, req resource.CreateRequest

}

err := r.waitUntilServiceStarted(ctx, "Create", consortiumID, environmentID, apiModel.ID, &apiModel, &data, &resp.Diagnostics)
err := r.waitUntilServiceStarted(ctx, "Create", consortiumID, environmentID, apiModel.ID, &apiModel, &data, resp.Diagnostics)
if err != nil {
resp.Diagnostics.AddError("failed to query service status", err.Error())
return
Expand All @@ -262,7 +262,7 @@ func (r *resourceService) Update(ctx context.Context, req resource.UpdateRequest
consortiumID := data.ConsortiumID.ValueString()
environmentID := data.EnvironmentID.ValueString()
serviceID := data.ID.ValueString()
r.dataToAPIModel(ctx, &data, &apiModel, &resp.Diagnostics)
r.dataToAPIModel(ctx, &data, &apiModel, resp.Diagnostics)

res, err := r.BaaS.UpdateService(consortiumID, environmentID, serviceID, &apiModel)
if err != nil {
Expand All @@ -288,7 +288,7 @@ func (r *resourceService) Update(ctx context.Context, req resource.UpdateRequest
return
}

err = r.waitUntilServiceStarted(ctx, "Update", consortiumID, environmentID, serviceID, &apiModel, &data, &resp.Diagnostics)
err = r.waitUntilServiceStarted(ctx, "Update", consortiumID, environmentID, serviceID, &apiModel, &data, resp.Diagnostics)
if err != nil {
resp.Diagnostics.AddError("failed to query service status", err.Error())
return
Expand Down Expand Up @@ -323,7 +323,7 @@ func (r *resourceService) Read(ctx context.Context, req resource.ReadRequest, re
return
}

r.copyServiceData(ctx, &apiModel, &data, &resp.Diagnostics)
r.copyServiceData(ctx, &apiModel, &data, resp.Diagnostics)

resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
}
Expand Down

0 comments on commit 3b57377

Please sign in to comment.