Skip to content

Commit 1f12811

Browse files
committed
feat(IAM Identity): Identity Preferences
1 parent 4e5a13e commit 1f12811

13 files changed

+949
-0
lines changed
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
# Examples for IAM Identity Services
2+
3+
These examples illustrate how to use the resources and data sources associated with IAM Identity Services.
4+
5+
The following resources are supported:
6+
* ibm_iam_identity_preference
7+
8+
The following data sources are supported:
9+
* ibm_iam_identity_preference
10+
11+
## Usage
12+
13+
To run this example, execute the following commands:
14+
15+
```bash
16+
$ terraform init
17+
$ terraform plan
18+
$ terraform apply
19+
```
20+
21+
Run `terraform destroy` when you don't need these resources.
22+
23+
## IAM Identity Services resources
24+
25+
### Resource: ibm_iam_identity_preference
26+
27+
```hcl
28+
resource "ibm_iam_identity_preference" "iam_identity_preference_instance" {
29+
account_id = var.iam_identity_preference_account_id
30+
iam_id = var.iam_identity_preference_iam_id
31+
service = var.iam_identity_preference_service
32+
preference_id = var.iam_identity_preference_preference_id
33+
value_string = var.iam_identity_preference_value_string
34+
value_list_of_strings = var.iam_identity_preference_value_list_of_strings
35+
}
36+
```
37+
38+
#### Inputs
39+
40+
| Name | Description | Type | Required |
41+
|------|-------------|------|---------|
42+
| ibmcloud\_api\_key | IBM Cloud API key | `string` | true |
43+
| account_id | Account id to update preference for. | `string` | true |
44+
| iam_id | IAM id to update the preference for. | `string` | true |
45+
| service | Service of the preference to be updated. | `string` | true |
46+
| preference_id | Identifier of preference to be updated. | `string` | true |
47+
| value_string | String value of the preference, only one value property is set, either 'value_string' or 'value_list_of_strings' is present. | `string` | true |
48+
| value_list_of_strings | List of value of the preference, only one value property is set, either 'value_string' or 'value_list_of_strings' is present. | `list(string)` | false |
49+
50+
#### Outputs
51+
52+
| Name | Description |
53+
|------|-------------|
54+
| scope | Scope of the preference, 'global' or 'account'. |
55+
| preference_id | Unique ID of the preference. |
56+
57+
## IAM Identity Services data sources
58+
59+
### Data source: ibm_iam_identity_preference
60+
61+
```hcl
62+
data "ibm_iam_identity_preference" "iam_identity_preference_instance" {
63+
account_id = var.data_iam_identity_preference_account_id
64+
iam_id = var.data_iam_identity_preference_iam_id
65+
service = var.data_iam_identity_preference_service
66+
preference_id = var.data_iam_identity_preference_preference_id
67+
}
68+
```
69+
70+
#### Inputs
71+
72+
| Name | Description | Type | Required |
73+
|------|-------------|------|---------|
74+
| account_id | Account id to get preference for. | `string` | true |
75+
| iam_id | IAM id to get the preference for. | `string` | true |
76+
| service | Service of the preference to be fetched. | `string` | true |
77+
| preference_id | Identifier of preference to be fetched. | `string` | true |
78+
79+
#### Outputs
80+
81+
| Name | Description |
82+
|------|-------------|
83+
| scope | Scope of the preference, 'global' or 'account'. |
84+
| value_string | String value of the preference, only one value property is set, either 'value_string' or 'value_list_of_strings' is present. |
85+
| value_list_of_strings | List of value of the preference, only one value property is set, either 'value_string' or 'value_list_of_strings' is present. |
86+
87+
## Assumptions
88+
89+
1. TODO
90+
91+
## Notes
92+
93+
1. TODO
94+
95+
## Requirements
96+
97+
| Name | Version |
98+
|------|---------|
99+
| terraform | ~> 0.12 |
100+
101+
## Providers
102+
103+
| Name | Version |
104+
|------|---------|
105+
| ibm | 1.13.1 |
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
provider "ibm" {
2+
ibmcloud_api_key = var.ibmcloud_api_key
3+
}
4+
5+
// Provision iam_identity_preference resource instance
6+
resource "ibm_iam_identity_preference" "iam_identity_preference_instance" {
7+
account_id = var.iam_identity_preference_account_id
8+
iam_id = var.iam_identity_preference_iam_id
9+
service = var.iam_identity_preference_service
10+
preference_id = var.iam_identity_preference_preference_id
11+
value_string = var.iam_identity_preference_value_string
12+
value_list_of_strings = var.iam_identity_preference_value_list_of_strings
13+
}
14+
15+
// Data source is not linked to a resource instance
16+
// Uncomment if an existing data source instance exists
17+
/*
18+
// Create iam_identity_preference data source
19+
data "ibm_iam_identity_preference" "iam_identity_preference_instance" {
20+
account_id = var.data_iam_identity_preference_account_id
21+
iam_id = var.data_iam_identity_preference_iam_id
22+
service = var.data_iam_identity_preference_service
23+
preference_id = var.data_iam_identity_preference_preference_id
24+
}
25+
*/
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// This output allows iam_identity_preference data to be referenced by other resources and the terraform CLI
2+
// Modify this output if only certain data should be exposed
3+
output "ibm_iam_identity_preference" {
4+
value = ibm_iam_identity_preference.iam_identity_preference_instance
5+
description = "iam_identity_preference resource instance"
6+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
variable "ibmcloud_api_key" {
2+
description = "IBM Cloud API key"
3+
type = string
4+
}
5+
6+
// Resource arguments for iam_identity_preference
7+
variable "iam_identity_preference_account_id" {
8+
description = "Account id to update preference for."
9+
type = string
10+
default = "account_id"
11+
}
12+
variable "iam_identity_preference_iam_id" {
13+
description = "IAM id to update the preference for."
14+
type = string
15+
default = "iam_id"
16+
}
17+
variable "iam_identity_preference_service" {
18+
description = "Service of the preference to be updated."
19+
type = string
20+
default = "service"
21+
}
22+
variable "iam_identity_preference_preference_id" {
23+
description = "Identifier of preference to be updated."
24+
type = string
25+
default = "preference_id"
26+
}
27+
variable "iam_identity_preference_value_string" {
28+
description = "String value of the preference, only one value property is set, either 'value_string' or 'value_list_of_strings' is present."
29+
type = string
30+
default = "value_string"
31+
}
32+
variable "iam_identity_preference_value_list_of_strings" {
33+
description = "List of value of the preference, only one value property is set, either 'value_string' or 'value_list_of_strings' is present."
34+
type = list(string)
35+
default = [ "value_list_of_strings" ]
36+
}
37+
38+
// Data source arguments for iam_identity_preference
39+
variable "data_iam_identity_preference_account_id" {
40+
description = "Account id to get preference for."
41+
type = string
42+
default = "account_id"
43+
}
44+
variable "data_iam_identity_preference_iam_id" {
45+
description = "IAM id to get the preference for."
46+
type = string
47+
default = "iam_id"
48+
}
49+
variable "data_iam_identity_preference_service" {
50+
description = "Service of the preference to be fetched."
51+
type = string
52+
default = "service"
53+
}
54+
variable "data_iam_identity_preference_preference_id" {
55+
description = "Identifier of preference to be fetched."
56+
type = string
57+
default = "preference_id"
58+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
terraform {
2+
required_version = ">= 1.0"
3+
required_providers {
4+
ibm = {
5+
source = "IBM-Cloud/ibm"
6+
version = "1.52.0-beta0"
7+
}
8+
}
9+
}

ibm/provider/provider.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -442,6 +442,7 @@ func Provider() *schema.Provider {
442442
"ibm_iam_trusted_profile_template": iamidentity.DataSourceIBMTrustedProfileTemplate(),
443443
"ibm_iam_account_settings_template_assignment": iamidentity.DataSourceIBMAccountSettingsTemplateAssignment(),
444444
"ibm_iam_trusted_profile_template_assignment": iamidentity.DataSourceIBMTrustedProfileTemplateAssignment(),
445+
"ibm_iam_identity_preference": iamidentity.DataSourceIBMIamIdentityPreference(),
445446
"ibm_iam_policy_template": iampolicy.DataSourceIBMIAMPolicyTemplate(),
446447
"ibm_iam_policy_template_version": iampolicy.DataSourceIBMIAMPolicyTemplateVersion(),
447448
"ibm_iam_policy_assignments": iampolicy.DataSourceIBMIAMPolicyAssignments(),
@@ -1290,6 +1291,7 @@ func Provider() *schema.Provider {
12901291
"ibm_iam_trusted_profile_template": iamidentity.ResourceIBMTrustedProfileTemplate(),
12911292
"ibm_iam_account_settings_template_assignment": iamidentity.ResourceIBMAccountSettingsTemplateAssignment(),
12921293
"ibm_iam_trusted_profile_template_assignment": iamidentity.ResourceIBMTrustedProfileTemplateAssignment(),
1294+
"ibm_iam_identity_preference": iamidentity.ResourceIBMIamIdentityPreference(),
12931295
"ibm_ipsec_vpn": classicinfrastructure.ResourceIBMIPSecVPN(),
12941296
"ibm_iam_policy_template": iampolicy.ResourceIBMIAMPolicyTemplate(),
12951297
"ibm_iam_policy_template_version": iampolicy.ResourceIBMIAMPolicyTemplateVersion(),
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
// Copyright IBM Corp. 2025 All Rights Reserved.
2+
// Licensed under the Mozilla Public License v2.0
3+
4+
/*
5+
* IBM OpenAPI Terraform Generator Version: 3.106.0-09823488-20250707-071701
6+
*/
7+
8+
package iamidentity
9+
10+
import (
11+
"context"
12+
"fmt"
13+
"log"
14+
15+
"github.com/IBM-Cloud/terraform-provider-ibm/ibm/conns"
16+
"github.com/IBM-Cloud/terraform-provider-ibm/ibm/flex"
17+
"github.com/IBM/go-sdk-core/v5/core"
18+
"github.com/IBM/platform-services-go-sdk/iamidentityv1"
19+
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
20+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
21+
)
22+
23+
func DataSourceIBMIamIdentityPreference() *schema.Resource {
24+
return &schema.Resource{
25+
ReadContext: dataSourceIBMIamIdentityPreferenceRead,
26+
27+
Schema: map[string]*schema.Schema{
28+
"account_id": &schema.Schema{
29+
Type: schema.TypeString,
30+
Required: true,
31+
Description: "Account id to get preference for.",
32+
},
33+
"iam_id": &schema.Schema{
34+
Type: schema.TypeString,
35+
Required: true,
36+
Description: "IAM id to get the preference for.",
37+
},
38+
"service": &schema.Schema{
39+
Type: schema.TypeString,
40+
Required: true,
41+
Description: "Service of the preference to be fetched.",
42+
},
43+
"preference_id": &schema.Schema{
44+
Type: schema.TypeString,
45+
Required: true,
46+
Description: "Identifier of preference to be fetched.",
47+
},
48+
"scope": &schema.Schema{
49+
Type: schema.TypeString,
50+
Computed: true,
51+
Description: "Scope of the preference, 'global' or 'account'.",
52+
},
53+
"value_string": &schema.Schema{
54+
Type: schema.TypeString,
55+
Computed: true,
56+
Description: "String value of the preference, only one value property is set, either 'value_string' or 'value_list_of_strings' is present.",
57+
},
58+
"value_list_of_strings": &schema.Schema{
59+
Type: schema.TypeList,
60+
Computed: true,
61+
Description: "List of value of the preference, only one value property is set, either 'value_string' or 'value_list_of_strings' is present.",
62+
Elem: &schema.Schema{
63+
Type: schema.TypeString,
64+
},
65+
},
66+
},
67+
}
68+
}
69+
70+
func dataSourceIBMIamIdentityPreferenceRead(context context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
71+
iamIdentityClient, err := meta.(conns.ClientSession).IAMIdentityV1API()
72+
if err != nil {
73+
tfErr := flex.DiscriminatedTerraformErrorf(err, err.Error(), "(Data) ibm_iam_identity_preference", "read", "initialize-client")
74+
log.Printf("[DEBUG]\n%s", tfErr.GetDebugMessage())
75+
return tfErr.GetDiag()
76+
}
77+
78+
getPreferenceOnScopeAccountOptions := &iamidentityv1.GetPreferencesOnScopeAccountOptions{}
79+
80+
getPreferenceOnScopeAccountOptions.SetAccountID(d.Get("account_id").(string))
81+
getPreferenceOnScopeAccountOptions.SetIamID(d.Get("iam_id").(string))
82+
getPreferenceOnScopeAccountOptions.SetService(d.Get("service").(string))
83+
getPreferenceOnScopeAccountOptions.SetPreferenceID(d.Get("preference_id").(string))
84+
85+
identityPreferenceResponse, _, err := iamIdentityClient.GetPreferencesOnScopeAccountWithContext(context, getPreferenceOnScopeAccountOptions)
86+
if err != nil {
87+
tfErr := flex.TerraformErrorf(err, fmt.Sprintf("GetPreferenceOnScopeAccountWithContext failed: %s", err.Error()), "(Data) ibm_iam_identity_preference", "read")
88+
log.Printf("[DEBUG]\n%s", tfErr.GetDebugMessage())
89+
return tfErr.GetDiag()
90+
}
91+
92+
d.SetId(fmt.Sprintf("%s/%s/%s/%s", *getPreferenceOnScopeAccountOptions.AccountID, *getPreferenceOnScopeAccountOptions.IamID, *getPreferenceOnScopeAccountOptions.Service, *getPreferenceOnScopeAccountOptions.PreferenceID))
93+
94+
if !core.IsNil(identityPreferenceResponse.Scope) {
95+
if err = d.Set("scope", identityPreferenceResponse.Scope); err != nil {
96+
return flex.DiscriminatedTerraformErrorf(err, fmt.Sprintf("Error setting scope: %s", err), "(Data) ibm_iam_identity_preference", "read", "set-scope").GetDiag()
97+
}
98+
}
99+
100+
if !core.IsNil(identityPreferenceResponse.ValueString) {
101+
if err = d.Set("value_string", identityPreferenceResponse.ValueString); err != nil {
102+
return flex.DiscriminatedTerraformErrorf(err, fmt.Sprintf("Error setting value_string: %s", err), "(Data) ibm_iam_identity_preference", "read", "set-value_string").GetDiag()
103+
}
104+
}
105+
106+
if !core.IsNil(identityPreferenceResponse.ValueListOfStrings) {
107+
valueListOfStrings := []interface{}{}
108+
for _, valueListOfStringsItem := range identityPreferenceResponse.ValueListOfStrings {
109+
valueListOfStrings = append(valueListOfStrings, valueListOfStringsItem)
110+
}
111+
if err = d.Set("value_list_of_strings", valueListOfStrings); err != nil {
112+
return flex.DiscriminatedTerraformErrorf(err, fmt.Sprintf("Error setting value_list_of_strings: %s", err), "(Data) ibm_iam_identity_preference", "read", "set-value_list_of_strings").GetDiag()
113+
}
114+
}
115+
116+
return nil
117+
}

0 commit comments

Comments
 (0)