|
9 | 9 | "strconv" |
10 | 10 | "strings" |
11 | 11 |
|
| 12 | + "github.com/hashicorp/terraform/helper/schema" |
| 13 | + |
12 | 14 | oci_core "github.com/oracle/oci-go-sdk/core" |
13 | 15 | oci_identity "github.com/oracle/oci-go-sdk/identity" |
14 | 16 | oci_load_balancer "github.com/oracle/oci-go-sdk/loadbalancer" |
@@ -132,6 +134,11 @@ func init() { |
132 | 134 | exportIdentitySmtpCredentialHints.getIdFn = getIdentitySmtpCredentialId |
133 | 135 |
|
134 | 136 | exportIdentitySwiftPasswordHints.getIdFn = getIdentitySwiftPasswordId |
| 137 | + |
| 138 | + exportKmsKeyHints.getIdFn = getKmsKeyId |
| 139 | + exportKmsKeyHints.processDiscoveredResourcesFn = processKmsKey |
| 140 | + |
| 141 | + exportKmsKeyVersionHints.getIdFn = getKmsKeyVersionId |
135 | 142 | } |
136 | 143 |
|
137 | 144 | // Custom functions to alter behavior of resource discovery and resource HCL representation |
@@ -240,6 +247,47 @@ func getNosqlIndexId(resource *OCIResource) (string, error) { |
240 | 247 | return getIndexCompositeId(name, tableNameOrId), nil |
241 | 248 | } |
242 | 249 |
|
| 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 | + |
243 | 291 | // Custom functions to alter behavior of resource discovery and resource HCL representation |
244 | 292 |
|
245 | 293 | func getBudgetAlertRuleId(resource *OCIResource) (string, error) { |
|
0 commit comments