Skip to content

Commit abe9904

Browse files
committed
Support resource discovery for kms resources
1 parent cb30d5d commit abe9904

9 files changed

+166
-8
lines changed

oci/export_compartment.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -200,20 +200,21 @@ func getExportConfig(d *schema.ResourceData) (interface{}, error) {
200200
return nil, err
201201
}
202202
exportConfigProvider = sdkConfigProvider
203-
// beware: global variable `configureClient` set here--used elsewhere outside this execution path
204-
configureClient, err := buildConfigureClientFn(sdkConfigProvider, httpClient)
203+
204+
configureClientLocal, err := buildConfigureClientFn(sdkConfigProvider, httpClient)
205205
if err != nil {
206206
return nil, err
207207
}
208208

209209
configureClientWithUserAgent := func(client *oci_common.BaseClient) error {
210-
if err := configureClient(client); err != nil {
210+
if err := configureClientLocal(client); err != nil {
211211
return err
212212
}
213213
client.UserAgent = userAgentString
214214
return nil
215215
}
216-
216+
// beware: global variable `configureClient` set here--used elsewhere outside this execution path
217+
configureClient = configureClientWithUserAgent
217218
err = createSDKClients(clients, sdkConfigProvider, configureClientWithUserAgent)
218219
if err != nil {
219220
return nil, err

oci/export_definitions.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
oci_file_storage "github.com/oracle/oci-go-sdk/filestorage"
1818
oci_functions "github.com/oracle/oci-go-sdk/functions"
1919
oci_identity "github.com/oracle/oci-go-sdk/identity"
20+
oci_kms "github.com/oracle/oci-go-sdk/keymanagement"
2021
oci_limits "github.com/oracle/oci-go-sdk/limits"
2122
oci_load_balancer "github.com/oracle/oci-go-sdk/loadbalancer"
2223
oci_monitoring "github.com/oracle/oci-go-sdk/monitoring"
@@ -949,6 +950,37 @@ var exportIdentityTagHints = &TerraformResourceHints{
949950
},
950951
}
951952

953+
var exportKmsKeyHints = &TerraformResourceHints{
954+
resourceClass: "oci_kms_key",
955+
datasourceClass: "oci_kms_keys",
956+
datasourceItemsAttr: "keys",
957+
resourceAbbreviation: "key",
958+
requireResourceRefresh: true,
959+
discoverableLifecycleStates: []string{
960+
string(oci_kms.KeyLifecycleStateEnabled),
961+
},
962+
}
963+
964+
var exportKmsKeyVersionHints = &TerraformResourceHints{
965+
resourceClass: "oci_kms_key_version",
966+
datasourceClass: "oci_kms_key_versions",
967+
datasourceItemsAttr: "key_versions",
968+
resourceAbbreviation: "key_version",
969+
discoverableLifecycleStates: []string{
970+
string(oci_kms.KeyVersionLifecycleStateEnabled),
971+
},
972+
}
973+
974+
var exportKmsVaultHints = &TerraformResourceHints{
975+
resourceClass: "oci_kms_vault",
976+
datasourceClass: "oci_kms_vaults",
977+
datasourceItemsAttr: "vaults",
978+
resourceAbbreviation: "vault",
979+
discoverableLifecycleStates: []string{
980+
string(oci_kms.VaultLifecycleStateActive),
981+
},
982+
}
983+
952984
var exportIdentityNetworkSourceHints = &TerraformResourceHints{
953985
resourceClass: "oci_identity_network_source",
954986
datasourceClass: "oci_identity_network_sources",

oci/export_graphs.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ var compartmentResourceGraphs = map[string]TerraformResourceGraph{
6060
"file_storage": fileStorageResourceGraph,
6161
"functions": functionsResourceGraph,
6262
"health_checks": healthChecksResourceGraph,
63+
"kms": kmsResourceGraph,
6364
"load_balancer": loadBalancerResourceGraph,
6465
"monitoring": monitoringResourceGraph,
6566
"nosql": nosqlResourceGraph,
@@ -423,6 +424,29 @@ var identityResourceGraph = TerraformResourceGraph{
423424
},
424425
}
425426

427+
var kmsResourceGraph = TerraformResourceGraph{
428+
"oci_identity_compartment": {
429+
{TerraformResourceHints: exportKmsVaultHints},
430+
},
431+
"oci_kms_key": {
432+
{
433+
TerraformResourceHints: exportKmsKeyVersionHints,
434+
datasourceQueryParams: map[string]string{
435+
"key_id": "id",
436+
"management_endpoint": "management_endpoint",
437+
},
438+
},
439+
},
440+
"oci_kms_vault": {
441+
{
442+
TerraformResourceHints: exportKmsKeyHints,
443+
datasourceQueryParams: map[string]string{
444+
"management_endpoint": "management_endpoint",
445+
},
446+
},
447+
},
448+
}
449+
426450
var limitsResourceGraph = TerraformResourceGraph{
427451
"oci_identity_tenancy": {
428452
{TerraformResourceHints: exportLimitsQuotaHints},

oci/export_resource_helpers.go

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

12+
"github.com/hashicorp/terraform/helper/schema"
13+
1214
oci_core "github.com/oracle/oci-go-sdk/core"
1315
oci_identity "github.com/oracle/oci-go-sdk/identity"
1416
oci_load_balancer "github.com/oracle/oci-go-sdk/loadbalancer"
@@ -132,6 +134,11 @@ func init() {
132134
exportIdentitySmtpCredentialHints.getIdFn = getIdentitySmtpCredentialId
133135

134136
exportIdentitySwiftPasswordHints.getIdFn = getIdentitySwiftPasswordId
137+
138+
exportKmsKeyHints.getIdFn = getKmsKeyId
139+
exportKmsKeyHints.processDiscoveredResourcesFn = processKmsKey
140+
141+
exportKmsKeyVersionHints.getIdFn = getKmsKeyVersionId
135142
}
136143

137144
// Custom functions to alter behavior of resource discovery and resource HCL representation
@@ -240,6 +247,47 @@ func getNosqlIndexId(resource *OCIResource) (string, error) {
240247
return getIndexCompositeId(name, tableNameOrId), nil
241248
}
242249

250+
func processKmsKey(clients *OracleClients, resources []*OCIResource) ([]*OCIResource, error) {
251+
for _, resource := range resources {
252+
resource.sourceAttributes["management_endpoint"] = resource.parent.sourceAttributes["management_endpoint"].(string)
253+
var resourceSchema *schema.ResourceData = resource.rawResource.(*schema.ResourceData)
254+
resource.sourceAttributes["id"] = resourceSchema.Id()
255+
}
256+
return resources, nil
257+
}
258+
259+
func getKmsKeyId(resource *OCIResource) (string, error) {
260+
managementEndpoint, ok := resource.parent.sourceAttributes["management_endpoint"].(string)
261+
if !ok {
262+
return "", fmt.Errorf("[ERROR] unable to find management_endpoint for Index id")
263+
}
264+
var keyId string
265+
// observed that Id is not always available in sourceAttributes - refer export_compartment.go->findResourcesGeneric() to visualize below docs
266+
// resource.sourceAttributes has the id in the cases where getKmsKeyId is called with LIST data source response, because list SetData() sets the Id, but this is only done temporarily to populate compositeID
267+
// When getKmsKeyId is called for resource, resource.sourceAttributes is not set yet,(so far we used LIST response to get composite Id) but we can get the real ocid after Read because Id was set in the method kms_key_resource.go->readKmsKey()
268+
switch resource.rawResource.(type) {
269+
case *schema.ResourceData:
270+
// rawResource from resource read response
271+
var resourceSchema *schema.ResourceData = resource.rawResource.(*schema.ResourceData)
272+
keyId = resourceSchema.Id()
273+
case map[string]interface{}:
274+
// rawResource from LIST data source read response
275+
var resourceMap map[string]interface{} = resource.rawResource.(map[string]interface{})
276+
keyId = resourceMap["id"].(string)
277+
}
278+
return getCompositeKeyId(managementEndpoint, keyId), nil
279+
}
280+
281+
func getKmsKeyVersionId(resource *OCIResource) (string, error) {
282+
managementEndpoint, ok := resource.parent.sourceAttributes["management_endpoint"].(string)
283+
if !ok {
284+
return "", fmt.Errorf("[ERROR] unable to find management_endpoint for Index id")
285+
}
286+
keyId := resource.parent.sourceAttributes["id"].(string)
287+
keyVersionId := resource.sourceAttributes["key_version_id"].(string)
288+
return getCompositeKeyVersionId(managementEndpoint, keyId, keyVersionId), nil
289+
}
290+
243291
// Custom functions to alter behavior of resource discovery and resource HCL representation
244292

245293
func getBudgetAlertRuleId(resource *OCIResource) (string, error) {

oci/kms_key_resource.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,14 @@ func deleteKmsKey(d *schema.ResourceData, m interface{}) error {
250250
return DeleteResource(d, sync)
251251
}
252252

253+
// existing Id is key OCID and it is not the format that readKmsKey expects managementEndpoint/{managementEndpoint}/keys/{keyId}
254+
// getCompositeKeyId is only used for resource discovery and it returns the Id as expected by readKmsKey method
255+
// terraform import oci_kms_key.test_key "managementEndpoint/{managementEndpoint}/keys/{keyId}"
256+
func getCompositeKeyId(managementEndpoint string, keyId string) string {
257+
compositeId := "managementEndpoint/" + managementEndpoint + "/keys/" + keyId
258+
return compositeId
259+
}
260+
253261
type KmsKeyResourceCrud struct {
254262
BaseCrud
255263
Client *oci_kms.KmsManagementClient

oci/kms_key_version_resource.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,15 @@ func deleteKmsKeyVersion(d *schema.ResourceData, m interface{}) error {
148148
return DeleteResource(d, sync)
149149
}
150150

151+
// existing ID() returned by getKeyVersionCompositeId method is of format keys/(.*)/keyVersions/(.*) and
152+
// that is not the format that readKmsKeyVersion expects managementEndpoint/{managementEndpoint}/keys/{keyId}/keyVersions/{keyVersionId}
153+
// getCompositeKeyVersionId is only used for resource discovery and it returns the Id as expected by readKmsKeyVersion
154+
// terraform import oci_kms_key_version.test_key_version "managementEndpoint/{managementEndpoint}/keys/{keyId}/keyVersions/{keyVersionId}"
155+
func getCompositeKeyVersionId(managementEndpoint string, keyId string, keyVersionId string) string {
156+
compositeId := "managementEndpoint/" + managementEndpoint + "/keys/" + keyId + "/keyVersions/" + keyVersionId
157+
return compositeId
158+
}
159+
151160
type KmsKeyVersionResourceCrud struct {
152161
BaseCrud
153162
Client *oci_kms.KmsManagementClient

oci/kms_key_version_test.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package oci
66
import (
77
"fmt"
88
"os"
9+
"strconv"
910
"testing"
1011
"time"
1112

@@ -61,6 +62,8 @@ func TestKmsKeyVersionResource_basic(t *testing.T) {
6162
datasourceName := "data.oci_kms_key_versions.test_key_versions"
6263
singularDatasourceName := "data.oci_kms_key_version.test_key_version"
6364

65+
var resId string
66+
6467
resource.Test(t, resource.TestCase{
6568
PreCheck: func() { testAccPreCheck(t) },
6669
Providers: map[string]terraform.ResourceProvider{
@@ -74,6 +77,16 @@ func TestKmsKeyVersionResource_basic(t *testing.T) {
7477
Check: resource.ComposeAggregateTestCheckFunc(
7578
resource.TestCheckResourceAttrSet(resourceName, "key_id"),
7679
resource.TestCheckResourceAttrSet(resourceName, "management_endpoint"),
80+
81+
func(s *terraform.State) (err error) {
82+
resId, err = fromInstanceState(s, resourceName, "id")
83+
if isEnableExportCompartment, _ := strconv.ParseBool(getEnvSettingWithDefault("enable_export_compartment", "false")); isEnableExportCompartment {
84+
if errExport := testExportCompartmentWithResourceName(&resId, &compartmentId, resourceName); errExport != nil {
85+
return errExport
86+
}
87+
}
88+
return err
89+
},
7790
),
7891
},
7992

oci/kms_key_versions_data_source.go

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

66
import (
77
"context"
8+
"regexp"
89

910
"fmt"
1011

@@ -67,10 +68,7 @@ func (s *KmsKeyVersionsDataSourceCrud) VoidState() {
6768
func (s *KmsKeyVersionsDataSourceCrud) Get() error {
6869
request := oci_kms.ListKeyVersionsRequest{}
6970

70-
if keyId, ok := s.D.GetOkExists("key_id"); ok {
71-
tmp := keyId.(string)
72-
request.KeyId = &tmp
73-
}
71+
request.KeyId = getKeyID(s)
7472

7573
request.RequestMetadata.RetryPolicy = getRetryPolicy(false, "kms")
7674

@@ -95,6 +93,24 @@ func (s *KmsKeyVersionsDataSourceCrud) Get() error {
9593
return nil
9694
}
9795

96+
// with resource discovery s.D.GetOkExists("key_id") can return one of the two things
97+
// 1) keyId (key ocid) (or)
98+
// 2) managementEndpoint/{managementEndpoint}/keys/{keyId}
99+
// getKeyID method handles both and will return the key OCID
100+
func getKeyID(s *KmsKeyVersionsDataSourceCrud) *string {
101+
var finalKeyId string
102+
if keyId, ok := s.D.GetOkExists("key_id"); ok {
103+
regex, _ := regexp.Compile("^managementEndpoint/(.*)/keys/(.*)$")
104+
tokens := regex.FindStringSubmatch(keyId.(string))
105+
if len(tokens) == 3 {
106+
finalKeyId = tokens[2]
107+
} else {
108+
finalKeyId = keyId.(string)
109+
}
110+
}
111+
return &finalKeyId
112+
}
113+
98114
func (s *KmsKeyVersionsDataSourceCrud) SetData() error {
99115
if s.Res == nil {
100116
return nil

website/docs/guides/resource_discovery.html.markdown

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ The generated `.tf` files contain the Terraform configuration with the resources
106106
* `functions` - Discovers functions resources within the specified compartment
107107
* `health_checks` - Discovers health_checks resources within the specified compartment
108108
* `identity` - Discovers identity resources across the entire tenancy
109+
* `kms` - Discovers kms resources within the specified compartment
109110
* `limits` - Discovers limits resources across the entire tenancy
110111
* `load_balancer` - Discovers load balancer resources within the specified compartment
111112
* `monitoring` - Discovers monitoring resources within the specified compartment
@@ -330,6 +331,12 @@ identity
330331
* oci\_identity\_user
331332
* oci\_identity\_network\_source
332333

334+
kms
335+
336+
* oci\_kms\_key
337+
* oci\_kms\_key\_version
338+
* oci\_kms\_vault
339+
333340
limits
334341

335342
* oci\_limits\_quota

0 commit comments

Comments
 (0)