Skip to content

Commit

Permalink
Add User-Agent Header to client requests
Browse files Browse the repository at this point in the history
REST client now pulls terraform provider version from CHANGELOG.md and passes the current version e.g. v3.0.0 as the User-Agent header in each request.
  • Loading branch information
rorywelch committed Jul 22, 2024
1 parent 4a795a8 commit a36eb37
Showing 1 changed file with 32 additions and 1 deletion.
33 changes: 32 additions & 1 deletion instana/restapi/rest-client.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
package restapi

import (
"bufio"
"context"
"crypto/tls"
"errors"
"fmt"
"log"
"os"
"path/filepath"
"runtime"
"strings"
"time"

Expand Down Expand Up @@ -134,7 +138,34 @@ func (client *restClientImpl) PutByQuery(resourcePath string, id string, queryPa
}

func (client *restClientImpl) createRequest() *resty.Request {
return client.restyClient.R().SetHeader("Accept", "application/json").SetHeader("Authorization", fmt.Sprintf("apiToken %s", client.apiToken))
//get path to root directory from runtime executor
_, b, _, _ := runtime.Caller(0)
basepath := filepath.Join(filepath.Dir(b), "../..")

//open CHANGELOG.md from root directory (only file storing updated version number)
terraformProviderVersion := ""
file, err := os.Open(basepath + "/CHANGELOG.md")
if err != nil {
log.Fatal(err)
}
defer file.Close()

//read lines from CHANGELOG.md until first line starting with ##
scanner := bufio.NewScanner(file)
for !strings.Contains(scanner.Text(), "##") {
scanner.Scan()
}
if err := scanner.Err(); err != nil {
log.Fatal(err)
}

//Read version number from first line with ##
terraformProviderVersion = scanner.Text()
terraformProviderVersion = strings.Split(terraformProviderVersion, "]")[0]
terraformProviderVersion = strings.Split(terraformProviderVersion, "[")[1]

//return client with headers needed for every call
return client.restyClient.R().SetHeader("Accept", "application/json").SetHeader("Authorization", fmt.Sprintf("apiToken %s", client.apiToken)).SetHeader("user-agent", terraformProviderVersion)
}

func (client *restClientImpl) executeRequestWithThrottling(method string, url string, req *resty.Request) ([]byte, error) {
Expand Down

0 comments on commit a36eb37

Please sign in to comment.