Skip to content

Commit d9548dc

Browse files
committed
Support for identity resource discovery
1 parent d80b4a5 commit d9548dc

24 files changed

+470
-12
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
- Support for `dbVersion` field added to Autonomous Database back resource
1010
- Support for patch and patch history in `database_vm_cluster`
1111
- Support resource discovery for `monitoring` resources
12+
- Support resource discovery for `identity` resources
1213

1314
## 3.79.0 (June 03, 2020)
1415

oci/export_definitions.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -867,6 +867,16 @@ var exportIdentitySmtpCredentialHints = &TerraformResourceHints{
867867
},
868868
}
869869

870+
var exportIdentitySwiftPasswordHints = &TerraformResourceHints{
871+
resourceClass: "oci_identity_swift_password",
872+
datasourceClass: "oci_identity_swift_passwords",
873+
datasourceItemsAttr: "passwords",
874+
resourceAbbreviation: "swift_password",
875+
discoverableLifecycleStates: []string{
876+
string(oci_identity.SwiftPasswordLifecycleStateActive),
877+
},
878+
}
879+
870880
var exportIdentityUiPasswordHints = &TerraformResourceHints{
871881
resourceClass: "oci_identity_ui_password",
872882
datasourceClass: "oci_identity_ui_password",
@@ -927,6 +937,17 @@ var exportIdentityTagHints = &TerraformResourceHints{
927937
},
928938
}
929939

