Skip to content

Commit

Permalink
Add HTTP response log calls (#4)
Browse files Browse the repository at this point in the history
* initial idea implemented, not tested

* better logging interface, jsonify objects in logs

* http response logging as an other log instead of integration

* add more specific header elements

* fix typo
  • Loading branch information
pballandras authored Mar 1, 2022
1 parent 50e08b0 commit e9baf2e
Show file tree
Hide file tree
Showing 10 changed files with 117 additions and 25 deletions.
54 changes: 54 additions & 0 deletions logging/logging.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package logging

import (
"encoding/json"
"net/http"
)

// logKeyValuePair is a helper struct to help organise the use of tflog. tflog
// uses pairs to print key-value items/args, so this is just to help visualize the
// way the logging works
type logKeyValuePair struct {
// Key is what will appear next to the value, e.g. keyID=...
Key string
// The value is what you want to log. e.g. Key=Value
Value interface{}
}

func makePair(key string, value interface{}) logKeyValuePair {
return logKeyValuePair{
Key: key,
Value: value,
}
}

// TryJsonify tries to marshall the object into a json string.
// If a failure should occur, it will return the untouched object.
func TryJsonify(object interface{}) interface{} {
jsonObject, err := json.Marshal(object)
if err == nil {
return string(jsonObject)
}
return object
}

// extractHttpResponseElements is an indirection for if it is ever needed to add more elements.
func extractHttpResponseElements(resp *http.Response) []logKeyValuePair {
return []logKeyValuePair{
makePair("responseContentType", resp.Header.Get("content-type")),
makePair("responseContentLength", resp.Header.Get("content-length")),
makePair("responseStatus", resp.Status),
makePair("requestUrl", resp.Request.URL.String()),
}
}

// ExtractHttpResponse extracts key-value pairs from the http.Response object.
// This is to match the args signature of the tflog package.
func ExtractHttpResponse(resp *http.Response) []interface{} {
elements := extractHttpResponseElements(resp)
args := []interface{}{}
for _, pair := range elements {
args = append(args, pair.Key, pair.Value)
}
return args
}
4 changes: 3 additions & 1 deletion sentry/data_source_sentry_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/jianyuan/go-sentry/sentry"
"github.com/jianyuan/terraform-provider-sentry/logging"
)

