From 750c799d26e2014b7b7399839eceeff1de5686e7 Mon Sep 17 00:00:00 2001 From: Clivern Date: Tue, 28 Feb 2023 01:14:07 +0100 Subject: [PATCH] Add logs --- CHANGELOG.md | 18 ++++++++++++++++-- internal/provider/environment_resource.go | 15 +++++++++++++++ internal/provider/project_resource.go | 16 ++++++++++++++-- internal/provider/snapshot_resource.go | 13 ++++++++++--- internal/provider/team_resource.go | 18 +++++++++++++++--- internal/provider/user_resource.go | 16 ++++++++++++++-- 6 files changed, 84 insertions(+), 12 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b76e247..5259261 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,17 @@ -## 0.1.0 (Unreleased) +## 0.3.0 (Unreleased) -FEATURES: +- Improve Logging. + + +## 0.2.0 (Unreleased) + +- Add User Resource. +- Add Team Resource. +- Add Snapshot Resource. +- Add Project Resource. +- Add Environment Resource. + + +## 0.1.0 + +- Initial Release. diff --git a/internal/provider/environment_resource.go b/internal/provider/environment_resource.go index c15f322..10f6792 100644 --- a/internal/provider/environment_resource.go +++ b/internal/provider/environment_resource.go @@ -134,6 +134,8 @@ func (r *EnvironmentResource) Create(ctx context.Context, req resource.CreateReq }, } + tflog.Info(ctx, fmt.Sprintf("Create an environment with name %s", newEnvironment.Name)) + createdEnvironment, err := r.client.CreateEnvironment(newEnvironment) if err != nil { @@ -144,6 +146,8 @@ func (r *EnvironmentResource) Create(ctx context.Context, req resource.CreateReq return } + tflog.Info(ctx, fmt.Sprintf("Environment with id %s got created", createdEnvironment.ID)) + // Set the created environment's ID in the Terraform state data.ID = types.StringValue(createdEnvironment.ID) data.Name = types.StringValue(createdEnvironment.Name) @@ -169,6 +173,8 @@ func (r *EnvironmentResource) Read(ctx context.Context, req resource.ReadRequest return } + tflog.Info(ctx, fmt.Sprintf("Read an environment with id %s", data.ID.ValueString())) + // Retrieve the environment using the GetEnvironment method environment, err := r.client.GetEnvironment(data.Project.ID.ValueString(), data.ID.ValueString()) @@ -216,6 +222,8 @@ func (r *EnvironmentResource) Update(ctx context.Context, req resource.UpdateReq }, } + tflog.Info(ctx, fmt.Sprintf("Update an environment with id %s", updatedEnvironment.ID)) + _, err := r.client.UpdateEnvironment(updatedEnvironment) if err != nil { @@ -225,6 +233,9 @@ func (r *EnvironmentResource) Update(ctx context.Context, req resource.UpdateReq ) return } + + tflog.Info(ctx, fmt.Sprintf("Environment with id %s got updated", updatedEnvironment.ID)) + // Save updated data into Terraform state resp.Diagnostics.Append(resp.State.Set(ctx, &data)...) } @@ -239,6 +250,8 @@ func (r *EnvironmentResource) Delete(ctx context.Context, req resource.DeleteReq return } + tflog.Info(ctx, fmt.Sprintf("Delete an environment with id %s", data.ID.ValueString())) + err := r.client.DeleteEnvironment(data.Project.ID.ValueString(), data.ID.ValueString()) if err != nil { @@ -248,6 +261,8 @@ func (r *EnvironmentResource) Delete(ctx context.Context, req resource.DeleteReq ) return } + + tflog.Info(ctx, fmt.Sprintf("Environment with id %s got deleted", data.ID.ValueString())) } func (r *EnvironmentResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) { diff --git a/internal/provider/project_resource.go b/internal/provider/project_resource.go index 7662647..5ecd001 100644 --- a/internal/provider/project_resource.go +++ b/internal/provider/project_resource.go @@ -128,6 +128,8 @@ func (r *ProjectResource) Create(ctx context.Context, req resource.CreateRequest createdProject, err := r.client.CreateProject(newProject) + tflog.Info(ctx, fmt.Sprintf("Read a project with name %s", newProject.Name)) + if err != nil { resp.Diagnostics.AddError( "Client Error", @@ -143,8 +145,7 @@ func (r *ProjectResource) Create(ctx context.Context, req resource.CreateRequest data.Description = types.StringValue(createdProject.Description) data.Team.ID = types.StringValue(createdProject.Team.ID) - // Write logs using the tflog package - tflog.Trace(ctx, "created a project") + tflog.Info(ctx, fmt.Sprintf("Project with id %s got created", createdProject.ID)) // Save data into Terraform state resp.Diagnostics.Append(resp.State.Set(ctx, &data)...) @@ -160,6 +161,8 @@ func (r *ProjectResource) Read(ctx context.Context, req resource.ReadRequest, re return } + tflog.Info(ctx, fmt.Sprintf("Read a project with id %s", data.ID.ValueString())) + // Retrieve the project using the GetProject method project, err := r.client.GetProject(data.ID.ValueString()) @@ -206,6 +209,8 @@ func (r *ProjectResource) Update(ctx context.Context, req resource.UpdateRequest }, } + tflog.Info(ctx, fmt.Sprintf("Update a project with id %s", updatedProject.ID)) + _, err := r.client.UpdateProject(updatedProject) if err != nil { @@ -215,6 +220,9 @@ func (r *ProjectResource) Update(ctx context.Context, req resource.UpdateRequest ) return } + + tflog.Info(ctx, fmt.Sprintf("Project with id %s got updated", updatedProject.ID)) + // Save updated data into Terraform state resp.Diagnostics.Append(resp.State.Set(ctx, &data)...) } @@ -229,6 +237,8 @@ func (r *ProjectResource) Delete(ctx context.Context, req resource.DeleteRequest return } + tflog.Info(ctx, fmt.Sprintf("Delete a project with id %s", data.ID.ValueString())) + err := r.client.DeleteProject(data.ID.ValueString()) if err != nil { @@ -238,6 +248,8 @@ func (r *ProjectResource) Delete(ctx context.Context, req resource.DeleteRequest ) return } + + tflog.Info(ctx, fmt.Sprintf("Project with id %s got deleted", data.ID.ValueString())) } func (r *ProjectResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) { diff --git a/internal/provider/snapshot_resource.go b/internal/provider/snapshot_resource.go index 9a4015a..d282ad1 100644 --- a/internal/provider/snapshot_resource.go +++ b/internal/provider/snapshot_resource.go @@ -127,6 +127,8 @@ func (r *SnapshotResource) Create(ctx context.Context, req resource.CreateReques }, } + tflog.Info(ctx, fmt.Sprintf("Create a snapshot with title %s", newSnapshot.Title)) + createdSnapshot, err := r.client.CreateSnapshot(newSnapshot) if err != nil { @@ -137,6 +139,8 @@ func (r *SnapshotResource) Create(ctx context.Context, req resource.CreateReques return } + tflog.Info(ctx, fmt.Sprintf("Snapshot with title %s got created", newSnapshot.Title)) + // Set the created snapshot's ID in the Terraform state data.ID = types.StringValue(createdSnapshot.ID) data.Title = types.StringValue(createdSnapshot.Title) @@ -144,9 +148,6 @@ func (r *SnapshotResource) Create(ctx context.Context, req resource.CreateReques data.RecordID = types.StringValue(createdSnapshot.RecordID) data.Team.ID = types.StringValue(createdSnapshot.Team.ID) - // Write logs using the tflog package - tflog.Trace(ctx, "created a snapshot") - // Save data into Terraform state resp.Diagnostics.Append(resp.State.Set(ctx, &data)...) } @@ -161,6 +162,8 @@ func (r *SnapshotResource) Read(ctx context.Context, req resource.ReadRequest, r return } + tflog.Info(ctx, fmt.Sprintf("Read a snapshot with id %s", data.ID.ValueString())) + // Retrieve the snapshot using the GetSnapshot method snapshot, err := r.client.GetSnapshot(data.ID.ValueString()) @@ -211,6 +214,8 @@ func (r *SnapshotResource) Delete(ctx context.Context, req resource.DeleteReques return } + tflog.Info(ctx, fmt.Sprintf("Delete a snapshot with id %s", data.ID.ValueString())) + err := r.client.DeleteSnapshot(data.ID.ValueString()) if err != nil { @@ -220,6 +225,8 @@ func (r *SnapshotResource) Delete(ctx context.Context, req resource.DeleteReques ) return } + + tflog.Info(ctx, fmt.Sprintf("Snapshot with id %s got deleted", data.ID.ValueString())) } func (r *SnapshotResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) { diff --git a/internal/provider/team_resource.go b/internal/provider/team_resource.go index a15f99b..d91286f 100644 --- a/internal/provider/team_resource.go +++ b/internal/provider/team_resource.go @@ -127,6 +127,8 @@ func (r *TeamResource) Create(ctx context.Context, req resource.CreateRequest, r Members: members, } + tflog.Info(ctx, fmt.Sprintf("Create a team with name %s", newTeam.Name)) + createdTeam, err := r.client.CreateTeam(newTeam) if err != nil { @@ -137,12 +139,11 @@ func (r *TeamResource) Create(ctx context.Context, req resource.CreateRequest, r return } + tflog.Info(ctx, fmt.Sprintf("Team with id %s got created", createdTeam.ID)) + // Set the created team's ID in the Terraform state data.ID = types.StringValue(createdTeam.ID) - // Write logs using the tflog package - tflog.Trace(ctx, "created a team") - // Save data into Terraform state resp.Diagnostics.Append(resp.State.Set(ctx, &data)...) } @@ -157,6 +158,8 @@ func (r *TeamResource) Read(ctx context.Context, req resource.ReadRequest, resp return } + tflog.Info(ctx, fmt.Sprintf("Read a team with id %s", data.ID.ValueString())) + // Retrieve the team using the GetTeam method team, err := r.client.GetTeam(data.ID.ValueString()) @@ -209,6 +212,8 @@ func (r *TeamResource) Update(ctx context.Context, req resource.UpdateRequest, r Members: members, } + tflog.Info(ctx, fmt.Sprintf("Update a team with id %s", updatedTeam.ID)) + _, err := r.client.UpdateTeam(updatedTeam) if err != nil { @@ -218,6 +223,9 @@ func (r *TeamResource) Update(ctx context.Context, req resource.UpdateRequest, r ) return } + + tflog.Info(ctx, fmt.Sprintf("Team with id %s got updated", updatedTeam.ID)) + // Save updated data into Terraform state resp.Diagnostics.Append(resp.State.Set(ctx, &data)...) } @@ -232,6 +240,8 @@ func (r *TeamResource) Delete(ctx context.Context, req resource.DeleteRequest, r return } + tflog.Info(ctx, fmt.Sprintf("Delete a team with id %s", data.ID.ValueString())) + err := r.client.DeleteTeam(data.ID.ValueString()) if err != nil { @@ -241,6 +251,8 @@ func (r *TeamResource) Delete(ctx context.Context, req resource.DeleteRequest, r ) return } + + tflog.Info(ctx, fmt.Sprintf("Team with id %s got deleted", data.ID.ValueString())) } func (r *TeamResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) { diff --git a/internal/provider/user_resource.go b/internal/provider/user_resource.go index 29a44e4..3d9d1cf 100644 --- a/internal/provider/user_resource.go +++ b/internal/provider/user_resource.go @@ -116,6 +116,8 @@ func (r *UserResource) Create(ctx context.Context, req resource.CreateRequest, r Password: data.Password.ValueString(), } + tflog.Info(ctx, fmt.Sprintf("Create a user with email %s", newUser.Email)) + createdUser, err := r.client.CreateUser(newUser) if err != nil { @@ -132,8 +134,7 @@ func (r *UserResource) Create(ctx context.Context, req resource.CreateRequest, r data.Email = types.StringValue(createdUser.Email) data.Role = types.StringValue(createdUser.Role) - // Write logs using the tflog package - tflog.Trace(ctx, "created a user") + tflog.Info(ctx, fmt.Sprintf("User with id %s got created", createdUser.ID)) // Save data into Terraform state resp.Diagnostics.Append(resp.State.Set(ctx, &data)...) @@ -149,6 +150,8 @@ func (r *UserResource) Read(ctx context.Context, req resource.ReadRequest, resp return } + tflog.Info(ctx, fmt.Sprintf("Read a user with id %s", data.ID.ValueString())) + // Retrieve the user using the GetUser method user, err := r.client.GetUser(data.ID.ValueString()) @@ -188,6 +191,8 @@ func (r *UserResource) Update(ctx context.Context, req resource.UpdateRequest, r Password: data.Password.ValueString(), } + tflog.Info(ctx, fmt.Sprintf("Update a user with id %s", updatedUser.ID)) + _, err := r.client.UpdateUser(updatedUser) if err != nil { @@ -197,6 +202,9 @@ func (r *UserResource) Update(ctx context.Context, req resource.UpdateRequest, r ) return } + + tflog.Info(ctx, fmt.Sprintf("User with id %s got updated", updatedUser.ID)) + // Save updated data into Terraform state resp.Diagnostics.Append(resp.State.Set(ctx, &data)...) } @@ -211,6 +219,8 @@ func (r *UserResource) Delete(ctx context.Context, req resource.DeleteRequest, r return } + tflog.Info(ctx, fmt.Sprintf("Delete a user with id %s", data.ID.ValueString())) + err := r.client.DeleteUser(data.ID.ValueString()) if err != nil { @@ -220,6 +230,8 @@ func (r *UserResource) Delete(ctx context.Context, req resource.DeleteRequest, r ) return } + + tflog.Info(ctx, fmt.Sprintf("User with id %s got deleted", data.ID.ValueString())) } func (r *UserResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) {