Skip to content

Commit 937114d

Browse files
committed
Add support for resource discovery for object_storage resources
1 parent c2a54e9 commit 937114d

File tree

7 files changed

+163
-0
lines changed

7 files changed

+163
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
- Added resource discovery support for `oce` resources
1313
- Support resource discovery for `oda` resources
1414
- Support resource discovery for `datascience` resources
15+
- Support resource discovery for `oci_objectstorage_object`, `oci_objectstorage_object_lifecycle_policy`, `oci_objectstorage_preauthrequest` resources
1516

1617
### Fixed
1718
- Fixed plan failure in case of missing required attributes in resource discovery. Placeholder values will be added for missing required attributes and the attributes will be added to `lifecycle ignore_changes`

oci/export_definitions.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1206,6 +1206,12 @@ var exportObjectStorageBucketHints = &TerraformResourceHints{
12061206
requireResourceRefresh: true,
12071207
}
12081208

1209+
var exportObjectStorageObjectLifecyclePolicyHints = &TerraformResourceHints{
1210+
resourceClass: "oci_objectstorage_object_lifecycle_policy",
1211+
datasourceClass: "oci_objectstorage_object_lifecycle_policy",
1212+
resourceAbbreviation: "object_lifecycle_policy",
1213+
}
1214+
12091215
var exportObjectStorageNamespaceHints = &TerraformResourceHints{
12101216
resourceClass: "oci_objectstorage_namespace",
12111217
datasourceClass: "oci_objectstorage_namespace",
@@ -1222,6 +1228,20 @@ var exportOdaOdaInstanceHints = &TerraformResourceHints{
12221228
},
12231229
}
12241230

