Skip to content

Commit

Permalink
TF now waits for instances to be AVAILABLE
Browse files Browse the repository at this point in the history
  • Loading branch information
craigcarl-oracle committed Feb 28, 2017
1 parent 6fcf666 commit 770baee
Show file tree
Hide file tree
Showing 43 changed files with 827 additions and 80 deletions.
4 changes: 0 additions & 4 deletions core/drg_resource_crud.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,6 @@ func (s *DrgResourceCrud) DeletedTarget() []string {
return []string{baremetal.ResourceTerminated}
}

func (s *DrgResourceCrud) State() string {
return s.Res.State
}

func (s *DrgResourceCrud) Create() (e error) {
opts := &baremetal.CreateOptions{}
compartmentID := s.D.Get("compartment_id").(string)
Expand Down
7 changes: 6 additions & 1 deletion core/instance_resource_crud.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
package core

import (
"github.com/MustWin/baremetal-sdk-go"
"time"

"github.com/MustWin/baremetal-sdk-go"
"github.com/MustWin/terraform-Oracle-BareMetal-Provider/crud"
)

Expand All @@ -17,6 +18,10 @@ func (s *InstanceResourceCrud) ID() string {
return s.Resource.ID
}

func (s *InstanceResourceCrud) CustomTimeout() time.Duration {
return 15 * time.Minute
}

func (s *InstanceResourceCrud) CreatedPending() []string {
return []string{
baremetal.ResourceProvisioning,
Expand Down
2 changes: 2 additions & 0 deletions core/security_list_datasource_crud.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ func (s *SecurityListDatasourceCrud) SetData() {
egressRule.ICMPOptions,
egressRule.TCPOptions,
egressRule.UDPOptions,
&egressRule.IsStateless,
)
confEgressRules = append(confEgressRules, confEgressRule)
}
Expand All @@ -82,6 +83,7 @@ func (s *SecurityListDatasourceCrud) SetData() {
ingressRule.ICMPOptions,
ingressRule.TCPOptions,
ingressRule.UDPOptions,
nil,
)
confIngressRules = append(confIngressRules, confIngressRule)
}
Expand Down
5 changes: 5 additions & 0 deletions core/security_list_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,11 @@ func SecurityListResource() *schema.Resource {
},
"tcp_options": transportSchema,
"udp_options": transportSchema,
"stateless": {
Type: schema.TypeBool,
Optional: true,
Default: false,
},
},
},
},
Expand Down
15 changes: 7 additions & 8 deletions core/security_list_resource_crud.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@
package core

import (
"time"
"fmt"

"github.com/MustWin/baremetal-sdk-go"
"github.com/MustWin/terraform-Oracle-BareMetal-Provider/crud"
)
Expand Down Expand Up @@ -35,11 +32,6 @@ func (s *SecurityListResourceCrud) DeletedTarget() []string {
return []string{baremetal.ResourceTerminated}
}

func (s *SecurityListResourceCrud) ExtraWaitPostDelete() time.Duration {
fmt.Println("=====================================")
return time.Duration(0 * time.Second)
}

func (s *SecurityListResourceCrud) State() string {
return s.Res.State
}
Expand Down Expand Up @@ -91,6 +83,7 @@ func (s *SecurityListResourceCrud) SetData() {
egressRule.ICMPOptions,
egressRule.TCPOptions,
egressRule.UDPOptions,
&egressRule.IsStateless,
)
confEgressRules = append(confEgressRules, confEgressRule)
}
Expand All @@ -106,6 +99,7 @@ func (s *SecurityListResourceCrud) SetData() {
ingressRule.ICMPOptions,
ingressRule.TCPOptions,
ingressRule.UDPOptions,
nil,
)
confIngressRules = append(confIngressRules, confIngressRule)
}
Expand All @@ -131,6 +125,7 @@ func (s *SecurityListResourceCrud) buildEgressRules() (sdkRules []baremetal.Egre
Protocol: confRule["protocol"].(string),
TCPOptions: s.buildTCPOptions(confRule),
UDPOptions: s.buildUDPOptions(confRule),
IsStateless: confRule["stateless"].(bool),
}

sdkRules = append(sdkRules, sdkRule)
Expand Down Expand Up @@ -218,6 +213,7 @@ func buildConfRule(
icmpOpts *baremetal.ICMPOptions,
tcpOpts *baremetal.TCPOptions,
udpOpts *baremetal.UDPOptions,
stateless *bool,
) map[string]interface{} {
confRule["protocol"] = protocol
if icmpOpts != nil {
Expand All @@ -229,5 +225,8 @@ func buildConfRule(
if udpOpts != nil {
confRule["udp_options"] = buildConfTransportOptions(udpOpts.DestinationPortRange)
}
if stateless != nil {
confRule["stateless"] = *stateless
}
return confRule
}
4 changes: 1 addition & 3 deletions core/subnet_resource_crud.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ package core

import (
"time"
"fmt"

"github.com/MustWin/baremetal-sdk-go"
"github.com/MustWin/terraform-Oracle-BareMetal-Provider/crud"
Expand Down Expand Up @@ -36,8 +35,7 @@ func (s *SubnetResourceCrud) DeletedTarget() []string {
}

func (s *SubnetResourceCrud) ExtraWaitPostDelete() time.Duration {
fmt.Println("=====================================")
return time.Duration(10 * time.Second)
return time.Duration(15 * time.Second)
}

func (s *SubnetResourceCrud) Create() (e error) {
Expand Down
4 changes: 0 additions & 4 deletions core/virtual_network_resource_crud.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,6 @@ func (s *VirtualNetworkResourceCrud) DeletedTarget() []string {
return []string{baremetal.ResourceTerminated}
}

func (s *VirtualNetworkResourceCrud) State() string {
return s.Res.State
}

func (s *VirtualNetworkResourceCrud) Create() (e error) {
cidrBlock := s.D.Get("cidr_block").(string)
compartmentID := s.D.Get("compartment_id").(string)
Expand Down
34 changes: 25 additions & 9 deletions core_instance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import (
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/terraform"

"github.com/MustWin/terraform-Oracle-BareMetal-Provider/core"
"github.com/MustWin/terraform-Oracle-BareMetal-Provider/crud"
"github.com/stretchr/testify/suite"
)

Expand Down Expand Up @@ -64,7 +66,7 @@ func (s *ResourceCoreInstanceTestSuite) SetupTest() {
CompartmentID: "compartment_id",
DisplayName: "display_name",
ID: "id",
ImageID: "imageid",
ImageID: "imageid",
Metadata: map[string]string{
"ssh_authorized_keys": "mypublickey",
},
Expand All @@ -76,7 +78,8 @@ func (s *ResourceCoreInstanceTestSuite) SetupTest() {
s.Res.ETag = "etag"
s.Res.RequestID = "opcrequestid"

s.DeletedRes = s.Res
s.DeletedRes = &baremetal.Instance{}
*s.DeletedRes = *s.Res
s.DeletedRes.State = baremetal.ResourceTerminated

opts := &baremetal.LaunchInstanceOptions{}
Expand All @@ -94,7 +97,8 @@ func (s *ResourceCoreInstanceTestSuite) SetupTest() {
}

func (s *ResourceCoreInstanceTestSuite) TestCreateResourceCoreInstance() {
s.Client.On("GetInstance", "id").Return(s.Res, nil)
s.Client.On("GetInstance", "id").Return(s.Res, nil).Twice()
s.Client.On("GetInstance", "id").Return(s.DeletedRes, nil)

resource.UnitTest(s.T(), resource.TestCase{
Providers: s.Providers,
Expand All @@ -116,7 +120,8 @@ func (s *ResourceCoreInstanceTestSuite) TestCreateResourceCoreInstance() {
}

func (s *ResourceCoreInstanceTestSuite) TestCreateResourceCoreInstanceWithoutDisplayName() {
s.Client.On("GetInstance", "id").Return(s.Res, nil)
s.Client.On("GetInstance", "id").Return(s.Res, nil).Twice()
s.Client.On("GetInstance", "id").Return(s.DeletedRes, nil)

s.Config = `
resource "baremetal_core_instance" "t" {
Expand Down Expand Up @@ -180,7 +185,7 @@ func (s ResourceCoreInstanceTestSuite) TestUpdateInstanceDisplayName() {
CompartmentID: "compartment_id",
DisplayName: "new_display_name",
ID: "id",
ImageID: "imageid",
ImageID: "imageid",
Metadata: map[string]string{
"ssh_authorized_keys": "mypublickey",
},
Expand All @@ -195,7 +200,8 @@ func (s ResourceCoreInstanceTestSuite) TestUpdateInstanceDisplayName() {
opts := &baremetal.UpdateOptions{}
opts.DisplayName = "new_display_name"
s.Client.On("UpdateInstance", "id", opts).Return(res, nil)
s.Client.On("GetInstance", "id").Return(res, nil)
s.Client.On("GetInstance", "id").Return(res, nil).Times(2)
s.Client.On("GetInstance", "id").Return(s.DeletedRes, nil).Times(2)

resource.UnitTest(s.T(), resource.TestCase{
Providers: s.Providers,
Expand All @@ -214,7 +220,7 @@ func (s ResourceCoreInstanceTestSuite) TestUpdateInstanceDisplayName() {
}

func (s ResourceCoreInstanceTestSuite) TestUpdateAvailabilityDomainForcesNewInstance() {
s.Client.On("GetInstance", "id").Return(s.Res, nil)
s.Client.On("GetInstance", "id").Return(s.Res, nil).Times(2)

config := `
resource "baremetal_core_instance" "t" {
Expand All @@ -237,7 +243,7 @@ func (s ResourceCoreInstanceTestSuite) TestUpdateAvailabilityDomainForcesNewInst
CompartmentID: "compartment_id",
DisplayName: "display_name",
ID: "new_id",
ImageID: "imageid",
ImageID: "imageid",
Metadata: map[string]string{
"ssh_authorized_keys": "mypublickey",
},
Expand All @@ -261,8 +267,10 @@ func (s ResourceCoreInstanceTestSuite) TestUpdateAvailabilityDomainForcesNewInst
"subnetid",
opts).Return(res, nil)

s.Client.On("GetInstance", res.ID).Return(res, nil)
s.Client.On("GetInstance", s.Res.ID).Return(s.DeletedRes, nil)
s.Client.On("GetInstance", res.ID).Return(res, nil).Times(2)
s.Client.On("TerminateInstance", res.ID, (*baremetal.IfMatchOptions)(nil)).Return(nil)
s.Client.On("GetInstance", "new_id").Return(s.DeletedRes, nil).Twice()

resource.UnitTest(s.T(), resource.TestCase{
Providers: s.Providers,
Expand Down Expand Up @@ -300,6 +308,14 @@ func (s *ResourceCoreInstanceTestSuite) TestTerminateInstance() {
s.Client.AssertCalled(s.T(), "TerminateInstance", "id", (*baremetal.IfMatchOptions)(nil))
}

func TestIsStatefulResource(t *testing.T) {
var sr crud.StatefulResource
sr = &core.InstanceResourceCrud{}
if sr == nil {
t.Fail()
}
}

func TestResourceCoreInstanceTestSuite(t *testing.T) {
suite.Run(t, new(ResourceCoreInstanceTestSuite))
}
22 changes: 17 additions & 5 deletions core_internet_gateway_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import (
"github.com/hashicorp/terraform/terraform"

"github.com/stretchr/testify/suite"
// "errors"
"errors"
)

type ResourceCoreInternetGatewayTestSuite struct {
Expand Down Expand Up @@ -65,7 +67,8 @@ func (s *ResourceCoreInternetGatewayTestSuite) SetupTest() {
s.Res.ETag = "etag"
s.Res.RequestID = "requestid"

s.DeletedRes = s.Res
s.DeletedRes = &baremetal.InternetGateway{}
*s.DeletedRes = *s.Res
s.DeletedRes.State = baremetal.ResourceTerminated

opts := &baremetal.CreateOptions{}
Expand All @@ -80,7 +83,8 @@ func (s *ResourceCoreInternetGatewayTestSuite) SetupTest() {
}

func (s *ResourceCoreInternetGatewayTestSuite) TestCreateResourceCoreInternetGateway() {
s.Client.On("GetInternetGateway", "id").Return(s.Res, nil)
s.Client.On("GetInternetGateway", "id").Return(s.Res, nil).Times(2)
s.Client.On("GetInternetGateway", "id").Return(s.DeletedRes, nil).Times(2)

resource.UnitTest(s.T(), resource.TestCase{
Providers: s.Providers,
Expand All @@ -100,7 +104,8 @@ func (s *ResourceCoreInternetGatewayTestSuite) TestCreateResourceCoreInternetGat
}

func (s ResourceCoreInternetGatewayTestSuite) TestUpdateCompartmentIDForcesNewInternetGateway() {
s.Client.On("GetInternetGateway", "id").Return(s.Res, nil)
s.Client.On("GetInternetGateway", s.Res.ID).Return(s.Res, nil).Times(2)
s.Client.On("GetInternetGateway", s.Res.ID).Return(s.DeletedRes, nil).Times(2)

config := `
resource "baremetal_core_internet_gateway" "t" {
Expand All @@ -115,7 +120,7 @@ func (s ResourceCoreInternetGatewayTestSuite) TestUpdateCompartmentIDForcesNewIn
res := &baremetal.InternetGateway{
CompartmentID: "new_compartment_id",
DisplayName: "display_name",
ID: "id2",
ID: "id",
IsEnabled: true,
State: baremetal.ResourceAvailable,
ModifiedTime: s.TimeCreated,
Expand All @@ -124,6 +129,10 @@ func (s ResourceCoreInternetGatewayTestSuite) TestUpdateCompartmentIDForcesNewIn
s.Res.ETag = "etag"
s.Res.RequestID = "requestid"

delRes := &baremetal.InternetGateway{}
*delRes = *res
delRes.State = baremetal.ResourceTerminated

opts := &baremetal.CreateOptions{}
opts.DisplayName = "display_name"
s.Client.On(
Expand All @@ -133,9 +142,12 @@ func (s ResourceCoreInternetGatewayTestSuite) TestUpdateCompartmentIDForcesNewIn
res.IsEnabled,
opts).Return(res, nil)

s.Client.On("GetInternetGateway", res.ID).Return(res, nil)
s.Client.On("DeleteInternetGateway", res.ID, (*baremetal.IfMatchOptions)(nil)).Return(nil)

s.Client.On("GetInternetGateway", res.ID).Return(res, nil).Times(2)
s.Client.On("GetInternetGateway", res.ID).Return(delRes, nil).Once()
s.Client.On("GetInternetGateway", res.ID).Return(nil, errors.New("blah does not exist"))

resource.UnitTest(s.T(), resource.TestCase{
Providers: s.Providers,
Steps: []resource.TestStep{
Expand Down
7 changes: 5 additions & 2 deletions core_security_list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import (
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/terraform"

"github.com/stretchr/testify/suite"
"github.com/MustWin/terraform-Oracle-BareMetal-Provider/crud"
"github.com/stretchr/testify/suite"
)

type ResourceCoreSecurityListTestSuite struct {
Expand Down Expand Up @@ -59,6 +59,7 @@ func (s *ResourceCoreSecurityListTestSuite) SetupTest() {
"type" = 2
}
protocol = "protocol"
stateless = true
}
ingress_security_rules {
tcp_options {
Expand All @@ -79,6 +80,7 @@ func (s *ResourceCoreSecurityListTestSuite) SetupTest() {
Destination: "destination",
ICMPOptions: &baremetal.ICMPOptions{Code: 1, Type: 2},
Protocol: "protocol",
IsStateless: true,
},
}
ingressRules := []baremetal.IngressSecurityRule{
Expand All @@ -104,7 +106,6 @@ func (s *ResourceCoreSecurityListTestSuite) SetupTest() {
s.Res.ETag = "etag"
s.Res.RequestID = "opcrequestid"


deletingRes := *s.Res
s.DeletingRes = &deletingRes
s.DeletingRes.State = baremetal.ResourceTerminating
Expand Down Expand Up @@ -140,6 +141,7 @@ func (s *ResourceCoreSecurityListTestSuite) TestCreateResourceCoreSecurityList()
resource.TestCheckResourceAttr(s.ResourceName, "compartment_id", s.Res.CompartmentID),
resource.TestCheckResourceAttr(s.ResourceName, "display_name", s.Res.DisplayName),
resource.TestCheckResourceAttr(s.ResourceName, "egress_security_rules.0.icmp_options.0.code", "1"),
resource.TestCheckResourceAttr(s.ResourceName, "egress_security_rules.0.stateless", "true"),
resource.TestCheckResourceAttr(s.ResourceName, "ingress_security_rules.0.tcp_options.0.max", "2"),
),
},
Expand All @@ -161,6 +163,7 @@ func (s ResourceCoreSecurityListTestSuite) TestUpdateSecurityList() {
"type" = 2
}
protocol = "protocol"
stateless = true
}
ingress_security_rules {
tcp_options {
Expand Down
Loading

0 comments on commit 770baee

Please sign in to comment.