Skip to content

Commit 1e6a256

Browse files
author
ndordevi
committed
Fix start action in Instance Pool
1 parent 2a211cb commit 1e6a256

File tree

3 files changed

+76
-22
lines changed

3 files changed

+76
-22
lines changed

oci/core_instance_pool_resource.go

Lines changed: 74 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import (
99
"log"
1010
"strings"
1111

12+
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
13+
1214
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
1315
"github.com/hashicorp/terraform-plugin-sdk/helper/validation"
1416

@@ -357,17 +359,7 @@ func (s *CoreInstancePoolResourceCrud) Create() error {
357359
return err
358360
}
359361

360-
desiredStateStr := instancePoolRunningState
361-
if desiredState, ok := s.D.GetOkExists("state"); ok {
362-
desiredStateStr = desiredState.(string)
363-
}
364-
365-
instancePool, err := s.setInstancePoolDesiredState(response.InstancePool.Id, desiredStateStr)
366-
if err != nil {
367-
return err
368-
}
369-
370-
s.Res = instancePool
362+
s.Res = &response.InstancePool
371363

372364
return nil
373365
}
@@ -487,17 +479,23 @@ func (s *CoreInstancePoolResourceCrud) Update() error {
487479
return err
488480
}
489481

490-
desiredStateStr := instancePoolRunningState
491-
if desiredState, ok := s.D.GetOkExists("state"); ok {
492-
desiredStateStr = desiredState.(string)
493-
}
482+
s.Res = &response.InstancePool
494483

495-
instancePool, err := s.setInstancePoolDesiredState(response.InstancePool.Id, desiredStateStr)
496-
if err != nil {
497-
return err
498-
}
484+
if _, ok := s.D.GetOkExists("state"); ok && s.D.HasChange("state") {
485+
oldRaw, newRaw := s.D.GetChange("state")
486+
oldState := strings.ToLower(oldRaw.(string))
487+
newState := strings.ToLower(newRaw.(string))
499488

500-
s.Res = instancePool
489+
if oldState == instancePoolRunningState && newState == instancePoolStoppedState ||
490+
oldState == instancePoolStoppedState && newState == instancePoolRunningState {
491+
instancePool, err := s.setInstancePoolDesiredState(response.InstancePool.Id, newState)
492+
if err != nil {
493+
return err
494+
}
495+
496+
s.Res = instancePool
497+
}
498+
}
501499