func dataSourceSentryKey() *schema.Resource {
Expand Down Expand Up @@ -86,7 +87,8 @@ func dataSourceSentryKeyRead(ctx context.Context, d *schema.ResourceData, meta i
project := d.Get("project").(string)

tflog.Debug(ctx, "Reading Sentry project keys", "org", org, "project", project)
keys, _, err := client.ProjectKeys.List(org, project)
keys, resp, err := client.ProjectKeys.List(org, project)
tflog.Debug(ctx, "Sentry key read http response data", logging.ExtractHttpResponse(resp)...)
if err != nil {
return diag.FromErr(err)
}
Expand Down
4 changes: 3 additions & 1 deletion sentry/data_source_sentry_organization.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/jianyuan/go-sentry/sentry"
"github.com/jianyuan/terraform-provider-sentry/logging"
)

func dataSourceSentryOrganization() *schema.Resource {
Expand Down Expand Up @@ -37,7 +38,8 @@ func dataSourceSentryOrganizationRead(ctx context.Context, d *schema.ResourceDat
slug := d.Get("slug").(string)

tflog.Debug(ctx, "Reading Sentry org", "orgSlug", slug)
org, _, err := client.Organizations.Get(slug)
org, resp, err := client.Organizations.Get(slug)
tflog.Debug(ctx, "Sentry organisation read http response data", logging.ExtractHttpResponse(resp)...)
if err != nil {
return diag.FromErr(err)
}
Expand Down
7 changes: 5 additions & 2 deletions sentry/resource_sentry_default_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/jianyuan/go-sentry/sentry"
"github.com/jianyuan/terraform-provider-sentry/logging"
)

func resourceSentryDefaultKey() *schema.Resource {
Expand Down Expand Up @@ -56,10 +57,12 @@ func resourceSentryDefaultKeyCreate(ctx context.Context, d *schema.ResourceData,
}

tflog.Debug(ctx, "Creating Sentry default key", "org", org, "project", project, "keyID", id)
if _, _, err := client.ProjectKeys.Update(org, project, id, params); err != nil {
key, resp, err := client.ProjectKeys.Update(org, project, id, params)
tflog.Debug(ctx, "Sentry default key create http response data", logging.ExtractHttpResponse(resp)...)
if err != nil {
return diag.FromErr(err)
}
tflog.Debug(ctx, "Created Sentry default key", "org", org, "project", project, "keyID", id)
tflog.Debug(ctx, "Created Sentry default key", "org", org, "project", project, "keyID", key.ID)

d.SetId(id)
return resourceSentryKeyRead(ctx, d, meta)
Expand Down
13 changes: 9 additions & 4 deletions sentry/resource_sentry_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/jianyuan/go-sentry/sentry"
"github.com/jianyuan/terraform-provider-sentry/logging"
)

func resourceSentryKey() *schema.Resource {
Expand Down Expand Up @@ -97,7 +98,8 @@ func resourceSentryKeyCreate(ctx context.Context, d *schema.ResourceData, meta i
}

tflog.Debug(ctx, "Creating Sentry key", "keyName", params.Name, "org", org, "project", project)
key, _, err := client.ProjectKeys.Create(org, project, params)
key, resp, err := client.ProjectKeys.Create(org, project, params)
tflog.Debug(ctx, "Sentry key create http response data", logging.ExtractHttpResponse(resp)...)
if err != nil {
return diag.FromErr(err)
}
Expand All @@ -116,10 +118,11 @@ func resourceSentryKeyRead(ctx context.Context, d *schema.ResourceData, meta int

tflog.Debug(ctx, "Reading Sentry key", "keyID", id, "org", org, "project", project)
keys, resp, err := client.ProjectKeys.List(org, project)
tflog.Debug(ctx, "Sentry key read http response data", logging.ExtractHttpResponse(resp)...)
if found, err := checkClientGet(resp, err, d); !found {
return diag.FromErr(err)
}
tflog.Trace(ctx, "Read Sentry keys", "keyCount", len(keys), "keys", keys)
tflog.Trace(ctx, "Read Sentry keys", "keyCount", len(keys), "keys", logging.TryJsonify(keys))

found := false

Expand Down Expand Up @@ -171,7 +174,8 @@ func resourceSentryKeyUpdate(ctx context.Context, d *schema.ResourceData, meta i
}

tflog.Debug(ctx, "Updating Sentry key", "keyID", id)
key, _, err := client.ProjectKeys.Update(org, project, id, params)
key, resp, err := client.ProjectKeys.Update(org, project, id, params)
tflog.Debug(ctx, "Sentry key update http response data", logging.ExtractHttpResponse(resp)...)
if err != nil {
return diag.FromErr(err)
}
Expand All @@ -189,7 +193,8 @@ func resourceSentryKeyDelete(ctx context.Context, d *schema.ResourceData, meta i
project := d.Get("project").(string)

tflog.Debug(ctx, "Deleting Sentry key", "keyID", id)
_, err := client.ProjectKeys.Delete(org, project, id)
resp, err := client.ProjectKeys.Delete(org, project, id)
tflog.Debug(ctx, "Sentry key delete http response data", logging.ExtractHttpResponse(resp)...)
tflog.Debug(ctx, "Deleted Sentry key", "keyID", id)
return diag.FromErr(err)
}
11 changes: 8 additions & 3 deletions sentry/resource_sentry_organization.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/jianyuan/go-sentry/sentry"
"github.com/jianyuan/terraform-provider-sentry/logging"
)

func resourceSentryOrganization() *schema.Resource {
Expand Down Expand Up @@ -50,7 +51,8 @@ func resourceSentryOrganizationCreate(ctx context.Context, d *schema.ResourceDat
}

tflog.Debug(ctx, "Creating Sentry organization", "orgName", params.Name)
org, _, err := client.Organizations.Create(params)
org, resp, err := client.Organizations.Create(params)
tflog.Debug(ctx, "Sentry organization create http response data", logging.ExtractHttpResponse(resp)...)
if err != nil {
return diag.FromErr(err)
}
Expand All @@ -67,6 +69,7 @@ func resourceSentryOrganizationRead(ctx context.Context, d *schema.ResourceData,

tflog.Debug(ctx, "Reading Sentry organization", "orgSlug", slug)
org, resp, err := client.Organizations.Get(slug)
tflog.Debug(ctx, "Sentry organization read http response data", logging.ExtractHttpResponse(resp)...)
if found, err := checkClientGet(resp, err, d); !found {
return diag.FromErr(err)
}
Expand All @@ -89,7 +92,8 @@ func resourceSentryOrganizationUpdate(ctx context.Context, d *schema.ResourceDat
}

tflog.Debug(ctx, "Updating Sentry organization", "orgSlug", slug)
org, _, err := client.Organizations.Update(slug, params)
org, resp, err := client.Organizations.Update(slug, params)
tflog.Debug(ctx, "Sentry Organization update http response data", logging.ExtractHttpResponse(resp)...)
if err != nil {
return diag.FromErr(err)
}
Expand All @@ -105,7 +109,8 @@ func resourceSentryOrganizationDelete(ctx context.Context, d *schema.ResourceDat
slug := d.Id()

tflog.Debug(ctx, "Deleting Sentry organization", "orgSlug", slug)
_, err := client.Organizations.Delete(slug)
resp, err := client.Organizations.Delete(slug)
tflog.Debug(ctx, "Sentry organization delete http response data", logging.ExtractHttpResponse(resp)...)
tflog.Debug(ctx, "Deleted Sentry organization", "orgSlug", slug)

return diag.FromErr(err)
Expand Down
11 changes: 8 additions & 3 deletions sentry/resource_sentry_project.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/jianyuan/go-sentry/sentry"
"github.com/jianyuan/terraform-provider-sentry/logging"
)

func resourceSentryProject() *schema.Resource {
Expand Down Expand Up @@ -112,7 +113,8 @@ func resourceSentryProjectCreate(ctx context.Context, d *schema.ResourceData, me
}

tflog.Debug(ctx, "Creating Sentry project", "teamName", team, "org", org)
proj, _, err := client.Projects.Create(org, team, params)
proj, resp, err := client.Projects.Create(org, team, params)
tflog.Debug(ctx, "Sentry project create http response data", logging.ExtractHttpResponse(resp)...)
if err != nil {
return diag.FromErr(err)
}
Expand All @@ -130,6 +132,7 @@ func resourceSentryProjectRead(ctx context.Context, d *schema.ResourceData, meta

tflog.Debug(ctx, "Reading Sentry project", "projectSlug", slug, "org", org)
proj, resp, err := client.Projects.Get(org, slug)
tflog.Debug(ctx, "Sentry project read http response data", logging.ExtractHttpResponse(resp)...)
if found, err := checkClientGet(resp, err, d); !found {
return diag.FromErr(err)
}
Expand Down Expand Up @@ -183,7 +186,8 @@ func resourceSentryProjectUpdate(ctx context.Context, d *schema.ResourceData, me
}

tflog.Debug(ctx, "Updating Sentry project", "projectSlug", slug, "org", org)
proj, _, err := client.Projects.Update(org, slug, params)
proj, resp, err := client.Projects.Update(org, slug, params)
tflog.Debug(ctx, "Sentry project update http response data", logging.ExtractHttpResponse(resp)...)
if err != nil {
return diag.FromErr(err)
}
Expand All @@ -200,7 +204,8 @@ func resourceSentryProjectDelete(ctx context.Context, d *schema.ResourceData, me
org := d.Get("organization").(string)

tflog.Debug(ctx, "Deleting Sentry project", "projectSlug", slug, "org", org)
_, err := client.Projects.Delete(org, slug)
resp, err := client.Projects.Delete(org, slug)
tflog.Debug(ctx, "Sentry project delete http response data", logging.ExtractHttpResponse(resp)...)
tflog.Debug(ctx, "Deleted Sentry project", "projectSlug", slug, "org", org)

return diag.FromErr(err)
Expand Down
14 changes: 10 additions & 4 deletions sentry/resource_sentry_project_plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/jianyuan/go-sentry/sentry"
"github.com/jianyuan/terraform-provider-sentry/logging"
)

func resourceSentryPlugin() *schema.Resource {
Expand Down Expand Up @@ -52,7 +53,8 @@ func resourceSentryPluginCreate(ctx context.Context, d *schema.ResourceData, met
project := d.Get("project").(string)

tflog.Debug(ctx, "Creating Sentry plugin", "pluginName", plugin, "org", org, "project", project)
_, err := client.ProjectPlugins.Enable(org, project, plugin)
resp, err := client.ProjectPlugins.Enable(org, project, plugin)
tflog.Debug(ctx, "Sentry plugin create http response data", logging.ExtractHttpResponse(resp)...)
if err != nil {
return diag.FromErr(err)
}
Expand All @@ -61,7 +63,8 @@ func resourceSentryPluginCreate(ctx context.Context, d *schema.ResourceData, met
d.SetId(plugin)

params := d.Get("config").(map[string]interface{})
if _, _, err := client.ProjectPlugins.Update(org, project, plugin, params); err != nil {
_, resp, err = client.ProjectPlugins.Update(org, project, plugin, params)
if err != nil {
return diag.FromErr(err)
}

Expand All @@ -77,6 +80,7 @@ func resourceSentryPluginRead(ctx context.Context, d *schema.ResourceData, meta

tflog.Debug(ctx, "Reading Sentry plugin", "pluginID", id, "org", org, "project", project)
plugin, resp, err := client.ProjectPlugins.Get(org, project, id)
tflog.Debug(ctx, "Sentry plugin read http response data", logging.ExtractHttpResponse(resp)...)
if found, err := checkClientGet(resp, err, d); !found {
return diag.FromErr(err)
}
Expand Down Expand Up @@ -110,7 +114,8 @@ func resourceSentryPluginUpdate(ctx context.Context, d *schema.ResourceData, met

tflog.Debug(ctx, "Updating Sentry plugin", "pluginID", id, "org", org, "project", project)
params := d.Get("config").(map[string]interface{})
plugin, _, err := client.ProjectPlugins.Update(org, project, id, params)
plugin, resp, err := client.ProjectPlugins.Update(org, project, id, params)
tflog.Debug(ctx, "Sentry plugin update http response data", logging.ExtractHttpResponse(resp)...)
if err != nil {
return diag.FromErr(err)
}
Expand All @@ -127,7 +132,8 @@ func resourceSentryPluginDelete(ctx context.Context, d *schema.ResourceData, met
project := d.Get("project").(string)

tflog.Debug(ctx, "Deleting Sentry plugin", "pluginID", id, "org", org, "project", project)
_, err := client.ProjectPlugins.Disable(org, project, id)
resp, err := client.ProjectPlugins.Disable(org, project, id)
tflog.Debug(ctx, "Sentry plugin delete http response data", logging.ExtractHttpResponse(resp)...)
tflog.Debug(ctx, "Deleted Sentry plugin", "pluginID", id, "org", org, "project", project)

return diag.FromErr(err)
Expand Down
13 changes: 9 additions & 4 deletions sentry/resource_sentry_project_rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/jianyuan/go-sentry/sentry"
"github.com/jianyuan/terraform-provider-sentry/logging"
"github.com/mitchellh/mapstructure"
)

Expand Down Expand Up @@ -149,7 +150,8 @@ func resourceSentryRuleCreate(ctx context.Context, d *schema.ResourceData, meta
}

tflog.Debug(ctx, "Creating Sentry rule", "ruleName", name, "org", org, "project", project)
rule, _, err := client.Rules.Create(org, project, params)
rule, resp, err := client.Rules.Create(org, project, params)
tflog.Debug(ctx, "Sentry rule create http response data", logging.ExtractHttpResponse(resp)...)
if err != nil {
return diag.FromErr(err)
}
Expand All @@ -168,10 +170,11 @@ func resourceSentryRuleRead(ctx context.Context, d *schema.ResourceData, meta in

tflog.Debug(ctx, "Reading Sentry rule", "ruleID", id, "org", org, "project", project)
rules, resp, err := client.Rules.List(org, project)
tflog.Debug(ctx, "Sentry rule read http response data", logging.ExtractHttpResponse(resp)...)
if found, err := checkClientGet(resp, err, d); !found {
return diag.FromErr(err)
}
tflog.Trace(ctx, "Read Sentry rules", "ruleCount", len(rules), "rules", rules)
tflog.Trace(ctx, "Read Sentry rules", "ruleCount", len(rules), "rules", logging.TryJsonify(rules))

var rule *sentry.Rule
for _, r := range rules {
Expand Down Expand Up @@ -289,7 +292,8 @@ func resourceSentryRuleUpdate(ctx context.Context, d *schema.ResourceData, meta
}

tflog.Debug(ctx, "Updating Sentry rule", "ruleID", id, "org", org, "project", project)
rule, _, err := client.Rules.Update(org, project, id, params)
rule, resp, err := client.Rules.Update(org, project, id, params)
tflog.Debug(ctx, "Sentry rule update http response data", logging.ExtractHttpResponse(resp)...)
if err != nil {
return diag.FromErr(err)
}
Expand All @@ -306,7 +310,8 @@ func resourceSentryRuleDelete(ctx context.Context, d *schema.ResourceData, meta
project := d.Get("project").(string)

tflog.Debug(ctx, "Deleting Sentry rule", "ruleID", id, "org", org, "project", project)
_, err := client.Rules.Delete(org, project, id)
resp, err := client.Rules.Delete(org, project, id)
tflog.Debug(ctx, "Sentry rule delete http response data", logging.ExtractHttpResponse(resp)...)
tflog.Debug(ctx, "Deleted Sentry rule", "ruleID", id, "org", org, "project", project)

return diag.FromErr(err)
Expand Down
Loading

0 comments on commit e9baf2e

Please sign in to comment.