Skip to content

Commit

Permalink
Merge pull request #15 from instana/user-agent-header
Browse files Browse the repository at this point in the history
Added support to send a User-Agent header with current Terraform Provider Version to Instana
  • Loading branch information
rorywelch authored Jul 24, 2024
2 parents 4a795a8 + 179a7f1 commit b7d55a3
Showing 1 changed file with 33 additions and 1 deletion.
34 changes: 33 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,35 @@ 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.Println("Error: couldn't open file", basepath+"/CHANGELOG.md", err)
return client.restyClient.R().SetHeader("Accept", "application/json").SetHeader("Authorization", fmt.Sprintf("apiToken %s", client.apiToken)).SetHeader("user-agent", terraformProviderVersion)
}
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.Println("Error: Couldn't scan file", err)
} else {
//Read version number from first line of CHANGELOG.md starting 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 b7d55a3

Please sign in to comment.