|
| 1 | +package resourcemanager |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "path" |
| 7 | + |
| 8 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 9 | + "github.com/hashicorp/terraform-provider-google/google/tpgresource" |
| 10 | + transport_tpg "github.com/hashicorp/terraform-provider-google/google/transport" |
| 11 | + "google.golang.org/api/iam/v1" |
| 12 | +) |
| 13 | + |
| 14 | +func DataSourceGoogleProjectIamCustomRoles() *schema.Resource { |
| 15 | + return &schema.Resource{ |
| 16 | + Read: dataSourceProjectIamCustomRoleRead, |
| 17 | + Schema: map[string]*schema.Schema{ |
| 18 | + "project": { |
| 19 | + Type: schema.TypeString, |
| 20 | + Optional: true, |
| 21 | + }, |
| 22 | + "show_deleted": { |
| 23 | + Type: schema.TypeBool, |
| 24 | + Optional: true, |
| 25 | + Default: false, |
| 26 | + }, |
| 27 | + "view": { |
| 28 | + Type: schema.TypeString, |
| 29 | + Optional: true, |
| 30 | + Default: "BASIC", |
| 31 | + ValidateFunc: validateView, |
| 32 | + }, |
| 33 | + "roles": { |
| 34 | + Type: schema.TypeList, |
| 35 | + Computed: true, |
| 36 | + Elem: &schema.Resource{ |
| 37 | + Schema: map[string]*schema.Schema{ |
| 38 | + "deleted": { |
| 39 | + Type: schema.TypeBool, |
| 40 | + Computed: true, |
| 41 | + }, |
| 42 | + "description": { |
| 43 | + Type: schema.TypeString, |
| 44 | + Computed: true, |
| 45 | + }, |
| 46 | + "id": { |
| 47 | + Type: schema.TypeString, |
| 48 | + Computed: true, |
| 49 | + }, |
| 50 | + "name": { |
| 51 | + Type: schema.TypeString, |
| 52 | + Computed: true, |
| 53 | + }, |
| 54 | + "permissions": { |
| 55 | + Type: schema.TypeList, |
| 56 | + Computed: true, |
| 57 | + Elem: &schema.Schema{Type: schema.TypeString}, |
| 58 | + }, |
| 59 | + "role_id": { |
| 60 | + Type: schema.TypeString, |
| 61 | + Computed: true, |
| 62 | + }, |
| 63 | + "stage": { |
| 64 | + Type: schema.TypeString, |
| 65 | + Computed: true, |
| 66 | + }, |
| 67 | + "title": { |
| 68 | + Type: schema.TypeString, |
| 69 | + Computed: true, |
| 70 | + }, |
| 71 | + }, |
| 72 | + }, |
| 73 | + }, |
| 74 | + }, |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +func validateView(val interface{}, key string) ([]string, []error) { |
| 79 | + v := val.(string) |
| 80 | + var errs []error |
| 81 | + |
| 82 | + if v != "BASIC" && v != "FULL" { |
| 83 | + errs = append(errs, fmt.Errorf("%q must be either 'BASIC' or 'FULL', got %q", key, v)) |
| 84 | + } |
| 85 | + |
| 86 | + return nil, errs |
| 87 | +} |
| 88 | + |
| 89 | +func dataSourceProjectIamCustomRoleRead(d *schema.ResourceData, meta interface{}) error { |
| 90 | + config := meta.(*transport_tpg.Config) |
| 91 | + userAgent, err := tpgresource.GenerateUserAgentString(d, config.UserAgent) |
| 92 | + if err != nil { |
| 93 | + return err |
| 94 | + } |
| 95 | + |
| 96 | + project, err := tpgresource.GetProject(d, config) |
| 97 | + if err != nil { |
| 98 | + return fmt.Errorf("Error fetching project for custom roles: %s", err) |
| 99 | + } |
| 100 | + |
| 101 | + roles := make([]map[string]interface{}, 0) |
| 102 | + |
| 103 | + showDeleted := d.Get("show_deleted").(bool) |
| 104 | + view := d.Get("view").(string) |
| 105 | + |
| 106 | + request := config.NewIamClient(userAgent).Projects.Roles.List("projects/" + project).ShowDeleted(showDeleted).View(view) |
| 107 | + |
| 108 | + err = request.Pages(context.Background(), func(roleList *iam.ListRolesResponse) error { |
| 109 | + for _, role := range roleList.Roles { |
| 110 | + var permissions []string |
| 111 | + |
| 112 | + switch view { |
| 113 | + case "BASIC": |
| 114 | + permissions = []string{} |
| 115 | + case "FULL": |
| 116 | + permissions = role.IncludedPermissions |
| 117 | + default: |
| 118 | + return fmt.Errorf("Unsupported view type: %s", view) |
| 119 | + } |
| 120 | + |
| 121 | + roles = append(roles, map[string]interface{}{ |
| 122 | + "deleted": role.Deleted, |
| 123 | + "description": role.Description, |
| 124 | + "id": role.Name, |
| 125 | + "name": role.Name, |
| 126 | + "permissions": permissions, |
| 127 | + "role_id": path.Base(role.Name), |
| 128 | + "stage": role.Stage, |
| 129 | + "title": role.Title, |
| 130 | + }) |
| 131 | + } |
| 132 | + return nil |
| 133 | + }) |
| 134 | + |
| 135 | + if err != nil { |
| 136 | + return fmt.Errorf("Error retrieving project custom roles: %s", err) |
| 137 | + } |
| 138 | + |
| 139 | + if err := d.Set("roles", roles); err != nil { |
| 140 | + return fmt.Errorf("Error setting project custom roles: %s", err) |
| 141 | + } |
| 142 | + |
| 143 | + d.SetId("projects/" + project) |
| 144 | + |
| 145 | + return nil |
| 146 | +} |
0 commit comments