|
| 1 | +// Copyright IBM Corp. 2023 All Rights Reserved. |
| 2 | +// Licensed under the Mozilla Public License v2.0 |
| 3 | + |
| 4 | +package iampolicy |
| 5 | + |
| 6 | +import ( |
| 7 | + "context" |
| 8 | + "fmt" |
| 9 | + "log" |
| 10 | + |
| 11 | + "github.com/hashicorp/terraform-plugin-sdk/v2/diag" |
| 12 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 13 | + |
| 14 | + "github.com/IBM-Cloud/terraform-provider-ibm/ibm/conns" |
| 15 | + "github.com/IBM-Cloud/terraform-provider-ibm/ibm/flex" |
| 16 | + "github.com/IBM/platform-services-go-sdk/iampolicymanagementv1" |
| 17 | +) |
| 18 | + |
| 19 | +func DataSourceIBMIAMActionControlTemplate() *schema.Resource { |
| 20 | + return &schema.Resource{ |
| 21 | + ReadContext: dataSourceIBMActionControlTemplateRead, |
| 22 | + |
| 23 | + Schema: map[string]*schema.Schema{ |
| 24 | + "action_control_templates": { |
| 25 | + Type: schema.TypeList, |
| 26 | + Computed: true, |
| 27 | + Description: "List of action control templates.", |
| 28 | + Elem: &schema.Resource{ |
| 29 | + Schema: map[string]*schema.Schema{ |
| 30 | + "name": { |
| 31 | + Type: schema.TypeString, |
| 32 | + Computed: true, |
| 33 | + Description: "name of template.", |
| 34 | + }, |
| 35 | + "description": { |
| 36 | + Type: schema.TypeString, |
| 37 | + Computed: true, |
| 38 | + Description: "description of template purpose.", |
| 39 | + }, |
| 40 | + "account_id": { |
| 41 | + Type: schema.TypeString, |
| 42 | + Computed: true, |
| 43 | + Description: "account id where this template will be created.", |
| 44 | + }, |
| 45 | + "version": { |
| 46 | + Type: schema.TypeString, |
| 47 | + Computed: true, |
| 48 | + Description: "Template version.", |
| 49 | + }, |
| 50 | + "committed": { |
| 51 | + Type: schema.TypeBool, |
| 52 | + Computed: true, |
| 53 | + Description: "Template version committed status.", |
| 54 | + }, |
| 55 | + "action_control": &schema.Schema{ |
| 56 | + Type: schema.TypeList, |
| 57 | + Computed: true, |
| 58 | + Description: "The action control properties that are created in an action resource when the template is assigned.", |
| 59 | + Elem: &schema.Resource{ |
| 60 | + Schema: map[string]*schema.Schema{ |
| 61 | + "service_name": &schema.Schema{ |
| 62 | + Type: schema.TypeString, |
| 63 | + Computed: true, |
| 64 | + Description: "The service name that the action control refers.", |
| 65 | + }, |
| 66 | + "description": &schema.Schema{ |
| 67 | + Type: schema.TypeString, |
| 68 | + Optional: true, |
| 69 | + Description: "Description of the action control.", |
| 70 | + }, |
| 71 | + "actions": &schema.Schema{ |
| 72 | + Type: schema.TypeSet, |
| 73 | + Computed: true, |
| 74 | + Description: "List of actions to control access.", |
| 75 | + Elem: &schema.Schema{Type: schema.TypeString}, |
| 76 | + }, |
| 77 | + }, |
| 78 | + }, |
| 79 | + }, |
| 80 | + "id": { |
| 81 | + Type: schema.TypeString, |
| 82 | + Computed: true, |
| 83 | + Description: "The action control template ID.", |
| 84 | + }, |
| 85 | + }, |
| 86 | + }, |
| 87 | + }, |
| 88 | + }, |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +func dataSourceIBMActionControlTemplateRead(context context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { |
| 93 | + iamPolicyManagementClient, err := meta.(conns.ClientSession).IAMPolicyManagementV1API() |
| 94 | + if err != nil { |
| 95 | + tfErr := flex.DiscriminatedTerraformErrorf(err, err.Error(), "(Data) ibm_iam_action_control_template", "read", "initialize-client") |
| 96 | + log.Printf("[DEBUG]\n%s", tfErr.GetDebugMessage()) |
| 97 | + return tfErr.GetDiag() |
| 98 | + } |
| 99 | + |
| 100 | + userDetails, err := meta.(conns.ClientSession).BluemixUserDetails() |
| 101 | + if err != nil { |
| 102 | + return diag.FromErr(fmt.Errorf("Failed to fetch BluemixUserDetails %s", err)) |
| 103 | + } |
| 104 | + |
| 105 | + accountID := userDetails.UserAccount |
| 106 | + |
| 107 | + listActionControlTemplatesOptions := &iampolicymanagementv1.ListActionControlTemplatesOptions{} |
| 108 | + |
| 109 | + listActionControlTemplatesOptions.SetAccountID(accountID) |
| 110 | + |
| 111 | + var pager *iampolicymanagementv1.ActionControlTemplatesPager |
| 112 | + pager, err = iamPolicyManagementClient.NewActionControlTemplatesPager(listActionControlTemplatesOptions) |
| 113 | + if err != nil { |
| 114 | + tfErr := flex.TerraformErrorf(err, err.Error(), "(Data) ibm_iam_action_control_template", "read") |
| 115 | + log.Printf("[DEBUG]\n%s", tfErr.GetDebugMessage()) |
| 116 | + return tfErr.GetDiag() |
| 117 | + } |
| 118 | + |
| 119 | + allItems, err := pager.GetAll() |
| 120 | + if err != nil { |
| 121 | + tfErr := flex.TerraformErrorf(err, fmt.Sprintf("ActionControlTemplatesPager.GetAll() failed %s", err), "(Data) ibm_iam_action_control_template", "read") |
| 122 | + log.Printf("[DEBUG] %s", tfErr.GetDebugMessage()) |
| 123 | + return tfErr.GetDiag() |
| 124 | + } |
| 125 | + |
| 126 | + d.SetId(accountID) |
| 127 | + |
| 128 | + mapSlice := []map[string]interface{}{} |
| 129 | + for _, modelItem := range allItems { |
| 130 | + modelMap, err := DataSourceIBMListActionControlTemplatesActionControlTemplateToMap(&modelItem) |
| 131 | + if err != nil { |
| 132 | + return flex.DiscriminatedTerraformErrorf(err, err.Error(), "(Data) ibm_iam_action_control_template", "read", "ActionControlTemplates-to-map").GetDiag() |
| 133 | + } |
| 134 | + mapSlice = append(mapSlice, modelMap) |
| 135 | + } |
| 136 | + |
| 137 | + if err = d.Set("action_control_templates", mapSlice); err != nil { |
| 138 | + return flex.DiscriminatedTerraformErrorf(err, fmt.Sprintf("Error setting action_control_templates %s", err), "(Data) ibm_iam_action_control_template", "read", "action_control_templates-set").GetDiag() |
| 139 | + } |
| 140 | + |
| 141 | + return nil |
| 142 | +} |
| 143 | + |
| 144 | +func DataSourceIBMListActionControlTemplatesActionControlTemplateToMap(model *iampolicymanagementv1.ActionControlTemplate) (map[string]interface{}, error) { |
| 145 | + modelMap := make(map[string]interface{}) |
| 146 | + modelMap["name"] = *model.Name |
| 147 | + if model.Description != nil { |
| 148 | + modelMap["description"] = *model.Description |
| 149 | + } |
| 150 | + modelMap["account_id"] = *model.AccountID |
| 151 | + if model.Committed != nil { |
| 152 | + modelMap["committed"] = *model.Committed |
| 153 | + } |
| 154 | + if model.ActionControl != nil { |
| 155 | + actionControlMap, err := DataSourceIBMListActionControlTemplatesTemplateActionControlToMap(model.ActionControl) |
| 156 | + if err != nil { |
| 157 | + return modelMap, err |
| 158 | + } |
| 159 | + modelMap["action_control"] = []map[string]interface{}{actionControlMap} |
| 160 | + } |
| 161 | + if model.ID != nil { |
| 162 | + modelMap["id"] = *model.ID |
| 163 | + } |
| 164 | + modelMap["version"] = *model.Version |
| 165 | + return modelMap, nil |
| 166 | +} |
| 167 | + |
| 168 | +func DataSourceIBMListActionControlTemplatesTemplateActionControlToMap(model *iampolicymanagementv1.TemplateActionControl) (map[string]interface{}, error) { |
| 169 | + modelMap := make(map[string]interface{}) |
| 170 | + modelMap["service_name"] = *model.ServiceName |
| 171 | + if model.Description != nil { |
| 172 | + modelMap["description"] = *model.Description |
| 173 | + } |
| 174 | + modelMap["actions"] = model.Actions |
| 175 | + return modelMap, nil |
| 176 | +} |
0 commit comments