|
| 1 | +package github |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + |
| 7 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 8 | +) |
| 9 | + |
| 10 | +func dataSourceGithubOrganizationRoles() *schema.Resource { |
| 11 | + return &schema.Resource{ |
| 12 | + Read: dataSourceGithubOrganizationRolesRead, |
| 13 | + |
| 14 | + Schema: map[string]*schema.Schema{ |
| 15 | + "roles": { |
| 16 | + Description: "The list of organization roles available to the organization.", |
| 17 | + Type: schema.TypeList, |
| 18 | + Computed: true, |
| 19 | + Elem: &schema.Resource{ |
| 20 | + Schema: map[string]*schema.Schema{ |
| 21 | + "id": { |
| 22 | + Description: "The unique identifier of the role.", |
| 23 | + Type: schema.TypeInt, |
| 24 | + Computed: true, |
| 25 | + }, |
| 26 | + "name": { |
| 27 | + Description: "The name of the role.", |
| 28 | + Type: schema.TypeString, |
| 29 | + Computed: true, |
| 30 | + }, |
| 31 | + "description": { |
| 32 | + Description: "A short description about who this role is for or what permissions it grants.", |
| 33 | + Type: schema.TypeString, |
| 34 | + Computed: true, |
| 35 | + }, |
| 36 | + "source": { |
| 37 | + Description: "Source answers the question, \"where did this role come from?\"", |
| 38 | + Type: schema.TypeString, |
| 39 | + Computed: true, |
| 40 | + }, |
| 41 | + "base_role": { |
| 42 | + Description: "The system role from which this role inherits permissions.", |
| 43 | + Type: schema.TypeString, |
| 44 | + Computed: true, |
| 45 | + }, |
| 46 | + "permissions": { |
| 47 | + Description: "A list of permissions included in this role.", |
| 48 | + Type: schema.TypeSet, |
| 49 | + Elem: &schema.Schema{Type: schema.TypeString}, |
| 50 | + Computed: true, |
| 51 | + }, |
| 52 | + }, |
| 53 | + }, |
| 54 | + }, |
| 55 | + }, |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +func dataSourceGithubOrganizationRolesRead(d *schema.ResourceData, meta interface{}) error { |
| 60 | + client := meta.(*Owner).v3client |
| 61 | + ctx := context.Background() |
| 62 | + orgName := meta.(*Owner).name |
| 63 | + |
| 64 | + ret, _, err := client.Organizations.ListRoles(ctx, orgName) |
| 65 | + if err != nil { |
| 66 | + return err |
| 67 | + } |
| 68 | + |
| 69 | + allRoles := make([]any, ret.GetTotalCount()) |
| 70 | + for _, role := range ret.CustomRepoRoles { |
| 71 | + r := map[string]any{ |
| 72 | + "id": role.GetID(), |
| 73 | + "name": role.GetName(), |
| 74 | + "description": role.GetDescription(), |
| 75 | + "source": role.GetSource(), |
| 76 | + "base_role": role.GetBaseRole(), |
| 77 | + "permissions": role.Permissions, |
| 78 | + } |
| 79 | + allRoles = append(allRoles, r) |
| 80 | + } |
| 81 | + |
| 82 | + d.SetId(fmt.Sprintf("%s/github-org-roles", orgName)) |
| 83 | + if err := d.Set("roles", allRoles); err != nil { |
| 84 | + return fmt.Errorf("error setting roles: %s", err) |
| 85 | + } |
| 86 | + |
| 87 | + return nil |
| 88 | +} |
0 commit comments