@@ -4,6 +4,10 @@ package oci
44
55import (
66 "context"
7+ "fmt"
8+ "strings"
9+
10+ "github.com/hashicorp/terraform/helper/validation"
711
812 "github.com/hashicorp/terraform/helper/schema"
913
@@ -66,8 +70,14 @@ func OdaOdaInstanceResource() *schema.Resource {
6670 Computed : true ,
6771 },
6872 "state" : {
69- Type : schema .TypeString ,
70- Computed : true ,
73+ Type : schema .TypeString ,
74+ Computed : true ,
75+ Optional : true ,
76+ DiffSuppressFunc : EqualIgnoreCaseSuppressDiff ,
77+ ValidateFunc : validation .StringInSlice ([]string {
78+ string (oci_oda .OdaInstanceLifecycleStateActive ),
79+ string (oci_oda .OdaInstanceLifecycleStateInactive ),
80+ }, true ),
7181 },
7282 "state_message" : {
7383 Type : schema .TypeString ,
@@ -94,7 +104,30 @@ func createOdaOdaInstance(d *schema.ResourceData, m interface{}) error {
94104 sync .D = d
95105 sync .Client = m .(* OracleClients ).odaClient
96106
97- return CreateResource (d , sync )
107+ var isInactiveRequest = false
108+ if configState , ok := sync .D .GetOkExists ("state" ); ok {
109+ wantedState := oci_oda .OdaInstanceLifecycleStateEnum (strings .ToUpper (configState .(string )))
110+ if wantedState == oci_oda .OdaInstanceLifecycleStateInactive {
111+ isInactiveRequest = true
112+ }
113+ }
114+
115+ if error := CreateResource (d , sync ); error != nil {
116+ return error
117+ }
118+
119+ if isInactiveRequest {
120+ return inactiveOdaIfNeeded (d , sync )
121+ }
122+
123+ return nil
124+ }
125+
126+ func inactiveOdaIfNeeded (d * schema.ResourceData , sync * OdaOdaInstanceResourceCrud ) error {
127+ if err := sync .StopOdaInstance (); err != nil {
128+ return err
129+ }
130+ return ReadResource (sync )
98131}
99132
100133func readOdaOdaInstance (d * schema.ResourceData , m interface {}) error {
@@ -110,7 +143,46 @@ func updateOdaOdaInstance(d *schema.ResourceData, m interface{}) error {
110143 sync .D = d
111144 sync .Client = m .(* OracleClients ).odaClient
112145
113- return UpdateResource (d , sync )
146+ // Start/Stop ODA instance
147+ stateActive , stateInactive := false , false
148+
149+ if sync .D .HasChange ("state" ) {
150+ wantedState := strings .ToUpper (sync .D .Get ("state" ).(string ))
151+ if oci_oda .OdaInstanceLifecycleStateActive == oci_oda .OdaInstanceLifecycleStateEnum (wantedState ) {
152+ stateActive = true
153+ stateInactive = false
154+ } else if oci_oda .OdaInstanceLifecycleStateInactive == oci_oda .OdaInstanceLifecycleStateEnum (wantedState ) {
155+ stateInactive = true
156+ stateActive = false
157+ } else {
158+ return fmt .Errorf ("[ERROR] Invalid state input for update %v" , wantedState )
159+ }
160+ }
161+
162+ if stateActive {
163+ if err := sync .StartOdaInstance (); err != nil {
164+ return err
165+ }
166+ if err := sync .D .Set ("state" , oci_oda .OdaInstanceLifecycleStateActive ); err != nil {
167+ return err
168+ }
169+ }
170+
171+ // when state is inactive, it is invalid to update resource
172+ if err := UpdateResource (d , sync ); err != nil {
173+ return err
174+ }
175+
176+ if stateInactive {
177+ if err := sync .StopOdaInstance (); err != nil {
178+ return err
179+ }
180+ if err := sync .D .Set ("state" , oci_oda .OdaInstanceLifecycleStateInactive ); err != nil {
181+ return err
182+ }
183+ }
184+
185+ return nil
114186}
115187
116188func deleteOdaOdaInstance (d * schema.ResourceData , m interface {}) error {
@@ -344,3 +416,47 @@ func (s *OdaOdaInstanceResourceCrud) updateCompartment(compartment interface{})
344416 }
345417 return nil
346418}
419+
420+ func (s * OdaOdaInstanceResourceCrud ) StartOdaInstance () error {
421+ state := oci_oda .OdaInstanceLifecycleStateActive
422+ if err := s .Get (); err != nil {
423+ return err
424+ }
425+ if s .Res .LifecycleState == state {
426+ fmt .Printf ("[WARN] The ODA instance already in the wanted state: %v" , state )
427+ return nil
428+ }
429+ request := oci_oda.StartOdaInstanceRequest {}
430+
431+ tmp := s .D .Id ()
432+ request .OdaInstanceId = & tmp
433+
434+ if _ , err := s .Client .StartOdaInstance (context .Background (), request ); err != nil {
435+ return err
436+ }
437+ retentionPolicyFunc := func () bool { return s .Res .LifecycleState == state }
438+
439+ return WaitForResourceCondition (s , retentionPolicyFunc , s .D .Timeout (schema .TimeoutUpdate ))
440+ }
441+
442+ func (s * OdaOdaInstanceResourceCrud ) StopOdaInstance () error {
443+ state := oci_oda .OdaInstanceLifecycleStateInactive
444+ if err := s .Get (); err != nil {
445+ return err
446+ }
447+ if s .Res .LifecycleState == state {
448+ fmt .Printf ("[WARN] The ODA instance already in the wanted state: %v" , state )
449+ return nil
450+ }
451+ request := oci_oda.StopOdaInstanceRequest {}
452+
453+ tmp := s .D .Id ()
454+ request .OdaInstanceId = & tmp
455+
456+ if _ , err := s .Client .StopOdaInstance (context .Background (), request ); err != nil {
457+ return err
458+ }
459+ retentionPolicyFunc := func () bool { return s .Res .LifecycleState == state }
460+
461+ return WaitForResourceCondition (s , retentionPolicyFunc , s .D .Timeout (schema .TimeoutUpdate ))
462+ }
0 commit comments