Skip to content

Commit 8c75867

Browse files
jotruonvarmax2511
authored andcommitted
Add ability to Stop/Start a Digital Assistant Instance
1 parent 0809715 commit 8c75867

File tree

4 files changed

+131
-4
lines changed

4 files changed

+131
-4
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
### Added
44
- Support for `description` field in networking routing rules in `oci_core_route_table` and `oci_core_security_list`
5+
- Support for Stop/Start Digital Assistant Instance
56

67
## 3.57.0 (January 09, 2020)
78

examples/oracle_digital_assistant/main.tf

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ variable "fingerprint" {}
66
variable "private_key_path" {}
77
variable "region" {}
88

9+
variable "oda_instance_state" {
10+
default = "INACTIVE"
11+
}
12+
913
variable "compartment_ocid" {}
1014

1115
provider "oci" {
@@ -21,6 +25,9 @@ resource "oci_oda_oda_instance" "TFOdaInstance" {
2125
shape_name = "DEVELOPMENT"
2226
description = "test instance"
2327
display_name = "TestInstance"
28+
29+
#Optional
30+
state = "${var.oda_instance_state}"
2431
}
2532

2633
data "oci_oda_oda_instances" "TFOdaInstances" {

oci/oda_oda_instance_resource.go

Lines changed: 120 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ package oci
44

55
import (
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

100133
func 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

116188
func 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+
}

oci/oda_oda_instance_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ var (
4646
"description": Representation{repType: Optional, create: `description`, update: `description2`},
4747
"display_name": Representation{repType: Optional, create: `displayName`, update: `displayName2`},
4848
"freeform_tags": Representation{repType: Optional, create: map[string]string{"bar-key": "value"}, update: map[string]string{"Department": "Accounting"}},
49+
"state": Representation{repType: Optional, create: `INACTIVE`, update: `ACTIVE`},
4950
}
5051

5152
OdaInstanceResourceDependencies = DefinedTagsDependencies
@@ -112,6 +113,7 @@ func TestOdaOdaInstanceResource_basic(t *testing.T) {
112113
resource.TestCheckResourceAttr(resourceName, "freeform_tags.%", "1"),
113114
resource.TestCheckResourceAttrSet(resourceName, "id"),
114115
resource.TestCheckResourceAttrSet(resourceName, "shape_name"),
116+
resource.TestCheckResourceAttr(resourceName, "state", "INACTIVE"),
115117

116118
func(s *terraform.State) (err error) {
117119
resId, err = fromInstanceState(s, resourceName, "id")
@@ -163,6 +165,7 @@ func TestOdaOdaInstanceResource_basic(t *testing.T) {
163165
resource.TestCheckResourceAttr(resourceName, "freeform_tags.%", "1"),
164166
resource.TestCheckResourceAttrSet(resourceName, "id"),
165167
resource.TestCheckResourceAttrSet(resourceName, "shape_name"),
168+
resource.TestCheckResourceAttr(resourceName, "state", "ACTIVE"),
166169

167170
func(s *terraform.State) (err error) {
168171
resId2, err = fromInstanceState(s, resourceName, "id")

0 commit comments

Comments
 (0)