Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added http headers for terraform requests #337

Merged
merged 4 commits into from
Dec 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions client/client.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2020 Rollbar, Inc.
* Copyright (c) 2022 Rollbar, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -33,6 +33,7 @@ import (

// DefaultBaseURL is the default base URL for the Rollbar API.
const DefaultBaseURL = "https://api.rollbar.com"
const Version = "v1.8.0"

// RollbarAPIClient is a client for the Rollbar API.
type RollbarAPIClient struct {
Expand All @@ -43,7 +44,7 @@ type RollbarAPIClient struct {
// NewClient sets up a new Rollbar API client.
func NewClient(baseURL, token string) *RollbarAPIClient {
log.Debug().Msg("Initializing Rollbar client")

now := time.Now().Format(time.RFC3339Nano)
// New Resty HTTP client
r := resty.New()

Expand All @@ -58,8 +59,10 @@ func NewClient(baseURL, token string) *RollbarAPIClient {
// Authentication
if token != "" {
r = r.SetHeaders(map[string]string{
"X-Rollbar-Access-Token": token,
"X-Rollbar-Terraform": "true"})
"X-Rollbar-Access-Token": token,
"X-Rollbar-Terraform-Version": Version,
"X-Rollbar-Terraform-Date": now,
})
} else {
log.Warn().Msg("Rollbar API token not set")
}
Expand Down
47 changes: 47 additions & 0 deletions rollbar/common.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Copyright (c) 2022 Rollbar, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

package rollbar

import "github.com/rollbar/terraform-provider-rollbar/client"

const ComplexImportSeparator = ","

const (
rollbarProject = "rollbar_project"
rollbarProjects = "rollbar_projects"
rollbarProjectAccessToken = "rollbar_project_access_token"
rollbarProjectAccessTokens = "rollbar_project_access_tokens"
rollbarTeam = "rollbar_team"
rollbarUser = "rollbar_user"
rollbarTeamUser = "rollbar_team_user"
rollbarNotification = "rollbar_notification"
rollbarServiceLink = "rollbar_service_link"
rollbarIntegration = "rollbar_integration"
)

func setResourceHeader(header string, c *client.RollbarAPIClient) {
c.Resty.SetHeader("X-Rollbar-Terraform-Resource", header)
}
func setDataSourceHeader(header string, c *client.RollbarAPIClient) {
c.Resty.SetHeader("X-Rollbar-Terraform-DataSource", header)
}
3 changes: 0 additions & 3 deletions rollbar/const.go

This file was deleted.

4 changes: 3 additions & 1 deletion rollbar/data_source_project.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2020 Rollbar, Inc.
* Copyright (c) 2022 Rollbar, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand All @@ -24,6 +24,7 @@ package rollbar

import (
"fmt"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/rollbar/terraform-provider-rollbar/client"
)
Expand Down Expand Up @@ -73,6 +74,7 @@ func dataSourceProjectRead(d *schema.ResourceData, meta interface{}) error {
name := d.Get("name").(string)

c := meta.(map[string]*client.RollbarAPIClient)[schemaKeyToken]
setDataSourceHeader(rollbarProject, c)
pl, err := c.ListProjects()
if err != nil {
return err
Expand Down
3 changes: 2 additions & 1 deletion rollbar/data_source_project_access_token.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2020 Rollbar, Inc.
* Copyright (c) 2022 Rollbar, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -118,6 +118,7 @@ func dataSourceProjectAccessTokenRead(ctx context.Context, d *schema.ResourceDat
l.Debug().Msg("Reading project access token from Rollbar")

c := m.(map[string]*client.RollbarAPIClient)[schemaKeyToken]
setDataSourceHeader(rollbarProjectAccessToken, c)
tokens, err := c.ListProjectAccessTokens(projectID)
if err != nil {
return diag.FromErr(err)
Expand Down
10 changes: 6 additions & 4 deletions rollbar/data_source_project_access_tokens.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2020 Rollbar, Inc.
* Copyright (c) 2022 Rollbar, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand All @@ -24,13 +24,14 @@ package rollbar

import (
"context"
"strconv"
"strings"
"time"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/rollbar/terraform-provider-rollbar/client"
"github.com/rs/zerolog/log"
"strconv"
"strings"
"time"
)

// dataSourceProjectAccessTokens is a data source for listing all project access
Expand Down Expand Up @@ -134,6 +135,7 @@ func dataSourceProjectAccessTokensRead(ctx context.Context, d *schema.ResourceDa
l.Debug().Msg("Reading project access token data from Rollbar")

c := m.(map[string]*client.RollbarAPIClient)[schemaKeyToken]
setDataSourceHeader(rollbarProjectAccessTokens, c)
tokens, err := c.ListProjectAccessTokens(projectID)
if err != nil {
return diag.FromErr(err)
Expand Down
8 changes: 5 additions & 3 deletions rollbar/data_source_projects.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2020 Rollbar, Inc.
* Copyright (c) 2022 Rollbar, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand All @@ -24,12 +24,13 @@ package rollbar

import (
"context"
"strconv"
"time"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/rollbar/terraform-provider-rollbar/client"
"github.com/rs/zerolog/log"
"strconv"
"time"
)

func dataSourceProjects() *schema.Resource {
Expand Down Expand Up @@ -83,6 +84,7 @@ func dataSourceProjectsRead(ctx context.Context, d *schema.ResourceData, m inter
log.Debug().Msg("Reading project list from API")
var diags diag.Diagnostics
c := m.(map[string]*client.RollbarAPIClient)[schemaKeyToken]
setDataSourceHeader(rollbarProjects, c)

projects, err := c.ListProjects()
if err != nil {
Expand Down
7 changes: 5 additions & 2 deletions rollbar/data_source_team.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2020 Rollbar, Inc.
* Copyright (c) 2022 Rollbar, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand All @@ -25,12 +25,13 @@ package rollbar
import (
"context"
"fmt"
"strconv"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/rollbar/terraform-provider-rollbar/client"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"strconv"
)

func dataSourceTeam() *schema.Resource {
Expand Down Expand Up @@ -70,6 +71,8 @@ func dataSourceTeamRead(ctx context.Context, d *schema.ResourceData, m interface
var l zerolog.Logger
teamID, ok := d.GetOk("team_id")
c := m.(map[string]*client.RollbarAPIClient)[schemaKeyToken]
setDataSourceHeader(rollbarTeam, c)

if ok {
l = log.With().
Int("id", teamID.(int)).
Expand Down
31 changes: 16 additions & 15 deletions rollbar/provider.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2020 Rollbar, Inc.
* Copyright (c) 2022 Rollbar, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand All @@ -25,11 +25,12 @@ package rollbar

import (
"context"
"strconv"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/mitchellh/mapstructure"
"github.com/rollbar/terraform-provider-rollbar/client"
"strconv"
)

const schemaKeyToken = "api_key"
Expand Down Expand Up @@ -60,21 +61,21 @@ func Provider() *schema.Provider {
},
},
ResourcesMap: map[string]*schema.Resource{
"rollbar_project": resourceProject(),
"rollbar_project_access_token": resourceProjectAccessToken(),
"rollbar_team": resourceTeam(),
"rollbar_user": resourceUser(),
"rollbar_team_user": resourceTeamUser(),
"rollbar_notification": resourceNotification(),
"rollbar_service_link": resourceServiceLink(),
"rollbar_integration": resourceIntegraion(),
rollbarProject: resourceProject(),
rollbarProjectAccessToken: resourceProjectAccessToken(),
rollbarTeam: resourceTeam(),
rollbarUser: resourceUser(),
rollbarTeamUser: resourceTeamUser(),
rollbarNotification: resourceNotification(),
rollbarServiceLink: resourceServiceLink(),
rollbarIntegration: resourceIntegraion(),
},
DataSourcesMap: map[string]*schema.Resource{
"rollbar_project": dataSourceProject(),
"rollbar_projects": dataSourceProjects(),
"rollbar_project_access_token": dataSourceProjectAccessToken(),
"rollbar_project_access_tokens": dataSourceProjectAccessTokens(),
"rollbar_team": dataSourceTeam(),
rollbarProject: dataSourceProject(),
rollbarProjects: dataSourceProjects(),
rollbarProjectAccessToken: dataSourceProjectAccessToken(),
rollbarProjectAccessTokens: dataSourceProjectAccessTokens(),
rollbarTeam: dataSourceTeam(),
},
ConfigureContextFunc: providerConfigure,
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,8 @@ func resourceIntegrationCreateUpdateDelete(integration string, bodyMap map[strin
l = l.With().Str("id", id).Logger()
}
c := m.(map[string]*client.RollbarAPIClient)[projectKeyToken]
setResourceHeader(rollbarIntegration, c)

intf, err := c.UpdateIntegration(integration, bodyMap)
if err != nil {
l.Err(err).Send()
Expand Down Expand Up @@ -274,6 +276,8 @@ func resourceIntegrationRead(ctx context.Context, d *schema.ResourceData, m inte
integration := spl[1]
l.Info().Msg("Reading rollbar_integration resource")
c := m.(map[string]*client.RollbarAPIClient)[projectKeyToken]
setResourceHeader(rollbarIntegration, c)

intf, err := c.ReadIntegration(integration)
if err == client.ErrNotFound {
d.SetId("")
Expand Down
6 changes: 5 additions & 1 deletion rollbar/resource_notification.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2021 Rollbar, Inc.
* Copyright (c) 2022 Rollbar, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -235,6 +235,7 @@ func resourceNotificationCreate(ctx context.Context, d *schema.ResourceData, m i
l.Info().Msg("Creating rollbar_notification resource")

c := m.(map[string]*client.RollbarAPIClient)[projectKeyToken]
setResourceHeader(rollbarNotification, c)
n, err := c.CreateNotification(channel, filters, trigger, config)
if err != nil {
l.Err(err).Send()
Expand All @@ -261,6 +262,7 @@ func resourceNotificationUpdate(ctx context.Context, d *schema.ResourceData, m i
l.Info().Msg("Creating rollbar_notification resource")

c := m.(map[string]*client.RollbarAPIClient)[projectKeyToken]
setResourceHeader(rollbarNotification, c)
n, err := c.UpdateNotification(id, channel, filters, trigger, config)

if err != nil {
Expand Down Expand Up @@ -329,6 +331,7 @@ func resourceNotificationRead(ctx context.Context, d *schema.ResourceData, m int
Logger()
l.Info().Msg("Reading rollbar_notification resource")
c := m.(map[string]*client.RollbarAPIClient)[projectKeyToken]
setResourceHeader(rollbarNotification, c)
n, err := c.ReadNotification(id, channel)
if err == client.ErrNotFound {
d.SetId("")
Expand All @@ -352,6 +355,7 @@ func resourceNotificationDelete(ctx context.Context, d *schema.ResourceData, m i
l := log.With().Int("id", id).Logger()
l.Info().Msg("Deleting rollbar_notification resource")
c := m.(map[string]*client.RollbarAPIClient)[projectKeyToken]
setResourceHeader(rollbarNotification, c)
err := c.DeleteNotification(id, channel)
if err != nil {
l.Err(err).Msg("Error deleting rollbar_notification resource")
Expand Down
9 changes: 7 additions & 2 deletions rollbar/resource_project.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2020 Rollbar, Inc.
* Copyright (c) 2022 Rollbar, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand All @@ -25,11 +25,12 @@ package rollbar
import (
"context"
"fmt"
"strconv"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/rollbar/terraform-provider-rollbar/client"
"github.com/rs/zerolog/log"
"strconv"
)

func resourceProject() *schema.Resource {
Expand Down Expand Up @@ -93,6 +94,7 @@ func resourceProjectCreate(ctx context.Context, d *schema.ResourceData, m interf
l.Info().Msg("Creating new Rollbar project resource")

c := m.(map[string]*client.RollbarAPIClient)[schemaKeyToken]
setResourceHeader(rollbarProject, c)
p, err := c.CreateProject(name)
if err != nil {
l.Err(err).Send()
Expand Down Expand Up @@ -161,6 +163,7 @@ func resourceProjectRead(ctx context.Context, d *schema.ResourceData, m interfac
l.Info().Msg("Reading Rollbar project resource")

c := m.(map[string]*client.RollbarAPIClient)[schemaKeyToken]
setResourceHeader(rollbarProject, c)
proj, err := c.ReadProject(projectID)
if err == client.ErrNotFound {
l.Debug().Msg("Project not found on Rollbar - removing from state")
Expand Down Expand Up @@ -203,6 +206,7 @@ func resourceProjectUpdate(ctx context.Context, d *schema.ResourceData, m interf
Logger()
l.Debug().Msg("Updating rollbar_project resource")
c := m.(map[string]*client.RollbarAPIClient)[schemaKeyToken]
setResourceHeader(rollbarProject, c)
err := c.UpdateProjectTeams(projectID, teamIDs)
if err != nil {
l.Err(err).Msg("Error updating rollbar_project resource")
Expand All @@ -220,6 +224,7 @@ func resourceProjectDelete(ctx context.Context, d *schema.ResourceData, m interf
Logger()
l.Info().Msg("Deleting rollbar_project resource")
c := m.(map[string]*client.RollbarAPIClient)[schemaKeyToken]
setResourceHeader(rollbarProject, c)
err := c.DeleteProject(projectID)
if err != nil {
l.Err(err).Msg("Error deleting rollbar_project resource")
Expand Down
Loading