Skip to content

Commit 4202853

Browse files
committed
Support resource discovery for ons resources_subscriptions
1 parent 685c84d commit 4202853

9 files changed

+53
-7
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
- Support for HTTP Header in load balancer rule set
88
- Support for new optional parameters in `oci_core_instance_configuration`
99
- Support for DBaaS One-off patching
10+
- Support resource discovery and import for `ons_subscriptions` resource
1011

1112
## 3.82.0 (June 24, 2020)
1213

oci/export_compartment.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -639,6 +639,11 @@ func getHCLStringFromMap(builder *strings.Builder, sourceAttributes map[string]i
639639
continue
640640
case []interface{}:
641641
switch tfSchema.Type {
642+
case schema.TypeString:
643+
if tfAttribute == "delivery_policy" {
644+
builder.WriteString(fmt.Sprintf("%s = %q\n", tfAttribute, parseDeliveryPolicy(v[0].(interface{}))))
645+
continue
646+
}
642647
case schema.TypeList, schema.TypeSet:
643648
switch elem := tfSchema.Elem.(type) {
644649
case *schema.Resource:

oci/export_definitions.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1303,6 +1303,17 @@ var exportOnsNotificationTopicHints = &TerraformResourceHints{
13031303
},
13041304
}
13051305

1306+
var exportOnsSubscriptionHints = &TerraformResourceHints{
1307+
resourceClass: "oci_ons_subscription",
1308+
datasourceClass: "oci_ons_subscriptions",
1309+
datasourceItemsAttr: "subscriptions",
1310+
resourceAbbreviation: "subscription",
1311+
discoverableLifecycleStates: []string{
1312+
string(oci_ons.SubscriptionLifecycleStatePending),
1313+
string(oci_ons.SubscriptionLifecycleStateActive),
1314+
},
1315+
}
1316+
13061317
var exportOsmanagementManagedInstanceGroupHints = &TerraformResourceHints{
13071318
resourceClass: "oci_osmanagement_managed_instance_group",
13081319
datasourceClass: "oci_osmanagement_managed_instance_groups",

oci/export_graphs.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -674,6 +674,7 @@ var odaResourceGraph = TerraformResourceGraph{
674674
var onsResourceGraph = TerraformResourceGraph{
675675
"oci_identity_compartment": {
676676
{TerraformResourceHints: exportOnsNotificationTopicHints},
677+
{TerraformResourceHints: exportOnsSubscriptionHints},
677678
},
678679
}
679680

oci/ons_subscription_resource.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ func init() {
2020

2121
func OnsSubscriptionResource() *schema.Resource {
2222
return &schema.Resource{
23+
Importer: &schema.ResourceImporter{
24+
State: schema.ImportStatePassthrough,
25+
},
2326
Timeouts: DefaultTimeout,
2427
Create: createOnsSubscription,
2528
Read: readOnsSubscription,

oci/ons_subscription_test.go

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ var (
4646
"topic_id": Representation{repType: Required, create: `${oci_ons_notification_topic.test_notification_topic.id}`},
4747
"defined_tags": Representation{repType: Optional, create: `${map("${oci_identity_tag_namespace.tag-namespace1.name}.${oci_identity_tag.tag1.name}", "value")}`, update: `${map("${oci_identity_tag_namespace.tag-namespace1.name}.${oci_identity_tag.tag1.name}", "updatedValue")}`},
4848
"freeform_tags": Representation{repType: Optional, create: map[string]string{"Department": "Finance"}, update: map[string]string{"Department": "Accounting"}},
49-
"delivery_policy": Representation{repType: Optional, update: `{\"backoffRetryPolicy\":{\"initialDelayInFailureRetry\":60000,\"maxRetryDuration\":7000000,\"policyType\":\"EXPONENTIAL\"}, \"maxReceiveRatePerSecond\" : 0}`},
49+
"delivery_policy": Representation{repType: Optional, update: `{\"backoffRetryPolicy\":{\"maxRetryDuration\":7000000,\"policyType\":\"EXPONENTIAL\"}}`},
5050
}
5151

5252
SubscriptionResourceDependencies = DefinedTagsDependencies +
@@ -116,11 +116,6 @@ func TestOnsSubscriptionResource_basic(t *testing.T) {
116116

117117
func(s *terraform.State) (err error) {
118118
resId, err = fromInstanceState(s, resourceName, "id")
119-
if isEnableExportCompartment, _ := strconv.ParseBool(getEnvSettingWithDefault("enable_export_compartment", "false")); isEnableExportCompartment {
120-
if errExport := testExportCompartmentWithResourceName(&resId, &compartmentId, resourceName); errExport != nil {
121-
return errExport
122-
}
123-
}
124119
return err
125120
},
126121
),
@@ -169,6 +164,11 @@ func TestOnsSubscriptionResource_basic(t *testing.T) {
169164

170165
func(s *terraform.State) (err error) {
171166
resId2, err = fromInstanceState(s, resourceName, "id")
167+
if isEnableExportCompartment, _ := strconv.ParseBool(getEnvSettingWithDefault("enable_export_compartment", "false")); isEnableExportCompartment {
168+
if errExport := testExportCompartmentWithResourceName(&resId2, &compartmentId, resourceName); errExport != nil {
169+
return errExport
170+
}
171+
}
172172
if resId != resId2 {
173173
return fmt.Errorf("Resource recreated when it was supposed to be updated.")
174174
}
@@ -219,6 +219,18 @@ func TestOnsSubscriptionResource_basic(t *testing.T) {
219219
resource.TestCheckResourceAttr(singularDatasourceName, "state", "ACTIVE"),
220220
),
221221
},
222+
// remove singular datasource from previous step so that it doesn't conflict with import tests
223+
{
224+
Config: config + compartmentIdVariableStr + SubscriptionResourceConfig,
225+
},
226+
// verify resource import
227+
{
228+
Config: config,
229+
ImportState: true,
230+
ImportStateVerify: true,
231+
ImportStateVerifyIgnore: []string{},
232+
ResourceName: resourceName,
233+
},
222234
},
223235
})
224236
}

oci/ons_subscriptions_data_source.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package oci
55

66
import (
77
"context"
8+
"fmt"
89
"strconv"
910

1011
"github.com/hashicorp/terraform/helper/schema"
@@ -156,6 +157,13 @@ func (s *OnsSubscriptionsDataSourceCrud) SetData() error {
156157
return nil
157158
}
158159

160+
func parseDeliveryPolicy(policy interface{}) string {
161+
backoffRetryPolicy := policy.(map[string]interface{})["backoff_retry_policy"].([]interface{})
162+
maxRetryDuration := backoffRetryPolicy[0].(map[string]interface{})["max_retry_duration"]
163+
policyType := backoffRetryPolicy[0].(map[string]interface{})["policy_type"]
164+
return fmt.Sprintf("{\"backoffRetryPolicy\":{\"maxRetryDuration\":%v,\"policyType\":\"%v\"}}", maxRetryDuration, policyType)
165+
}
166+
159167
func modifySubscriptionSchema(resourceSchema *schema.Resource) *schema.Resource {
160168
resourceSchema.Schema["delivery_policy"] = &schema.Schema{
161169
Type: schema.TypeList,

website/docs/guides/resource_discovery.html.markdown

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -434,6 +434,7 @@ oda
434434
ons
435435

436436
* oci\_ons\_notification\_topic
437+
* oci\_ons\_subscription
437438

438439
osmanagement
439440

website/docs/r/ons_subscription.html.markdown

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,5 +78,9 @@ The following attributes are exported:
7878

7979
## Import
8080

81-
Import is not supported for this resource.
81+
Subscriptions can be imported using the `id`, e.g.
82+
83+
```
84+
$ terraform import oci_ons_subscription.test_subscription "id"
85+
```
8286

0 commit comments

Comments
 (0)