|
| 1 | +package github |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "strconv" |
| 6 | + "strings" |
| 7 | + |
| 8 | + "github.com/google/go-github/v82/github" |
| 9 | + "github.com/hashicorp/terraform-plugin-sdk/v2/diag" |
| 10 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 11 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" |
| 12 | +) |
| 13 | + |
| 14 | +func dataSourceGithubEnterpriseTeam() *schema.Resource { |
| 15 | + return &schema.Resource{ |
| 16 | + Description: "Gets information about a GitHub enterprise team.", |
| 17 | + ReadContext: dataSourceGithubEnterpriseTeamRead, |
| 18 | + |
| 19 | + Schema: map[string]*schema.Schema{ |
| 20 | + "enterprise_slug": { |
| 21 | + Type: schema.TypeString, |
| 22 | + Required: true, |
| 23 | + Description: "The slug of the enterprise.", |
| 24 | + ValidateDiagFunc: validation.ToDiagFunc(validation.All(validation.StringIsNotWhiteSpace, validation.StringIsNotEmpty)), |
| 25 | + }, |
| 26 | + "slug": { |
| 27 | + Type: schema.TypeString, |
| 28 | + Optional: true, |
| 29 | + Computed: true, |
| 30 | + ExactlyOneOf: []string{"slug", "team_id"}, |
| 31 | + Description: "The slug of the enterprise team.", |
| 32 | + ValidateDiagFunc: validation.ToDiagFunc(validation.All(validation.StringIsNotWhiteSpace, validation.StringIsNotEmpty)), |
| 33 | + }, |
| 34 | + "team_id": { |
| 35 | + Type: schema.TypeInt, |
| 36 | + Optional: true, |
| 37 | + Computed: true, |
| 38 | + ExactlyOneOf: []string{"slug", "team_id"}, |
| 39 | + Description: "The numeric ID of the enterprise team.", |
| 40 | + ValidateDiagFunc: validation.ToDiagFunc(validation.IntAtLeast(1)), |
| 41 | + }, |
| 42 | + "name": { |
| 43 | + Type: schema.TypeString, |
| 44 | + Computed: true, |
| 45 | + Description: "The name of the enterprise team.", |
| 46 | + }, |
| 47 | + "description": { |
| 48 | + Type: schema.TypeString, |
| 49 | + Computed: true, |
| 50 | + Description: "A description of the enterprise team.", |
| 51 | + }, |
| 52 | + "organization_selection_type": { |
| 53 | + Type: schema.TypeString, |
| 54 | + Computed: true, |
| 55 | + Description: "Specifies which organizations in the enterprise should have access to this team.", |
| 56 | + }, |
| 57 | + "group_id": { |
| 58 | + Type: schema.TypeString, |
| 59 | + Computed: true, |
| 60 | + Description: "The ID of the IdP group to assign team membership with.", |
| 61 | + }, |
| 62 | + }, |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +func dataSourceGithubEnterpriseTeamRead(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics { |
| 67 | + client := meta.(*Owner).v3client |
| 68 | + enterpriseSlug := strings.TrimSpace(d.Get("enterprise_slug").(string)) |
| 69 | + |
| 70 | + var te *github.EnterpriseTeam |
| 71 | + if v, ok := d.GetOk("team_id"); ok { |
| 72 | + teamID := int64(v.(int)) |
| 73 | + if teamID != 0 { |
| 74 | + found, err := findEnterpriseTeamByID(ctx, client, enterpriseSlug, teamID) |
| 75 | + if err != nil { |
| 76 | + return diag.FromErr(err) |
| 77 | + } |
| 78 | + if found == nil { |
| 79 | + return diag.Errorf("could not find enterprise team %d in enterprise %s", teamID, enterpriseSlug) |
| 80 | + } |
| 81 | + te = found |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + if te == nil { |
| 86 | + teamSlug := strings.TrimSpace(d.Get("slug").(string)) |
| 87 | + if teamSlug == "" { |
| 88 | + return diag.Errorf("one of slug or team_id must be set") |
| 89 | + } |
| 90 | + found, _, err := client.Enterprise.GetTeam(ctx, enterpriseSlug, teamSlug) |
| 91 | + if err != nil { |
| 92 | + return diag.FromErr(err) |
| 93 | + } |
| 94 | + te = found |
| 95 | + } |
| 96 | + |
| 97 | + d.SetId(buildTwoPartID(enterpriseSlug, strconv.FormatInt(te.ID, 10))) |
| 98 | + if err := d.Set("enterprise_slug", enterpriseSlug); err != nil { |
| 99 | + return diag.FromErr(err) |
| 100 | + } |
| 101 | + if err := d.Set("slug", te.Slug); err != nil { |
| 102 | + return diag.FromErr(err) |
| 103 | + } |
| 104 | + if err := d.Set("team_id", int(te.ID)); err != nil { |
| 105 | + return diag.FromErr(err) |
| 106 | + } |
| 107 | + if err := d.Set("name", te.Name); err != nil { |
| 108 | + return diag.FromErr(err) |
| 109 | + } |
| 110 | + if te.Description != nil { |
| 111 | + if err := d.Set("description", *te.Description); err != nil { |
| 112 | + return diag.FromErr(err) |
| 113 | + } |
| 114 | + } else { |
| 115 | + if err := d.Set("description", ""); err != nil { |
| 116 | + return diag.FromErr(err) |
| 117 | + } |
| 118 | + } |
| 119 | + orgSel := "" |
| 120 | + if te.OrganizationSelectionType != nil { |
| 121 | + orgSel = *te.OrganizationSelectionType |
| 122 | + } |
| 123 | + if orgSel == "" { |
| 124 | + orgSel = "disabled" |
| 125 | + } |
| 126 | + if err := d.Set("organization_selection_type", orgSel); err != nil { |
| 127 | + return diag.FromErr(err) |
| 128 | + } |
| 129 | + if te.GroupID != "" { |
| 130 | + if err := d.Set("group_id", te.GroupID); err != nil { |
| 131 | + return diag.FromErr(err) |
| 132 | + } |
| 133 | + } else { |
| 134 | + if err := d.Set("group_id", ""); err != nil { |
| 135 | + return diag.FromErr(err) |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + return nil |
| 140 | +} |
0 commit comments