• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

rollbar / terraform-provider-rollbar / 11973969186

22 Nov 2024 01:57PM UTC coverage: 92.097% (-0.1%) from 92.222%
11973969186

Pull #417

github

web-flow
Merge 7861e3554 into 1d57b09d8
Pull Request #417: Bump github.com/go-resty/resty/v2 from 2.11.0 to 2.16.2

1480 of 1607 relevant lines covered (92.1%)

5.73 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

84.47
/client/client.go
1
/*
2
 * Copyright (c) 2024 Rollbar, Inc.
3
 *
4
 * Permission is hereby granted, free of charge, to any person obtaining a copy
5
 * of this software and associated documentation files (the "Software"), to deal
6
 * in the Software without restriction, including without limitation the rights
7
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
 * copies of the Software, and to permit persons to whom the Software is
9
 * furnished to do so, subject to the following conditions:
10
 *
11
 * The above copyright notice and this permission notice shall be included in all
12
 * copies or substantial portions of the Software.
13
 *
14
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20
 * SOFTWARE.
21
 */
22

23
// Package client is a client library for accessing the Rollbar API.
24
package client
25

26
import (
27
        "net/http"
28
        "sync"
29
        "time"
30

31
        "github.com/go-resty/resty/v2"
32
        "github.com/rs/zerolog/log"
33
)
34

35
// DefaultBaseURL is the default base URL for the Rollbar API.
36
const DefaultBaseURL = "https://api.rollbar.com"
37
const Version = "v1.15.1"
38

39
// RollbarAPIClient is a client for the Rollbar API.
40
type RollbarAPIClient struct {
41
        BaseURL string // Base URL for Rollbar API
42
        Resty   *resty.Client
43

44
        m sync.Mutex
45
}
46

47
// NewTestClient sets up a new Rollbar API test client.
48
func NewTestClient(baseURL, token string) *RollbarAPIClient {
1✔
49
        log.Debug().Msg("Initializing Rollbar client")
1✔
50

1✔
51
        // New Resty HTTP client
1✔
52
        r := resty.New()
1✔
53

1✔
54
        // Use default transport - needed for VCR
1✔
55
        r.SetTransport(http.DefaultTransport).
1✔
56
                // set timeout on http client
1✔
57
                SetTimeout(30 * time.Second).
1✔
58
                // Set retry count to 1 (try 2 times before it fails)
1✔
59
                SetRetryCount(1).
1✔
60
                SetRetryWaitTime(1 * time.Second).
1✔
61
                SetRetryMaxWaitTime(5 * time.Second)
1✔
62

1✔
63
        // Authentication
1✔
64
        if token != "" {
2✔
65
                r = r.SetHeaders(map[string]string{
1✔
66
                        "X-Rollbar-Access-Token": token,
1✔
67
                        "X-Rollbar-Terraform":    "true"})
1✔
68
        } else {
1✔
69
                log.Warn().Msg("Rollbar API token not set")
×
70
        }
×
71

72
        // Authentication
73
        if baseURL == "" {
1✔
74
                log.Error().Msg("Rollbar API base URL not set")
×
75
        }
×
76

77
        // Configure Resty to use Zerolog for logging
78
        r.SetLogger(restyZeroLogger{log.Logger})
1✔
79

1✔
80
        // Rollbar client
1✔
81
        c := RollbarAPIClient{
1✔
82
                Resty:   r,
1✔
83
                BaseURL: baseURL,
1✔
84
        }
1✔
85
        return &c
1✔
86
}
87

88
func (c *RollbarAPIClient) SetHeaderResource(header string) {
×
89
        c.m.Lock()
×
90
        defer c.m.Unlock()
×
91
        c.Resty.SetHeader("X-Rollbar-Terraform-Resource", header)
×
92
}
×
93

94
func (c *RollbarAPIClient) SetHeaderDataSource(header string) {
×
95
        c.m.Lock()
×
96
        defer c.m.Unlock()
×
97
        c.Resty.SetHeader("X-Rollbar-Terraform-DataSource", header)
×
98
}
×
99

100
// NewClient sets up a new Rollbar API client.
101
func NewClient(baseURL, token string) *RollbarAPIClient {
3✔
102
        log.Debug().Msg("Initializing Rollbar client")
3✔
103
        now := time.Now().Format(time.RFC3339Nano)
3✔
104
        // New Resty HTTP client
3✔
105
        r := resty.New()
3✔
106

3✔
107
        // Use default transport - needed for VCR
3✔
108
        r.SetTransport(http.DefaultTransport).
3✔
109
                // set timeout on http client
3✔
110
                SetTimeout(30 * time.Second).
3✔
111
                // Set retry count to 4 (try 5 times before it fails)
3✔
112
                SetRetryCount(4).
3✔
113
                SetRetryWaitTime(8 * time.Second).
3✔
114
                SetRetryMaxWaitTime(50 * time.Second).
3✔
115
                AddRetryCondition(
3✔
116
                        func(r *resty.Response, err error) bool {
19✔
117
                                if err != nil { // network error
16✔
118
                                        return true
×
119
                                }
×
120
                                return r.StatusCode() == http.StatusNotFound ||
16✔
121
                                        r.StatusCode() == http.StatusTooManyRequests ||
16✔
122
                                        r.StatusCode() == http.StatusInternalServerError ||
16✔
123
                                        r.StatusCode() == http.StatusBadGateway
16✔
124
                        })
125
        // Authentication
126

127
        if token != "" {
4✔
128
                r = r.SetHeaders(map[string]string{
1✔
129
                        "X-Rollbar-Access-Token":      token,
1✔
130
                        "X-Rollbar-Terraform-Version": Version,
1✔
131
                        "X-Rollbar-Terraform-Date":    now,
1✔
132
                })
1✔
133
        } else {
3✔
134
                log.Warn().Msg("Rollbar API token not set")
2✔
135
        }
2✔
136

137
        // Authentication
138
        if baseURL == "" {
4✔
139
                log.Error().Msg("Rollbar API base URL not set")
1✔
140
        }
1✔
141

142
        // Configure Resty to use Zerolog for logging
143
        r.SetLogger(restyZeroLogger{log.Logger})
3✔
144

3✔
145
        // Rollbar client
3✔
146
        c := RollbarAPIClient{
3✔
147
                Resty:   r,
3✔
148
                BaseURL: baseURL,
3✔
149
        }
3✔
150
        return &c
3✔
151
}
152

153
// Status represents the enabled or disabled status of an entity.
154
type Status string
155

156
// Possible values for status
157
const (
158
        StatusEnabled  = Status("enabled")
159
        StatusDisabled = Status("disabled")
160
)
161

162
// errorFromResponse interprets the status code of Resty response, returning nil
163
// on success or an appropriate error code
164
func errorFromResponse(resp *resty.Response) error {
204✔
165
        switch resp.StatusCode() {
204✔
166
        case http.StatusOK, http.StatusCreated:
71✔
167
                return nil
71✔
168
        case http.StatusUnauthorized:
42✔
169
                return ErrUnauthorized
42✔
170
        case http.StatusNotFound:
44✔
171
                return ErrNotFound
44✔
172
        default:
47✔
173
                er := resp.Error().(*ErrorResult)
47✔
174
                log.Error().
47✔
175
                        Int("StatusCode", resp.StatusCode()).
47✔
176
                        Str("Status", resp.Status()).
47✔
177
                        Interface("ErrorResult", er).
47✔
178
                        Send()
47✔
179
                return er
47✔
180
        }
181
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc