Skip to content

Commit

Permalink
Merge pull request #112 from MustWin/master
Browse files Browse the repository at this point in the history
Latest Updates
  • Loading branch information
codycushing authored May 20, 2017
2 parents 790bdbd + f5a4210 commit c9d8d56
Show file tree
Hide file tree
Showing 128 changed files with 2,054 additions and 7,739 deletions.
8 changes: 4 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ clean:
fmt:
gofmt -w $(GOFMT_FILES)

test:
TF_ORACLE_ENV=test go test -v
test_acceptance_debug:
TF_LOG=DEBUG DEBUG=true TF_ORACLE_ENV=test TF_ACC=1 go test -v -timeout 120m

test_acceptance:
TF_ORACLE_ENV=test TF_ACC=1 go test -v
TF_ORACLE_ENV=test TF_ACC=1 go test -v -timeout 120m

build: test
build:
go build -o terraform-provider-baremetal

cross: test_acceptance
Expand Down
10 changes: 5 additions & 5 deletions client/bare_metal_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,11 +120,11 @@ type BareMetalClient interface {
ListCompartments(opts *baremetal.ListOptions) (resources *baremetal.ListCompartments, e error)
ListConsoleHistories(compartmentID string, opts *baremetal.ListConsoleHistoriesOptions) (icHistories *baremetal.ListConsoleHistories, e error)
ListCpes(compartmentID string, opts *baremetal.ListOptions) (cpes *baremetal.ListCpes, e error)
ListDBHomes(compartmentID, dbSystemID string, limit uint64, opts *baremetal.PageListOptions) (res *baremetal.ListDBHomes, e error)
ListDBNodes(compartmentID, dbSystemID string, limit uint64, opts *baremetal.PageListOptions) (resources *baremetal.ListDBNodes, e error)
ListDBSystemShapes(availabilityDomain, compartmentID string, limit uint64, opts *baremetal.PageListOptions) (resources *baremetal.ListDBSystemShapes, e error)
ListDBSystems(compartmentID string, limit uint64, opts *baremetal.PageListOptions) (res *baremetal.ListDBSystems, e error)
ListDBVersions(compartmentID string, limit uint64, opts *baremetal.PageListOptions) (resources *baremetal.ListDBVersions, e error)
ListDBHomes(compartmentID, dbSystemID string, opts *baremetal.ListOptions) (res *baremetal.ListDBHomes, e error)
ListDBNodes(compartmentID, dbSystemID string, opts *baremetal.ListOptions) (resources *baremetal.ListDBNodes, e error)
ListDBSystemShapes(availabilityDomain, compartmentID string, opts *baremetal.ListOptions) (resources *baremetal.ListDBSystemShapes, e error)
ListDBSystems(compartmentID string, opts *baremetal.ListOptions) (res *baremetal.ListDBSystems, e error)
ListDBVersions(compartmentID string, opts *baremetal.ListOptions) (resources *baremetal.ListDBVersions, e error)
ListDHCPOptions(compartmentID, vcnID string, opts *baremetal.ListOptions) (res *baremetal.ListDHCPOptions, e error)
ListDatabases(compartmentID, dbHomeID string, limit uint64, opts *baremetal.PageListOptions) (resources *baremetal.ListDatabases, e error)
ListDrgAttachments(compartmentID string, opts *baremetal.ListDrgAttachmentsOptions) (res *baremetal.ListDrgAttachments, e error)
Expand Down
74 changes: 38 additions & 36 deletions client/mocks/bare_metal_client.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package mocks

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

import client "github.com/oracle/terraform-provider-baremetal/client"
import mock "github.com/stretchr/testify/mock"

// BareMetalClient is an autogenerated mock type for the BareMetalClient type
Expand Down Expand Up @@ -2215,114 +2215,114 @@ func (_m *BareMetalClient) ListCpes(compartmentID string, opts *baremetal.ListOp
return r0, r1
}

// ListDBHomes provides a mock function with given fields: compartmentID, dbSystemID, limit, opts
func (_m *BareMetalClient) ListDBHomes(compartmentID string, dbSystemID string, limit uint64, opts *baremetal.PageListOptions) (*baremetal.ListDBHomes, error) {
ret := _m.Called(compartmentID, dbSystemID, limit, opts)
// ListDBHomes provides a mock function with given fields: compartmentID, dbSystemID, opts
func (_m *BareMetalClient) ListDBHomes(compartmentID string, dbSystemID string, opts *baremetal.ListOptions) (*baremetal.ListDBHomes, error) {
ret := _m.Called(compartmentID, dbSystemID, opts)

var r0 *baremetal.ListDBHomes
if rf, ok := ret.Get(0).(func(string, string, uint64, *baremetal.PageListOptions) *baremetal.ListDBHomes); ok {
r0 = rf(compartmentID, dbSystemID, limit, opts)
if rf, ok := ret.Get(0).(func(string, string, *baremetal.ListOptions) *baremetal.ListDBHomes); ok {
r0 = rf(compartmentID, dbSystemID, opts)
} else {
if ret.Get(0) != nil {
r0 = ret.Get(0).(*baremetal.ListDBHomes)
}
}

var r1 error
if rf, ok := ret.Get(1).(func(string, string, uint64, *baremetal.PageListOptions) error); ok {
r1 = rf(compartmentID, dbSystemID, limit, opts)
if rf, ok := ret.Get(1).(func(string, string, *baremetal.ListOptions) error); ok {
r1 = rf(compartmentID, dbSystemID, opts)
} else {
r1 = ret.Error(1)
}

return r0, r1
}

// ListDBNodes provides a mock function with given fields: compartmentID, dbSystemID, limit, opts
func (_m *BareMetalClient) ListDBNodes(compartmentID string, dbSystemID string, limit uint64, opts *baremetal.PageListOptions) (*baremetal.ListDBNodes, error) {
ret := _m.Called(compartmentID, dbSystemID, limit, opts)
// ListDBNodes provides a mock function with given fields: compartmentID, dbSystemID, opts
func (_m *BareMetalClient) ListDBNodes(compartmentID string, dbSystemID string, opts *baremetal.ListOptions) (*baremetal.ListDBNodes, error) {
ret := _m.Called(compartmentID, dbSystemID, opts)

var r0 *baremetal.ListDBNodes
if rf, ok := ret.Get(0).(func(string, string, uint64, *baremetal.PageListOptions) *baremetal.ListDBNodes); ok {
r0 = rf(compartmentID, dbSystemID, limit, opts)
if rf, ok := ret.Get(0).(func(string, string, *baremetal.ListOptions) *baremetal.ListDBNodes); ok {
r0 = rf(compartmentID, dbSystemID, opts)
} else {
if ret.Get(0) != nil {
r0 = ret.Get(0).(*baremetal.ListDBNodes)
}
}

var r1 error
if rf, ok := ret.Get(1).(func(string, string, uint64, *baremetal.PageListOptions) error); ok {
r1 = rf(compartmentID, dbSystemID, limit, opts)
if rf, ok := ret.Get(1).(func(string, string, *baremetal.ListOptions) error); ok {
r1 = rf(compartmentID, dbSystemID, opts)
} else {
r1 = ret.Error(1)
}

return r0, r1
}

// ListDBSystemShapes provides a mock function with given fields: availabilityDomain, compartmentID, limit, opts
func (_m *BareMetalClient) ListDBSystemShapes(availabilityDomain string, compartmentID string, limit uint64, opts *baremetal.PageListOptions) (*baremetal.ListDBSystemShapes, error) {
ret := _m.Called(availabilityDomain, compartmentID, limit, opts)
// ListDBSystemShapes provides a mock function with given fields: availabilityDomain, compartmentID, opts
func (_m *BareMetalClient) ListDBSystemShapes(availabilityDomain string, compartmentID string, opts *baremetal.ListOptions) (*baremetal.ListDBSystemShapes, error) {
ret := _m.Called(availabilityDomain, compartmentID, opts)

var r0 *baremetal.ListDBSystemShapes
if rf, ok := ret.Get(0).(func(string, string, uint64, *baremetal.PageListOptions) *baremetal.ListDBSystemShapes); ok {
r0 = rf(availabilityDomain, compartmentID, limit, opts)
if rf, ok := ret.Get(0).(func(string, string, *baremetal.ListOptions) *baremetal.ListDBSystemShapes); ok {
r0 = rf(availabilityDomain, compartmentID, opts)
} else {
if ret.Get(0) != nil {
r0 = ret.Get(0).(*baremetal.ListDBSystemShapes)
}
}

var r1 error
if rf, ok := ret.Get(1).(func(string, string, uint64, *baremetal.PageListOptions) error); ok {
r1 = rf(availabilityDomain, compartmentID, limit, opts)
if rf, ok := ret.Get(1).(func(string, string, *baremetal.ListOptions) error); ok {
r1 = rf(availabilityDomain, compartmentID, opts)
} else {
r1 = ret.Error(1)
}

return r0, r1
}

// ListDBSystems provides a mock function with given fields: compartmentID, limit, opts
func (_m *BareMetalClient) ListDBSystems(compartmentID string, limit uint64, opts *baremetal.PageListOptions) (*baremetal.ListDBSystems, error) {
ret := _m.Called(compartmentID, limit, opts)
// ListDBSystems provides a mock function with given fields: compartmentID, opts
func (_m *BareMetalClient) ListDBSystems(compartmentID string, opts *baremetal.ListOptions) (*baremetal.ListDBSystems, error) {
ret := _m.Called(compartmentID, opts)

var r0 *baremetal.ListDBSystems
if rf, ok := ret.Get(0).(func(string, uint64, *baremetal.PageListOptions) *baremetal.ListDBSystems); ok {
r0 = rf(compartmentID, limit, opts)
if rf, ok := ret.Get(0).(func(string, *baremetal.ListOptions) *baremetal.ListDBSystems); ok {
r0 = rf(compartmentID, opts)
} else {
if ret.Get(0) != nil {
r0 = ret.Get(0).(*baremetal.ListDBSystems)
}
}

var r1 error
if rf, ok := ret.Get(1).(func(string, uint64, *baremetal.PageListOptions) error); ok {
r1 = rf(compartmentID, limit, opts)
if rf, ok := ret.Get(1).(func(string, *baremetal.ListOptions) error); ok {
r1 = rf(compartmentID, opts)
} else {
r1 = ret.Error(1)
}

return r0, r1
}

// ListDBVersions provides a mock function with given fields: compartmentID, limit, opts
func (_m *BareMetalClient) ListDBVersions(compartmentID string, limit uint64, opts *baremetal.PageListOptions) (*baremetal.ListDBVersions, error) {
ret := _m.Called(compartmentID, limit, opts)
// ListDBVersions provides a mock function with given fields: compartmentID, opts
func (_m *BareMetalClient) ListDBVersions(compartmentID string, opts *baremetal.ListOptions) (*baremetal.ListDBVersions, error) {
ret := _m.Called(compartmentID, opts)

var r0 *baremetal.ListDBVersions
if rf, ok := ret.Get(0).(func(string, uint64, *baremetal.PageListOptions) *baremetal.ListDBVersions); ok {
r0 = rf(compartmentID, limit, opts)
if rf, ok := ret.Get(0).(func(string, *baremetal.ListOptions) *baremetal.ListDBVersions); ok {
r0 = rf(compartmentID, opts)
} else {
if ret.Get(0) != nil {
r0 = ret.Get(0).(*baremetal.ListDBVersions)
}
}

var r1 error
if rf, ok := ret.Get(1).(func(string, uint64, *baremetal.PageListOptions) error); ok {
r1 = rf(compartmentID, limit, opts)
if rf, ok := ret.Get(1).(func(string, *baremetal.ListOptions) error); ok {
r1 = rf(compartmentID, opts)
} else {
r1 = ret.Error(1)
}
Expand Down Expand Up @@ -3658,3 +3658,5 @@ func (_m *BareMetalClient) UploadAPIKey(userID string, key string, opts *baremet

return r0, r1
}

var _ client.BareMetalClient = (*BareMetalClient)(nil)
79 changes: 73 additions & 6 deletions crud/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ package crud

import (
"log"
"os"
"reflect"
"strings"
"time"
Expand All @@ -13,6 +12,9 @@ import (
"github.com/hashicorp/terraform/helper/schema"

"github.com/oracle/terraform-provider-baremetal/client"
"github.com/MustWin/baremetal-sdk-go"
"errors"
"strconv"
)

var (
Expand Down Expand Up @@ -66,13 +68,73 @@ func (s *BaseCrud) State() string {
}

func handleMissingResourceError(sync ResourceVoider, err *error) {
if err != nil && strings.Contains((*err).Error(), "does not exist") {
if err != nil && (
strings.Contains((*err).Error(), "does not exist") ||
strings.Contains((*err).Error(), " not present in ") ||
strings.Contains((*err).Error(), "resource not found") ||
(strings.Contains((*err).Error(), "Load balancer") && strings.Contains((*err).Error(), " has no "))) {
log.Println("[DEBUG] Object does not exist, voiding resource and nullifying error")
sync.VoidState()
*err = nil
}
}

func LoadBalancerResourceID(res interface{}, workReq *baremetal.WorkRequest) (id *string, workReqSucceeded bool) {
v := reflect.ValueOf(res).Elem()
if v.IsValid() {
// This is super fugly. It's this way because this API has no convention for ID formats.

// Load balancer
id := v.FieldByName("ID")
if id.IsValid() {
s := id.String()
return &s, false
}
// backendset, certificate, listener
name := v.FieldByName("Name")
if name.IsValid() {
s := name.String()
return &s, false
}
// backend
ip := v.FieldByName("ip_address")
port := v.FieldByName("port")
if ip.IsValid() && port.IsValid() {
s := ip.String() + ":" + strconv.Itoa(int(int(port.Int())))
return &s, false
}
}
if workReq != nil {
if workReq.State == baremetal.WorkRequestSucceeded {
return nil, true
} else {
return &workReq.ID, false
}
}
return nil, false
}

func LoadBalancerResourceGet(s BaseCrud, workReq *baremetal.WorkRequest) (id string, stillWorking bool, err error) {
id = s.D.Id()
log.Printf("================== ID in LoadbalancerResourceGet: %s\n", id)
// NOTE: if the id is for a work request, refresh its state and loadBalancerID.
if strings.HasPrefix(id, "ocid1.loadbalancerworkrequest.") {
updatedWorkReq, err := s.Client.GetWorkRequest(id, nil)
if err != nil {
return "", false, err
}
if workReq != nil {
*workReq = *updatedWorkReq
s.D.Set("state", workReq.State)
if workReq.State == baremetal.WorkRequestSucceeded {
return "", false, nil
}
}
return "", true, nil
}
return id, false, nil
}

func CreateResource(d *schema.ResourceData, sync ResourceCreator) (e error) {
if e = sync.Create(); e != nil {
// Check for conflicts and retry
Expand All @@ -95,6 +157,10 @@ func CreateResource(d *schema.ResourceData, sync ResourceCreator) (e error) {
d.SetId(sync.ID())
sync.SetData()

if ew, waitOK := sync.(ExtraWaitPostCreateDelete); waitOK {
time.Sleep(ew.ExtraWaitPostCreateDelete())
}

return
}

Expand Down Expand Up @@ -133,10 +199,8 @@ func DeleteResource(d *schema.ResourceData, sync ResourceDeleter) (e error) {

}

if ew, waitOK := sync.(ExtraWaitPostDelete); waitOK {
if os.Getenv("TF_ORACLE_ENV") != "test" {
time.Sleep(ew.ExtraWaitPostDelete())
}
if ew, waitOK := sync.(ExtraWaitPostCreateDelete); waitOK {
time.Sleep(ew.ExtraWaitPostCreateDelete())
}

if e == nil {
Expand Down Expand Up @@ -179,6 +243,9 @@ func waitForStateRefresh(sync StatefulResource, timeout time.Duration, pending,
handleMissingResourceError(sync, &e)
return
}
if sync.State() == baremetal.ResourceFailed {
return errors.New("Resource creation failed, state FAILED")
}

return
}
Expand Down
4 changes: 2 additions & 2 deletions crud/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ type ResourceDeleter interface {
// of that fact. This facility allows us to add an artificial delay for
// resources that need a little time to let the oracle API backend catch
// up with reality.
type ExtraWaitPostDelete interface {
ExtraWaitPostDelete() time.Duration
type ExtraWaitPostCreateDelete interface {
ExtraWaitPostCreateDelete() time.Duration
}

type StatefulResource interface {
Expand Down
Loading

0 comments on commit c9d8d56

Please sign in to comment.