|
| 1 | +package github |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "net/http" |
| 7 | + |
| 8 | + "github.com/google/go-github/v88/github" |
| 9 | + "github.com/hashicorp/terraform-plugin-log/tflog" |
| 10 | + "github.com/hashicorp/terraform-plugin-sdk/v2/diag" |
| 11 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 12 | +) |
| 13 | + |
| 14 | +func resourceGithubCopilotTeamSeatAssignment() *schema.Resource { |
| 15 | + return &schema.Resource{ |
| 16 | + CreateContext: resourceGithubCopilotTeamSeatAssignmentCreate, |
| 17 | + ReadContext: resourceGithubCopilotTeamSeatAssignmentRead, |
| 18 | + DeleteContext: resourceGithubCopilotTeamSeatAssignmentDelete, |
| 19 | + Importer: &schema.ResourceImporter{ |
| 20 | + StateContext: schema.ImportStatePassthroughContext, |
| 21 | + }, |
| 22 | + |
| 23 | + Schema: map[string]*schema.Schema{ |
| 24 | + "team": { |
| 25 | + Type: schema.TypeString, |
| 26 | + Required: true, |
| 27 | + ForceNew: true, |
| 28 | + DiffSuppressFunc: caseInsensitive(), |
| 29 | + Description: "The slug of the team to assign Copilot seats.", |
| 30 | + }, |
| 31 | + }, |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +func resourceGithubCopilotTeamSeatAssignmentCreate(ctx context.Context, d *schema.ResourceData, m any) diag.Diagnostics { |
| 36 | + meta := m.(*Owner) |
| 37 | + client := meta.v3client |
| 38 | + org := meta.name |
| 39 | + |
| 40 | + team := d.Get("team").(string) |
| 41 | + |
| 42 | + _, _, err := client.Copilot.AddCopilotTeams(ctx, org, []string{team}) |
| 43 | + if err != nil { |
| 44 | + return diag.FromErr(err) |
| 45 | + } |
| 46 | + |
| 47 | + d.SetId(team) |
| 48 | + |
| 49 | + return resourceGithubCopilotTeamSeatAssignmentRead(ctx, d, m) |
| 50 | +} |
| 51 | + |
| 52 | +func resourceGithubCopilotTeamSeatAssignmentRead(ctx context.Context, d *schema.ResourceData, m any) diag.Diagnostics { |
| 53 | + meta := m.(*Owner) |
| 54 | + client := meta.v3client |
| 55 | + org := meta.name |
| 56 | + |
| 57 | + teamSlug := d.Id() |
| 58 | + |
| 59 | + // The Copilot API has no single-team seat lookup; scan all seats for a team assignee matching our slug. |
| 60 | + opts := &github.ListOptions{PerPage: 100} |
| 61 | + for { |
| 62 | + resp_data, resp, err := client.Copilot.ListCopilotSeats(ctx, org, opts) |
| 63 | + if err != nil { |
| 64 | + if ghErr, ok := errors.AsType[*github.ErrorResponse](err); ok && ghErr.Response.StatusCode == http.StatusNotFound { |
| 65 | + tflog.Info(ctx, "Copilot team seat assignment no longer exists, removing from state", map[string]any{"team": teamSlug}) |
| 66 | + d.SetId("") |
| 67 | + return nil |
| 68 | + } |
| 69 | + return diag.FromErr(err) |
| 70 | + } |
| 71 | + |
| 72 | + for _, seat := range resp_data.Seats { |
| 73 | + if t, ok := seat.GetTeam(); ok && t.GetSlug() == teamSlug { |
| 74 | + if err := d.Set("team", teamSlug); err != nil { |
| 75 | + return diag.FromErr(err) |
| 76 | + } |
| 77 | + return nil |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + if resp.NextPage == 0 { |
| 82 | + break |
| 83 | + } |
| 84 | + opts.Page = resp.NextPage |
| 85 | + } |
| 86 | + |
| 87 | + // Team not found in any seat — it was removed outside Terraform. |
| 88 | + tflog.Info(ctx, "Copilot team seat assignment no longer exists, removing from state", map[string]any{"team": teamSlug}) |
| 89 | + d.SetId("") |
| 90 | + return nil |
| 91 | +} |
| 92 | + |
| 93 | +func resourceGithubCopilotTeamSeatAssignmentDelete(ctx context.Context, d *schema.ResourceData, m any) diag.Diagnostics { |
| 94 | + meta := m.(*Owner) |
| 95 | + client := meta.v3client |
| 96 | + org := meta.name |
| 97 | + |
| 98 | + teamSlug := d.Id() |
| 99 | + |
| 100 | + _, _, err := client.Copilot.RemoveCopilotTeams(ctx, org, []string{teamSlug}) |
| 101 | + if err != nil { |
| 102 | + if ghErr, ok := errors.AsType[*github.ErrorResponse](err); ok && ghErr.Response.StatusCode == http.StatusNotFound { |
| 103 | + tflog.Info(ctx, "Copilot team seat assignment no longer exists, skipping delete", map[string]any{"team": teamSlug}) |
| 104 | + return nil |
| 105 | + } |
| 106 | + return diag.FromErr(err) |
| 107 | + } |
| 108 | + |
| 109 | + return nil |
| 110 | +} |
0 commit comments