diff --git a/logging/logging.go b/logging/logging.go new file mode 100644 index 00000000..4732181a --- /dev/null +++ b/logging/logging.go @@ -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 +} diff --git a/sentry/data_source_sentry_key.go b/sentry/data_source_sentry_key.go index 418a194c..b9861cdf 100644 --- a/sentry/data_source_sentry_key.go +++ b/sentry/data_source_sentry_key.go @@ -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 { @@ -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) } diff --git a/sentry/data_source_sentry_organization.go b/sentry/data_source_sentry_organization.go index 705e799d..15f47b4b 100644 --- a/sentry/data_source_sentry_organization.go +++ b/sentry/data_source_sentry_organization.go @@ -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 { @@ -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) } diff --git a/sentry/resource_sentry_default_key.go b/sentry/resource_sentry_default_key.go index 8f161591..ab60f188 100644 --- a/sentry/resource_sentry_default_key.go +++ b/sentry/resource_sentry_default_key.go @@ -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 { @@ -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) diff --git a/sentry/resource_sentry_key.go b/sentry/resource_sentry_key.go index 323e79cc..59af8549 100644 --- a/sentry/resource_sentry_key.go +++ b/sentry/resource_sentry_key.go @@ -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 { @@ -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) } @@ -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 @@ -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) } @@ -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) } diff --git a/sentry/resource_sentry_organization.go b/sentry/resource_sentry_organization.go index a5acbe69..d7c59297 100644 --- a/sentry/resource_sentry_organization.go +++ b/sentry/resource_sentry_organization.go @@ -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 { @@ -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) } @@ -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) } @@ -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) } @@ -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) diff --git a/sentry/resource_sentry_project.go b/sentry/resource_sentry_project.go index 2d5e262f..423aaf28 100644 --- a/sentry/resource_sentry_project.go +++ b/sentry/resource_sentry_project.go @@ -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 { @@ -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) } @@ -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) } @@ -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) } @@ -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) diff --git a/sentry/resource_sentry_project_plugin.go b/sentry/resource_sentry_project_plugin.go index 7cf6bcbd..1030a38d 100644 --- a/sentry/resource_sentry_project_plugin.go +++ b/sentry/resource_sentry_project_plugin.go @@ -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 { @@ -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) } @@ -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) } @@ -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) } @@ -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) } @@ -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) diff --git a/sentry/resource_sentry_project_rule.go b/sentry/resource_sentry_project_rule.go index 87dffc3b..44b32308 100755 --- a/sentry/resource_sentry_project_rule.go +++ b/sentry/resource_sentry_project_rule.go @@ -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" ) @@ -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) } @@ -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 { @@ -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) } @@ -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) diff --git a/sentry/resource_sentry_team.go b/sentry/resource_sentry_team.go index 151b3cdf..deb42787 100644 --- a/sentry/resource_sentry_team.go +++ b/sentry/resource_sentry_team.go @@ -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 resourceSentryTeam() *schema.Resource { @@ -66,7 +67,8 @@ func resourceSentryTeamCreate(ctx context.Context, d *schema.ResourceData, meta } tflog.Debug(ctx, "Creating Sentry team", "teamName", params.Name, "org", org) - team, _, err := client.Teams.Create(org, params) + team, resp, err := client.Teams.Create(org, params) + tflog.Debug(ctx, "Sentry team create http response data", logging.ExtractHttpResponse(resp)...) if err != nil { return diag.FromErr(err) } @@ -84,6 +86,7 @@ func resourceSentryTeamRead(ctx context.Context, d *schema.ResourceData, meta in tflog.Debug(ctx, "Reading Sentry team", "teamSlug", slug, "org", org) team, resp, err := client.Teams.Get(org, slug) + tflog.Debug(ctx, "Sentry team read http response data", logging.ExtractHttpResponse(resp)...) if found, err := checkClientGet(resp, err, d); !found { return diag.FromErr(err) } @@ -111,7 +114,8 @@ func resourceSentryTeamUpdate(ctx context.Context, d *schema.ResourceData, meta } tflog.Debug(ctx, "Updating Sentry team", "teamSlug", slug, "org", org) - team, _, err := client.Teams.Update(org, slug, params) + team, resp, err := client.Teams.Update(org, slug, params) + tflog.Debug(ctx, "Sentry team update http response data", logging.ExtractHttpResponse(resp)...) if err != nil { return diag.FromErr(err) } @@ -128,7 +132,8 @@ func resourceSentryTeamDelete(ctx context.Context, d *schema.ResourceData, meta org := d.Get("organization").(string) tflog.Debug(ctx, "Deleting Sentry team", "teamSlug", slug, "org", org) - _, err := client.Teams.Delete(org, slug) + resp, err := client.Teams.Delete(org, slug) + tflog.Debug(ctx, "Sentry team delete http response data", logging.ExtractHttpResponse(resp)...) tflog.Debug(ctx, "Deleted Sentry team", "teamSlug", slug, "org", org) return diag.FromErr(err)