Skip to content

Commit

Permalink
adds timeout (#42)
Browse files Browse the repository at this point in the history
* adds timeout
  • Loading branch information
mical authored Aug 16, 2023
1 parent cd86bb1 commit ae17479
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 5 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# Release v1.15.1 (2023-08-16)
* Adds timeout in api client

# Release v1.15.0 (2023-08-01)
* Adds OIDC support in integrations & login

Expand Down
29 changes: 28 additions & 1 deletion buddy/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ import (
"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/types"
"os"
"strconv"
buddyresource "terraform-provider-buddy/buddy/resource"
buddysource "terraform-provider-buddy/buddy/source"
"time"
)

var _ provider.Provider = &BuddyProvider{}
Expand All @@ -25,6 +27,7 @@ type BuddyProviderModel struct {
Token types.String `tfsdk:"token"`
BaseUrl types.String `tfsdk:"base_url"`
Insecure types.Bool `tfsdk:"insecure"`
Timeout types.Int64 `tfsdk:"timeout"`
}

func (p *BuddyProvider) Metadata(_ context.Context, _ provider.MetadataRequest, resp *provider.MetadataResponse) {
Expand All @@ -48,6 +51,10 @@ func (p *BuddyProvider) Schema(_ context.Context, _ provider.SchemaRequest, resp
MarkdownDescription: "Disable SSL verification of API calls. You may need to set this to `true` if you are using Buddy On-Premises without signed certificate. Can be specified with the `BUDDY_INSECURE` environmental variable",
Optional: true,
},
"timeout": schema.Int64Attribute{
MarkdownDescription: "The Buddy API client timeout in seconds. Can be specified with the `BUDDY_TIMEOUT` environmental variable. Default: 30s",
Optional: true,
},
},
}
}
Expand Down Expand Up @@ -79,6 +86,13 @@ func (p *BuddyProvider) Configure(ctx context.Context, req provider.ConfigureReq
"The provider cannot create the Buddy API client as there is unknown configuration value for the Buddy insecure attribute",
)
}
if config.Timeout.IsUnknown() {
resp.Diagnostics.AddAttributeError(
path.Root("timeout"),
"Unknown Buddy timeout value for the API endpoint",
"The provider cannot create the Buddy API client as there is unknown configuration value for the Buddy timeout attribute",
)
}
if resp.Diagnostics.HasError() {
return
}
Expand All @@ -95,8 +109,21 @@ func (p *BuddyProvider) Configure(ctx context.Context, req provider.ConfigureReq
if !config.Insecure.IsNull() {
insecure = config.Insecure.ValueBool()
}
timeout := 30
t := os.Getenv("BUDDY_TIMEOUT")
if t != "" {
var err error
timeout, err = strconv.Atoi(t)
if err != nil {
resp.Diagnostics.AddError("Wrong value in BUDDY_TIMEOUT env variable", "The provider cannot create the Buddy API client as there is wrong value for the BUDDY_TIMEOUT env variable")
return
}
}
if !config.Timeout.IsNull() {
timeout = int(config.Timeout.ValueInt64())
}

client, err := buddy.NewClient(token, baseUrl, insecure)
client, err := buddy.NewClientWithTimeout(token, baseUrl, insecure, time.Duration(timeout)*time.Second)
if err != nil {
resp.Diagnostics.AddError("Failed to create Buddy Client from provider configuration", fmt.Sprintf("The provider failed to create a new Buddy Client from the giver configuration: %s", err.Error()))
return
Expand Down
5 changes: 4 additions & 1 deletion buddy/source/test/member_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ import (
func TestAccSourceMember_upgrade(t *testing.T) {
domain := util.UniqueString()
config := testAccSourceMemberConfig(domain)
p, _, _ := acc.ApiClient.ProfileService.Get()
p, _, err := acc.ApiClient.ProfileService.Get()
if err != nil {
t.Fatal(err)
}
resource.Test(t, resource.TestCase{
Steps: []resource.TestStep{
{
Expand Down
1 change: 1 addition & 0 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,5 @@ provider "buddy" {

- `base_url` (String) The Buddy API base url. You may need to set this to your Buddy On-Premises API endpoint. Can be specified with the `BUDDY_BASE_URL` environment variable. Default: `https://api.buddy.works`
- `insecure` (Boolean) Disable SSL verification of API calls. You may need to set this to `true` if you are using Buddy On-Premises without signed certificate. Can be specified with the `BUDDY_INSECURE` environmental variable
- `timeout` (Number) The Buddy API client timeout in seconds. Can be specified with the `BUDDY_TIMEOUT` environmental variable. Default: 30s
- `token` (String, Sensitive) The OAuth2 token or Personal Access Token. Can be specified with the `BUDDY_TOKEN` environment variable.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.19

require (
github.com/bflad/tfproviderlint v0.29.0
github.com/buddy/api-go-sdk v1.13.1
github.com/buddy/api-go-sdk v1.13.4
github.com/golangci/golangci-lint v1.52.2
github.com/hashicorp/terraform-plugin-docs v0.14.1
github.com/hashicorp/terraform-plugin-framework v1.2.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,8 @@ github.com/breml/bidichk v0.2.4 h1:i3yedFWWQ7YzjdZJHnPo9d/xURinSq3OM+gyM43K4/8=
github.com/breml/bidichk v0.2.4/go.mod h1:7Zk0kRFt1LIZxtQdl9W9JwGAcLTTkOs+tN7wuEYGJ3s=
github.com/breml/errchkjson v0.3.1 h1:hlIeXuspTyt8Y/UmP5qy1JocGNR00KQHgfaNtRAjoxQ=
github.com/breml/errchkjson v0.3.1/go.mod h1:XroxrzKjdiutFyW3nWhw34VGg7kiMsDQox73yWCGI2U=
github.com/buddy/api-go-sdk v1.13.1 h1:bDHXha/MmoNfDWqYdygXoGq+AkASyZMX9XnjF2SJc9g=
github.com/buddy/api-go-sdk v1.13.1/go.mod h1:l3gE/GMTiq92cQGjQsBWwoTMaTPkLZqNZwLQOBAQiQw=
github.com/buddy/api-go-sdk v1.13.4 h1:Ht0A+cuGYHYEB3oYCS+6XkNdy2EBhS2WA69h6bqf8Ik=
github.com/buddy/api-go-sdk v1.13.4/go.mod h1:l3gE/GMTiq92cQGjQsBWwoTMaTPkLZqNZwLQOBAQiQw=
github.com/butuzov/ireturn v0.1.1 h1:QvrO2QF2+/Cx1WA/vETCIYBKtRjc30vesdoPUNo1EbY=
github.com/butuzov/ireturn v0.1.1/go.mod h1:Wh6Zl3IMtTpaIKbmwzqi6olnM9ptYQxxVacMsOEFPoc=
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
Expand Down

0 comments on commit ae17479

Please sign in to comment.