Skip to content

Commit

Permalink
Add logs
Browse files Browse the repository at this point in the history
  • Loading branch information
Clivern committed Apr 9, 2024
1 parent bdba1f2 commit 750c799
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 12 deletions.
18 changes: 16 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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.
15 changes: 15 additions & 0 deletions internal/provider/environment_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)
Expand All @@ -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())

Expand Down Expand Up @@ -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 {
Expand All @@ -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)...)
}
Expand All @@ -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 {
Expand All @@ -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) {
Expand Down
16 changes: 14 additions & 2 deletions internal/provider/project_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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)...)
Expand All @@ -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())

Expand Down Expand Up @@ -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 {
Expand All @@ -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)...)
}
Expand All @@ -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 {
Expand All @@ -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) {
Expand Down
13 changes: 10 additions & 3 deletions internal/provider/snapshot_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -137,16 +139,15 @@ 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)
data.RecordType = types.StringValue(createdSnapshot.RecordType)
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)...)
}
Expand All @@ -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())

Expand Down Expand Up @@ -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 {
Expand All @@ -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) {
Expand Down
18 changes: 15 additions & 3 deletions internal/provider/team_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)...)
}
Expand All @@ -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())

Expand Down Expand Up @@ -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 {
Expand All @@ -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)...)
}
Expand All @@ -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 {
Expand All @@ -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) {
Expand Down
16 changes: 14 additions & 2 deletions internal/provider/user_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)...)
Expand All @@ -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())

Expand Down Expand Up @@ -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 {
Expand All @@ -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)...)
}
Expand All @@ -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 {
Expand All @@ -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) {
Expand Down

0 comments on commit 750c799

Please sign in to comment.