|
| 1 | +package github |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "log" |
| 7 | + |
| 8 | + "github.com/google/go-github/v53/github" |
| 9 | + "github.com/hashicorp/terraform-plugin-sdk/helper/schema" |
| 10 | +) |
| 11 | + |
| 12 | +func resourceGithubOrganizationCustomRole() *schema.Resource { |
| 13 | + return &schema.Resource{ |
| 14 | + Create: resourceGithubOrganizationCustomRoleCreate, |
| 15 | + Read: resourceGithubOrganizationCustomRoleRead, |
| 16 | + Update: resourceGithubOrganizationCustomRoleUpdate, |
| 17 | + Delete: resourceGithubOrganizationCustomRoleDelete, |
| 18 | + Importer: &schema.ResourceImporter{ |
| 19 | + State: schema.ImportStatePassthrough, |
| 20 | + }, |
| 21 | + |
| 22 | + Schema: map[string]*schema.Schema{ |
| 23 | + "name": { |
| 24 | + Type: schema.TypeString, |
| 25 | + Required: true, |
| 26 | + Description: "The organization custom repository role to create.", |
| 27 | + }, |
| 28 | + "base_role": { |
| 29 | + Type: schema.TypeString, |
| 30 | + Required: true, |
| 31 | + Description: "The base role for the custom repository role.", |
| 32 | + ValidateFunc: validateValueFunc([]string{"read", "triage", "write", "maintain"}), |
| 33 | + }, |
| 34 | + "permissions": { |
| 35 | + Type: schema.TypeSet, |
| 36 | + Required: true, |
| 37 | + Elem: &schema.Schema{Type: schema.TypeString}, |
| 38 | + MinItems: 1, // At least one permission should be passed. |
| 39 | + Description: "The permissions for the custom repository role.", |
| 40 | + }, |
| 41 | + "description": { |
| 42 | + Type: schema.TypeString, |
| 43 | + Optional: true, |
| 44 | + Description: "The description of the custom repository role.", |
| 45 | + }, |
| 46 | + }, |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +func resourceGithubOrganizationCustomRoleCreate(d *schema.ResourceData, meta interface{}) error { |
| 51 | + client := meta.(*Owner).v3client |
| 52 | + orgName := meta.(*Owner).name |
| 53 | + ctx := context.Background() |
| 54 | + |
| 55 | + err := checkOrganization(meta) |
| 56 | + if err != nil { |
| 57 | + return err |
| 58 | + } |
| 59 | + |
| 60 | + permissions := d.Get("permissions").(*schema.Set).List() |
| 61 | + permissionsStr := make([]string, len(permissions)) |
| 62 | + for i, v := range permissions { |
| 63 | + permissionsStr[i] = v.(string) |
| 64 | + } |
| 65 | + |
| 66 | + role, _, err := client.Organizations.CreateCustomRepoRole(ctx, orgName, &github.CreateOrUpdateCustomRoleOptions{ |
| 67 | + Name: github.String(d.Get("name").(string)), |
| 68 | + Description: github.String(d.Get("description").(string)), |
| 69 | + BaseRole: github.String(d.Get("base_role").(string)), |
| 70 | + Permissions: permissionsStr, |
| 71 | + }) |
| 72 | + |
| 73 | + if err != nil { |
| 74 | + return fmt.Errorf("error creating GitHub custom repository role %s (%s): %s", orgName, d.Get("name").(string), err) |
| 75 | + } |
| 76 | + |
| 77 | + d.SetId(fmt.Sprint(*role.ID)) |
| 78 | + return resourceGithubOrganizationCustomRoleRead(d, meta) |
| 79 | +} |
| 80 | + |
| 81 | +func resourceGithubOrganizationCustomRoleRead(d *schema.ResourceData, meta interface{}) error { |
| 82 | + client := meta.(*Owner).v3client |
| 83 | + ctx := context.Background() |
| 84 | + orgName := meta.(*Owner).name |
| 85 | + |
| 86 | + err := checkOrganization(meta) |
| 87 | + if err != nil { |
| 88 | + return err |
| 89 | + } |
| 90 | + |
| 91 | + roleID := d.Id() |
| 92 | + |
| 93 | + // ListCustomRepoRoles returns a list of all custom repository roles for an organization. |
| 94 | + // There is an API endpoint for getting a single custom repository role, but is not |
| 95 | + // implemented in the go-github library. |
| 96 | + roleList, _, err := client.Organizations.ListCustomRepoRoles(ctx, orgName) |
| 97 | + if err != nil { |
| 98 | + return fmt.Errorf("error querying GitHub custom repository roles %s: %s", orgName, err) |
| 99 | + } |
| 100 | + |
| 101 | + var role *github.CustomRepoRoles |
| 102 | + for _, r := range roleList.CustomRepoRoles { |
| 103 | + if fmt.Sprint(*r.ID) == roleID { |
| 104 | + role = r |
| 105 | + break |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + if role == nil { |
| 110 | + log.Printf("[WARN] GitHub custom repository role (%s/%s) not found, removing from state", orgName, roleID) |
| 111 | + d.SetId("") |
| 112 | + return nil |
| 113 | + } |
| 114 | + |
| 115 | + d.Set("name", role.Name) |
| 116 | + d.Set("description", role.Description) |
| 117 | + d.Set("base_role", role.BaseRole) |
| 118 | + d.Set("permissions", role.Permissions) |
| 119 | + |
| 120 | + return nil |
| 121 | +} |
| 122 | + |
| 123 | +func resourceGithubOrganizationCustomRoleUpdate(d *schema.ResourceData, meta interface{}) error { |
| 124 | + client := meta.(*Owner).v3client |
| 125 | + ctx := context.Background() |
| 126 | + orgName := meta.(*Owner).name |
| 127 | + |
| 128 | + err := checkOrganization(meta) |
| 129 | + if err != nil { |
| 130 | + return err |
| 131 | + } |
| 132 | + |
| 133 | + roleID := d.Id() |
| 134 | + permissions := d.Get("permissions").(*schema.Set).List() |
| 135 | + permissionsStr := make([]string, len(permissions)) |
| 136 | + for i, v := range permissions { |
| 137 | + permissionsStr[i] = v.(string) |
| 138 | + } |
| 139 | + |
| 140 | + update := &github.CreateOrUpdateCustomRoleOptions{ |
| 141 | + Name: github.String(d.Get("name").(string)), |
| 142 | + Description: github.String(d.Get("description").(string)), |
| 143 | + BaseRole: github.String(d.Get("base_role").(string)), |
| 144 | + Permissions: permissionsStr, |
| 145 | + } |
| 146 | + |
| 147 | + if _, _, err := client.Organizations.UpdateCustomRepoRole(ctx, orgName, roleID, update); err != nil { |
| 148 | + return fmt.Errorf("error updating GitHub custom repository role %s (%s): %s", orgName, roleID, err) |
| 149 | + } |
| 150 | + |
| 151 | + return resourceGithubOrganizationCustomRoleRead(d, meta) |
| 152 | +} |
| 153 | + |
| 154 | +func resourceGithubOrganizationCustomRoleDelete(d *schema.ResourceData, meta interface{}) error { |
| 155 | + client := meta.(*Owner).v3client |
| 156 | + ctx := context.Background() |
| 157 | + orgName := meta.(*Owner).name |
| 158 | + |
| 159 | + err := checkOrganization(meta) |
| 160 | + if err != nil { |
| 161 | + return err |
| 162 | + } |
| 163 | + roleID := d.Id() |
| 164 | + |
| 165 | + _, err = client.Organizations.DeleteCustomRepoRole(ctx, orgName, roleID) |
| 166 | + if err != nil { |
| 167 | + return fmt.Errorf("Error deleting GitHub custom repository role %s (%s): %s", orgName, roleID, err) |
| 168 | + } |
| 169 | + |
| 170 | + return nil |
| 171 | +} |
0 commit comments