Skip to content

Commit ee71aff

Browse files
authored
Merge pull request #5 from MustWin/custom-timeout
Custom timeout
2 parents d8e665f + 97ba718 commit ee71aff

File tree

5 files changed

+31
-2
lines changed

5 files changed

+31
-2
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ terraform-Oracle-BareMetal-Provider
3434
*.orig
3535
.idea
3636
terraform-Oracle-BareMetal-Provider.iml
37+
terraform-provider-baremetal.iml
3738
.env
3839
crash.log
3940
terraform-encrypted-des.sh

crud/helpers.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ import (
1212
"github.com/oracle/terraform-provider-baremetal/client"
1313
"github.com/hashicorp/terraform/helper/resource"
1414
"github.com/hashicorp/terraform/helper/schema"
15+
"strconv"
16+
"errors"
1517
)
1618

1719
const FiveMinutes time.Duration = 5 * time.Minute
@@ -140,9 +142,16 @@ func stateRefreshFunc(sync StatefulResource) resource.StateRefreshFunc {
140142
}
141143

142144
func waitForStateRefresh(sync StatefulResource, pending, target []string) (e error) {
143-
timeout := FiveMinutes
145+
timeoutStr := os.Getenv("TF_VAR_timeout_minutes")
146+
t, err := strconv.Atoi(timeoutStr)
147+
if err != nil {
148+
return errors.New("timeout_minutes: " + err.Error())
149+
}
150+
timeout := time.Duration(t) * time.Minute
144151
if customTimeouter, ok := sync.(CustomTimeouter); ok {
145-
timeout = customTimeouter.CustomTimeout()
152+
if customTimeouter.CustomTimeout() > timeout {
153+
timeout = customTimeouter.CustomTimeout()
154+
}
146155
}
147156
stateConf := &resource.StateChangeConf{
148157
Pending: pending,

docs/examples/compute/single-instance/provider.tf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@ provider "baremetal" {
33
user_ocid = "${var.user_ocid}"
44
fingerprint = "${var.fingerprint}"
55
private_key_path = "${var.private_key_path}"
6+
timeout_minutes = "${var.timeout_minutes}"
67
}

docs/examples/compute/single-instance/variables.tf

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ variable "AD" {
1414
default = "1"
1515
}
1616

17+
variable "timeout_minutes" {
18+
default = 5
19+
}
20+
1721
variable "InstanceShape" {
1822
default = "VM.Standard1.2"
1923
}

provider.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ import (
1313
"github.com/oracle/terraform-provider-baremetal/objectstorage"
1414
"github.com/hashicorp/terraform/helper/schema"
1515
"github.com/hashicorp/terraform/terraform"
16+
"os"
17+
"strconv"
1618
)
1719

1820
var descriptions map[string]string
@@ -27,11 +29,15 @@ func init() {
2729
"private_key_path": "(Optional) The path to the user's PEM formatted private key.\n" +
2830
"A private_key or a private_key_path must be provided.",
2931
"private_key_password": "(Optional) The password used to secure the private key.",
32+
"timeout_minutes": "(Optional) The minimum API timeout for requests",
3033
}
3134
}
3235

3336
// Provider is the adapter for terraform, that gives access to all the resources
3437
func Provider(configfn schema.ConfigureFunc) terraform.ResourceProvider {
38+
if os.Getenv("TF_ORACLE_ENV") == "test" && os.Getenv("TF_VAR_timeout_minutes") == "" {
39+
os.Setenv("TF_VAR_timeout_minutes", "5") // This is for testing, it is overwritten correctly in ConfigureFunc when not in testmode
40+
}
3541
return &schema.Provider{
3642
DataSourcesMap: dataSourcesMap(),
3743
Schema: schemaMap(),
@@ -65,6 +71,12 @@ func schemaMap() map[string]*schema.Schema {
6571
Sensitive: true,
6672
Description: descriptions["private_key"],
6773
},
74+
"timeout_minutes": {
75+
Type: schema.TypeInt,
76+
Optional: true,
77+
Default: 5,
78+
Description: descriptions["timeout_minutes"],
79+
},
6880
"private_key_path": {
6981
Type: schema.TypeString,
7082
Optional: true,
@@ -167,6 +179,8 @@ func providerConfig(d *schema.ResourceData) (client interface{}, err error) {
167179
privateKeyBuffer, hasKey := d.Get("private_key").(string)
168180
privateKeyPath, hasKeyPath := d.Get("private_key_path").(string)
169181
privateKeyPassword, hasKeyPass := d.Get("private_key_password").(string)
182+
defaultTimeout := d.Get("timeout_minutes").(int)
183+
os.Setenv("TF_VAR_timeout_minutes", strconv.Itoa(defaultTimeout))
170184

171185
clientOpts := []baremetal.NewClientOptionsFunc{
172186
func(o *baremetal.NewClientOptions) {

0 commit comments

Comments
 (0)