502500
return nil
503501
}
@@ -843,6 +841,13 @@ func (s *CoreInstancePoolResourceCrud) updateLoadBalancers(oldRaw interface{}, n
843841
attachLoadBalancerRequest.RequestMetadata.RetryPolicy = getRetryPolicy(s.DisableNotFoundRetries, "core")
844842
attachLoadBalancerRequest.AttachLoadBalancerDetails = newLoadBalancer
845843
_, err := s.Client.AttachLoadBalancer(context.Background(), attachLoadBalancerRequest)
844+
845+
if err != nil {
846+
return err
847+
}
848+
849+
_, err = s.pollForLbOperationCompletion(&id, &attachLoadBalancerRequest.AttachLoadBalancerDetails)
850+
846851
if err != nil {
847852
return err
848853
}
@@ -855,6 +860,13 @@ func (s *CoreInstancePoolResourceCrud) updateLoadBalancers(oldRaw interface{}, n
855860
detachLoadBalancerRequest.InstancePoolId = &id
856861
detachLoadBalancerRequest.BackendSetName = oldLoadbalancer.BackendSetName
857862
_, err := s.Client.DetachLoadBalancer(context.Background(), detachLoadBalancerRequest)
863+
864+
if err != nil {
865+
return err
866+
}
867+
868+
_, err = s.pollForLbOperationCompletion(&id, &oldLoadbalancer)
869+
858870
if err != nil {
859871
return err
860872
}
@@ -876,3 +888,45 @@ func mapToAttachLoadBalancerDetails(item map[string]interface{}) oci_core.Attach
876888

877889
return result
878890
}
891+
892+
func (s *CoreInstancePoolResourceCrud) pollForLbOperationCompletion(poolId *string, lbToTrack *oci_core.AttachLoadBalancerDetails) (*oci_core.InstancePool, error) {
893+
response := oci_core.GetInstancePoolResponse{}
894+
stateConf := &resource.StateChangeConf{
895+
Pending: []string{
896+
string(oci_core.InstancePoolLoadBalancerAttachmentLifecycleStateAttaching),
897+
string(oci_core.InstancePoolLoadBalancerAttachmentLifecycleStateDetaching),
898+
},
899+
Target: []string{
900+
string(oci_core.InstancePoolLoadBalancerAttachmentLifecycleStateAttached),
901+
string(oci_core.InstancePoolLoadBalancerAttachmentLifecycleStateDetached),
902+
},
903+
Refresh: func() (interface{}, string, error) {
904+
var err error
905+
906+
response, err = s.Client.GetInstancePool(context.Background(),
907+
oci_core.GetInstancePoolRequest{
908+
InstancePoolId: poolId,
909+
})
910+
911+
ip := response.InstancePool
912+
loadBalancers := ip.LoadBalancers
913+
914+
for i := 0; i < len(loadBalancers); i++ {
915+
if *loadBalancers[i].LoadBalancerId == *lbToTrack.LoadBalancerId &&
916+
*loadBalancers[i].BackendSetName == *lbToTrack.BackendSetName {
917+
return ip, string(loadBalancers[i].LifecycleState), err
918+
}
919+
}
920+
921+
// if there is no match than fail
922+
return ip, "Not found", fmt.Errorf("load balancer attachment not found")
923+
},
924+
Timeout: s.D.Timeout(schema.TimeoutUpdate),
925+
}
926+
927+
if _, e := stateConf.WaitForState(); e != nil {
928+
return &response.InstancePool, e
929+
}
930+
931+
return &response.InstancePool, nil
932+
}

oci/core_instance_pool_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -370,8 +370,8 @@ func TestCoreInstancePoolResource_basic(t *testing.T) {
370370
resource.TestCheckResourceAttrSet(resourceName, "load_balancers.0.id"),
371371
resource.TestCheckResourceAttrSet(resourceName, "load_balancers.0.instance_pool_id"),
372372
resource.TestCheckResourceAttrSet(resourceName, "load_balancers.0.load_balancer_id"),
373-
resource.TestCheckResourceAttr(resourceName, "load_balancers.0.port", "10"),
374373
resource.TestCheckResourceAttrSet(resourceName, "load_balancers.0.state"),
374+
resource.TestCheckResourceAttr(resourceName, "load_balancers.0.port", "10"),
375375
resource.TestCheckResourceAttr(resourceName, "load_balancers.0.vnic_selection", "PrimaryVnic"),
376376
resource.TestCheckResourceAttr(resourceName, "placement_configurations.#", "1"),
377377
resource.TestCheckResourceAttrSet(resourceName, "placement_configurations.0.availability_domain"),

website/docs/r/core_instance_pool.html.markdown

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ The following arguments are supported:
8282
* `display_name` - (Optional) (Updatable) The display name of the VNIC. This is also use to match against the instance configuration defined secondary VNIC.
8383
* `subnet_id` - (Required) (Updatable) The subnet [OCID](https://docs.cloud.oracle.com/iaas/Content/General/Concepts/identifiers.htm) for the secondary VNIC.
8484
* `size` - (Required) (Updatable) The number of instances that should be in the instance pool. Modifying this value will override the size of the instance pool. If the instance pool is linked with autoscaling configuration, autoscaling configuration could resize the instance pool at a later point. The instance pool's actual size may differ from the configured size if it is associated with an autoscaling configuration. For the actual size of the instance pool, refer to the `actual_size` attribute.
85-
* `state` - (Optional) (Updatable) The target state for the instance pool. Could be set to RUNNING or STOPPED.
85+
* `state` - (Optional) (Updatable) The target state for the instance pool update operation (ignored at create time and should not be set). Could be set to RUNNING or STOPPED.
8686

8787

8888
** IMPORTANT **

0 commit comments

Comments
 (0)