1231+
var exportObjectStorageObjectHints = &TerraformResourceHints{
1232+
resourceClass: "oci_objectstorage_object",
1233+
datasourceClass: "oci_objectstorage_objects",
1234+
datasourceItemsAttr: "objects",
1235+
resourceAbbreviation: "object",
1236+
}
1237+
1238+
var exportObjectStoragePreauthenticatedRequestHints = &TerraformResourceHints{
1239+
resourceClass: "oci_objectstorage_preauthrequest",
1240+
datasourceClass: "oci_objectstorage_preauthrequests",
1241+
datasourceItemsAttr: "preauthenticated_requests",
1242+
resourceAbbreviation: "preauthenticated_request",
1243+
}
1244+
12251245
var exportOsmanagementManagedInstanceGroupHints = &TerraformResourceHints{
12261246
resourceClass: "oci_osmanagement_managed_instance_group",
12271247
datasourceClass: "oci_osmanagement_managed_instance_groups",

oci/export_graphs.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -596,6 +596,29 @@ var objectStorageResourceGraph = TerraformResourceGraph{
596596
"oci_identity_compartment": {
597597
{TerraformResourceHints: exportObjectStorageNamespaceHints},
598598
},
599+
"oci_objectstorage_bucket": {
600+
{
601+
TerraformResourceHints: exportObjectStorageObjectHints,
602+
datasourceQueryParams: map[string]string{
603+
"bucket": "name",
604+
"namespace": "namespace",
605+
},
606+
},
607+
{
608+
TerraformResourceHints: exportObjectStorageObjectLifecyclePolicyHints,
609+
datasourceQueryParams: map[string]string{
610+
"namespace": "namespace",
611+
"bucket": "name",
612+
},
613+
},
614+
{
615+
TerraformResourceHints: exportObjectStoragePreauthenticatedRequestHints,
616+
datasourceQueryParams: map[string]string{
617+
"namespace": "namespace",
618+
"bucket": "name",
619+
},
620+
},
621+
},
599622
"oci_objectstorage_namespace": {
600623
{
601624
TerraformResourceHints: exportObjectStorageBucketHints,

oci/export_resource_helpers.go

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,11 @@ func init() {
134134
exportObjectStorageNamespaceHints.alwaysExportable = true
135135

136136
exportObjectStorageBucketHints.getIdFn = getObjectStorageBucketId
137+
exportObjectStorageObjectHints.getIdFn = getObjectStorageObjectId
138+
exportObjectStorageObjectHints.requireResourceRefresh = true
139+
exportObjectStorageObjectLifecyclePolicyHints.getIdFn = getObjectStorageObjectLifecyclePolicyId
140+
exportObjectStoragePreauthenticatedRequestHints.getIdFn = getObjectStorageObjectPreauthenticatedRequestId
141+
exportObjectStoragePreauthenticatedRequestHints.processDiscoveredResourcesFn = processObjectStoragePreauthenticatedRequest
137142

138143
exportStreamingStreamHints.processDiscoveredResourcesFn = processStreamingStream
139144

@@ -638,6 +643,93 @@ func getObjectStorageBucketId(resource *OCIResource) (string, error) {
638643
return getBucketCompositeId(name, namespace), nil
639644
}
640645

646+
func getObjectStorageObjectId(resource *OCIResource) (string, error) {
647+
name, ok := resource.sourceAttributes["name"].(string)
648+
if !ok {
649+
return "", fmt.Errorf("[ERROR] unable to find name for object id")
650+
}
651+
652+
bucket, ok := resource.parent.sourceAttributes["name"].(string)
653+
if !ok {
654+
return "", fmt.Errorf("[ERROR] unable to find bucket for object id")
655+
}
656+
657+
namespace, ok := resource.parent.sourceAttributes["namespace"].(string)
658+
if !ok {
659+
return "", fmt.Errorf("[ERROR] unable to find namespace for object id")
660+
}
661+
662+
return getObjectCompositeId(bucket, namespace, name), nil
663+
}
664+
665+
func getObjectStorageObjectLifecyclePolicyId(resource *OCIResource) (string, error) {
666+
bucket, ok := resource.sourceAttributes["bucket"].(string)
667+
if !ok {
668+
return "", fmt.Errorf("[ERROR] unable to find bucket for object Lifecycle Policy id")
669+
}
670+
671+
namespace, ok := resource.sourceAttributes["namespace"].(string)
672+
if !ok {
673+
return "", fmt.Errorf("[ERROR] unable to find namespace for object Lifecycle Policy id")
674+
}
675+
676+
return getObjectLifecyclePolicyCompositeId(bucket, namespace), nil
677+
}
678+
679+
func getObjectStorageObjectPreauthenticatedRequestId(resource *OCIResource) (string, error) {
680+
parId, ok := resource.sourceAttributes["id"].(string)
681+
if !ok {
682+
return "", fmt.Errorf("[ERROR] unable to find parId for PreauthenticatedRequest id")
683+
}
684+
685+
bucket, ok := resource.parent.sourceAttributes["name"].(string)
686+
if !ok {
687+
return "", fmt.Errorf("[ERROR] unable to find bucket for PreauthenticatedRequest id")
688+
}
689+
690+
namespace, ok := resource.parent.sourceAttributes["namespace"].(string)
691+
if !ok {
692+
return "", fmt.Errorf("[ERROR] unable to find namespace for PreauthenticatedRequest id")
693+
}
694+
695+
return getPreauthenticatedRequestCompositeId(bucket, namespace, parId), nil
696+
}
697+
698+
func processObjectStoragePreauthenticatedRequest(clients *OracleClients, resources []*OCIResource) ([]*OCIResource, error) {
699+
for _, index := range resources {
700+
index.sourceAttributes["bucket"] = index.parent.sourceAttributes["name"].(string)
701+
index.sourceAttributes["namespace"] = index.parent.sourceAttributes["namespace"].(string)
702+
}
703+
return resources, nil
704+
}
705+
706+
func getObjectStorageReplicationPolicyId(resource *OCIResource) (string, error) {
707+
replicationId, ok := resource.sourceAttributes["id"].(string)
708+
if !ok {
709+
return "", fmt.Errorf("[ERROR] unable to find replicationId for replication id")
710+
}
711+
712+
bucket, ok := resource.parent.sourceAttributes["name"].(string)
713+
if !ok {
714+
return "", fmt.Errorf("[ERROR] unable to find bucket for replication id")
715+
}
716+
717+
namespace, ok := resource.parent.sourceAttributes["namespace"].(string)
718+
if !ok {
719+
return "", fmt.Errorf("[ERROR] unable to find namespace for replication id")
720+
}
721+
722+
return getReplicationPolicyCompositeId(bucket, namespace, replicationId), nil
723+
}
724+
725+
func processObjectStorageReplicationPolicy(clients *OracleClients, resources []*OCIResource) ([]*OCIResource, error) {
726+
for _, index := range resources {
727+
index.sourceAttributes["bucket"] = index.parent.sourceAttributes["name"].(string)
728+
index.sourceAttributes["namespace"] = index.parent.sourceAttributes["namespace"].(string)
729+
}
730+
return resources, nil
731+
}
732+
641733
func findIdentityTags(clients *OracleClients, tfMeta *TerraformResourceAssociation, parent *OCIResource) ([]*OCIResource, error) {
642734
// List on Tags does not return validator, and resource Read requires tagNamespaceId
643735
// which is also not returned in Summary response. Tags also do not have composite id in state.

oci/objectstorage_preauthrequest_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"context"
88
"fmt"
99
"log"
10+
"strconv"
1011
"testing"
1112
"time"
1213

@@ -72,6 +73,7 @@ func TestObjectStoragePreauthenticatedRequestResource_basic(t *testing.T) {
7273
datasourceName := "data.oci_objectstorage_preauthrequests.test_preauthenticated_requests"
7374
singularDatasourceName := "data.oci_objectstorage_preauthrequest.test_preauthenticated_request"
7475

76+
var resId string
7577
resource.Test(t, resource.TestCase{
7678
PreCheck: func() { testAccPreCheck(t) },
7779
Providers: map[string]terraform.ResourceProvider{
@@ -110,6 +112,16 @@ func TestObjectStoragePreauthenticatedRequestResource_basic(t *testing.T) {
110112
resource.TestCheckResourceAttr(resourceName, "object", "my-test-object-1"),
111113
resource.TestCheckResourceAttrSet(resourceName, "time_created"),
112114
resource.TestCheckResourceAttr(resourceName, "time_expires", expirationTimeForPar.Format(time.RFC3339Nano)),
115+
116+
func(s *terraform.State) (err error) {
117+
resId, err = fromInstanceState(s, resourceName, "id")
118+
if isEnableExportCompartment, _ := strconv.ParseBool(getEnvSettingWithDefault("enable_export_compartment", "false")); isEnableExportCompartment {
119+
if errExport := testExportCompartmentWithResourceName(&resId, &compartmentId, resourceName); errExport != nil {
120+
return errExport
121+
}
122+
}
123+
return err
124+
},
113125
),
114126
},
115127

oci/objectstorage_replication_policy_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"context"
88
"fmt"
99
"log"
10+
"strconv"
1011
"testing"
1112

1213
"github.com/hashicorp/terraform/helper/resource"
@@ -65,6 +66,7 @@ func TestObjectStorageReplicationPolicyResource_basic(t *testing.T) {
6566
datasourceName := "data.oci_objectstorage_replication_policies.test_replication_policies"
6667
singularDatasourceName := "data.oci_objectstorage_replication_policy.test_replication_policy"
6768

69+
var resId string
6870
resource.Test(t, resource.TestCase{
6971
PreCheck: func() { testAccPreCheck(t) },
7072
Providers: map[string]terraform.ResourceProvider{
@@ -102,6 +104,16 @@ func TestObjectStorageReplicationPolicyResource_basic(t *testing.T) {
102104
resource.TestCheckResourceAttrSet(datasourceName, "replication_policies.0.status"),
103105
resource.TestCheckResourceAttrSet(datasourceName, "replication_policies.0.status_message"),
104106
resource.TestCheckResourceAttrSet(datasourceName, "replication_policies.0.time_created"),
107+
108+
func(s *terraform.State) (err error) {
109+
resId, err = fromInstanceState(s, resourceName, "id")
110+
if isEnableExportCompartment, _ := strconv.ParseBool(getEnvSettingWithDefault("enable_export_compartment", "false")); isEnableExportCompartment {
111+
if errExport := testExportCompartmentWithResourceName(&resId, &compartmentId, resourceName); errExport != nil {
112+
return errExport
113+
}
114+
}
115+
return err
116+
},
105117
),
106118
},
107119

website/docs/guides/resource_discovery.html.markdown

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,9 @@ nosql
397397
object_storage
398398

399399
* oci\_objectstorage\_bucket
400+
* oci\_objectstorage\_object\_lifecycle\_policy
401+
* oci\_objectstorage\_object
402+
* oci\_objectstorage\_preauthrequest
400403

401404
oce
402405

0 commit comments

Comments
 (0)