|
| 1 | +package iam |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + |
| 6 | + "github.com/hashicorp/terraform-plugin-sdk/v2/diag" |
| 7 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 8 | + "github.com/philips-software/go-hsdp-api/iam" |
| 9 | + "github.com/philips-software/terraform-provider-hsdp/internal/config" |
| 10 | +) |
| 11 | + |
| 12 | +func DataSourceIAMRoleSharingPolicies() *schema.Resource { |
| 13 | + return &schema.Resource{ |
| 14 | + ReadContext: dataSourceIAMRoleSharingPoliciesRead, |
| 15 | + Schema: map[string]*schema.Schema{ |
| 16 | + "role_id": { |
| 17 | + Type: schema.TypeString, |
| 18 | + Required: true, |
| 19 | + }, |
| 20 | + "target_organization_id": { |
| 21 | + Type: schema.TypeString, |
| 22 | + Optional: true, |
| 23 | + }, |
| 24 | + "sharing_policy": { |
| 25 | + Type: schema.TypeString, |
| 26 | + Optional: true, |
| 27 | + }, |
| 28 | + "ids": { |
| 29 | + Type: schema.TypeList, |
| 30 | + Computed: true, |
| 31 | + Elem: &schema.Schema{Type: schema.TypeString}, |
| 32 | + }, |
| 33 | + "sharing_policies": { |
| 34 | + Type: schema.TypeList, |
| 35 | + Computed: true, |
| 36 | + Elem: &schema.Schema{Type: schema.TypeString}, |
| 37 | + }, |
| 38 | + "target_organization_ids": { |
| 39 | + Type: schema.TypeList, |
| 40 | + Computed: true, |
| 41 | + Elem: &schema.Schema{Type: schema.TypeString}, |
| 42 | + }, |
| 43 | + "source_organization_ids": { |
| 44 | + Type: schema.TypeList, |
| 45 | + Computed: true, |
| 46 | + Elem: &schema.Schema{Type: schema.TypeString}, |
| 47 | + }, |
| 48 | + "role_names": { |
| 49 | + Type: schema.TypeList, |
| 50 | + Computed: true, |
| 51 | + Elem: &schema.Schema{Type: schema.TypeString}, |
| 52 | + }, |
| 53 | + "purposes": { |
| 54 | + Type: schema.TypeList, |
| 55 | + Computed: true, |
| 56 | + Elem: &schema.Schema{Type: schema.TypeString}, |
| 57 | + }, |
| 58 | + }, |
| 59 | + } |
| 60 | + |
| 61 | +} |
| 62 | + |
| 63 | +func dataSourceIAMRoleSharingPoliciesRead(_ context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { |
| 64 | + c := meta.(*config.Config) |
| 65 | + |
| 66 | + var diags diag.Diagnostics |
| 67 | + |
| 68 | + client, err := c.IAMClient() |
| 69 | + if err != nil { |
| 70 | + return diag.FromErr(err) |
| 71 | + } |
| 72 | + |
| 73 | + roleID := d.Get("role_id").(string) |
| 74 | + sharingPolicy := d.Get("sharing_policy").(string) |
| 75 | + targetOrganizationID := d.Get("target_organization_id").(string) |
| 76 | + |
| 77 | + listOptions := &iam.ListSharingPoliciesOptions{} |
| 78 | + if sharingPolicy != "" { |
| 79 | + listOptions.SharingPolicy = &sharingPolicy |
| 80 | + } |
| 81 | + if targetOrganizationID != "" { |
| 82 | + listOptions.TargetOrganizationID = &targetOrganizationID |
| 83 | + } |
| 84 | + |
| 85 | + policies, _, err := client.Roles.ListSharingPolicies(iam.Role{ID: roleID}, listOptions) |
| 86 | + if err != nil { |
| 87 | + return diag.FromErr(err) |
| 88 | + } |
| 89 | + |
| 90 | + var ids []string |
| 91 | + var sharingPolicies []string |
| 92 | + var targetOrganizationIDs []string |
| 93 | + var sourceOrganizationIDs []string |
| 94 | + var roleNames []string |
| 95 | + var purposes []string |
| 96 | + |
| 97 | + for _, policy := range *policies { |
| 98 | + ids = append(ids, policy.InternalID) |
| 99 | + sharingPolicies = append(sharingPolicies, policy.SharingPolicy) |
| 100 | + targetOrganizationIDs = append(targetOrganizationIDs, policy.TargetOrganizationID) |
| 101 | + sourceOrganizationIDs = append(sourceOrganizationIDs, policy.TargetOrganizationID) |
| 102 | + roleNames = append(roleNames, policy.RoleName) |
| 103 | + purposes = append(purposes, policy.Purpose) |
| 104 | + } |
| 105 | + _ = d.Set("ids", ids) |
| 106 | + _ = d.Set("sharing_policies", sharingPolicies) |
| 107 | + _ = d.Set("target_organization_ids", targetOrganizationIDs) |
| 108 | + _ = d.Set("source_organization_ids", sourceOrganizationIDs) |
| 109 | + _ = d.Set("role_names", roleNames) |
| 110 | + _ = d.Set("purposes", purposes) |
| 111 | + |
| 112 | + d.SetId(roleID + targetOrganizationID) |
| 113 | + return diags |
| 114 | +} |
0 commit comments