940+
var exportIdentityNetworkSourceHints = &TerraformResourceHints{
941+
resourceClass: "oci_identity_network_source",
942+
datasourceClass: "oci_identity_network_sources",
943+
datasourceItemsAttr: "network_sources",
944+
resourceAbbreviation: "network_source",
945+
requireResourceRefresh: true,
946+
discoverableLifecycleStates: []string{
947+
string(oci_identity.NetworkSourcesLifecycleStateActive),
948+
},
949+
}
950+
930951
var exportLimitsQuotaHints = &TerraformResourceHints{
931952
resourceClass: "oci_limits_quota",
932953
datasourceClass: "oci_limits_quotas",

oci/export_graphs.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,7 @@ var identityResourceGraph = TerraformResourceGraph{
350350
datasourceQueryParams: map[string]string{"compartment_id": "id"},
351351
},
352352
{TerraformResourceHints: exportIdentityUserHints},
353+
{TerraformResourceHints: exportIdentityNetworkSourceHints},
353354
},
354355
"oci_identity_compartment": {
355356
{
@@ -394,6 +395,12 @@ var identityResourceGraph = TerraformResourceGraph{
394395
"user_id": "id",
395396
},
396397
},
398+
{
399+
TerraformResourceHints: exportIdentitySwiftPasswordHints,
400+
datasourceQueryParams: map[string]string{
401+
"user_id": "id",
402+
},
403+
},
397404
{
398405
TerraformResourceHints: exportIdentityUiPasswordHints,
399406
datasourceQueryParams: map[string]string{

oci/export_resource_helpers.go

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,10 +120,82 @@ func init() {
120120
exportFileStorageMountTargetHints.requireResourceRefresh = true
121121

122122
exportBudgetAlertRuleHints.getIdFn = getBudgetAlertRuleId
123+
124+
exportIdentityApiKeyHints.getIdFn = getIdentityApiKeyId
125+
126+
exportIdentityAuthTokenHints.getIdFn = getIdentityAuthTokenId
127+
128+
exportIdentityCustomerSecretKeyHints.getIdFn = getIdentityCustomerSecretKeyId
129+
130+
exportIdentityIdpGroupMappingHints.getIdFn = getIdentityIdpGroupMappingId
131+
132+
exportIdentitySmtpCredentialHints.getIdFn = getIdentitySmtpCredentialId
133+
134+
exportIdentitySwiftPasswordHints.getIdFn = getIdentitySwiftPasswordId
123135
}
124136

125137
// Custom functions to alter behavior of resource discovery and resource HCL representation
126138

139+
func getIdentityApiKeyId(resource *OCIResource) (string, error) {
140+
fingerPrint, ok := resource.sourceAttributes["fingerprint"].(string)
141+
if !ok {
142+
return "", fmt.Errorf("[ERROR] unable to find fingerprint for Api Key")
143+
}
144+
userId := resource.parent.id
145+
146+
return getApiKeyCompositeId(fingerPrint, userId), nil
147+
}
148+
149+
func getIdentityAuthTokenId(resource *OCIResource) (string, error) {
150+
authTokenId, ok := resource.sourceAttributes["id"].(string)
151+
if !ok {
152+
return "", fmt.Errorf("[ERROR] unable to find id for Auth Token")
153+
}
154+
userId := resource.parent.id
155+
156+
return getAuthTokenCompositeId(authTokenId, userId), nil
157+
}
158+
159+
func getIdentityCustomerSecretKeyId(resource *OCIResource) (string, error) {
160+
id, ok := resource.sourceAttributes["id"].(string)
161+
if !ok {
162+
return "", fmt.Errorf("[ERROR] unable to find id for Customer Secrest Key")
163+
}
164+
userId := resource.parent.id
165+
166+
return getCustomerSecretKeyCompositeId(id, userId), nil
167+
}
168+
169+
func getIdentityIdpGroupMappingId(resource *OCIResource) (string, error) {
170+
id, ok := resource.sourceAttributes["id"].(string)
171+
if !ok {
172+
return "", fmt.Errorf("[ERROR] unable to find id for Customer Secrest Key")
173+
}
174+
providerId := resource.parent.id
175+
176+
return getIdpGroupMappingCompositeId(providerId, id), nil
177+
}
178+
179+
func getIdentitySmtpCredentialId(resource *OCIResource) (string, error) {
180+
id, ok := resource.sourceAttributes["id"].(string)
181+
if !ok {
182+
return "", fmt.Errorf("[ERROR] unable to find id for Smtp Credential")
183+
}
184+
userId := resource.parent.id
185+
186+
return getSmtpCredentialCompositeId(id, userId), nil
187+
}
188+
189+
func getIdentitySwiftPasswordId(resource *OCIResource) (string, error) {
190+
id, ok := resource.sourceAttributes["id"].(string)
191+
if !ok {
192+
return "", fmt.Errorf("[ERROR] unable to find id for Swift Password")
193+
}
194+
userId := resource.parent.id
195+
196+
return getSwiftPasswordCompositeId(id, userId), nil
197+
}
198+
127199
func processContainerengineNodePool(clients *OracleClients, resources []*OCIResource) ([]*OCIResource, error) {
128200
for _, nodePool := range resources {
129201
// subnet_ids and quantity_per_subnet are deprecated and conflict with node_config_details

oci/identity_api_key_resource.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,12 @@ package oci
66
import (
77
"context"
88
"errors"
9+
"fmt"
10+
"log"
11+
"net/url"
912
"regexp"
1013
"strconv"
14+
"strings"
1115

1216
"github.com/hashicorp/terraform/helper/schema"
1317

@@ -20,6 +24,9 @@ func init() {
2024

2125
func IdentityApiKeyResource() *schema.Resource {
2226
return &schema.Resource{
27+
Importer: &schema.ResourceImporter{
28+
State: schema.ImportStatePassthrough,
29+
},
2330
Timeouts: DefaultTimeout,
2431
Create: createIdentityApiKey,
2532
Read: readIdentityApiKey,
@@ -162,6 +169,14 @@ func (s *IdentityApiKeyResourceCrud) Get() error {
162169
request.UserId = &tmp
163170
}
164171

172+
fingerprintFromCompositeId, userId, err := parseApiKeyCompositeId(s.D.Id())
173+
if err == nil {
174+
s.D.Set("fingerprint", fingerprintFromCompositeId)
175+
request.UserId = &userId
176+
} else {
177+
log.Printf("[WARN] Get() unable to parse current ID: %s", s.D.Id())
178+
}
179+
165180
request.RequestMetadata.RetryPolicy = getRetryPolicy(s.DisableNotFoundRetries, "identity")
166181

167182
response, err := s.Client.ListApiKeys(context.Background(), request)
@@ -224,3 +239,23 @@ func (s *IdentityApiKeyResourceCrud) SetData() error {
224239

225240
return nil
226241
}
242+
243+
func getApiKeyCompositeId(fingerprint string, userId string) string {
244+
fingerprint = url.PathEscape(fingerprint)
245+
userId = url.PathEscape(userId)
246+
compositeId := "users/" + userId + "/apiKeys/" + fingerprint
247+
return compositeId
248+
}
249+
250+
func parseApiKeyCompositeId(compositeId string) (fingerprint string, userId string, err error) {
251+
parts := strings.Split(compositeId, "/")
252+
match, _ := regexp.MatchString("users/.*/apiKeys/.*", compositeId)
253+
if !match || len(parts) != 4 {
254+
err = fmt.Errorf("illegal compositeId %s encountered", compositeId)
255+
return
256+
}
257+
userId, _ = url.PathUnescape(parts[1])
258+
fingerprint, _ = url.PathUnescape(parts[3])
259+
260+
return
261+
}

oci/identity_api_key_test.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ package oci
66
import (
77
"context"
88
"fmt"
9+
"log"
10+
"strconv"
911
"testing"
1012

1113
"github.com/hashicorp/terraform/helper/resource"
@@ -68,6 +70,8 @@ func TestIdentityApiKeyResource_basic(t *testing.T) {
6870
resourceName := "oci_identity_api_key.test_api_key"
6971
datasourceName := "data.oci_identity_api_keys.test_api_keys"
7072

73+
var compositeId, fingerprint string
74+
7175
resource.Test(t, resource.TestCase{
7276
PreCheck: func() { testAccPreCheck(t) },
7377
Providers: map[string]terraform.ResourceProvider{
@@ -84,6 +88,32 @@ func TestIdentityApiKeyResource_basic(t *testing.T) {
8488
resource.TestCheckResourceAttrSet(resourceName, "user_id"),
8589
),
8690
},
91+
// delete before next create
92+
{
93+
Config: config + apiKeyVarStr + compartmentIdVariableStr + ApiKeyResourceDependencies,
94+
},
95+
// verify create with export
96+
{
97+
Config: config + apiKeyVarStr + compartmentIdVariableStr + ApiKeyResourceDependencies +
98+
generateResourceFromRepresentationMap("oci_identity_api_key", "test_api_key", Required, Create, apiKeyRepresentation),
99+
Check: resource.ComposeAggregateTestCheckFunc(
100+
resource.TestCheckResourceAttr(resourceName, "key_value", apiKey),
101+
resource.TestCheckResourceAttrSet(resourceName, "user_id"),
102+
103+
func(s *terraform.State) (err error) {
104+
fingerprint, _ = fromInstanceState(s, resourceName, "fingerprint")
105+
userId, _ := fromInstanceState(s, resourceName, "user_id")
106+
compositeId = "users/" + userId + "/apiKeys/" + fingerprint
107+
log.Printf("[DEBUG] Composite ID to import: %s", compositeId)
108+
if isEnableExportCompartment, _ := strconv.ParseBool(getEnvSettingWithDefault("enable_export_compartment", "false")); isEnableExportCompartment {
109+
if errExport := testExportCompartmentWithResourceName(&compositeId, &compartmentId, resourceName); errExport != nil {
110+
return errExport
111+
}
112+
}
113+
return err
114+
},
115+
),
116+
},
87117

88118
// verify datasource
89119
{
@@ -103,6 +133,14 @@ func TestIdentityApiKeyResource_basic(t *testing.T) {
103133
resource.TestCheckResourceAttrSet(datasourceName, "api_keys.0.user_id"),
104134
),
105135
},
136+
// verify resource import
137+
//{
138+
// Config: config,
139+
// ImportState: true,
140+
// ImportStateVerify: true,
141+
// ImportStateVerifyIgnore: []string{},
142+
// ResourceName: resourceName,
143+
//},
106144
},
107145
})
108146
}

oci/identity_auth_token_resource.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,12 @@ package oci
66
import (
77
"context"
88
"errors"
9+
"fmt"
10+
"log"
11+
"net/url"
12+
"regexp"
913
"strconv"
14+
"strings"
1015

1116
"github.com/hashicorp/terraform/helper/schema"
1217

@@ -19,6 +24,9 @@ func init() {
1924

2025
func IdentityAuthTokenResource() *schema.Resource {
2126
return &schema.Resource{
27+
Importer: &schema.ResourceImporter{
28+
State: schema.ImportStatePassthrough,
29+
},
2230
Timeouts: DefaultTimeout,
2331
Create: createIdentityAuthToken,
2432
Read: readIdentityAuthToken,
@@ -167,6 +175,14 @@ func (s *IdentityAuthTokenResourceCrud) Get() error {
167175
request.UserId = &tmp
168176
}
169177

178+
authTokenId, userId, err := parseAuthTokenCompositeId(s.D.Id())
179+
if err == nil {
180+
s.D.SetId(authTokenId)
181+
request.UserId = &userId
182+
} else {
183+
log.Printf("[WARN] Get() unable to parse current ID: %s", s.D.Id())
184+
}
185+
170186
request.RequestMetadata.RetryPolicy = getRetryPolicy(s.DisableNotFoundRetries, "identity")
171187

172188
response, err := s.Client.ListAuthTokens(context.Background(), request)
@@ -258,3 +274,23 @@ func (s *IdentityAuthTokenResourceCrud) SetData() error {
258274

259275
return nil
260276
}
277+
278+
func getAuthTokenCompositeId(authTokenId string, userId string) string {
279+
authTokenId = url.PathEscape(authTokenId)
280+
userId = url.PathEscape(userId)
281+
compositeId := "users/" + userId + "/authTokens/" + authTokenId
282+
return compositeId
283+
}
284+
285+
func parseAuthTokenCompositeId(compositeId string) (authTokenId string, userId string, err error) {
286+
parts := strings.Split(compositeId, "/")
287+
match, _ := regexp.MatchString("users/.*/authTokens/.*", compositeId)
288+
if !match || len(parts) != 4 {
289+
err = fmt.Errorf("illegal compositeId %s encountered", compositeId)
290+
return
291+
}
292+
userId, _ = url.PathUnescape(parts[1])
293+
authTokenId, _ = url.PathUnescape(parts[3])
294+
295+
return
296+
}

oci/identity_auth_token_test.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package oci
66
import (
77
"context"
88
"fmt"
9+
"log"
910
"strconv"
1011
"testing"
1112

@@ -48,6 +49,7 @@ func TestIdentityAuthTokenResource_basic(t *testing.T) {
4849
datasourceName := "data.oci_identity_auth_tokens.test_auth_tokens"
4950

5051
var resId, resId2 string
52+
var compositeId string
5153

5254
resource.Test(t, resource.TestCase{
5355
PreCheck: func() { testAccPreCheck(t) },
@@ -66,8 +68,11 @@ func TestIdentityAuthTokenResource_basic(t *testing.T) {
6668

6769
func(s *terraform.State) (err error) {
6870
resId, err = fromInstanceState(s, resourceName, "id")
71+
userId, _ := fromInstanceState(s, resourceName, "user_id")
72+
compositeId = "users/" + userId + "/authTokens/" + resId
73+
log.Printf("[DEBUG] Composite ID to import: %s", compositeId)
6974
if isEnableExportCompartment, _ := strconv.ParseBool(getEnvSettingWithDefault("enable_export_compartment", "false")); isEnableExportCompartment {
70-
if errExport := testExportCompartmentWithResourceName(&resId, &compartmentId, resourceName); errExport != nil {
75+
if errExport := testExportCompartmentWithResourceName(&compositeId, &compartmentId, resourceName); errExport != nil {
7176
return errExport
7277
}
7378
}
@@ -110,6 +115,14 @@ func TestIdentityAuthTokenResource_basic(t *testing.T) {
110115
resource.TestCheckResourceAttrSet(datasourceName, "tokens.0.user_id"),
111116
),
112117
},
118+
// verify resource import
119+
//{
120+
// Config: config,
121+
// ImportState: true,
122+
// ImportStateVerify: true,
123+
// ImportStateVerifyIgnore: []string{},
124+
// ResourceName: resourceName,
125+
//},
113126
},
114127
})
115128
}

0 commit comments

Comments
 (0)